Files
blockly/core/serialization/variables.js
Beka Westberg e954193009 fix: project cereal cleanup (#5398)
* fix: make exception constructors package

* fix: rename blocks.load to blocks.append

* fix: inline docs

* fix: consistency in block serialization

* fix: remove unexported functions

* fix: interface requires

* fix: tag TODO with issue number
2021-09-20 13:08:35 -07:00

92 lines
2.4 KiB
JavaScript

/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Handles serializing variables to plain JavaScript objects, only
* containing state.
*/
'use strict';
goog.module('Blockly.serialization.variables');
goog.module.declareLegacyNamespace();
// eslint-disable-next-line no-unused-vars
const {ISerializer} = goog.require('Blockly.serialization.ISerializer');
// eslint-disable-next-line no-unused-vars
const Workspace = goog.requireType('Blockly.Workspace');
const priorities = goog.require('Blockly.serialization.priorities');
const serializationRegistry = goog.require('Blockly.serialization.registry');
/**
* Represents the state of a given variable.
* @typedef {{
* name: string,
* id: string,
* type: (string|undefined),
* }}
*/
var State;
exports.State = State;
/**
* Serializer for saving and loading variable state.
* @implements {ISerializer}
*/
class VariableSerializer {
constructor() {
/**
* The priority for deserializing variables.
* @type {number}
*/
this.priority = priorities.VARIABLES;
}
/**
* Serializes the variables of the given workspace.
* @param {!Workspace} workspace The workspace to save the variables of.
* @return {?Array<!State>} The state of the workspace's variables, or null
* if there are no variables.
*/
save(workspace) {
const variableStates = [];
for (const variable of workspace.getAllVariables()) {
const state = {
'name': variable.name,
'id': variable.getId()
};
if (variable.type) {
state['type'] = variable.type;
}
variableStates.push(state);
}
return variableStates.length ? variableStates : null;
}
/**
* Deserializes the variable defined by the given state into the given
* workspace.
* @param {!Array<!State>} state The state of the variables to deserialize.
* @param {!Workspace} workspace The workspace to deserialize into.
*/
load(state, 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} workspace The workspace to clear the variables of.
*/
clear(workspace) {
workspace.getVariableMap().clear();
}
}
serializationRegistry.register('variables', new VariableSerializer());