UNPKG

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