How does config.loadModule work?

130 views
Skip to first unread message

Shane Dev

unread,
Sep 18, 2021, 4:47:19 AM9/18/21
to Ajax.org Cloud9 Editor (Ace)

Hello,

I have been debugging a simple project based on src-noconflict, see the demo here. From debugging the function config.loadModule, I see it resolves and downloads the module script with net.loadScript but how is module script executed? I assumed this was done by afterLoad callback but when I place a breakpoint in first line of the module script, it breaks there before afterLoad is called and in the call stack I see only "global". Can anyone enlighten me?

Harutyun Amirjanyan

unread,
Sep 18, 2021, 7:48:07 AM9/18/21
to ace-d...@googlegroups.com
Hi
net.laodScript adds a script tag,
browser loads and evaluates the script tag, 
and after that calls the onload handler of the script https://github.com/ajaxorg/ace/blob/master/lib/ace/lib/net.js#L36
which finally calls the afterLoad callback

Shane Dev

unread,
Sep 18, 2021, 10:13:25 AM9/18/21
to Ajax.org Cloud9 Editor (Ace)
Thanks for your reply Harutyun. The module mode script is executed before afterLoad and the debugger Call stack (see below) does not indicate what called it. How is it executed?

mode-json_debug.png

Harutyun Amirjanyan

unread,
Sep 18, 2021, 2:45:45 PM9/18/21
to ace-d...@googlegroups.com
Because there is nothing calling it, ace creates a script tag, and the browser itself calls it when the script tag is loaded.

Shane Dev

unread,
Sep 19, 2021, 6:29:43 AM9/19/21
to Ajax.org Cloud9 Editor (Ace)
Hi Harutyun, does the afterLoad callback fire the onload events defined in edit_session.setMode? Normally I would expect `require` function in first line of the callback to trigger in the insertion of `script` tag in the html document which calls the wrapped code. However this was already completed by the previously called net.loadScript so what is its real purpose?

Harutyun Amirjanyan

unread,
Sep 19, 2021, 9:02:24 AM9/19/21
to ace-d...@googlegroups.com
When the script is already loaded require simply invokes the callback with the loaded value of the module

Shane Dev

unread,
Sep 19, 2021, 11:04:46 AM9/19/21
to Ajax.org Cloud9 Editor (Ace)
I observe (see xxx console log messages below) that the module script mode-json.js is invoked before the afterLoad callback -

xxx net.loadScript mode-json.js downloaded ace.js:3389:10
xxx mode-json.js invoked mode-json.js:1:9
xxx loadModule afterLoad invoked ace.js:3772:11
xxx loadModule afterLoad require invoked ace.js:3774:12
xxx worker-json.js invoked worker-json.js:2:9
xxx worker-json.js invoked worker-json.js:2:9

If comment out the require in the afterLoad callback then worker-json.js is not invoked at all. I stepped through this require but I can't see where worker-json.js is resolved and invoked - do you know?

Harutyun Amirjanyan

unread,
Sep 19, 2021, 4:33:12 PM9/19/21
to ace-d...@googlegroups.com
On the line where you have `console.log('xxx net.loadScript mode-json.js downloaded')` the script is not downloaded yet
only the tag is created, 

worker-json is not be handled by loadModule because it is loaded in WebWorker, search for `new Worker(` in ace.js

Shane Dev

unread,
Sep 19, 2021, 4:48:07 PM9/19/21
to Ajax.org Cloud9 Editor (Ace)
You're right, `head.appendChild(s);` downloads the script. I have updated code but it does change the order the xxx console log messages. I found the Worker related code but it is very deep in the call stack

Harutyun Amirjanyan

unread,
Sep 19, 2021, 4:53:39 PM9/19/21
to ace-d...@googlegroups.com
The script is downloaded later than that, afaik browser does not emit any events after downloading and before evaluating the script.
Could you maybe tell what problem you are trying to solve? because I am not sure if I understand your question, everything seems to work as expected.

Shane Dev

unread,
Sep 20, 2021, 1:29:17 AM9/20/21
to Ajax.org Cloud9 Editor (Ace)
I would like to build a simplified version of lib/ace with only the TextMate theme and the JSON mode. I would also like to replace its dynamic module loading system with ES6 modules which works better with other tools. Now I am trying figure out where to put the `import` and `export` statements. Do you have any tips / strategy advice?
Reply all
Reply to author
Forward
0 new messages