define(["exports"], function (exports) {
  "use strict";

  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.pub = pub;
  exports.sub = sub;
  exports.unsub = unsub;

  var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
    return typeof obj;
  } : function (obj) {
    return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  };

  /**
   * Module core/pubsubhub
   *
   * Returns a singleton that can be used for message broadcasting
   * and message receiving. Replaces legacy "msg" code in ReSpec.
   */
  var subscriptions = new Map();

  function pub(topic) {
    for (var _len = arguments.length, data = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
      data[_key - 1] = arguments[_key];
    }

    if (!subscriptions.has(topic)) {
      return; // Nothing to do...
    }
    Array.from(subscriptions.get(topic)).forEach(function (cb) {
      return cb.apply(null, data);
    });
    if (window.parent === window.self) {
      return;
    }
    // If this is an iframe, postMessage parent (used in testing).
    var args = data
    // to structured clonable
    .map(function (arg) {
      return String(JSON.stringify(arg.stack || arg));
    });
    window.parent.postMessage({ topic: topic, args: args }, window.parent.location.origin);
  }

  function sub(topic, cb) {
    var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { once: false };

    if (opts.once) {
      var _ret = function () {
        var opaque = sub(topic, function () {
          unsub(opaque);
          return cb.apply(undefined, arguments);
        });
        return {
          v: void 0
        };
      }();

      if ((typeof _ret === "undefined" ? "undefined" : _typeof(_ret)) === "object") return _ret.v;
    }
    if (!subscriptions.has(topic)) {
      subscriptions.set(topic, [cb]);
    } else {
      subscriptions.get(topic).push(cb);
    }
    return { topic: topic, cb: cb };
  }

  function unsub(opaque) {
    // opaque is whatever is returned by sub()
    var callbacks = subscriptions.get(opaque.topic);
    if (!callbacks || callbacks.indexOf(opaque.cb) === -1) {
      console.warn("Already unsubscribed:", opaque.topic, opaque.cb);
      return;
    }
    callbacks.splice(callbacks.indexOf(opaque.cb), 1);
  }

  sub("error", function (err) {
    console.error(err.stack || err);
  });

  sub("warn", function (str) {
    console.warn(str);
  });
});
//# sourceMappingURL=pubsubhub.js.map