|
| 1 | +--- |
| 2 | +id: 8147 |
| 3 | +title: 'Enhancement: Async Remote Method Registration and Robust Worker Checking' |
| 4 | +state: CLOSED |
| 5 | +labels: |
| 6 | + - enhancement |
| 7 | + - ai |
| 8 | + - core |
| 9 | +assignees: |
| 10 | + - tobiu |
| 11 | +createdAt: '2025-12-19T21:07:00Z' |
| 12 | +updatedAt: '2025-12-19T21:27:41Z' |
| 13 | +githubUrl: 'https://github.com/neomjs/neo/issues/8147' |
| 14 | +author: tobiu |
| 15 | +commentsCount: 0 |
| 16 | +parentIssue: null |
| 17 | +subIssues: [] |
| 18 | +subIssuesCompleted: 0 |
| 19 | +subIssuesTotal: 0 |
| 20 | +blockedBy: [] |
| 21 | +blocking: [] |
| 22 | +closedAt: '2025-12-19T21:27:41Z' |
| 23 | +--- |
| 24 | +# Enhancement: Async Remote Method Registration and Robust Worker Checking |
| 25 | + |
| 26 | +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. |
| 27 | + |
| 28 | +**Key Changes:** |
| 29 | + |
| 30 | +1. **Async Remote Registration (`core.Base`):** |
| 31 | + * Renamed `sendRemotes` to `promiseRemotes`. |
| 32 | + * `promiseRemotes` now returns a `Promise` that resolves only when the target worker acknowledges the registration. |
| 33 | + * `initRemote` and `initAsync` are now fully async and await this process. |
| 34 | + * Added `remotesReady()` method and `#remotesReadyPromise` to allow external callers (like `Neo.Main`) to await full initialization. |
| 35 | + |
| 36 | +2. **Robust Worker Checking (`hasWorker`):** |
| 37 | + * Implemented `hasWorker(name)` in `Neo.worker.Base` and `Neo.worker.Manager`. |
| 38 | + * This method checks `Neo.config` (e.g., `useCanvasWorker`, `useServiceWorker`) or the active worker list to determine if a target worker actually exists. |
| 39 | + * `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. |
| 40 | + |
| 41 | +3. **Main Thread Synchronization (`Neo.Main` & `Neo.worker.Manager`):** |
| 42 | + * **`Neo.Main`:** `onDomContentLoaded` now explicitly awaits `remotesReady()` for all registered addons and the main instance itself. Removed the arbitrary `timeout(20)` race-condition workaround. |
| 43 | + * **`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. |
| 44 | + |
| 45 | +4. **Addon Registration Fix (`Neo.Main`):** |
| 46 | + * 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. |
| 47 | + |
| 48 | +5. **Service Worker Support:** |
| 49 | + * Added `service` worker support to `hasWorker` checks, honoring `Neo.config.useServiceWorker`. |
| 50 | + |
| 51 | +**Goal:** |
| 52 | +Eliminate race conditions during application startup and ensure that all remote methods are guaranteed to be registered before the application logic proceeds. |
| 53 | + |
| 54 | +## Activity Log |
| 55 | + |
| 56 | +- 2025-12-19 @tobiu added the `enhancement` label |
| 57 | +- 2025-12-19 @tobiu added the `ai` label |
| 58 | +- 2025-12-19 @tobiu added the `core` label |
| 59 | +- 2025-12-19 @tobiu assigned to @tobiu |
| 60 | +- 2025-12-19 @tobiu referenced in commit `252196e` - "Enhancement: Async Remote Method Registration and Robust Worker Checking #8147" |
| 61 | +- 2025-12-19 @tobiu closed this issue |
| 62 | + |
0 commit comments