/**
 * observer.js
 *
 * @Author: apple
 * @Date: 2018/1/2 23:35
 */
var pubsub = {
  _private: {}
};

(function (q) {
  var topics = Object.assign({}, pubsub._private); // 回调函数存放的数组

  var subUid = -1; // 发布方法

  q.publish = function (topic, args) {
    if (!topics[topic]) {
      return false;
    }

    setTimeout(function () {
      var subscribers = topics[topic];
      var len = subscribers ? subscribers.length : 0;

      while (len--) {
        subscribers[len].func(topic, args);
      }
    }, 0);
    return true;
  }; // 订阅方法


  q.subscribe = function (topic, func) {
    if (!topics[topic]) {
      topics[topic] = [];
    }

    var token = (++subUid).toString();
    topics[topic].push({
      token: token,
      func: func
    });
    return token;
  }; // 退订方法


  q.unsubscribe = function (token) {
    for (var m in topics) {
      if (topics[m]) {
        for (var i = 0, j = topics[m].length; i < j; i++) {
          if (topics[m][i].token === token) {
            topics[m].splice(i, 1);
            return token;
          }
        }
      }
    }

    return false;
  };
})(pubsub);

export default pubsub;