Last active
June 23, 2025 20:33
-
-
Save spotsccc/ba08076f6489ebbde8cf6c14e6b1d99a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { context, Fn, noop, top, wrap } from '@reatom/core'; | |
function takeNested(cb: Fn) { | |
const frame = top(); | |
return new Promise<void>((resolve, reject) => { | |
let i = 1; | |
const check = () => { | |
if (i === 0) { | |
resolve(); | |
} | |
if (--i === 0) Promise.resolve().then(check); | |
}; | |
const { pushQueue } = frame.root; | |
frame.root.pushQueue = (cb: Fn, queue) => { | |
pushQueue.call( | |
frame.root, | |
() => { | |
const result = cb(); | |
if (result instanceof Promise) { | |
++i; | |
result.finally(check).catch(noop); | |
} | |
return result; | |
}, | |
queue, | |
); | |
}; | |
const result = frame.run(cb); | |
check(); | |
return result; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment