Last active
February 27, 2020 11:32
-
-
Save Sawtaytoes/7b6bf968510fe88df3ec98846a641779 to your computer and use it in GitHub Desktop.
`createIteratorSubject` allows someone to create an observable out of a generator and tell RxJS when to pull the next value instead of relying on the official implementation which creates backpressure.
This file contains 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
const { BehaviorSubject, Subject } = require('rxjs') | |
const { map, tap, withLatestFrom } = require('rxjs/operators') | |
const createIteratorSubject = ( | |
iterator, | |
) => { | |
const iterator$ = ( | |
new BehaviorSubject() | |
) | |
const pushNextValue = ({ | |
done, | |
value, | |
}) => { | |
done | |
&& value === undefined | |
? ( | |
iterator$ | |
.complete() | |
) | |
: ( | |
iterator$ | |
.next(value) | |
) | |
} | |
iterator$ | |
.push = ( | |
value, | |
) => ( | |
pushNextValue( | |
iterator | |
.next(value) | |
) | |
) | |
iterator$ | |
.push() | |
return iterator$ | |
} | |
module.exports = createIteratorSubject |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@anilanar I now have an eslint extension available.