Last active
December 22, 2017 13:51
-
-
Save jooyunghan/d9aa1de22540d042e8bdbc36d84d67e9 to your computer and use it in GitHub Desktop.
AsyncIterator for Observable Using Ag() coroutine
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
function createAsyncIterator() { | |
const self = this; | |
return ag(function*() { | |
const resume = []; | |
const values = []; | |
const subscription = self.materialize().subscribe(value => { | |
values.push(value); | |
resume.splice(0).forEach(r => r()); | |
}); | |
try { | |
while (true) { | |
while (values.length > 0) { | |
const v = values.shift(); | |
if (v.kind == 'N') yield v.value; | |
else if (v.kind == 'E') throw v.error; | |
else return; | |
} | |
yield new Promise(r => resume.push(r)); | |
} | |
} finally { | |
subscription.unsubscribe(); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ag()
는 https://gist.github.com/jooyunghan/c04d1d9a58cd153be114b8f501c1f5b3 참고