From 73af7b568fdf1c09d940899d2573190a93ac2ffb Mon Sep 17 00:00:00 2001 From: Paul Berberian Date: Wed, 20 Mar 2024 18:20:41 +0100 Subject: [PATCH] Fix SyncOrAsync util syncValue transfer Our `SyncOrAsync` util is used for cases where a task is >99% of the time synchronous, yet <1% of the time asynchronous, and where we don't want to incur any overhead/supplementary complexity of awaiting a Promise which inherently schedule a microtask when the value is most probably already there. It worked well for most usages, but it turns out that a task that starts as asynchronous would then always lead to the need to rely on Promises, even once the task is finished (basically, if it started as an "async value" it could never transform itself to a "sync value"). This does not create any issue but we could gain some minuscule advantage (well, we could argue that `SyncOrAsync`'s advantage is in itself minuscule) here by just relying on the value synchronously once the task is finished. --- src/utils/sync_or_async.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/utils/sync_or_async.ts b/src/utils/sync_or_async.ts index 856570e454..c0e0ab123d 100644 --- a/src/utils/sync_or_async.ts +++ b/src/utils/sync_or_async.ts @@ -78,12 +78,14 @@ const SyncOrAsync = { * @returns {Object} */ createAsync(val: Promise): ISyncOrAsyncValue { - let ret = null; - val.then((resolved) => { + let ret: T | null = null; + val.then((resolved: T) => { ret = resolved; }, noop); return { - syncValue: ret, + get syncValue(): T | null { + return ret; + }, getValueAsAsync() { return val; },