-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add basic observable implementations of procedure interfaces #6489
Merged
BeksOmega
merged 13 commits into
google:develop
from
BeksOmega:feat/procedure-models-implementions
Oct 13, 2022
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6d995d9
feat: implement basic observable procedure model
BeksOmega b7495ab
feat: implement basic observable procedure model
BeksOmega 0de5d02
feat: implement basic observable parameter model
BeksOmega ca0c48c
feat: implement basic observable procedure map
BeksOmega e26a398
chore: format
BeksOmega 8c536fa
chore: refactor parameter model
BeksOmega 22b0e01
chore: update the observable procedure model to match interface
BeksOmega 1a3c6dc
chore: update the observable parameter model to match interface
BeksOmega c882325
chore: update the observable procedure map
BeksOmega acdf6a4
chore: update concrete implementations to use this return type
BeksOmega c8a319f
chore: format
BeksOmega d44b501
chore: remove legacy module IDs
BeksOmega cfb5335
chore: fix typo
BeksOmega File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* @license | ||
* Copyright 2022 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import type {IParameterModel} from '../interfaces/i_parameter_model.js'; | ||
import {genUid} from '../utils/idgenerator.js'; | ||
import type {VariableModel} from '../variable_model.js'; | ||
import type {Workspace} from '../workspace.js'; | ||
|
||
|
||
export class ObservableParameterModel implements IParameterModel { | ||
private id: string; | ||
private variable: VariableModel; | ||
|
||
constructor( | ||
private readonly workspace: Workspace, name: string, id?: string) { | ||
this.id = id ?? genUid(); | ||
this.variable = workspace.createVariable(name); | ||
} | ||
|
||
/** | ||
* Sets the name of this parameter to the given name. | ||
*/ | ||
setName(name: string): this { | ||
if (name == this.variable.name) return this; | ||
this.variable = | ||
this.workspace.getVariable(name) ?? this.workspace.createVariable(name); | ||
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. | ||
*/ | ||
setTypes(_types: string[]): this { | ||
console.warn( | ||
'The built-in ParameterModel does not support typing. You need to ' + | ||
'implement your own custom ParameterModel.'); | ||
cpcallen marked this conversation as resolved.
Show resolved
Hide resolved
BeksOmega marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return this; | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* @license | ||
* Copyright 2022 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import type {IProcedureModel} from '../interfaces/i_procedure_model.js'; | ||
import type {Workspace} from '../workspace.js'; | ||
|
||
|
||
export class ObservableProcedureMap extends Map<string, IProcedureModel> { | ||
constructor(private readonly workspace: Workspace) { | ||
BeksOmega marked this conversation as resolved.
Show resolved
Hide resolved
|
||
super(); | ||
} | ||
|
||
/** | ||
* Adds the given procedure model to the procedure map. | ||
*/ | ||
override set(id: string, proc: IProcedureModel): this { | ||
// TODO(#6156): Fire events. | ||
super.set(id, proc); | ||
return this; | ||
} | ||
|
||
/** | ||
* Deletes the ProcedureModel with the given ID from the procedure map (if it | ||
* exists). | ||
*/ | ||
override delete(id: string): boolean { | ||
// TODO(#6156): Fire events. | ||
return super.delete(id); | ||
} | ||
|
||
/** | ||
* Removes all ProcedureModels from the procedure map. | ||
*/ | ||
override clear() { | ||
// TODO(#6156): Fire events. | ||
super.clear(); | ||
} | ||
|
||
/** | ||
* Adds the given ProcedureModel to the map of procedure models, so that | ||
* blocks can find it. | ||
*/ | ||
add(proc: IProcedureModel): this { | ||
// TODO(#6156): Fire events. | ||
return this.set(proc.getId(), proc); | ||
} | ||
cpcallen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* @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 type {Workspace} from '../workspace.js'; | ||
import {genUid} from '../utils/idgenerator.js'; | ||
|
||
|
||
export class ObservableProcedureModel implements IProcedureModel { | ||
private id: string; | ||
private name = ''; | ||
private parameters: IParameterModel[] = []; | ||
private returnTypes: string[]|null = null; | ||
private enabled = true; | ||
|
||
constructor(private readonly workspace: Workspace, id?: string) { | ||
this.id = id ?? genUid(); | ||
} | ||
|
||
/** Sets the human-readable name of the procedure. */ | ||
setName(name: string): this { | ||
this.name = name; | ||
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 { | ||
this.parameters.splice(index, 0, parameterModel); | ||
return this; | ||
} | ||
|
||
/** Removes the parameter at the given index from the parameter list. */ | ||
deleteParameter(index: number): this { | ||
this.parameters.splice(index, 1); | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the return type(s) of the procedure. | ||
* | ||
* Pass null to represent a procedure that does not return. | ||
*/ | ||
setReturnTypes(types: string[]|null): this { | ||
this.returnTypes = types; | ||
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 { | ||
this.enabled = enabled; | ||
return this; | ||
} | ||
|
||
/** 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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this try
.getVariable(name)
first, likesetName
does? And is there a way of doing that by callingsetName
—i.e., can tsc infer (or be made to infer) thatsetName
does the initialisation on behalf of the constructor?(Aside: the latter is the sort of thing I bet Ezno can handle no problem, no special annotation required…)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will look into this.