fix: JSON deserialization not firing variable create events for blocks (#8122)

* fix: JSON deserialization not firing variable create events for blocks

* fix: extract logic for checking added variables and firing event to checkNewVariables function
This commit is contained in:
Sampada Bhujel
2024-05-16 04:31:23 +05:45
committed by GitHub
parent dfcf5317c1
commit 049993405e

View File

@@ -29,6 +29,8 @@ import {
} from './exceptions.js';
import * as priorities from './priorities.js';
import * as serializationRegistry from './registry.js';
import * as Variables from '../variables.js';
import {VariableModel} from '../variable_model.js';
// TODO(#5160): Remove this once lint is fixed.
/* eslint-disable no-use-before-define */
@@ -417,6 +419,7 @@ export function appendInternal(
}
eventUtils.disable();
const variablesBeforeCreation = workspace.getAllVariables();
let block;
try {
block = appendPrivate(state, workspace, {parentConnection, isShadow});
@@ -424,7 +427,12 @@ export function appendInternal(
eventUtils.enable();
}
// Fire a VarCreate event for each (if any) new variable created.
checkNewVariables(workspace, variablesBeforeCreation);
if (eventUtils.isEnabled()) {
// Block events come after var events, in case they refer to newly created
// variables.
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CREATE))(block));
}
eventUtils.setGroup(existingGroup);
@@ -485,6 +493,33 @@ function appendPrivate(
return block;
}
/**
* Checks the workspace for any new variables that were created during the
* deserialization of a block and fires a VarCreate event for each.
*
* @param workspace The workspace where new variables are being created
* @param originalVariables The array of variables that existed in the workspace
* before adding the new block.
*/
function checkNewVariables(
workspace: Workspace,
originalVariables: VariableModel[],
) {
if (eventUtils.isEnabled()) {
const newVariables = Variables.getAddedVariables(
workspace,
originalVariables,
);
// Fire a VarCreate event for each (if any) new variable created.
for (let i = 0; i < newVariables.length; i++) {
const thisVariable = newVariables[i];
eventUtils.fire(
new (eventUtils.get(eventUtils.VAR_CREATE))(thisVariable),
);
}
}
}
/**
* Applies any coordinate information available on the state object to the
* block.