Created
April 22, 2020 18:05
-
-
Save ancms2600/09df67681fcdcfb4175a0c6c145a07f6 to your computer and use it in GitHub Desktop.
Iterator Loop
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
Utils.iteratorLoop = () => { | |
const buf = []; | |
let next, done = false; | |
const push = e => { | |
buf.push(e); | |
if (null != next) next(); | |
}; | |
const loop = async function* () { | |
while (true) { | |
yield* buf.splice(0); | |
if (done) break; | |
await new Promise(ok=>{next=ok;}); | |
} | |
}; | |
return { iter: loop(), push, done: () => { done = true; } }; | |
}; | |
// const { iter, push, done } = iteratorLoop(); | |
// state.c.on('connect', (...args) => push({ name: 'connect', args })); | |
// return iter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment