Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- console.clear()
- class RealDealObserver {
- constructor(destination) {
- this.destination = destination
- }
- next(value) {
- const destination = this.destination
- if (destination.next && !this.isUnsubscribed) {
- destination.next && this.destination.next(value)
- }
- }
- error(err) {
- const destination = this.destination
- if (!this.isUnsubscribed) {
- if (destination.error) {
- destination.error(err)
- }
- this.unsubscribe()
- }
- }
- complete() {
- const destination = this.destination
- if (!this.isUnsubscribed) {
- if (destination.complete) {
- destination.complete()
- }
- this.unsubscribe()
- }
- }
- unsubscribe() {
- this.isUnsubscribed = true
- if (this._unsubscribe) {
- this._unsubscribe()
- }
- }
- }
- class Observable {
- constructor(_subscribe) {
- this._subscribe = _subscribe
- }
- subscribe(observer) {
- const realDealObserver = new RealDealObserver(observer)
- realDealObserver._unsubscribe = this._subscribe(realDealObserver)
- return () => realDealObserver.unsubscribe()
- }
- }
- const a = [
- 'Fred',
- 'Quentin',
- 'idiot',
- 'Doug',
- 'tr0n',
- 'James',
- 'Bitchface',
- 'Maude',
- 'Gladys',
- 'Ploppy',
- 'Manuel',
- 'Greg'
- ]
- const observable$ = new Observable((observer) => {
- let i = 0
- const id = setInterval(() => {
- if (i < a.length) {
- observer.next(`Sup ${a[i]}`)
- i++
- } else {
- observer.complete()
- }
- }, 100)
- return () => {
- console.log('Are you sure you want to unsubscribe? Kidding, unsubscribed.')
- clearInterval(id)
- }
- })
- const observer = {
- next(x) { console.log('next -> ' + x)},
- error(err) {},
- complete() { console.log('We are done diddly done!')}
- }
- const subscription$ = observable$.subscribe(observer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement