Skip to content

Commit

Permalink
feat: trigger updates to IProcedureBlock blocks (#6570)
Browse files Browse the repository at this point in the history
* feat: add interface and method for updating procedure blocks

* chore: remove module ID declarations

* feat: add actually triggering updates

* chore: format

* chore: clean up tests
  • Loading branch information
BeksOmega authored Oct 25, 2022
1 parent 7ffe1fa commit c9ced48
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 6 deletions.
19 changes: 19 additions & 0 deletions core/interfaces/i_procedure_block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @license
* Copyright 2022 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import type {Block} from '../block.js';


/** The interface for a block which models a procedure. */
export interface IProcedureBlock {
doProcedureUpdate(): void;
}

/** A type guard which checks if the given block is a procedure block. */
export function isProcedureBlock(block: Block|
IProcedureBlock): block is IProcedureBlock {
return (block as IProcedureBlock).doProcedureUpdate !== undefined;
}
4 changes: 3 additions & 1 deletion core/procedures/observable_parameter_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
* SPDX-License-Identifier: Apache-2.0
*/

import type {IParameterModel} from '../interfaces/i_parameter_model.js';
import {genUid} from '../utils/idgenerator.js';
import type {IParameterModel} from '../interfaces/i_parameter_model.js';
import {triggerProceduresUpdate} from './update_procedures.js';
import type {VariableModel} from '../variable_model.js';
import type {Workspace} from '../workspace.js';

Expand All @@ -29,6 +30,7 @@ export class ObservableParameterModel implements IParameterModel {
if (name == this.variable.name) return this;
this.variable =
this.workspace.getVariable(name) ?? this.workspace.createVariable(name);
triggerProceduresUpdate(this.workspace);
return this;
}

Expand Down
6 changes: 5 additions & 1 deletion core/procedures/observable_procedure_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import type {IProcedureModel} from '../interfaces/i_procedure_model.js';
import {triggerProceduresUpdate} from './update_procedures.js';
import type {Workspace} from '../workspace.js';


Expand All @@ -28,7 +29,9 @@ export class ObservableProcedureMap extends Map<string, IProcedureModel> {
*/
override delete(id: string): boolean {
// TODO(#6516): Fire events.
return super.delete(id);
const existed = super.delete(id);
triggerProceduresUpdate(this.workspace);
return existed;
}

/**
Expand All @@ -37,6 +40,7 @@ export class ObservableProcedureMap extends Map<string, IProcedureModel> {
override clear() {
// TODO(#6516): Fire events.
super.clear();
triggerProceduresUpdate(this.workspace);
}

/**
Expand Down
8 changes: 7 additions & 1 deletion core/procedures/observable_procedure_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
* SPDX-License-Identifier: Apache-2.0
*/

import {genUid} from '../utils/idgenerator.js';
import type {IParameterModel} from '../interfaces/i_parameter_model.js';
import type {IProcedureModel} from '../interfaces/i_procedure_model.js';
import {triggerProceduresUpdate} from './update_procedures.js';
import type {Workspace} from '../workspace.js';
import {genUid} from '../utils/idgenerator.js';


export class ObservableProcedureModel implements IProcedureModel {
Expand All @@ -25,6 +26,7 @@ export class ObservableProcedureModel implements IProcedureModel {
setName(name: string): this {
// TODO(#6516): Fire events.
this.name = name;
triggerProceduresUpdate(this.workspace);
return this;
}

Expand All @@ -36,13 +38,15 @@ export class ObservableProcedureModel implements IProcedureModel {
insertParameter(parameterModel: IParameterModel, index: number): this {
// TODO(#6516): Fire events.
this.parameters.splice(index, 0, parameterModel);
triggerProceduresUpdate(this.workspace);
return this;
}

/** Removes the parameter at the given index from the parameter list. */
deleteParameter(index: number): this {
// TODO(#6516): Fire events.
this.parameters.splice(index, 1);
triggerProceduresUpdate(this.workspace);
return this;
}

Expand All @@ -54,6 +58,7 @@ export class ObservableProcedureModel implements IProcedureModel {
setReturnTypes(types: string[]|null): this {
// TODO(#6516): Fire events.
this.returnTypes = types;
triggerProceduresUpdate(this.workspace);
return this;
}

Expand All @@ -64,6 +69,7 @@ export class ObservableProcedureModel implements IProcedureModel {
setEnabled(enabled: boolean): this {
// TODO(#6516): Fire events.
this.enabled = enabled;
triggerProceduresUpdate(this.workspace);
return this;
}

Expand Down
22 changes: 22 additions & 0 deletions core/procedures/update_procedures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @license
* Copyright 2022 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import {isProcedureBlock} from '../interfaces/i_procedure_block.js';
import {Workspace} from '../workspace.js';


/**
* Calls the `doProcedureUpdate` method on all blocks which implement it.
*
* @internal
*/
export function triggerProceduresUpdate(workspace: Workspace) {
for (const block of workspace.getAllBlocks(false)) {
if (isProcedureBlock(block)) {
block.doProcedureUpdate();
}
}
}
7 changes: 4 additions & 3 deletions tests/mocha/procedure_map_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ suite('Procedure Map', function() {
sharedTestSetup.call(this);
this.workspace = new Blockly.Workspace();
// this.procedureMap = this.workspace.getProcedureMap();
this.procedureMap =
new Blockly.procedures.ObservableProcedureMap(this.workspace);
});

teardown(function() {
sharedTestTeardown.call(this);
});

// TODO (#6515): Unskip tests.
suite.skip('triggering block updates', function() {
suite('triggering block updates', function() {
setup(function() {
Blockly.Blocks['procedure_mock'] = {
init: function() { },
doProcedureUpdate: function() {},
doProcedureUpdate: function() { },
};

this.procedureBlock = this.workspace.newBlock('procedure_mock');
Expand Down

0 comments on commit c9ced48

Please sign in to comment.