forked from google/blockly
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add JSO serialization and deserialization of variables (google#…
…5131) * Add variable serialization tests * Fix requires for new file * Add serialization and deserialization of variables * Remove only in tests * Cleanup
- Loading branch information
1 parent
56d3cb6
commit 28ff2c8
Showing
5 changed files
with
95 additions
and
10 deletions.
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
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,62 @@ | ||
/** | ||
* @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 VariableModel = goog.requireType('Blockly.VariableModel'); | ||
// eslint-disable-next-line no-unused-vars | ||
const Workspace = goog.requireType('Blockly.Workspace'); | ||
|
||
|
||
/** | ||
* Represents the state of a given variable. | ||
* @typedef {{ | ||
* name: string, | ||
* id: string, | ||
* type: (string|undefined), | ||
* }} | ||
*/ | ||
var State; | ||
exports.State = State; | ||
|
||
/** | ||
* Returns the state of the variable as a plain JavaScript object. | ||
* @param {!VariableModel} variableModel The variable to serialize. | ||
* @return {!State} The serialized state of the variable. | ||
*/ | ||
const save = function(variableModel) { | ||
const state = { | ||
'name': variableModel.name, | ||
'id': variableModel.getId() | ||
}; | ||
if (variableModel.type) { | ||
state['type'] = variableModel.type; | ||
} | ||
return state; | ||
}; | ||
/** @package */ | ||
exports.save = save; | ||
|
||
/** | ||
* Loads the variable represented by the given state into the given workspace. | ||
* Do not call this directly, use workspace.createVariable instead. | ||
* @param {!State} state The state of a variable to deserialize into the | ||
* workspace. | ||
* @param {!Workspace} workspace The workspace to add the variable to. | ||
*/ | ||
const load = function(state, workspace) { | ||
workspace.createVariable(state['name'], state['type'], state['id']); | ||
}; | ||
/** @package */ | ||
exports.load = load; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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