Files
blockly/core/serialization/variables.ts
Christopher Allen b0a7c004a9 refactor(build): Delete Closure Library (#7415)
* fix(build): Restore erroneously-deleted filter function

  This was deleted in PR #7406 as it was mainly being used to
  filter core/ vs. test/mocha/ deps into separate deps files -
  but it turns out also to be used for filtering error
  messages too.  Oops.

* refactor(tests): Migrate advanced compilation test to ES Modules

* refactor(build): Migrate main.js to TypeScript

  This turns out to be pretty straight forward, even if it would
  cause crashing if one actually tried to import this module
  instead of just feeding it to Closure Compiler.

* chore(build): Remove goog.declareModuleId calls

  Replace goog.declareModuleId calls with a comment recording the
  former module ID for posterity (or at least until we decide
  how to reformat the renamings file.

* chore(tests): Delete closure/goog/*

  For the moment we still need something to serve as base.js for
  the benefit of closure-make-deps, so we keep a vestigial
  base.js around, containing only the @provideGoog declaration.

* refactor(build): Remove vestigial base.js

  By changing slightly the command line arguments to
  closure-make-deps and closure-calculate-chunks the need to have
  any base.js is eliminated.

* chore: Typo fix for PR #7415
2023-08-31 00:24:47 +01:00

90 lines
2.3 KiB
TypeScript

/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
// Former goog.module ID: Blockly.serialization.variables
import type {ISerializer} from '../interfaces/i_serializer.js';
import type {Workspace} from '../workspace.js';
import * as priorities from './priorities.js';
import * as serializationRegistry from './registry.js';
/**
* Represents the state of a given variable.
*/
export interface State {
name: string;
id: string;
type: string | undefined;
}
/**
* Serializer for saving and loading variable state.
*/
export class VariableSerializer implements ISerializer {
priority: number;
/* eslint-disable-next-line require-jsdoc */
constructor() {
/** The priority for deserializing variables. */
this.priority = priorities.VARIABLES;
}
/**
* Serializes the variables of the given workspace.
*
* @param workspace The workspace to save the variables of.
* @returns The state of the workspace's variables, or null if there are no
* variables.
*/
save(workspace: Workspace): State[] | null {
const variableStates = [];
for (const variable of workspace.getAllVariables()) {
const state = {
'name': variable.name,
'id': variable.getId(),
};
if (variable.type) {
(state as AnyDuringMigration)['type'] = variable.type;
}
variableStates.push(state);
}
// AnyDuringMigration because: Type '{ name: string; id: string; }[] |
// null' is not assignable to type 'State[] | null'.
return (
variableStates.length ? variableStates : null
) as AnyDuringMigration;
}
/**
* Deserializes the variable defined by the given state into the given
* workspace.
*
* @param state The state of the variables to deserialize.
* @param workspace The workspace to deserialize into.
*/
load(state: State[], workspace: Workspace) {
for (const varState of state) {
workspace.createVariable(
varState['name'],
varState['type'],
varState['id'],
);
}
}
/**
* Disposes of any variables that exist on the workspace.
*
* @param workspace The workspace to clear the variables of.
*/
clear(workspace: Workspace) {
workspace.getVariableMap().clear();
}
}
serializationRegistry.register('variables', new VariableSerializer());