All files / @angular data-observer.ts

28.57% Statements 4/14
100% Branches 0/0
16.67% Functions 1/6
28.57% Lines 4/14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38                            1x                             1x       1x       1x
import { OnDestroy, OnInit } from '@angular/core'
import { subscribe, unsubscribe } from '../core'
 
/**
 * Every component that uses `@BindAction` must extends from this
 * class in order to make sure that AOT won't delete OnInit and
 * OnDestroy life-cycle events used by the decorator, irrespective
 * of the fact that it may or may not be used by the component itself
 *
 * @export
 * @class DataObserver
 * @implements {OnInit}
 * @implements {OnDestroy}
 */
export class DataObserver implements OnInit, OnDestroy {
 
  constructor() {
    const originalInit = this.ngOnInit
    this.ngOnInit = () => {
      subscribe.bind(this)()
      originalInit.bind(this)()
    }
    const originalDestroy = this.ngOnDestroy
    this.ngOnDestroy = () => {
      unsubscribe.bind(this)()
      originalDestroy.bind(this)()
    }
  }
 
  ngOnInit() {
    // empty on purpose
  }
 
  ngOnDestroy() {
    // empty on purpose
    unsubscribe.bind(this)()
  }
}