(function (global, factory) {
  if (typeof define === "function" && define.amd) {
    define(["exports", "isomorphic-ws", "rxjs", "rxjs/operators", "../internals/actions"], factory);
  } else if (typeof exports !== "undefined") {
    factory(exports, require("isomorphic-ws"), require("rxjs"), require("rxjs/operators"), require("../internals/actions"));
  } else {
    var mod = {
      exports: {}
    };
    factory(mod.exports, global.isomorphicWs, global.rxjs, global.operators, global.actions);
    global.ws = mod.exports;
  }
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _isomorphicWs, _rxjs, _operators, actions) {
  "use strict";

  Object.defineProperty(_exports, "__esModule", {
    value: true
  });
  _exports.default = void 0;
  _isomorphicWs = _interopRequireDefault(_isomorphicWs);
  actions = _interopRequireWildcard(actions);

  function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

  function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

  const ws = function ws({
    url,
    protocols = null,
    socketOptions = {},
    stop$ = new _rxjs.Observable(),
    _createSocket = (_url, _socketOptions) => new _isomorphicWs.default(_url, _socketOptions),
    _actions = actions
  }) {
    const socket = _createSocket(url, protocols, socketOptions);

    const event$ = new _rxjs.Observable(obs => {
      obs.next(_actions.createClient({
        client: socket
      }));

      socket.onopen = e => obs.next(_actions.socketConnect({
        client: socket,
        event: e
      }));

      socket.onclose = e => obs.next(_actions.disconnect({
        event: e
      }));

      socket.onerror = error => obs.error(error);

      socket.onmessage = message => obs.next(_actions.newMessage({
        message
      }));
    });
    return event$.pipe((0, _operators.map)(event => [socket, event]), (0, _operators.filter)(data => data.indexOf(undefined) === -1), (0, _operators.takeUntil)(stop$), (0, _operators.shareReplay)(1));
  };

  var _default = ws;
  _exports.default = _default;
});
//# sourceMappingURL=ws.js.map