diff --git a/core/procedures.ts b/core/procedures.ts index 3adcee5a6..8a976016f 100644 --- a/core/procedures.ts +++ b/core/procedures.ts @@ -30,8 +30,6 @@ import {IProcedureMap} from './interfaces/i_procedure_map.js'; import {IProcedureModel} from './interfaces/i_procedure_model.js'; import {IProcedureBlock, isProcedureBlock} from './interfaces/i_procedure_block.js'; import {ObservableProcedureMap} from './procedures/observable_procedure_map.js'; -import {ObservableProcedureModel} from './procedures/observable_procedure_model.js'; -import {ObservableParameterModel} from './procedures/observable_parameter_model.js'; import {triggerProceduresUpdate} from './procedures/update_procedures.js'; import * as utilsXml from './utils/xml.js'; import * as Variables from './variables.js'; @@ -502,8 +500,6 @@ export function getDefinition(name: string, workspace: Workspace): Block|null { export { ObservableProcedureMap, - ObservableProcedureModel, - ObservableParameterModel, triggerProceduresUpdate, IParameterModel, IProcedureBlock, diff --git a/core/procedures/observable_parameter_model.ts b/core/procedures/observable_parameter_model.ts deleted file mode 100644 index 1f973b007..000000000 --- a/core/procedures/observable_parameter_model.ts +++ /dev/null @@ -1,113 +0,0 @@ -/** - * @license - * Copyright 2022 Google LLC - * SPDX-License-Identifier: Apache-2.0 - */ - -import * as eventUtils from '../events/utils.js'; -import {genUid} from '../utils/idgenerator.js'; -import type {IParameterModel} from '../interfaces/i_parameter_model.js'; -import type {IProcedureModel} from '../interfaces/i_procedure_model'; -import {triggerProceduresUpdate} from './update_procedures.js'; -import type {VariableModel} from '../variable_model.js'; -import type {Workspace} from '../workspace.js'; -import * as goog from '../../closure/goog/goog.js'; -goog.declareModuleId('Blockly.procedures.ObservableParameterModel'); - - -export class ObservableParameterModel implements IParameterModel { - private id: string; - private variable: VariableModel; - private shouldFireEvents = false; - private procedureModel: IProcedureModel|null = null; - - constructor( - private readonly workspace: Workspace, name: string, id?: string, - varId?: string) { - this.id = id ?? genUid(); - this.variable = this.workspace.getVariable(name) ?? - workspace.createVariable(name, '', varId); - } - - /** - * Sets the name of this parameter to the given name. - */ - setName(name: string): this { - if (name === this.variable.name) return this; - const oldName = this.variable.name; - this.variable = - this.workspace.getVariable(name) ?? this.workspace.createVariable(name); - triggerProceduresUpdate(this.workspace); - return this; - } - - /** - * Unimplemented. The built-in ParameterModel does not support typing. - * If you want your procedure blocks to have typed parameters, you need to - * implement your own ParameterModel. - * - * @throws Throws for the ObservableParameterModel specifically because this - * method is unimplemented. - */ - setTypes(_types: string[]): this { - throw new Error( - 'The built-in ParameterModel does not support typing. You need to ' + - 'implement your own custom ParameterModel.'); - } - - /** - * Returns the name of this parameter. - */ - getName(): string { - return this.variable.name; - } - - /** - * Returns the types of this parameter. - */ - getTypes(): string[] { - return []; - } - - /** - * Returns the unique language-neutral ID for the parameter. - * - * This represents the identify of the variable model which does not change - * over time. - */ - getId(): string { - return this.id; - } - - /** Returns the variable model associated with the parameter model. */ - getVariableModel(): VariableModel { - return this.variable; - } - - /** - * Tells the parameter model it should fire events. - * - * @internal - */ - startPublishing() { - this.shouldFireEvents = true; - } - - /** - * Tells the parameter model it should not fire events. - * - * @internal - */ - stopPublishing() { - this.shouldFireEvents = false; - } - - /** Sets the procedure model this parameter is a part of. */ - setProcedureModel(model: IProcedureModel): this { - // TODO: Not sure if we want to do this, or accept it via the constructor. - // That means it could be non-null, but it would also break the fluent - // API. - this.procedureModel = model; - return this; - } -} diff --git a/core/procedures/observable_procedure_model.ts b/core/procedures/observable_procedure_model.ts deleted file mode 100644 index fbbce53bf..000000000 --- a/core/procedures/observable_procedure_model.ts +++ /dev/null @@ -1,195 +0,0 @@ -/** - * @license - * Copyright 2022 Google LLC - * SPDX-License-Identifier: Apache-2.0 - */ - -import * as eventUtils from '../events/utils.js'; -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 {isObservable} from '../interfaces/i_observable.js'; -import {triggerProceduresUpdate} from './update_procedures.js'; -import type {Workspace} from '../workspace.js'; -import * as goog from '../../closure/goog/goog.js'; -goog.declareModuleId('Blockly.procedures.ObservableProcedureModel'); - - -export class ObservableProcedureModel implements IProcedureModel { - private id: string; - private name: string; - private parameters: IParameterModel[] = []; - private returnTypes: string[]|null = null; - private enabled = true; - private shouldFireEvents = false; - private shouldTriggerUpdates = true; - - constructor( - private readonly workspace: Workspace, name: string, id?: string) { - this.id = id ?? genUid(); - this.name = name; - } - - /** Sets the human-readable name of the procedure. */ - setName(name: string): this { - if (name === this.name) return this; - const prevName = this.name; - this.name = name; - if (this.shouldTriggerUpdates) triggerProceduresUpdate(this.workspace); - return this; - } - - /** - * Inserts a parameter into the list of parameters. - * - * To move a parameter, first delete it, and then re-insert. - */ - insertParameter(parameterModel: IParameterModel, index: number): this { - if (this.parameters[index] && - this.parameters[index].getId() === parameterModel.getId()) { - return this; - } - - this.parameters.splice(index, 0, parameterModel); - parameterModel.setProcedureModel(this); - if (isObservable(parameterModel)) { - if (this.shouldFireEvents) { - parameterModel.startPublishing(); - } else { - parameterModel.stopPublishing(); - } - } - - if (this.shouldTriggerUpdates) triggerProceduresUpdate(this.workspace); - return this; - } - - /** Removes the parameter at the given index from the parameter list. */ - deleteParameter(index: number): this { - if (!this.parameters[index]) return this; - const oldParam = this.parameters[index]; - - this.parameters.splice(index, 1); - if (this.shouldTriggerUpdates) triggerProceduresUpdate(this.workspace); - if (isObservable(oldParam)) { - oldParam.stopPublishing(); - } - return this; - } - - /** - * Sets whether the procedure has a return value (empty array) or no return - * value (null). - * - * The built-in procedure model does not support procedures that have actual - * return types (i.e. non-empty arrays, e.g. ['number']). If you want your - * procedure block to have return types, you need to implement your own - * procedure model. - */ - setReturnTypes(types: string[]|null): this { - if (types && types.length) { - throw new Error( - 'The built-in ProcedureModel does not support typing. You need to ' + - 'implement your own custom ProcedureModel.'); - } - // Either they're both an empty array, or both null. Noop either way. - if (!!types === !!this.returnTypes) return this; - const oldReturnTypes = this.returnTypes; - this.returnTypes = types; - if (this.shouldTriggerUpdates) triggerProceduresUpdate(this.workspace); - return this; - } - - /** - * Sets whether this procedure is enabled/disabled. If a procedure is disabled - * all procedure caller blocks should be disabled as well. - */ - setEnabled(enabled: boolean): this { - if (enabled === this.enabled) return this; - this.enabled = enabled; - if (this.shouldTriggerUpdates) triggerProceduresUpdate(this.workspace); - return this; - } - - /** - * Disables triggering updates to procedure blocks until the endBulkUpdate - * is called. - * - * @internal - */ - startBulkUpdate() { - this.shouldTriggerUpdates = false; - } - - /** - * Triggers an update to procedure blocks. Should be used with - * startBulkUpdate. - * - * @internal - */ - endBulkUpdate() { - this.shouldTriggerUpdates = true; - triggerProceduresUpdate(this.workspace); - } - - /** Returns the unique language-neutral ID for the procedure. */ - getId(): string { - return this.id; - } - - /** Returns the human-readable name of the procedure. */ - getName(): string { - return this.name; - } - - /** Returns the parameter at the given index in the parameter list. */ - getParameter(index: number): IParameterModel { - return this.parameters[index]; - } - - /** Returns an array of all of the parameters in the parameter list. */ - getParameters(): IParameterModel[] { - return [...this.parameters]; - } - - /** - * Returns the return type of the procedure. - * - * Null represents a procedure that does not return a value. - */ - getReturnTypes(): string[]|null { - return this.returnTypes; - } - - /** - * Returns whether the procedure is enabled/disabled. If a procedure is - * disabled, all procedure caller blocks should be disabled as well. - */ - getEnabled(): boolean { - return this.enabled; - } - - /** - * Tells the procedure model it should fire events. - * - * @internal - */ - startPublishing() { - this.shouldFireEvents = true; - for (const param of this.parameters) { - if (isObservable(param)) param.startPublishing(); - } - } - - /** - * Tells the procedure model it should not fire events. - * - * @internal - */ - stopPublishing() { - this.shouldFireEvents = false; - for (const param of this.parameters) { - if (isObservable(param)) param.stopPublishing(); - } - } -} diff --git a/core/serialization/procedures.ts b/core/serialization/procedures.ts index 8e18dda66..b29cd571d 100644 --- a/core/serialization/procedures.ts +++ b/core/serialization/procedures.ts @@ -7,10 +7,7 @@ import {IParameterModel} from '../interfaces/i_parameter_model.js'; import {IProcedureModel} from '../interfaces/i_procedure_model.js'; import type {ISerializer} from '../interfaces/i_serializer.js'; -import {ObservableProcedureModel} from '../procedures/observable_procedure_model.js'; -import {ObservableParameterModel} from '../procedures/observable_parameter_model.js'; import * as priorities from './priorities.js'; -import * as serializationRegistry from './registry.js'; import type {Workspace} from '../workspace.js'; @@ -163,12 +160,3 @@ export class ProcedureSerializer { - param.setTypes(['some', 'types']); - }, - 'The built-in ParameterModel does not support typing'); - }); }); }); diff --git a/tests/mocha/test_helpers/procedures.js b/tests/mocha/test_helpers/procedures.js index 74c8a7fd2..ec33e1aeb 100644 --- a/tests/mocha/test_helpers/procedures.js +++ b/tests/mocha/test_helpers/procedures.js @@ -148,3 +148,92 @@ export function createProcCallBlock( `` ), workspace); } + +export class MockProcedureModel { + constructor() { + this.id = Blockly.utils.idGenerator.genUid(); + this.name = ''; + this.parameters = []; + this.returnTypes = null; + this.enabled = true; + } + + setName(name) { + this.name = name; + return this; + } + + insertParameter(parameterModel, index) { + this.parameters.splice(index, 0, parameterModel); + return this; + } + + deleteParameter(index) { + this.parameters.splice(index, 1); + return this; + } + + setReturnTypes(types) { + this.returnTypes = types; + return this; + } + + setEnabled(enabled) { + this.enabled = enabled; + return this; + } + + getId() { + return this.id; + } + + getName() { + return this.name; + } + + getParameter(index) { + return this.parameters[index]; + } + + getParameters() { + return [...this.parameters]; + } + + getReturnTypes() { + return this.returnTypes; + } + + getEnabled() { + return this.enabled; + } +} + +export class MockParameterModel { + constructor(name) { + this.id = Blockly.utils.idGenerator.genUid(); + this.name = name; + this.types = []; + } + + setName(name) { + this.name = name; + return this; + } + + setTypes(types) { + this.types = types; + return this; + } + + getName() { + return this.name; + } + + getTypes() { + return this.types; + } + + getId() { + return this.id; + } +}