daily pastebin goal
12%
SHARE
TWEET

Untitled

a guest Jun 13th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const lifeCycleInstance = Symbol('LifeCycleInstance');
  2. const singletonEnforcer = Symbol('SingletonEnforcer');
  3.  
  4. const VU_DRAWN_EVT = 'visual-unit.drawn';
  5. const CANVAS_INIT_EVT = 'canvas.init';
  6.  
  7. const EVENT_LIST = [VU_DRAWN_EVT, CANVAS_INIT_EVT];
  8.  
  9. const resolver = resolveFn => notifier => resolveFn(notifier);
  10.  
  11. class LifeCycleManager {
  12.  
  13.   constructor(enforcer) {
  14.     if (enforcer !== singletonEnforcer) {
  15.       throw new Error('Can not construct LifeCycle');
  16.     }
  17.     this._eventList = EVENT_LIST;
  18.     this._promises = new Map();
  19.     this._notifiers = {};
  20.     this._init();
  21.   }
  22.  
  23.  
  24.   _init() {
  25.     this._notifiers = this._eventList.reduce((acc, name) => {
  26.       acc[name] = resolver;
  27.       return acc;
  28.     }, {});
  29.     this._initPromises();
  30.   }
  31.  
  32.   retrieve(promiseName) {
  33.     return this._promises.get(promiseName);
  34.   }
  35.  
  36.   _makeNotifierPromise(eventName) {
  37.     return new Promise((resolve) => {
  38.       this._notifiers[eventName] = this._notifiers[eventName](resolve);
  39.     });
  40.   }
  41.  
  42.   _initPromises() {
  43.     this._eventList.forEach((eventName) => {
  44.       this._promises.set(eventName, this._makeNotifierPromise(eventName));
  45.     });
  46.   }
  47.  
  48.   notify(notifier) {
  49.     let formalName = notifier.formalName || notifier.client.constructor.formalName();
  50.     this._notifiers[`${formalName}.${notifier.action}`](notifier);
  51.   }
  52.  
  53.   static get instance() {
  54.     if (!this[lifeCycleInstance]) {
  55.       this[lifeCycleInstance] = new LifeCycleManager(singletonEnforcer);
  56.     }
  57.     return this[lifeCycleInstance];
  58.   }
  59. }
  60.  
  61. // ----------- Picasso Library --------------- //
  62.  
  63. class VisualUnit {
  64.  
  65.   constructor() {
  66.     this.lifeCycleManager = LifeCycleManager.instance;
  67.   }
  68.  
  69.   static formalName() {
  70.     return 'visual-unit';
  71.   }
  72.  
  73.   draw() {
  74.     console.log('drawing visual-unit');
  75.   }
  76. }
  77.  
  78.  
  79. class Canvas {
  80.   constructor() {
  81.     console.log('init canvas');
  82.     this.lifeCycleManager = LifeCycleManager.instance;
  83.     this.lifeCycleManager.notify({
  84.       client: this,
  85.       action: 'init'
  86.     });
  87.   }
  88.  
  89.   static formalName() {
  90.     return 'canvas';
  91.   }
  92.  
  93.   hooks(hookList) {
  94.     const promiseBag = [];
  95.     hookList.forEach(hookName => {
  96.       promiseBag.push(this.lifeCycleManager.retrieve(hookName));
  97.     });
  98.     return promiseBag;
  99.   }
  100.  
  101.   mount() {
  102.     // create multiple visual units
  103.     let units = [];
  104.     for (let i = 0; i < 10; i++) {
  105.       let visualUnit = new VisualUnit();
  106.       visualUnit.draw();
  107.       units.push(visualUnit);
  108.     }
  109.  
  110.     this.lifeCycleManager.notify({
  111.       client: units,
  112.       action: 'drawn',
  113.       formalName: 'visual-unit'
  114.     });
  115.   }
  116. }
  117.  
  118.  
  119. // -------------- User Code ------------ //
  120.  
  121. let canvas = new Canvas();
  122.  
  123. const promises = canvas.hooks(['visual-unit.drawn', 'canvas.init']);
  124.  
  125. Promise.all(promises).then(d => {
  126.   console.log(d);
  127. })
  128.  
  129. canvas.mount();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top