Skip to content

Instantly share code, notes, and snippets.

@alexesca
Created September 1, 2020 12:28
Show Gist options
  • Save alexesca/edf15878f16a95ece99d250858a277dd to your computer and use it in GitHub Desktop.
Save alexesca/edf15878f16a95ece99d250858a277dd to your computer and use it in GitHub Desktop.
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