mirror of
https://github.com/google/blockly.git
synced 2026-01-10 10:27:08 +01:00
chore: revert procedure events (#6795)
* chore: delete procedure events * chore: delete event related tests
This commit is contained in:
@@ -28,16 +28,6 @@ import {CommentCreate, CommentCreateJson} from './events_comment_create.js';
|
||||
import {CommentDelete} from './events_comment_delete.js';
|
||||
import {CommentMove, CommentMoveJson} from './events_comment_move.js';
|
||||
import {MarkerMove, MarkerMoveJson} from './events_marker_move.js';
|
||||
import {ProcedureBase} from './events_procedure_base.js';
|
||||
import {ProcedureChangeReturn} from './events_procedure_change_return.js';
|
||||
import {ProcedureCreate} from './events_procedure_create.js';
|
||||
import {ProcedureDelete} from './events_procedure_delete.js';
|
||||
import {ProcedureEnable} from './events_procedure_enable.js';
|
||||
import {ProcedureRename} from './events_procedure_rename.js';
|
||||
import {ProcedureParameterBase} from './events_procedure_parameter_base.js';
|
||||
import {ProcedureParameterCreate} from './events_procedure_parameter_create.js';
|
||||
import {ProcedureParameterDelete} from './events_procedure_parameter_delete.js';
|
||||
import {ProcedureParameterRename} from './events_procedure_parameter_rename.js';
|
||||
import {Selected, SelectedJson} from './events_selected.js';
|
||||
import {ThemeChange, ThemeChangeJson} from './events_theme_change.js';
|
||||
import {ToolboxItemSelect, ToolboxItemSelectJson} from './events_toolbox_item_select.js';
|
||||
@@ -87,16 +77,6 @@ export {FinishedLoading};
|
||||
export {FinishedLoadingJson};
|
||||
export {MarkerMove};
|
||||
export {MarkerMoveJson};
|
||||
export {ProcedureBase};
|
||||
export {ProcedureChangeReturn};
|
||||
export {ProcedureCreate};
|
||||
export {ProcedureDelete};
|
||||
export {ProcedureEnable};
|
||||
export {ProcedureRename};
|
||||
export {ProcedureParameterBase};
|
||||
export {ProcedureParameterCreate};
|
||||
export {ProcedureParameterDelete};
|
||||
export {ProcedureParameterRename};
|
||||
export {Selected};
|
||||
export {SelectedJson};
|
||||
export {ThemeChange};
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import {Abstract as AbstractEvent, AbstractEventJson} from './events_abstract.js';
|
||||
import type {IProcedureModel} from '../interfaces/i_procedure_model.js';
|
||||
import type {Workspace} from '../workspace.js';
|
||||
|
||||
|
||||
/**
|
||||
* The base event for an event associated with a procedure.
|
||||
*/
|
||||
export abstract class ProcedureBase extends AbstractEvent {
|
||||
isBlank = false;
|
||||
|
||||
/**
|
||||
* @param workspace The workspace the procedure model exists in.
|
||||
* @param model The procedure model associated with this event.
|
||||
*/
|
||||
constructor(workspace: Workspace, public readonly model: IProcedureModel) {
|
||||
super();
|
||||
this.workspaceId = workspace.id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the event as JSON.
|
||||
*
|
||||
* @returns JSON representation.
|
||||
*/
|
||||
toJson(): ProcedureBaseJson {
|
||||
const json = super.toJson() as ProcedureBaseJson;
|
||||
json['procedureId'] = this.model.getId();
|
||||
return json;
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProcedureBaseJson extends AbstractEventJson {
|
||||
procedureId: string;
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import type {IProcedureModel} from '../interfaces/i_procedure_model.js';
|
||||
import * as registry from '../registry.js';
|
||||
import type {Workspace} from '../workspace.js';
|
||||
|
||||
import {ProcedureBase, ProcedureBaseJson} from './events_procedure_base.js';
|
||||
import * as eventUtils from './utils.js';
|
||||
|
||||
|
||||
/**
|
||||
* Notifies listeners that a procedure's return type/status has changed.
|
||||
*/
|
||||
export class ProcedureChangeReturn extends ProcedureBase {
|
||||
/** A string used to check the type of the event. */
|
||||
type = eventUtils.PROCEDURE_CHANGE_RETURN;
|
||||
|
||||
/** The new type(s) the procedure's return has been set to. */
|
||||
private newTypes: string[]|null;
|
||||
|
||||
/**
|
||||
* @param oldTypes The type(s) the procedure's return was set to before it
|
||||
* changed.
|
||||
*/
|
||||
constructor(
|
||||
workpace: Workspace, model: IProcedureModel,
|
||||
public readonly oldTypes: string[]|null) {
|
||||
super(workpace, model);
|
||||
|
||||
this.newTypes = model.getReturnTypes();
|
||||
}
|
||||
|
||||
run(forward: boolean) {
|
||||
const procedureModel =
|
||||
this.getEventWorkspace_().getProcedureMap().get(this.model.getId());
|
||||
if (!procedureModel) {
|
||||
throw new Error(
|
||||
'Cannot change the type of a procedure that does not exist ' +
|
||||
'in the procedure map');
|
||||
}
|
||||
if (forward) {
|
||||
procedureModel.setReturnTypes(this.newTypes);
|
||||
} else {
|
||||
procedureModel.setReturnTypes(this.oldTypes);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the event as JSON.
|
||||
*
|
||||
* @returns JSON representation.
|
||||
*/
|
||||
toJson(): ProcedureChangeReturnJson {
|
||||
const json = super.toJson() as ProcedureChangeReturnJson;
|
||||
json['oldTypes'] = this.oldTypes;
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the JSON event.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
static fromJson(json: ProcedureChangeReturnJson, workspace: Workspace):
|
||||
ProcedureChangeReturn {
|
||||
const model = workspace.getProcedureMap().get(json['procedureId']);
|
||||
if (!model) {
|
||||
throw new Error(
|
||||
'Cannot deserialize procedure change return event because the ' +
|
||||
'target procedure does not exist');
|
||||
}
|
||||
return new ProcedureChangeReturn(workspace, model, json['oldTypes']);
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProcedureChangeReturnJson extends ProcedureBaseJson {
|
||||
oldTypes: string[]|null;
|
||||
}
|
||||
|
||||
registry.register(
|
||||
registry.Type.EVENT, eventUtils.PROCEDURE_CHANGE_RETURN,
|
||||
ProcedureChangeReturn);
|
||||
@@ -1,74 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import type {IProcedureModel} from '../interfaces/i_procedure_model.js';
|
||||
import {ObservableParameterModel, ObservableProcedureModel} from '../procedures.js';
|
||||
import * as registry from '../registry.js';
|
||||
import {loadProcedure, saveProcedure, State as ProcedureState} from '../serialization/procedures.js';
|
||||
import type {Workspace} from '../workspace.js';
|
||||
|
||||
import {ProcedureBase, ProcedureBaseJson} from './events_procedure_base.js';
|
||||
import * as eventUtils from './utils.js';
|
||||
|
||||
|
||||
/**
|
||||
* Notifies listeners that a procedure data model has been created.
|
||||
*/
|
||||
export class ProcedureCreate extends ProcedureBase {
|
||||
/** A string used to check the type of the event. */
|
||||
type = eventUtils.PROCEDURE_CREATE;
|
||||
|
||||
constructor(workspace: Workspace, model: IProcedureModel) {
|
||||
super(workspace, model);
|
||||
}
|
||||
|
||||
run(forward: boolean) {
|
||||
const workspace = this.getEventWorkspace_();
|
||||
const procedureMap = workspace.getProcedureMap();
|
||||
const procedureModel = procedureMap.get(this.model.getId());
|
||||
if (forward) {
|
||||
if (procedureModel) return;
|
||||
// TODO: This should add the model to the map instead of creating a dupe.
|
||||
procedureMap.add(new ObservableProcedureModel(
|
||||
workspace, this.model.getName(), this.model.getId()));
|
||||
} else {
|
||||
if (!procedureModel) return;
|
||||
procedureMap.delete(this.model.getId());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the event as JSON.
|
||||
*
|
||||
* @returns JSON representation.
|
||||
*/
|
||||
toJson(): ProcedureCreateJson {
|
||||
const json = super.toJson() as ProcedureCreateJson;
|
||||
json['model'] = saveProcedure(this.model);
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the JSON event.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
static fromJson(json: ProcedureCreateJson, workspace: Workspace):
|
||||
ProcedureCreate {
|
||||
return new ProcedureCreate(
|
||||
workspace,
|
||||
loadProcedure(
|
||||
ObservableProcedureModel, ObservableParameterModel, json['model'],
|
||||
workspace));
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProcedureCreateJson extends ProcedureBaseJson {
|
||||
model: ProcedureState,
|
||||
}
|
||||
|
||||
registry.register(
|
||||
registry.Type.EVENT, eventUtils.PROCEDURE_CREATE, ProcedureCreate);
|
||||
@@ -1,71 +0,0 @@
|
||||
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import type {IProcedureModel} from '../interfaces/i_procedure_model.js';
|
||||
import {ObservableProcedureModel} from '../procedures.js';
|
||||
import * as registry from '../registry.js';
|
||||
import type {Workspace} from '../workspace.js';
|
||||
|
||||
import {ProcedureBase, ProcedureBaseJson} from './events_procedure_base.js';
|
||||
import * as eventUtils from './utils.js';
|
||||
|
||||
|
||||
/**
|
||||
* Notifies listeners that a procedure data model has been deleted.
|
||||
*/
|
||||
export class ProcedureDelete extends ProcedureBase {
|
||||
/** A string used to check the type of the event. */
|
||||
type = eventUtils.PROCEDURE_DELETE;
|
||||
|
||||
constructor(workspace: Workspace, model: IProcedureModel) {
|
||||
super(workspace, model);
|
||||
}
|
||||
|
||||
run(forward: boolean) {
|
||||
const workspace = this.getEventWorkspace_();
|
||||
const procedureMap = workspace.getProcedureMap();
|
||||
const procedureModel = procedureMap.get(this.model.getId());
|
||||
if (forward) {
|
||||
if (!procedureModel) return;
|
||||
procedureMap.delete(this.model.getId());
|
||||
} else {
|
||||
if (procedureModel) return;
|
||||
procedureMap.add(new ObservableProcedureModel(
|
||||
workspace, this.model.getName(), this.model.getId()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the event as JSON.
|
||||
*
|
||||
* @returns JSON representation.
|
||||
*/
|
||||
toJson(): ProcedureDeleteJson {
|
||||
return super.toJson() as ProcedureDeleteJson;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the JSON event.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
static fromJson(json: ProcedureDeleteJson, workspace: Workspace):
|
||||
ProcedureDelete {
|
||||
const model = workspace.getProcedureMap().get(json['procedureId']);
|
||||
if (!model) {
|
||||
throw new Error(
|
||||
'Cannot deserialize procedure delete event because the ' +
|
||||
'target procedure does not exist');
|
||||
}
|
||||
return new ProcedureDelete(workspace, model);
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProcedureDeleteJson extends ProcedureBaseJson {}
|
||||
|
||||
registry.register(
|
||||
registry.Type.EVENT, eventUtils.PROCEDURE_DELETE, ProcedureDelete);
|
||||
@@ -1,77 +0,0 @@
|
||||
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import type {IProcedureModel} from '../interfaces/i_procedure_model.js';
|
||||
import * as registry from '../registry.js';
|
||||
import type {Workspace} from '../workspace.js';
|
||||
|
||||
import {ProcedureBase, ProcedureBaseJson} from './events_procedure_base.js';
|
||||
import * as eventUtils from './utils.js';
|
||||
|
||||
/**
|
||||
* Notifies listeners that the procedure data model has been enabled or
|
||||
* disabled.
|
||||
*/
|
||||
export class ProcedureEnable extends ProcedureBase {
|
||||
/** A string used to check the type of the event. */
|
||||
type = eventUtils.PROCEDURE_ENABLE;
|
||||
|
||||
private oldState: boolean;
|
||||
private newState: boolean;
|
||||
|
||||
constructor(workspace: Workspace, model: IProcedureModel) {
|
||||
super(workspace, model);
|
||||
|
||||
this.oldState = !model.getEnabled();
|
||||
this.newState = model.getEnabled();
|
||||
}
|
||||
|
||||
run(forward: boolean) {
|
||||
const procedureModel =
|
||||
this.getEventWorkspace_().getProcedureMap().get(this.model.getId());
|
||||
if (!procedureModel) {
|
||||
throw new Error(
|
||||
'Cannot change the enabled state of a procedure that does not ' +
|
||||
'exist in the procedure map');
|
||||
}
|
||||
if (forward) {
|
||||
procedureModel.setEnabled(this.newState);
|
||||
} else {
|
||||
procedureModel.setEnabled(this.oldState);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the event as JSON.
|
||||
*
|
||||
* @returns JSON representation.
|
||||
*/
|
||||
toJson(): ProcedureEnableJson {
|
||||
return super.toJson() as ProcedureEnableJson;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the JSON event.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
static fromJson(json: ProcedureEnableJson, workspace: Workspace):
|
||||
ProcedureEnable {
|
||||
const model = workspace.getProcedureMap().get(json['procedureId']);
|
||||
if (!model) {
|
||||
throw new Error(
|
||||
'Cannot deserialize procedure enable event because the ' +
|
||||
'target procedure does not exist');
|
||||
}
|
||||
return new ProcedureEnable(workspace, model);
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProcedureEnableJson extends ProcedureBaseJson {}
|
||||
|
||||
registry.register(
|
||||
registry.Type.EVENT, eventUtils.PROCEDURE_ENABLE, ProcedureEnable);
|
||||
@@ -1,44 +0,0 @@
|
||||
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import {IParameterModel} from '../interfaces/i_parameter_model.js';
|
||||
import {IProcedureModel} from '../interfaces/i_procedure_model.js';
|
||||
|
||||
import {ProcedureBase, ProcedureBaseJson} from './events_procedure_base.js';
|
||||
|
||||
import type {Workspace} from '../workspace.js';
|
||||
|
||||
/**
|
||||
* The base event for an event associated with a procedure parameter.
|
||||
*/
|
||||
export abstract class ProcedureParameterBase extends ProcedureBase {
|
||||
/**
|
||||
* @param workspace The workspace the parameter model exists in.
|
||||
* @param model The procedure model the parameter model belongs to.
|
||||
* @param parameter The parameter model associated with this event.
|
||||
*/
|
||||
constructor(
|
||||
workspace: Workspace, model: IProcedureModel,
|
||||
public readonly parameter: IParameterModel) {
|
||||
super(workspace, model);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the event as JSON.
|
||||
*
|
||||
* @returns JSON representation.
|
||||
*/
|
||||
toJson(): ProcedureParameterBaseJson {
|
||||
const json = super.toJson() as ProcedureParameterBaseJson;
|
||||
json['parameterId'] = this.model.getId();
|
||||
return json;
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProcedureParameterBaseJson extends ProcedureBaseJson {
|
||||
parameterId: string,
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import type {IParameterModel} from '../interfaces/i_parameter_model.js';
|
||||
import type {IProcedureModel} from '../interfaces/i_procedure_model.js';
|
||||
import {ObservableParameterModel} from '../procedures/observable_parameter_model.js';
|
||||
import * as registry from '../registry.js';
|
||||
import {loadParameter, ParameterState, saveParameter} from '../serialization/procedures.js';
|
||||
import type {Workspace} from '../workspace.js';
|
||||
|
||||
import {ProcedureParameterBase, ProcedureParameterBaseJson} from './events_procedure_parameter_base.js';
|
||||
import * as eventUtils from './utils.js';
|
||||
|
||||
|
||||
/**
|
||||
* Notifies listeners that a parameter has been added to a procedure model.
|
||||
*/
|
||||
export class ProcedureParameterCreate extends ProcedureParameterBase {
|
||||
/** A string used to check the type of the event. */
|
||||
type = eventUtils.PROCEDURE_PARAMETER_CREATE;
|
||||
|
||||
/**
|
||||
* @param parameter The parameter model that was just added to the procedure.
|
||||
* @param index The index the parameter was inserted at.
|
||||
*/
|
||||
constructor(
|
||||
workspace: Workspace, procedure: IProcedureModel,
|
||||
parameter: IParameterModel, public readonly index: number) {
|
||||
super(workspace, procedure, parameter);
|
||||
}
|
||||
|
||||
run(forward: boolean) {
|
||||
const workspace = this.getEventWorkspace_();
|
||||
const procedureMap = workspace.getProcedureMap();
|
||||
const procedureModel = procedureMap.get(this.model.getId());
|
||||
if (!procedureModel) {
|
||||
throw new Error(
|
||||
'Cannot add a parameter to a procedure that does not exist ' +
|
||||
'in the procedure map');
|
||||
}
|
||||
const parameterModel = procedureModel.getParameter(this.index);
|
||||
if (forward) {
|
||||
if (this.parameterMatches(parameterModel)) return;
|
||||
// TODO: This should just add the parameter instead of creating a dupe.
|
||||
procedureModel.insertParameter(
|
||||
new ObservableParameterModel(
|
||||
workspace, this.parameter.getName(), this.parameter.getId()),
|
||||
this.index);
|
||||
} else {
|
||||
if (!this.parameterMatches(parameterModel)) return;
|
||||
procedureModel.deleteParameter(this.index);
|
||||
}
|
||||
}
|
||||
|
||||
parameterMatches(param: IParameterModel) {
|
||||
return param && param.getId() === this.parameter.getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the event as JSON.
|
||||
*
|
||||
* @returns JSON representation.
|
||||
*/
|
||||
toJson(): ProcedureParameterCreateJson {
|
||||
const json = super.toJson() as ProcedureParameterCreateJson;
|
||||
json['parameter'] = saveParameter(this.parameter);
|
||||
json['index'] = this.index;
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the JSON event.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
static fromJson(json: ProcedureParameterCreateJson, workspace: Workspace):
|
||||
ProcedureParameterCreate {
|
||||
const procedure = workspace.getProcedureMap().get(json['procedureId']);
|
||||
if (!procedure) {
|
||||
throw new Error(
|
||||
'Cannot deserialize parameter create event because the ' +
|
||||
'target procedure does not exist');
|
||||
}
|
||||
return new ProcedureParameterCreate(
|
||||
workspace, procedure,
|
||||
loadParameter(ObservableParameterModel, json['parameter'], workspace),
|
||||
json['index']);
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProcedureParameterCreateJson extends
|
||||
ProcedureParameterBaseJson {
|
||||
parameter: ParameterState, index: number,
|
||||
}
|
||||
|
||||
registry.register(
|
||||
registry.Type.EVENT, eventUtils.PROCEDURE_PARAMETER_CREATE,
|
||||
ProcedureParameterCreate);
|
||||
@@ -1,97 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import type {IParameterModel} from '../interfaces/i_parameter_model.js';
|
||||
import type {IProcedureModel} from '../interfaces/i_procedure_model.js';
|
||||
import {ObservableParameterModel} from '../procedures/observable_parameter_model.js';
|
||||
import * as registry from '../registry.js';
|
||||
import type {Workspace} from '../workspace.js';
|
||||
|
||||
import {ProcedureParameterBase, ProcedureParameterBaseJson} from './events_procedure_parameter_base.js';
|
||||
import * as eventUtils from './utils.js';
|
||||
|
||||
/**
|
||||
* Notifies listeners that a parameter has been removed from a procedure.
|
||||
*/
|
||||
export class ProcedureParameterDelete extends ProcedureParameterBase {
|
||||
/** A string used to check the type of the event. */
|
||||
type = eventUtils.PROCEDURE_PARAMETER_DELETE;
|
||||
|
||||
/**
|
||||
* @param parameter The parameter model that was just removed from the
|
||||
* procedure.
|
||||
* @param index The index the parameter was at before it was removed.
|
||||
*/
|
||||
constructor(
|
||||
workspace: Workspace, procedure: IProcedureModel,
|
||||
parameter: IParameterModel, public readonly index: number) {
|
||||
super(workspace, procedure, parameter);
|
||||
}
|
||||
|
||||
run(forward: boolean) {
|
||||
const workspace = this.getEventWorkspace_();
|
||||
const procedureMap = workspace.getProcedureMap();
|
||||
const procedureModel = procedureMap.get(this.model.getId());
|
||||
if (!procedureModel) {
|
||||
throw new Error(
|
||||
'Cannot add a parameter to a procedure that does not exist ' +
|
||||
'in the procedure map');
|
||||
}
|
||||
const parameterModel = procedureModel.getParameter(this.index);
|
||||
if (forward) {
|
||||
if (!this.parameterMatches(parameterModel)) return;
|
||||
procedureModel.deleteParameter(this.index);
|
||||
} else {
|
||||
if (this.parameterMatches(parameterModel)) return;
|
||||
// TODO: this should just insert the model instead of creating a dupe.
|
||||
procedureModel.insertParameter(
|
||||
new ObservableParameterModel(
|
||||
workspace, this.parameter.getName(), this.parameter.getId()),
|
||||
this.index);
|
||||
}
|
||||
}
|
||||
|
||||
parameterMatches(param: IParameterModel) {
|
||||
return param && param.getId() === this.parameter.getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the event as JSON.
|
||||
*
|
||||
* @returns JSON representation.
|
||||
*/
|
||||
toJson(): ProcedureParameterDeleteJson {
|
||||
const json = super.toJson() as ProcedureParameterDeleteJson;
|
||||
json['index'] = this.index;
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the JSON event.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
static fromJson(json: ProcedureParameterDeleteJson, workspace: Workspace):
|
||||
ProcedureParameterDelete {
|
||||
const model = workspace.getProcedureMap().get(json['procedureId']);
|
||||
if (!model) {
|
||||
throw new Error(
|
||||
'Cannot deserialize procedure delete event because the ' +
|
||||
'target procedure does not exist');
|
||||
}
|
||||
const param = model.getParameter(json['index']);
|
||||
return new ProcedureParameterDelete(workspace, model, param, json['index']);
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProcedureParameterDeleteJson extends
|
||||
ProcedureParameterBaseJson {
|
||||
index: number;
|
||||
}
|
||||
|
||||
registry.register(
|
||||
registry.Type.EVENT, eventUtils.PROCEDURE_PARAMETER_DELETE,
|
||||
ProcedureParameterDelete);
|
||||
@@ -1,105 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import type {IParameterModel} from '../interfaces/i_parameter_model.js';
|
||||
import type {IProcedureModel} from '../interfaces/i_procedure_model.js';
|
||||
import * as registry from '../registry.js';
|
||||
import type {Workspace} from '../workspace.js';
|
||||
|
||||
import {ProcedureParameterBase, ProcedureParameterBaseJson} from './events_procedure_parameter_base.js';
|
||||
import * as eventUtils from './utils.js';
|
||||
|
||||
/**
|
||||
* Notifies listeners that a procedure parameter was renamed.
|
||||
*/
|
||||
export class ProcedureParameterRename extends ProcedureParameterBase {
|
||||
/** A string used to check the type of the event. */
|
||||
type = eventUtils.PROCEDURE_PARAMETER_RENAME;
|
||||
|
||||
/** The new name of the procedure parameter. */
|
||||
private readonly newName: string;
|
||||
|
||||
/** @param oldName The old name of the procedure parameter. */
|
||||
constructor(
|
||||
workspace: Workspace, procedure: IProcedureModel,
|
||||
parameter: IParameterModel, public readonly oldName: string) {
|
||||
super(workspace, procedure, parameter);
|
||||
|
||||
this.newName = parameter.getName();
|
||||
}
|
||||
|
||||
run(forward: boolean) {
|
||||
const parameterModel = findMatchingParameter(
|
||||
this.getEventWorkspace_(), this.model.getId(), this.parameter.getId());
|
||||
if (!parameterModel) {
|
||||
throw new Error(
|
||||
'Cannot rename a parameter that does not exist ' +
|
||||
'in the procedure map');
|
||||
}
|
||||
if (forward) {
|
||||
parameterModel.setName(this.newName);
|
||||
} else {
|
||||
parameterModel.setName(this.oldName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the event as JSON.
|
||||
*
|
||||
* @returns JSON representation.
|
||||
*/
|
||||
toJson(): ProcedureParameterRenameJson {
|
||||
const json = super.toJson() as ProcedureParameterRenameJson;
|
||||
json['oldName'] = this.oldName;
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the JSON event.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
static fromJson(json: ProcedureParameterRenameJson, workspace: Workspace):
|
||||
ProcedureParameterRename {
|
||||
const model = workspace.getProcedureMap().get(json['procedureId']);
|
||||
if (!model) {
|
||||
throw new Error(
|
||||
'Cannot deserialize procedure delete event because the ' +
|
||||
'target procedure does not exist');
|
||||
}
|
||||
const param = findMatchingParameter(
|
||||
workspace, json['procedureId'], json['parameterId']);
|
||||
if (!param) {
|
||||
throw new Error(
|
||||
'Cannot deserialize parameter rename event because the ' +
|
||||
'target parameter does not exist');
|
||||
}
|
||||
return new ProcedureParameterRename(
|
||||
workspace, model, param, json['oldName']);
|
||||
}
|
||||
}
|
||||
|
||||
function findMatchingParameter(
|
||||
workspace: Workspace, modelId: string, paramId: string): IParameterModel|
|
||||
undefined {
|
||||
const procedureModel = workspace.getProcedureMap().get(modelId);
|
||||
if (!procedureModel) {
|
||||
throw new Error(
|
||||
'Cannot rename the parameter of a procedure that does not exist ' +
|
||||
'in the procedure map');
|
||||
}
|
||||
return procedureModel.getParameters().find((p) => p.getId() === paramId);
|
||||
}
|
||||
|
||||
|
||||
export interface ProcedureParameterRenameJson extends
|
||||
ProcedureParameterBaseJson {
|
||||
oldName: string;
|
||||
}
|
||||
|
||||
registry.register(
|
||||
registry.Type.EVENT, eventUtils.PROCEDURE_PARAMETER_RENAME,
|
||||
ProcedureParameterRename);
|
||||
@@ -1,78 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import type {IProcedureModel} from '../interfaces/i_procedure_model.js';
|
||||
import * as registry from '../registry.js';
|
||||
import type {Workspace} from '../workspace.js';
|
||||
|
||||
import {ProcedureBase, ProcedureBaseJson} from './events_procedure_base.js';
|
||||
import * as eventUtils from './utils.js';
|
||||
|
||||
/** Notifies listeners that a procedure model has been renamed. */
|
||||
export class ProcedureRename extends ProcedureBase {
|
||||
/** A string used to check the type of the event. */
|
||||
type = eventUtils.PROCEDURE_RENAME;
|
||||
|
||||
private newName: string;
|
||||
|
||||
/** @param oldName The old name of the procedure model. */
|
||||
constructor(
|
||||
workspace: Workspace, model: IProcedureModel,
|
||||
public readonly oldName: string) {
|
||||
super(workspace, model);
|
||||
|
||||
this.newName = model.getName();
|
||||
}
|
||||
|
||||
run(forward: boolean) {
|
||||
const procedureModel =
|
||||
this.getEventWorkspace_().getProcedureMap().get(this.model.getId());
|
||||
if (!procedureModel) {
|
||||
throw new Error(
|
||||
'Cannot change the type of a procedure that does not exist ' +
|
||||
'in the procedure map');
|
||||
}
|
||||
if (forward) {
|
||||
procedureModel.setName(this.newName);
|
||||
} else {
|
||||
procedureModel.setName(this.oldName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the event as JSON.
|
||||
*
|
||||
* @returns JSON representation.
|
||||
*/
|
||||
toJson(): ProcedureRenameJson {
|
||||
const json = super.toJson() as ProcedureRenameJson;
|
||||
json['oldName'] = this.oldName;
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the JSON event.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
static fromJson(json: ProcedureRenameJson, workspace: Workspace):
|
||||
ProcedureRename {
|
||||
const model = workspace.getProcedureMap().get(json['procedureId']);
|
||||
if (!model) {
|
||||
throw new Error(
|
||||
'Cannot deserialize procedure rename event because the ' +
|
||||
'target procedure does not exist');
|
||||
}
|
||||
return new ProcedureRename(workspace, model, json['oldName']);
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProcedureRenameJson extends ProcedureBaseJson {
|
||||
oldName: string;
|
||||
}
|
||||
|
||||
registry.register(
|
||||
registry.Type.EVENT, eventUtils.PROCEDURE_RENAME, ProcedureRename);
|
||||
@@ -240,30 +240,6 @@ export const COMMENT_MOVE = 'comment_move';
|
||||
*/
|
||||
export const FINISHED_LOADING = 'finished_loading';
|
||||
|
||||
/** Name of event that creates a procedure model. */
|
||||
export const PROCEDURE_CREATE = 'procedure_create';
|
||||
|
||||
/** Name of event that deletes a procedure model. */
|
||||
export const PROCEDURE_DELETE = 'procedure_delete';
|
||||
|
||||
/** Name of event that renames a procedure model. */
|
||||
export const PROCEDURE_RENAME = 'procedure_rename';
|
||||
|
||||
/** Name of event that enables/disables a procedure model. */
|
||||
export const PROCEDURE_ENABLE = 'procedure_enable';
|
||||
|
||||
/** Name of event that changes the returntype of a procedure model. */
|
||||
export const PROCEDURE_CHANGE_RETURN = 'procedure_change_return';
|
||||
|
||||
/** Name of event that creates a procedure parameter. */
|
||||
export const PROCEDURE_PARAMETER_CREATE = 'procedure_parameter_create';
|
||||
|
||||
/** Name of event that deletes a procedure parameter. */
|
||||
export const PROCEDURE_PARAMETER_DELETE = 'procedure_parameter_delete';
|
||||
|
||||
/** Name of event that renames a procedure parameter. */
|
||||
export const PROCEDURE_PARAMETER_RENAME = 'procedure_parameter_rename';
|
||||
|
||||
/**
|
||||
* Type of events that cause objects to be bumped back into the visible
|
||||
* portion of the workspace.
|
||||
|
||||
@@ -38,11 +38,6 @@ export class ObservableParameterModel implements IParameterModel {
|
||||
this.variable =
|
||||
this.workspace.getVariable(name) ?? this.workspace.createVariable(name);
|
||||
triggerProceduresUpdate(this.workspace);
|
||||
if (this.shouldFireEvents) {
|
||||
eventUtils.fire(
|
||||
new (eventUtils.get(eventUtils.PROCEDURE_PARAMETER_RENAME))(
|
||||
this.workspace, this.procedureModel, this, oldName));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,8 +24,6 @@ export class ObservableProcedureMap extends
|
||||
override set(id: string, proc: IProcedureModel): this {
|
||||
if (this.get(id) === proc) return this;
|
||||
super.set(id, proc);
|
||||
eventUtils.fire(new (eventUtils.get(eventUtils.PROCEDURE_CREATE))(
|
||||
this.workspace, proc));
|
||||
if (isObservable(proc)) proc.startPublishing();
|
||||
return this;
|
||||
}
|
||||
@@ -39,8 +37,6 @@ export class ObservableProcedureMap extends
|
||||
const existed = super.delete(id);
|
||||
if (!existed) return existed;
|
||||
triggerProceduresUpdate(this.workspace);
|
||||
eventUtils.fire(new (eventUtils.get(eventUtils.PROCEDURE_DELETE))(
|
||||
this.workspace, proc));
|
||||
if (isObservable(proc)) proc.stopPublishing();
|
||||
return existed;
|
||||
}
|
||||
@@ -53,8 +49,6 @@ export class ObservableProcedureMap extends
|
||||
for (const id of this.keys()) {
|
||||
const proc = this.get(id);
|
||||
super.delete(id);
|
||||
eventUtils.fire(new (eventUtils.get(eventUtils.PROCEDURE_DELETE))(
|
||||
this.workspace, proc));
|
||||
}
|
||||
triggerProceduresUpdate(this.workspace);
|
||||
}
|
||||
|
||||
@@ -36,10 +36,6 @@ export class ObservableProcedureModel implements IProcedureModel {
|
||||
const prevName = this.name;
|
||||
this.name = name;
|
||||
if (this.shouldTriggerUpdates) triggerProceduresUpdate(this.workspace);
|
||||
if (this.shouldFireEvents) {
|
||||
eventUtils.fire(new (eventUtils.get(eventUtils.PROCEDURE_RENAME))(
|
||||
this.workspace, this, prevName));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -65,11 +61,6 @@ export class ObservableProcedureModel implements IProcedureModel {
|
||||
}
|
||||
|
||||
if (this.shouldTriggerUpdates) triggerProceduresUpdate(this.workspace);
|
||||
if (this.shouldFireEvents) {
|
||||
eventUtils.fire(
|
||||
new (eventUtils.get(eventUtils.PROCEDURE_PARAMETER_CREATE))(
|
||||
this.workspace, this, parameterModel, index));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -83,12 +74,6 @@ export class ObservableProcedureModel implements IProcedureModel {
|
||||
if (isObservable(oldParam)) {
|
||||
oldParam.stopPublishing();
|
||||
}
|
||||
|
||||
if (this.shouldFireEvents) {
|
||||
eventUtils.fire(
|
||||
new (eventUtils.get(eventUtils.PROCEDURE_PARAMETER_DELETE))(
|
||||
this.workspace, this, oldParam, index));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -112,10 +97,6 @@ export class ObservableProcedureModel implements IProcedureModel {
|
||||
const oldReturnTypes = this.returnTypes;
|
||||
this.returnTypes = types;
|
||||
if (this.shouldTriggerUpdates) triggerProceduresUpdate(this.workspace);
|
||||
if (this.shouldFireEvents) {
|
||||
eventUtils.fire(new (eventUtils.get(eventUtils.PROCEDURE_CHANGE_RETURN))(
|
||||
this.workspace, this, oldReturnTypes));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -127,10 +108,6 @@ export class ObservableProcedureModel implements IProcedureModel {
|
||||
if (enabled === this.enabled) return this;
|
||||
this.enabled = enabled;
|
||||
if (this.shouldTriggerUpdates) triggerProceduresUpdate(this.workspace);
|
||||
if (this.shouldFireEvents) {
|
||||
eventUtils.fire(new (eventUtils.get(eventUtils.PROCEDURE_ENABLE))(
|
||||
this.workspace, this));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user