You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Modernize dependency system with Promise-based run blockers
Replace the callback-based dependenciesFulfilled/runDependencies system
with a modern async/await approach using "run blockers" (Promises).
This modernizes the startup sequence, ensuring preRun and other startup
hooks are executed exactly once by pausing and resuming execution flow
instead of re-running the entry point.
To maintain backward compatibility, a bridge is implemented for the
existing addRunDependency and removeRunDependency APIs, routing them
through the new Promise-based blocking mechanism.
- Make run() in postamble.js async to support await.
- Implement $addRunBlocker and $resolveRunBlockers in libcore.js.
- Bridge $addRunDependency and $removeRunDependency to use $addRunBlocker.
- Wrap runDependencies == 0 check in ASSERTIONS guard to avoid empty block in optimized builds (fixes Closure Compiler warnings).
Copy file name to clipboardExpand all lines: site/source/docs/api_reference/emscripten.h.rst
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -327,7 +327,7 @@ Functions
327
327
328
328
Asynchronously loads a script from a URL.
329
329
330
-
This integrates with the run dependencies system, so your script can call ``addRunDependency`` multiple times, prepare various asynchronous tasks, and call ``removeRunDependency`` on them; when all are complete (or if there were no run dependencies to begin with), ``onload`` is called. An example use for this is to load an asset module, that is, the output of the file packager.
330
+
This integrates with the run-blocker system, so your script can call ``addRunBlocker`` multiple times, with various asynchronous tasks (promises). Only when all are complete will ``onload`` be called. An example use for this is to load an asset module, that is, the output of the file packager.
331
331
332
332
This function is currently only available in main browser thread, and it will immediately fail by calling the supplied onerror() handler if called in a pthread.
Note that generally run dependencies are managed by the file packager and other parts of the system. It is rare for developers to use this API directly.
274
-
275
-
276
-
.. js:function::addRunDependency(id)
270
+
Run blockers (dependencies)
271
+
===========================
277
272
278
-
Adds an ``id`` to the list of run dependencies.
273
+
Note that generally run blockers are managed by the file packager and other internal systems. It is rare for developers to use this API directly.
279
274
280
-
This adds a run dependency and increments the run dependency counter.
275
+
.. js:function::addRunBlocker(promise)
281
276
282
-
.. COMMENT (not rendered): **HamishW** Remember to link to Execution lifecycle in Browser environment or otherwise link to information on using this. Possibly its own topic.
283
-
284
-
:param id: An arbitrary id representing the operation.
285
-
:type id: String
277
+
Adds a promise that must be resolved before the program starts running.
286
278
279
+
.. js:function::addRunDependency(id)
287
280
281
+
Deprecated: Use ``addRunBlocker`` instead
288
282
289
283
.. js:function::removeRunDependency(id)
290
284
291
-
Removes a specified ``id`` from the list of run dependencies.
292
-
293
-
:param id: The identifier for the specific dependency to be removed (added with :js:func:`addRunDependency`)
294
-
:type id: String
295
-
285
+
Deprecated: When using ``addRunBlocker``, there is no need to manually remove dependencies, just resolve the promise.
Copy file name to clipboardExpand all lines: site/source/docs/porting/emscripten-runtime-environment.rst
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -118,11 +118,11 @@ Execution lifecycle
118
118
119
119
When an Emscripten-compiled application is loaded, it starts by preparing data in the ``preloading`` phase. Files you marked for :ref:`preloading <emcc-preload-file>` (using ``emcc --preload-file``, or manually from JavaScript with :js:func:`FS.createPreloadedFile`) are set up at this stage.
120
120
121
-
You can add additional operations with :js:func:`addRunDependency`, which is a counter of all dependencies to be executed before compiled code can run. As these are completed you can call :js:func:`removeRunDependency` to remove the completed dependencies.
121
+
You can add additional operations with :js:func:`addRunBlocker`, which takes a promise that will prevent your program's ``main()`` function from running until it is resolved.
122
122
123
123
.. note:: Generally it is not necessary to add additional operations — preloading is suitable for almost all use cases.
124
124
125
-
When all dependencies are met, Emscripten will call your programs's ``main()`` function. The ``main()`` function should be used to perform initialization tasks, and will often call :c:func:`emscripten_set_main_loop` (as :ref:`described above <emscripten-runtime-environment-howto-main-loop>`). The main loop function will be then be called at the requested frequency.
125
+
When all the blockers are resolved, Emscripten will call your programs's ``main()`` function. The ``main()`` function should be used to perform initialization tasks, and will often call :c:func:`emscripten_set_main_loop` (as :ref:`described above <emscripten-runtime-environment-howto-main-loop>`). The main loop function will be then be called at the requested frequency.
126
126
127
127
You can affect the operation of the main loop in several ways:
0 commit comments