mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-08-28 22:30:40 -04:00
Add ability to create cloud variables, and have cloud provider wait for confirmation from server.
This commit is contained in:
parent
0a68722b80
commit
3fc1b89b30
4 changed files with 58 additions and 6 deletions
|
@ -366,7 +366,7 @@ class Blocks {
|
|||
// into a state where a local var was requested for the stage,
|
||||
// create a stage (global) var after checking for name conflicts
|
||||
// on all the sprites.
|
||||
if (e.isLocal && editingTarget && !editingTarget.isStage) {
|
||||
if (e.isLocal && editingTarget && !editingTarget.isStage && !e.isCloud) {
|
||||
if (!editingTarget.lookupVariableById(e.varId)) {
|
||||
editingTarget.createVariable(e.varId, e.varName, e.varType);
|
||||
}
|
||||
|
@ -378,7 +378,7 @@ class Blocks {
|
|||
return;
|
||||
}
|
||||
}
|
||||
stage.createVariable(e.varId, e.varName, e.varType);
|
||||
stage.createVariable(e.varId, e.varName, e.varType, e.isCloud);
|
||||
}
|
||||
break;
|
||||
case 'var_rename':
|
||||
|
|
|
@ -322,7 +322,7 @@ class Runtime extends EventEmitter {
|
|||
/** @type {Object.<string, Object>} */
|
||||
this.ioDevices = {
|
||||
clock: new Clock(),
|
||||
cloud: new Cloud(),
|
||||
cloud: new Cloud(this),
|
||||
deviceManager: new DeviceManager(),
|
||||
keyboard: new Keyboard(this),
|
||||
mouse: new Mouse(this),
|
||||
|
|
|
@ -233,11 +233,16 @@ class Target extends EventEmitter {
|
|||
* @param {string} id Id of variable
|
||||
* @param {string} name Name of variable.
|
||||
* @param {string} type Type of variable, '', 'broadcast_msg', or 'list'
|
||||
* @param {boolean} isCloud Whether the variable to create has the isCloud flag set.
|
||||
* Additional checks are made that the variable can be created as a cloud variable.
|
||||
*/
|
||||
createVariable (id, name, type) {
|
||||
createVariable (id, name, type, isCloud) {
|
||||
if (!this.variables.hasOwnProperty(id)) {
|
||||
const newVariable = new Variable(id, name, type, false);
|
||||
this.variables[id] = newVariable;
|
||||
if (isCloud && this.isStage) {
|
||||
this.runtime.ioDevices.cloud.requestCreateCloudVariable(newVariable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -26,6 +26,13 @@ class Cloud {
|
|||
* @property {(number | string)} value The scalar value to update the variable with
|
||||
*/
|
||||
|
||||
/**
|
||||
* Part of a cloud io data post indicating a cloud variable was successfully
|
||||
* created.
|
||||
* @typedef {object} VarCreateData
|
||||
* @property {string} name The name of the variable to create
|
||||
*/
|
||||
|
||||
/**
|
||||
* A cloud io data post message.
|
||||
* @typedef {object} CloudIOData
|
||||
|
@ -38,8 +45,9 @@ class Cloud {
|
|||
* Cloud IO Device responsible for sending and receiving messages from
|
||||
* cloud provider (mananging the cloud server connection) and interacting
|
||||
* with cloud variables in the current project.
|
||||
* @param {Runtime} runtime The runtime context for this cloud io device.
|
||||
*/
|
||||
constructor () {
|
||||
constructor (runtime) {
|
||||
/**
|
||||
* Reference to the cloud data provider, responsible for mananging
|
||||
* the web socket connection to the cloud data server.
|
||||
|
@ -47,6 +55,12 @@ class Cloud {
|
|||
*/
|
||||
this.provider = null;
|
||||
|
||||
/**
|
||||
* Reference to the runtime that owns this cloud io device.
|
||||
* @type {!Runtime}
|
||||
*/
|
||||
this.runtime = runtime;
|
||||
|
||||
/**
|
||||
* Reference to the stage target which owns the cloud variables
|
||||
* in the project.
|
||||
|
@ -80,6 +94,21 @@ class Cloud {
|
|||
if (data.varUpdate) {
|
||||
this.updateCloudVariable(data.varUpdate);
|
||||
}
|
||||
|
||||
if (data.varCreate) {
|
||||
this.createCloudVariable(data.varCreate);
|
||||
}
|
||||
}
|
||||
|
||||
requestCreateCloudVariable (variable) {
|
||||
if (this.runtime.canAddCloudVariable()) {
|
||||
if (this.provider) {
|
||||
this.provider.createVariable(variable.name, variable.value);
|
||||
// We'll set the cloud flag and update the
|
||||
// cloud variable limit when we actually
|
||||
// get a confirmation from the cloud data server
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -94,10 +123,28 @@ class Cloud {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a cloud variable based on the message
|
||||
* received from the cloud provider.
|
||||
* @param {VarCreateData} varCreate A {@link VarCreateData} object received from the
|
||||
* cloud data provider confirming the creation of a cloud variable,
|
||||
* providing its name and value.
|
||||
*/
|
||||
createCloudVariable (varCreate) {
|
||||
const varName = varCreate.name;
|
||||
|
||||
const variable = this.stage.lookupVariableByNameAndType(varName, Variable.SCALAR_TYPE);
|
||||
if (!variable) {
|
||||
log.error(`Could not find cloud variable with name: ${varName}`);
|
||||
}
|
||||
variable.isCloud = true;
|
||||
this.runtime.addCloudVariable();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a cloud variable in the runtime based on the message received
|
||||
* from the cloud provider.
|
||||
* @param {VarUpdateData} varUpdate A {@link VarUpdateData} object describing
|
||||
* @param {VarData} varUpdate A {@link VarData} object describing
|
||||
* a cloud variable update received from the cloud data provider.
|
||||
*/
|
||||
updateCloudVariable (varUpdate) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue