How to call a function after workspace load finished?

297 views
Skip to first unread message

Agrawal

unread,
May 20, 2022, 3:15:22 AM5/20/22
to Blockly
Hello All,

I want to perform some action after workspace load finished. I am currently using 'Blockly.Events.FINISHED_LOADING' with the help of 'Blockly.mainWorkspace.addChangeListener' and it is working fine but the main problem is that the change listener event is calling at every change in workspace, which is making application a bit slower. So is there any way that I can call a function on finished loading without impacting the performance and it should call only when workspace load finished?
Please help.

Regards,
Agrawal

Neil Fraser

unread,
May 20, 2022, 3:24:34 AM5/20/22
to blo...@googlegroups.com
Could you have your event handler call Blockly.mainWorkspace.removeChangeListener once you see the FINISHED_LOADING event?

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/b8c2578a-51ca-4228-aaba-8ac62046649an%40googlegroups.com.


--

Agrawal

unread,
May 20, 2022, 3:51:56 AM5/20/22
to Blockly
Yes, but the problem is in between loading start and loading end because there are thousands of blocks are present at the time of loading and the add and change events are calling thousand times before workspace load complete. 

Jason Schanker

unread,
May 20, 2022, 2:37:06 PM5/20/22
to Blockly
Hi,

Just to clarify, you're writing something like this:

Blockly.mainWorkspace.addChangeListener(e => {
  if (e.type === Blockly.Events.FINISHED_LOADING) {
    // Code to perform action
  }
});

And the function that adds the change listener only gets called once?  If so, I'm surprised that a simple function call and check like this, even if repeated thousands of time, would cause any noticeable performance degradation.

However, if this is really the case, a potential but fragile workaround based on the current implementation would be writing the following immediately after the call to Blockly.serialization.workspaces.load :

setTimeout(() => {
  // Code to perform action
}, 0);

I believe this should generally work based on the current implementation because the FINISHED_LOADING event is synchronously fired at the end of the function definition on  https://github.com/google/blockly/blob/2bf842ac65456e61d8c655daec99f8fbc75d2c29/core/serialization/workspaces.js#L98.  The reason for the setTimeout then is because calling fire adds the event to a queue and listeners for these events will be called asynchronously (See: https://github.com/google/blockly/blob/2bf842ac65456e61d8c655daec99f8fbc75d2c29/core/events/utils.js#L331).  However, the workaround may not result in consistent behavior because given the following:

setTimeout(functionA, 0);
setTimeout(functionB, 0);


Best,
Jason
Reply all
Reply to author
Forward
0 new messages