MCPcopy
hub / github.com/RaspberryPiFoundation/blockly / createVariable

Function createVariable

packages/blockly/core/variables.ts:732–761  ·  view source on GitHub ↗

* Helper function to create a variable on the given workspace. * * @param workspace The workspace in which to create the variable. It may be a * flyout workspace or main workspace. * @param id The ID to use to create the variable, or null. * @param opt_name The string to use to create the

(
  workspace: Workspace,
  id: string | null,
  opt_name?: string,
  opt_type?: string,
)

Source from the content-addressed store, hash-verified

730 * combination.
731 */
732function createVariable(
733 workspace: Workspace,
734 id: string | null,
735 opt_name?: string,
736 opt_type?: string,
737): IVariableModel<IVariableState> {
738 const variableMap = workspace.getVariableMap();
739 const potentialVariableMap = workspace.getPotentialVariableMap();
740 // Variables without names get uniquely named for this workspace.
741 if (!opt_name) {
742 const ws = workspace.isFlyout
743 ? (workspace as WorkspaceSvg).targetWorkspace
744 : workspace;
745 opt_name = generateUniqueName(ws!);
746 }
747
748 // Create a potential variable if in the flyout.
749 let variable = null;
750 if (potentialVariableMap) {
751 variable = potentialVariableMap.createVariable(
752 opt_name,
753 opt_type,
754 id ?? undefined,
755 );
756 } else {
757 // In the main workspace, create a real variable.
758 variable = variableMap.createVariable(opt_name, opt_type, id);
759 }
760 return variable;
761}
762
763/**
764 * Helper function to get the list of variables that have been added to the

Callers 1

Calls 4

generateUniqueNameFunction · 0.85
getVariableMapMethod · 0.80
createVariableMethod · 0.65

Tested by

no test coverage detected