Skip to content

Instantly share code, notes, and snippets.

@hkwon
Created July 29, 2019 02:05
Show Gist options
  • Select an option

  • Save hkwon/1bba4f2bdfa6ad5db3a1392e4b563f3d to your computer and use it in GitHub Desktop.

Select an option

Save hkwon/1bba4f2bdfa6ad5db3a1392e4b563f3d to your computer and use it in GitHub Desktop.

Revisions

  1. hkwon created this gist Jul 29, 2019.
    17 changes: 17 additions & 0 deletions Observable.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    module.exports = class Observable {
    constructor() {
    this._observers = new Set();
    }

    subscribe(observer) {
    this._observers.add(observer);
    }

    unsubscribe(observer) {
    this._observers = [...this._observers].filter(subscriber => subscriber !== observer);
    }

    notify(data) {
    this._observers.forEach(observer => observer(data));
    }
    };