Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: procedure parameter serialization #2125

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions plugins/block-shareable-procedures/src/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,11 @@ const procedureDefMutator = {
const newParams = state['params'] ?? [];
const newIds = new Set(newParams.map((p) => p.id));
const currParams = model.getParameters();
for (let i = currParams.length - 1; i >= 0; i--) {
if (!newIds.has(currParams[i].getId)) {
model.deleteParameter(i);
if (state['fullSerialization']) {
for (let i = currParams.length - 1; i >= 0; i--) {
if (!newIds.has(currParams[i].getId)) {
model.deleteParameter(i);
}
}
}
for (let i = 0; i < newParams.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ const {testHelpers} = require('@blockly/dev-tools');
const {ObservableParameterModel} = require('../src/observable_parameter_model');
const {ObservableProcedureModel} = require('../src/observable_procedure_model');
const {blocks} = require('../src/blocks');
const {unregisterProcedureBlocks} = require('../src/index');
const {
unregisterProcedureBlocks,
registerProcedureSerializer,
} = require('../src/index');
const {ProcedureDelete} = require('../src/events_procedure_delete');
const {ProcedureCreate} = require('../src/events_procedure_create');

Expand All @@ -37,6 +40,8 @@ suite('Procedures', function () {
unregisterProcedureBlocks();
Blockly.common.defineBlocks(blocks);

registerProcedureSerializer();

this.workspace = Blockly.inject('blocklyDiv', {});

this.eventSpy = this.sandbox.spy();
Expand Down Expand Up @@ -1969,13 +1974,6 @@ suite('Procedures', function () {
type: 'procedures_defnoreturn',
extraState: {
procedureId: 'procId',
params: [
{
name: 'x',
id: 'varId',
paramId: 'paramId',
},
],
},
fields: {
NAME: 'do something',
Expand Down Expand Up @@ -2012,67 +2010,6 @@ suite('Procedures', function () {
['varId'],
);
});

test(
'multiple definitions pointing to the same model end up with ' +
'different models',
function () {
Blockly.serialization.workspaces.load(
{
blocks: {
languageVersion: 0,
blocks: [
{
type: 'procedures_defnoreturn',
extraState: {
procedureId: 'procId',
},
fields: {
NAME: 'do something',
},
},
{
type: 'procedures_defnoreturn',
y: 10,
extraState: {
procedureId: 'procId',
},
fields: {
NAME: 'do something',
},
},
],
},
procedures: [
{
id: 'procId',
name: 'do something',
returnTypes: null,
},
],
},
this.workspace,
);
const def1 = this.workspace.getTopBlocks(true)[0];
const def2 = this.workspace.getTopBlocks(true)[1];
chai.assert.equal(
def1.getProcedureModel().getName(),
'do something',
'Expected the first procedure definition to have the ' +
'name in XML',
);
chai.assert.equal(
def2.getProcedureModel().getName(),
'do something2',
'Expected the second procedure definition to be renamed',
);
chai.assert.notEqual(
def1.getProcedureModel(),
def2.getProcedureModel(),
'Expected the procedures to have different models',
);
},
);
});

suite('getDefinition', function () {
Expand Down
Loading