This PR introduces a robust, asynchronous mechanism for registering remote methods across workers, replacing the previous "fire-and-forget" approach with a deterministic promise-based flow. It also includes critical fixes for main thread synchronization and addon registration.
Key Changes:
Async Remote Registration (core.Base):
- Renamed
sendRemotes to promiseRemotes.
promiseRemotes now returns a Promise that resolves only when the target worker acknowledges the registration.
initRemote and initAsync are now fully async and await this process.
- Added
remotesReady() method and #remotesReadyPromise to allow external callers (like Neo.Main) to await full initialization.
Robust Worker Checking (hasWorker):
- Implemented
hasWorker(name) in Neo.worker.Base and Neo.worker.Manager.
- This method checks
Neo.config (e.g., useCanvasWorker, useServiceWorker) or the active worker list to determine if a target worker actually exists.
promiseRemotes now uses origin.hasWorker() to prevent sending messages to non-existent optional workers (like task or canvas), fixing runtime errors when these are disabled.
Main Thread Synchronization (Neo.Main & Neo.worker.Manager):
Neo.Main: onDomContentLoaded now explicitly awaits remotesReady() for all registered addons and the main instance itself. Removed the arbitrary timeout(20) race-condition workaround.
Neo.worker.Manager: Updated onWorkerConstructed to check for activeWorkers + 1. The + 1 accounts for the main thread itself, ensuring loadApplication is only triggered when all threads (workers + main) are ready.
Addon Registration Fix (Neo.Main):
- Fixed a race condition in
registerAddon. If called multiple times (e.g., via remote method access) before completion, it could incorrectly overwrite the singleton instance in the namespace with the class constructor. The fix ensures Neo.applyToGlobalNs(addon) is only called when a new instance is actually created.
Service Worker Support:
- Added
service worker support to hasWorker checks, honoring Neo.config.useServiceWorker.
Goal:
Eliminate race conditions during application startup and ensure that all remote methods are guaranteed to be registered before the application logic proceeds.
This PR introduces a robust, asynchronous mechanism for registering remote methods across workers, replacing the previous "fire-and-forget" approach with a deterministic promise-based flow. It also includes critical fixes for main thread synchronization and addon registration.
Key Changes:
Async Remote Registration (
core.Base):sendRemotestopromiseRemotes.promiseRemotesnow returns aPromisethat resolves only when the target worker acknowledges the registration.initRemoteandinitAsyncare now fully async and await this process.remotesReady()method and#remotesReadyPromiseto allow external callers (likeNeo.Main) to await full initialization.Robust Worker Checking (
hasWorker):hasWorker(name)inNeo.worker.BaseandNeo.worker.Manager.Neo.config(e.g.,useCanvasWorker,useServiceWorker) or the active worker list to determine if a target worker actually exists.promiseRemotesnow usesorigin.hasWorker()to prevent sending messages to non-existent optional workers (liketaskorcanvas), fixing runtime errors when these are disabled.Main Thread Synchronization (
Neo.Main&Neo.worker.Manager):Neo.Main:onDomContentLoadednow explicitly awaitsremotesReady()for all registered addons and the main instance itself. Removed the arbitrarytimeout(20)race-condition workaround.Neo.worker.Manager: UpdatedonWorkerConstructedto check foractiveWorkers + 1. The+ 1accounts for the main thread itself, ensuringloadApplicationis only triggered when all threads (workers + main) are ready.Addon Registration Fix (
Neo.Main):registerAddon. If called multiple times (e.g., via remote method access) before completion, it could incorrectly overwrite the singleton instance in the namespace with the class constructor. The fix ensuresNeo.applyToGlobalNs(addon)is only called when a new instance is actually created.Service Worker Support:
serviceworker support tohasWorkerchecks, honoringNeo.config.useServiceWorker.Goal: Eliminate race conditions during application startup and ensure that all remote methods are guaranteed to be registered before the application logic proceeds.