mirror of
https://github.com/google/blockly.git
synced 2026-01-09 18:10:08 +01:00
feat: trigger updates to IProcedureBlock blocks (#6570)
* 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
This commit is contained in:
19
core/interfaces/i_procedure_block.ts
Normal file
19
core/interfaces/i_procedure_block.ts
Normal file
@@ -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,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';
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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';
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,6 +40,7 @@ export class ObservableProcedureMap extends Map<string, IProcedureModel> {
|
||||
override clear() {
|
||||
// TODO(#6516): Fire events.
|
||||
super.clear();
|
||||
triggerProceduresUpdate(this.workspace);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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 {
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -36,6 +38,7 @@ 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;
|
||||
}
|
||||
|
||||
@@ -43,6 +46,7 @@ export class ObservableProcedureModel implements IProcedureModel {
|
||||
deleteParameter(index: number): this {
|
||||
// TODO(#6516): Fire events.
|
||||
this.parameters.splice(index, 1);
|
||||
triggerProceduresUpdate(this.workspace);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
22
core/procedures/update_procedures.ts
Normal file
22
core/procedures/update_procedures.ts
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user