{"size": 928, "ext": "js", "max_stars_repo_path": "src/dispatcher.js", "max_stars_repo_name": "cgx9/evoflux", "max_stars_repo_stars_event_min_datetime": "2015-02-05T08:21:35.000Z", "max_stars_repo_stars_event_max_datetime": "2019-07-25T10:31:33.000Z", "max_issues_repo_path": "src/dispatcher.js", "max_issues_repo_name": "cgx9/evoflux", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/dispatcher.js", "max_forks_repo_name": "cgx9/evoflux", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 9, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.2, "max_line_length": 51, "alphanum_fraction": 0.6702586207}
"use strict";
//source code from:bleeding-edge-sample-app
var Dispatcher = function(){
  this.handlers = [];
  this.isDispatching = false;
  this.pendingPayload = null;
}

Dispatcher.prototype.register = function(cb) {
  this.handlers.push({
    isPending:false,
    isHandled:false,
    callback:cb
  });
};

Dispatcher.prototype.dispatch = function(payload) {
  if(this.isDispatching){
    throw new Error("cannot dispatch");
  }
  this.handlers.forEach(function(handler){
    handler.isPending = false,
    handler.isHandled = false
  })
  this.pendingPayload = payload;
  this.isDispatching = true;//准备开始调度
  try{
    this.handlers.forEach(function(handler){
      if(!handler.isPending){
        handler.isPending = true;
        handler.callback(payload);
        handler.isHandled = true;
      }
    })
  }finally{
    this.isDispatching = false;
    this.pendingPayload = null;
  }
};
module.exports = new Dispatcher();