Backpack now saves state when serializing workspace

33 views
Skip to first unread message

Johnny Oshika

unread,
Jul 25, 2023, 4:02:08 PM7/25/23
to Blockly
I noticed that when using JSON serialization in the workspace, the backpack now includes its state as part of the workspace serialization. For example it looks like this:

{
  "blocks": [],
  "backpack": [/* backpack state */]
}

In my use case, the backpack follows the user, while the blockly workspaces are attached to documents that are shared by many people. So in other words, the backpack and workspace do not live together. So I'm finding myself doing this when serializing the workspace:

const state = Blockly.serialization.workspaces.save(workspace);
delete state['backpack'];
// Now save JSON.stringify(state) to the database

It also seems like Blockly doesn't like it when it gets hydrated with an empty JSON object, so I'm finding myself doing this:

if (!Object.keys(state).length) return;
Blockly.serialization.workspaces.load(state, workspace);


What was the rationale for storing backpack state with workspace serialization? Is there a way to opt out of this?

Thanks,
Johnny

Maribeth Moffatt

unread,
Jul 25, 2023, 6:23:57 PM7/25/23
to Blockly
Hello, you can unregister the serializer called `backpack` with: `Blockly.serialization.registry.unregister('backpack');` right after you initialize the backpack plugin. That would basically undo the results of this.

I've filed https://github.com/google/blockly-samples/issues/1826 to add a configurable behavior to the plugin so that it doesn't register the serializer automatically if you set some option. We would welcome a PR to change this if you'd like to see this happen sooner. 

Maribeth

Johnny Oshika

unread,
Jul 25, 2023, 9:35:37 PM7/25/23
to Blockly
'unregister' works like a charm, thank you Maribeth!

I'm happy to contribute a PR for this option. I left a question on the issue.

Reply all
Reply to author
Forward
0 new messages