Skip to content

Instantly share code, notes, and snippets.

@johndavedecano
Forked from minwe/EventSystem.js
Created April 17, 2016 14:38
Show Gist options
  • Select an option

  • Save johndavedecano/e668c7c44a0f74b870294de1fbad79e1 to your computer and use it in GitHub Desktop.

Select an option

Save johndavedecano/e668c7c44a0f74b870294de1fbad79e1 to your computer and use it in GitHub Desktop.

Revisions

  1. @yitsushi yitsushi created this gist May 6, 2014.
    28 changes: 28 additions & 0 deletions EventSystem.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    var EventSystem = (function() {
    var self = this;

    self.queue = {};

    return {
    publish: function (event, data) {
    var queue = self.queue[event];

    if (typeof queue === 'undefined') {
    return false;
    }

    while(queue.length > 0) {
    (queue.shift())(data);
    }

    return true;
    },
    subscribe: function(event, callback) {
    if (typeof self.queue[event] === 'undefined') {
    self.queue[event] = [];
    }

    self.queue[event].push(callback);
    }
    };
    }());
    12 changes: 12 additions & 0 deletions sample-usage-to-publish.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    var RiskList = React.createClass({
    getInitialState: function() {
    return { risks: [] };
    },
    componentDidMount: function() {
    // someting
    },
    render: function() {
    EventSystem.publish('risk.count.update', this.state.risks.length);
    // something
    }
    }
    20 changes: 20 additions & 0 deletions sample-usage-to-subscribe.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    var RiskPage = React.createClass({
    updateRiskCount: function(count) {
    this.setState({
    riskCount: count
    });
    },
    componentDidMount: function() {
    EventSystem.subscribe('risk.count.update', this.updateRiskCount);
    },
    getInitialState: function() {
    return {
    riskCount: 0
    };
    },
    render: function() {
    // something
    }
    });