/*! pseudo-worker
 *
 * License: MIT
 *   See https://github.com/chaoyangnz/webworker-polyfill/blob/master/LICENSE
 *
 * Copyright (c) 2018 Chao Yang
 */
!function(f) {
  if ("object" == typeof exports && "undefined" != typeof module) module.exports = f(); else if ("function" == typeof define && define.amd) define([], f); else {
    var g;
    g = "undefined" != typeof window ? window : "undefined" != typeof global ? global : "undefined" != typeof self ? self : this,
    g.PseudoWorker = f();
  }
}(function() {
  var define, module, exports;
  return function() {
    function r(e, n, t) {
      function o(i, f) {
        if (!n[i]) {
          if (!e[i]) {
            var c = "function" == typeof require && require;
            if (!f && c) return c(i, !0);
            if (u) return u(i, !0);
            var a = new Error("Cannot find module '" + i + "'");
            throw a.code = "MODULE_NOT_FOUND", a;
          }
          var p = n[i] = {
            exports: {}
          };
          e[i][0].call(p.exports, function(r) {
            var n = e[i][1][r];
            return o(n || r);
          }, p, p.exports, r, e, n, t);
        }
        return n[i].exports;
      }
      for (var u = "function" == typeof require && require, i = 0; i < t.length; i++) o(t[i]);
      return o;
    }
    return r;
  }()({
    1: [ function(require, module, exports) {
      (function(global) {
        "use strict";
        function doEval(self, __pseudoworker_script) {
          (function() {
            eval(__pseudoworker_script);
          }).call(global);
        }
        function PseudoWorker(path) {
          function executeEach(arr, fun) {
            for (var i = -1; ++i < arr.length; ) arr[i] && fun(arr[i]);
          }
          function callErrorListener(err) {
            return function(listener) {
              listener({
                type: "error",
                error: err,
                message: err.message
              });
            };
          }
          function addEventListener(type, fun) {
            "message" === type ? messageListeners.push(fun) : "error" === type && errorListeners.push(fun);
          }
          function removeEventListener(type, fun) {
            var listeners;
            if ("message" === type) listeners = messageListeners; else {
              if ("error" !== type) return;
              listeners = errorListeners;
            }
            for (var i = -1; ++i < listeners.length; ) {
              var listener = listeners[i];
              if (listener === fun) {
                delete listeners[i];
                break;
              }
            }
          }
          function postError(err) {
            var callFun = callErrorListener(err);
            "function" == typeof api.onerror && callFun(api.onerror), workerSelf && "function" == typeof workerSelf.onerror && callFun(workerSelf.onerror),
            executeEach(errorListeners, callFun), executeEach(workerErrorListeners, callFun);
          }
          function runPostMessage(msg) {
            function callFun(listener) {
              try {
                listener({
                  data: msg
                });
              } catch (err) {
                postError(err);
              }
            }
            workerSelf && "function" == typeof workerSelf.onmessage && callFun(workerSelf.onmessage),
            executeEach(workerMessageListeners, callFun);
          }
          function postMessage(msg) {
            if ("undefined" == typeof msg) throw new Error("postMessage() requires an argument");
            if (!terminated) return script ? void runPostMessage(msg) : void postMessageListeners.push(msg);
          }
          function terminate() {
            terminated = !0;
          }
          function workerPostMessage(msg) {
            function callFun(listener) {
              listener({
                data: msg
              });
            }
            terminated || ("function" == typeof api.onmessage && callFun(api.onmessage), executeEach(messageListeners, callFun));
          }
          function workerAddEventListener(type, fun) {
            "message" === type ? workerMessageListeners.push(fun) : "error" === type && workerErrorListeners.push(fun);
          }
          var script, workerSelf, messageListeners = [], errorListeners = [], workerMessageListeners = [], workerErrorListeners = [], postMessageListeners = [], terminated = !1, api = this, xhr = new XMLHttpRequest();
          return xhr.open("GET", path), xhr.onreadystatechange = function() {
            if (4 === xhr.readyState) if (xhr.status >= 200 && xhr.status < 400) {
              script = xhr.responseText, workerSelf = {
                postMessage: workerPostMessage,
                addEventListener: workerAddEventListener,
                close: terminate
              }, doEval(workerSelf, script);
              var currentListeners = postMessageListeners;
              postMessageListeners = [];
              for (var i = 0; i < currentListeners.length; i++) runPostMessage(currentListeners[i]);
            } else postError(new Error("cannot find script " + path));
          }, xhr.send(), api.postMessage = postMessage, api.addEventListener = addEventListener,
          api.removeEventListener = removeEventListener, api.terminate = terminate, api;
        }
        module.exports = PseudoWorker;
      }).call(this, "undefined" != typeof global ? global : "undefined" != typeof self ? self : "undefined" != typeof window ? window : {});
    }, {} ]
  }, {}, [ 1 ])(1);
});