Skip to content

Commit 6588bd9

Browse files
committed
#6863 core.Base: smarter approach, which enables us to move initRemote() from the macro to the micro task queue.
1 parent e2b3a52 commit 6588bd9

1 file changed

Lines changed: 9 additions & 14 deletions

File tree

src/core/Base.mjs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ class Base {
107107
}
108108

109109
/**
110-
* If a class extension does not implement `async initAsync()`, `construct()` will set this internal flag to `true`.
111-
* Otherwise, the flag will get set to `true` once the Promise of `async initAsync()` is resolved.
110+
* The internal flag will get set to `true` once the Promise of `async initAsync()` is resolved.
112111
* method body.
113112
* @member {Boolean} isReady=false
114113
*/
@@ -175,21 +174,13 @@ class Base {
175174

176175
if (me.initAsync) {
177176
// Triggers async logic after the construction chain is done.
178-
me.readyPromise = (async () => {
177+
me.readyPromise = Promise.resolve().then(async () => {
179178
await me.initAsync();
180179
me.isReady = true
181-
})()
180+
})
182181
} else {
183182
me.isReady = true
184183
}
185-
186-
/*
187-
* We have to use the macro task queue here. initRemote() relies on accessing Neo.currentWorker, which does
188-
* get defined inside worker.Base: construct(). Workers can import singletons inside static top-level imports.
189-
* The micro task queue will execute before the worker construction chain starts, so using it here
190-
* would lead to errors, since Neo.currentWorker will be undefined.
191-
*/
192-
me.remote && setTimeout(me.initRemote.bind(me), 0)
193184
}
194185

195186
/**
@@ -406,14 +397,18 @@ class Base {
406397
init() {}
407398

408399
/**
409-
* You can implement this method in subclasses to perform asynchronous initialization logic.
400+
* You can override this method in subclasses to perform asynchronous initialization logic.
401+
* Make sure to use the parent call `await super.initAsync()` at the beginning of their implementations.
410402
*
411403
* A common use case is requiring conditional or optional dynamic imports or fetching initial data.
412404
*
413405
* The promise returned by this method (or implicitly created if it's an `async` function)
414-
* will be stored in `this.readyPromise`. Once this promise is fulfilled, the `this.isReady` flag will be set to `true`.
406+
* will be stored in `this.readyPromise`. Once this `Promise` is fulfilled, the `this.isReady` flag will be set to `true`.
415407
* @returns {Promise<void>} A promise that resolves when the asynchronous initialization is complete.
416408
*/
409+
async initAsync() {
410+
this.remote && this.initRemote()
411+
}
417412

418413
/**
419414
* Applies all class configs to this instance

0 commit comments

Comments
 (0)