1 | "use strict";
|
2 |
|
3 | require("core-js/modules/es.object.freeze.js");
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.default = exports.Channel = void 0;
|
9 |
|
10 | require("core-js/modules/es.array.slice.js");
|
11 |
|
12 | require("core-js/modules/es.object.to-string.js");
|
13 |
|
14 | require("core-js/modules/es.regexp.to-string.js");
|
15 |
|
16 | require("core-js/modules/web.immediate.js");
|
17 |
|
18 | require("core-js/modules/es.object.keys.js");
|
19 |
|
20 | require("core-js/modules/es.array.filter.js");
|
21 |
|
22 | require("core-js/modules/web.dom-collections.for-each.js");
|
23 |
|
24 | var _utilDeprecate = _interopRequireDefault(require("util-deprecate"));
|
25 |
|
26 | var _tsDedent = _interopRequireDefault(require("ts-dedent"));
|
27 |
|
28 | var _templateObject;
|
29 |
|
30 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
31 |
|
32 | function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
|
33 |
|
34 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
35 |
|
36 | function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
37 |
|
38 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
39 |
|
40 | var generateRandomId = function generateRandomId() {
|
41 |
|
42 | return Math.random().toString(16).slice(2);
|
43 | };
|
44 |
|
45 | var Channel = function () {
|
46 | function Channel() {
|
47 | var _this = this;
|
48 |
|
49 | var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
50 | transport = _ref.transport,
|
51 | _ref$async = _ref.async,
|
52 | async = _ref$async === void 0 ? false : _ref$async;
|
53 |
|
54 | _classCallCheck(this, Channel);
|
55 |
|
56 | this.isAsync = void 0;
|
57 | this.sender = generateRandomId();
|
58 | this.events = {};
|
59 | this.data = {};
|
60 | this.transport = void 0;
|
61 | this.addPeerListener = (0, _utilDeprecate.default)(function (eventName, listener) {
|
62 | _this.addListener(eventName, listener);
|
63 | }, (0, _tsDedent.default)(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n channel.addPeerListener is deprecated\n "]))));
|
64 | this.isAsync = async;
|
65 |
|
66 | if (transport) {
|
67 | this.transport = transport;
|
68 | this.transport.setHandler(function (event) {
|
69 | return _this.handleEvent(event);
|
70 | });
|
71 | }
|
72 | }
|
73 |
|
74 | _createClass(Channel, [{
|
75 | key: "hasTransport",
|
76 | get: function get() {
|
77 | return !!this.transport;
|
78 | }
|
79 | }, {
|
80 | key: "addListener",
|
81 | value: function addListener(eventName, listener) {
|
82 | this.events[eventName] = this.events[eventName] || [];
|
83 | this.events[eventName].push(listener);
|
84 | }
|
85 | }, {
|
86 | key: "emit",
|
87 | value: function emit(eventName) {
|
88 | var _this2 = this;
|
89 |
|
90 | for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
91 | args[_key - 1] = arguments[_key];
|
92 | }
|
93 |
|
94 | var event = {
|
95 | type: eventName,
|
96 | args: args,
|
97 | from: this.sender
|
98 | };
|
99 | var options = {};
|
100 |
|
101 | if (args.length >= 1 && args[0] && args[0].options) {
|
102 | options = args[0].options;
|
103 | }
|
104 |
|
105 | var handler = function handler() {
|
106 | if (_this2.transport) {
|
107 | _this2.transport.send(event, options);
|
108 | }
|
109 |
|
110 | _this2.handleEvent(event);
|
111 | };
|
112 |
|
113 | if (this.isAsync) {
|
114 |
|
115 | setImmediate(handler);
|
116 | } else {
|
117 | handler();
|
118 | }
|
119 | }
|
120 | }, {
|
121 | key: "last",
|
122 | value: function last(eventName) {
|
123 | return this.data[eventName];
|
124 | }
|
125 | }, {
|
126 | key: "eventNames",
|
127 | value: function eventNames() {
|
128 | return Object.keys(this.events);
|
129 | }
|
130 | }, {
|
131 | key: "listenerCount",
|
132 | value: function listenerCount(eventName) {
|
133 | var listeners = this.listeners(eventName);
|
134 | return listeners ? listeners.length : 0;
|
135 | }
|
136 | }, {
|
137 | key: "listeners",
|
138 | value: function listeners(eventName) {
|
139 | var listeners = this.events[eventName];
|
140 | return listeners || undefined;
|
141 | }
|
142 | }, {
|
143 | key: "once",
|
144 | value: function once(eventName, listener) {
|
145 | var onceListener = this.onceListener(eventName, listener);
|
146 | this.addListener(eventName, onceListener);
|
147 | }
|
148 | }, {
|
149 | key: "removeAllListeners",
|
150 | value: function removeAllListeners(eventName) {
|
151 | if (!eventName) {
|
152 | this.events = {};
|
153 | } else if (this.events[eventName]) {
|
154 | delete this.events[eventName];
|
155 | }
|
156 | }
|
157 | }, {
|
158 | key: "removeListener",
|
159 | value: function removeListener(eventName, listener) {
|
160 | var listeners = this.listeners(eventName);
|
161 |
|
162 | if (listeners) {
|
163 | this.events[eventName] = listeners.filter(function (l) {
|
164 | return l !== listener;
|
165 | });
|
166 | }
|
167 | }
|
168 | }, {
|
169 | key: "on",
|
170 | value: function on(eventName, listener) {
|
171 | this.addListener(eventName, listener);
|
172 | }
|
173 | }, {
|
174 | key: "off",
|
175 | value: function off(eventName, listener) {
|
176 | this.removeListener(eventName, listener);
|
177 | }
|
178 | }, {
|
179 | key: "handleEvent",
|
180 | value: function handleEvent(event) {
|
181 | var listeners = this.listeners(event.type);
|
182 |
|
183 | if (listeners && listeners.length) {
|
184 | listeners.forEach(function (fn) {
|
185 | fn.apply(event, event.args);
|
186 | });
|
187 | }
|
188 |
|
189 | this.data[event.type] = event.args;
|
190 | }
|
191 | }, {
|
192 | key: "onceListener",
|
193 | value: function onceListener(eventName, listener) {
|
194 | var _this3 = this;
|
195 |
|
196 | var onceListener = function onceListener() {
|
197 | _this3.removeListener(eventName, onceListener);
|
198 |
|
199 | return listener.apply(void 0, arguments);
|
200 | };
|
201 |
|
202 | return onceListener;
|
203 | }
|
204 | }]);
|
205 |
|
206 | return Channel;
|
207 | }();
|
208 |
|
209 | exports.Channel = Channel;
|
210 | var _default = Channel;
|
211 | exports.default = _default; |
\ | No newline at end of file |