Created
September 1, 2020 12:28
-
-
Save alexesca/edf15878f16a95ece99d250858a277dd 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
const observable = events => { | |
const INTERVAL = 1 * 1000; | |
let schedulerId; | |
return { | |
subscribe: observer => { | |
schedulerId = setInterval(() => { | |
if(events.length === 0) { | |
observer.complete(); | |
clearInterval(schedulerId); | |
schedulerId = undefined; | |
} | |
else { | |
observer.next(events.shift()); | |
} | |
}, INTERVAL); | |
return { | |
unsubscribe: () => { | |
if(schedulerId) { | |
clearInterval(schedulerId); | |
} | |
} | |
} | |
} | |
} | |
} | |
let sub = observable([1,2,3]).subscribe({ | |
next: console.log, | |
complete: () => console.log('Done!') | |
}) | |
/** | |
* How to create an observable from scratch. Code from RXJS in Action | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment