Way to know when domToWorkspace completes

85 views
Skip to first unread message

Nick Allred

unread,
Jun 24, 2018, 11:05:13 AM6/24/18
to Blockly
The call to domToWorkspace seems to be asynchronous.  After you've called domToWorkspace "Create" and "Move" events are fired as the workspace is being setup.

I can't figure out how I can tell that the workspace has finished loading?  domToWorkspace returns an array of blockIds, so I could potentially use that information in combination with the Create and Move events to determine when loading is done, but it seems like there should be a simpler way to figure this out.

Nick Allred

unread,
Jun 24, 2018, 11:25:47 AM6/24/18
to Blockly
For what its worth, this seems to work to trigger an event after all the Move/Create events caused by the call to domToWorkspace
Blockly.Xml.domToWorkspace(Blockly.Xml.textToDom(xml), this._workspace)
setTimeout(() => this._workspace.fireChangeListener('DONE'))


I'm not confident that this is reliable, though.

Rachel Fenichel

unread,
Jun 25, 2018, 2:31:40 PM6/25/18
to Blockly
Is the goal to know when all of the events have been fired, or to know when the blocks have all been created?

Only one piece of block creation is on a setTimeout, and that's a detail of how we track connections while dragging.  

Events get added to a queue and fired slightly later, but all of the blocks should have been created and moved into place by the time the function returns.  

Cheers,
Rachel

Nick Allred

unread,
Jun 25, 2018, 4:05:55 PM6/25/18
to Blockly

Yea, I've realized that once the function returns the workspace is completely setup, its just the events that are occurring asynchronously.

The goal is to know when all of the events have been fired.  When events fire I use that to determine if I should save.  My issue has been that when I load a program I was calling my "save" function multiple times as the events triggered from domToWorkspace occur.  As the events occur they build up state in my application, so if I call "save" before all the triggered events have been called I end up calling "save" with incomplete data, and then eventually with the correct original data that was loaded.  Its the intermediate calls to "save" that are messing things up higher up in my application.

I need to be able to know when all the creation events that were caused by loading a workspace are done so that I can resume calling my "save" function like normal.

Using a setTimeout and calling "fireChangeListener" myself to know when the events are finished has been the easiest way to work through this (and may be the correct way?)

I just didn't find anything in the code that seemed to address this situation so I thought I'd ask!

Thanks!
Nick

Rachel Fenichel

unread,
Jun 25, 2018, 6:41:24 PM6/25/18
to Blockly
I suppose you could make a new event type and shove an event of that type into the queue as soon as the call finishes.  Then you would listen to all events and when you saw your event come back out, you would know that the queue had been fully flushed.

You could also maybe use a UI event for it and just shove some information into it so that you can parse it.

There's nothing that will do it directly, though.

Rachel
Reply all
Reply to author
Forward
0 new messages