Move main workspace to a new file, common.js #5244
Merged
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.
The basics
The details
Resolves
Part of #5208
Proposed Changes
Moves a function and property from
blockly.jsto a new leaf namespace namedBlockly.common, and adds aliases to keep the old function and property accessible.@samelhusseini pointed out that as long as I get rid of the call to
goog.module.declareLegacyNamespace()before the next release,Blockly.commonwill never be externally visible, so an overly broad name likecommondoes not add to our API worries. I therefore propose to move the other parts ofblockly.jsinto this file to help resolve circular dependencies.I did add a call to
declareLegacyNamespaceso that I don't have to simultaneously convert all other files that use these properties and functions. I will need to remove that at the end of the quarter.Blockly.mainWorkspace
I moved the
Blockly.mainWorkspaceto common.js and did not export it. Instead I exported two functions,getMainWorkspaceandsetMainWorkspace. I also aliasedBlockly.getMainWorkspaceto refer toBlockly.common.getMainWorkspace.Getter and setter
In
blockly.jsI usedObject.definePropertyto add a getter and setter formainWorkspaceto theBlocklyobject:Open question: how do I add jsdoc to this?
I confirmed that the getter and setter work by using the playground and setting the property in the console, then verifying that
Blockly.common.mainWorkspaceupdated appropriately.I think this pattern works for dealing with properties that could beset externally. I also think that this property should not be externally visible, and I propose that I add deprecation warnings for both the getter and the setter.
However,
Blockly.getMainWorkspace()should remain public, since we've used it extensively in demos.Initialization change
In
blockly.jsthis property was originally set tonull, and then set to a real workspace as soon as a workspace was injected. Later uses ofmainWorkspacecallgetMainWorkspace, which casts it to a non-nullableWorkspacein order to avoid propagating this initialnullthrough the rest of the codebase.In this case, I instead declared
mainWorkspaceas a non-nullableWorkspace, and set it through a function that accepts a non-nullableWorkspaceas an argument. This removed the need for the cast on the getter. I think this is safe because trying to access the main workspace before it is first set will throw an error, but I want an extra eye on it.Update uses
The second commit updates uses of
Blockly.mainWorkspaceandBlockly.getMainWorkspace()in core to use the versions incommon.js.