SHARE
TWEET

Untitled

a guest May 19th, 2017 41 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Observable {
  2.  
  3.   constructor(subscriber) {
  4.     this.subscriber = subscriber
  5.   }
  6.  
  7.   subscribe(observer) {
  8.     if (typeof observer == 'function') observer = {next: observer}
  9.     if (!observer.next)     observer.next     = () => {}
  10.     if (!observer.complete) observer.complete = () => {}
  11.     if (!observer.error)    observer.error    = () => {}
  12.     return {unsubscribe: this.subscriber(observer) || (() => {})}
  13.   }
  14.  
  15.   static fromEvent(object, event) {
  16.     return new Observable(observer => {
  17.       const handler = event => observer.next(event)
  18.       object.addEventListener(event, handler)
  19.       return () => object.removeEventListener(event, handler)
  20.     })
  21.   }
  22.  
  23.   do(cb) {
  24.     return new Observable(observer => {
  25.       const subscription = this.subscribe({
  26.         next: value => {
  27.           cb(value)
  28.           observer.next(value)
  29.         },
  30.         complete: () => observer.complete(),
  31.         error: err => observer.error(err),
  32.       })
  33.       return () => subscription.unsubscribe()
  34.     })
  35.   }
  36.  
  37.   map(cb) {
  38.     return new Observable(observer => {
  39.       const subscription = this.subscribe({
  40.         next: value => observer.next(cb(value)),
  41.         complete: () => observer.complete(),
  42.         error: err => observer.error(err),
  43.       })
  44.       return () => subscription.unsubscribe()
  45.     })
  46.   }
  47.  
  48.   filter(cb) {
  49.     return new Observable(observer => {
  50.       const subscription = this.subscribe({
  51.         next: value => { if (cb(value)) observer.next(value) },
  52.         complete: () => observer.complete(),
  53.         error: err => observer.error(err),
  54.       })
  55.       return () => subscription.unsubscribe()
  56.     })
  57.   }
  58.  
  59.   filterMap(cb) {
  60.     return new Observable(observer => {
  61.       const subscription = this.subscribe({
  62.         next: value => {
  63.           value = cb(value)
  64.           if (value) observer.next(value)
  65.         },
  66.         complete: () => observer.complete(),
  67.         error: err => observer.error(err),
  68.       })
  69.       return () => subscription.unsubscribe()
  70.     })
  71.   }
  72.  
  73.   debounceTime(time) {
  74.     return new Observable(observer => {
  75.       let i
  76.       const subscription = this.subscribe({
  77.         next: value => {
  78.           if (i) clearTimeout(i)
  79.           i = setTimeout(() => observer.next(value), time)
  80.         },
  81.         complete: () => setTimeout(() => observer.complete(), time),
  82.         error: err => setTimeout(() => observer.error(err), time),
  83.       })
  84.       return () => subscription.unsubscribe()
  85.     })
  86.   }
  87.  
  88.   distinctUntilChanged() {
  89.     return new Observable(observer => {
  90.       let last = null
  91.       const subscription = this.subscribe({
  92.         next: value => {
  93.           if (value == last) return
  94.           last = value
  95.           observer.next(value)
  96.         },
  97.         complete: () => observer.complete(),
  98.         error: err => observer.error(err),
  99.       })
  100.       return () => subscription.unsubscribe()
  101.     })
  102.   }
  103.  
  104. }
RAW Paste Data
Top