|
8 | 8 | * http://stackoverflow.com/q/24182409/387194
|
9 | 9 | *
|
10 | 10 | */
|
11 |
| -/* global define, module, exports, localStorage, setTimeout */ |
| 11 | +/* global define, module, exports, Symbol, Promise */ |
12 | 12 | (function (root, factory) {
|
13 | 13 | if (typeof define === 'function' && define.amd) {
|
14 | 14 | define(['sysend'], factory);
|
|
68 | 68 | id: target_id,
|
69 | 69 | broadcast: function(event, data) {
|
70 | 70 | if (channel && !force_ls) {
|
71 |
| - log('broadcast', { event, data }); |
| 71 | + log('broadcast', { event: event, data: data }); |
72 | 72 | channel.postMessage({name: event, data: serialize(data)});
|
73 | 73 | } else {
|
74 | 74 | set(event, to_json(data));
|
|
81 | 81 | return sysend;
|
82 | 82 | },
|
83 | 83 | emit: function(event, data) {
|
84 |
| - log('emit', { event, data }); |
| 84 | + log('emit', { event: event, data: data }); |
85 | 85 | sysend.broadcast(event, data);
|
86 | 86 | invoke(event, data);
|
87 | 87 | return sysend;
|
|
95 | 95 | serializer.from = from;
|
96 | 96 | return sysend;
|
97 | 97 | },
|
98 |
| - proxy: function(...args) { |
| 98 | + proxy: function() { |
| 99 | + var args = Array.prototype.slice.call(arguments); |
99 | 100 | args.forEach(function(url) {
|
100 | 101 | if (is_string(url) && host(url) !== window.location.host) {
|
101 | 102 | domains = domains || [];
|
|
153 | 154 | callbacks[event].push(fn);
|
154 | 155 | return sysend;
|
155 | 156 | },
|
156 |
| - off: function(event, fn, internal = false) { |
| 157 | + off: function(event, fn, internal) { |
157 | 158 | if (callbacks[event]) {
|
158 | 159 | if (fn) {
|
159 | 160 | for (var i = callbacks[event].length; i--;) {
|
|
167 | 168 | }
|
168 | 169 | return sysend;
|
169 | 170 | },
|
170 |
| - track: function(event, fn, internal = false) { |
| 171 | + track: function(event, fn, internal) { |
171 | 172 | if (internal) {
|
172 | 173 | fn[Symbol.for(uniq_prefix)] = true;
|
173 | 174 | }
|
|
176 | 177 | }
|
177 | 178 | return sysend;
|
178 | 179 | },
|
179 |
| - untrack: function(event, fn, internal = false) { |
| 180 | + untrack: function(event, fn, internal) { |
180 | 181 | if (events.includes(event) && handlers[event].length) {
|
181 | 182 | if (fn === undefined) {
|
182 | 183 | if (internal) {
|
|
208 | 209 | return new Promise(function(resolve) {
|
209 | 210 | var ids = [];
|
210 | 211 | function handler(data) {
|
211 |
| - log('__window_ack__', { data, marker }); |
| 212 | + log('__window_ack__', { data: data, marker: marker }); |
212 | 213 | if (data.origin.target === target_id && data.origin.id === id) {
|
213 | 214 | ids.push({
|
214 | 215 | id: data.id,
|
|
219 | 220 | sysend.on(make_internal('__window_ack__'), handler);
|
220 | 221 | sysend.broadcast(make_internal('__window__'), { id: marker });
|
221 | 222 | timer().then(function() {
|
222 |
| - log('timeout', { ids }); |
| 223 | + log('timeout', { ids: ids }); |
223 | 224 | sysend.off(make_internal('__window_ack__'), handler);
|
224 | 225 | resolve(ids);
|
225 | 226 | });
|
226 | 227 | });
|
227 | 228 | },
|
228 |
| - channel: function(...args) { |
| 229 | + channel: function() { |
| 230 | + var args = Array.prototype.slice.call(arguments); |
229 | 231 | domains = args.map(origin);
|
230 | 232 | return sysend;
|
231 | 233 | },
|
|
241 | 243 | },
|
242 | 244 | rpc: function(object) {
|
243 | 245 | var prefix = ++rpc_count;
|
244 |
| - var req = `__${prefix}_rpc_request__`; |
245 |
| - var res = `__${prefix}_rpc_response__`; |
| 246 | + var req = "__" + prefix + "_rpc_request__"; |
| 247 | + var res = "__" + prefix + "_rpc_response__"; |
246 | 248 | var request_index = 0;
|
247 | 249 | var timeout = 1000;
|
248 |
| - function request(id, method, args = []) { |
| 250 | + function request(id, method, args) { |
| 251 | + args = args || []; |
249 | 252 | var req_id = ++request_index;
|
250 | 253 | return new Promise(function(resolve, reject) {
|
251 |
| - sysend.track('message', function handler({data, origin}) { |
| 254 | + sysend.track('message', function handler(message) { |
| 255 | + var data = message.data; |
| 256 | + var origin = message.origin; |
252 | 257 | if (data.type === res) {
|
253 |
| - var { result, error, id: res_id } = data; |
254 |
| - if (origin === id && req_id === res_id) { |
255 |
| - if (error) { |
256 |
| - reject(error); |
| 258 | + if (origin === id && req_id === data.id) { |
| 259 | + if (data.error) { |
| 260 | + reject(data.error); |
257 | 261 | } else {
|
258 |
| - resolve(result); |
| 262 | + resolve(data.result); |
259 | 263 | }
|
260 | 264 | clearTimeout(timer);
|
261 | 265 | sysend.untrack('message', handler);
|
262 | 266 | }
|
263 | 267 | }
|
264 | 268 | }, true);
|
265 |
| - sysend.post(id, { method, id: req_id, type: req, args }); |
| 269 | + var payload = { |
| 270 | + method: method, |
| 271 | + id: req_id, |
| 272 | + type: req, |
| 273 | + args: args |
| 274 | + }; |
| 275 | + sysend.post(id, payload); |
266 | 276 | var timer = setTimeout(function() {
|
267 | 277 | reject(new Error('Timeout error'));
|
268 | 278 | }, timeout);
|
269 | 279 | });
|
270 | 280 | }
|
271 | 281 |
|
272 |
| - sysend.track('message', async function handler({ data, origin }) { |
| 282 | + sysend.track('message', function handler(message) { |
| 283 | + var data = message.data; |
| 284 | + var origin = message.origin; |
273 | 285 | if (data.type == req) {
|
274 |
| - var { method, args, id } = data; |
275 | 286 | var type = res;
|
276 |
| - if (Object.hasOwn(object, method)) { |
| 287 | + if (Object.hasOwn(object, data.method)) { |
277 | 288 | try {
|
278 |
| - unpromise(object[method](...args), function(result) { |
279 |
| - sysend.post(origin, { result, id, type }); |
| 289 | + var fn = object[data.method]; |
| 290 | + unpromise(fn.apply(object, data.args), function(result) { |
| 291 | + sysend.post(origin, { result: result, id: data.id, type: type }); |
280 | 292 | }, function(error) {
|
281 |
| - sysend.post(origin, { error: error.message, id, type }); |
| 293 | + sysend.post(origin, { error: error.message, id: data.id, type: type }); |
282 | 294 | });
|
283 | 295 | } catch(e) {
|
284 |
| - sysend.post(origin, { error: e.message, id, type }); |
| 296 | + sysend.post(origin, { |
| 297 | + error: e.message, |
| 298 | + id: id, |
| 299 | + type: type |
| 300 | + }); |
285 | 301 | }
|
286 | 302 | } else {
|
287 |
| - sysend.post(origin, { error: 'Method not found', id, type }); |
| 303 | + sysend.post(origin, { |
| 304 | + error: 'Method not found', |
| 305 | + id: id, |
| 306 | + type: type |
| 307 | + }); |
288 | 308 |
|
289 | 309 | }
|
290 | 310 | }
|
291 | 311 | }, true);
|
292 | 312 | var error_msg = 'You need to specify the target window/tab';
|
293 | 313 | return Object.fromEntries(Object.keys(object).map(function(name) {
|
294 |
| - return [name, function(id, ...args) { |
| 314 | + return [name, function(id) { |
295 | 315 | if (!id) {
|
296 | 316 | return Promise.reject(new Error(error_msg));
|
297 | 317 | }
|
| 318 | + var args = Array.prototype.slice.call(arguments, 1); |
298 | 319 | return request(id, name, args);
|
299 | 320 | }];
|
300 | 321 | }));
|
|
333 | 354 | };
|
334 | 355 | })();
|
335 | 356 | // -------------------------------------------------------------------------
|
336 |
| - function unpromise(obj, callback, error = null) { |
| 357 | + function unpromise(obj, callback, error) { |
337 | 358 | if (is_promise(obj)) {
|
338 | 359 | var ret = obj.then(callback);
|
339 |
| - if (error === null) { |
| 360 | + if (!error) { |
340 | 361 | return ret;
|
341 | 362 | } else {
|
342 | 363 | return ret.catch(error);
|
|
417 | 438 | }
|
418 | 439 | // -------------------------------------------------------------------------
|
419 | 440 | function is_promise(obj) {
|
420 |
| - return obj && typeof object == 'object' && is_function(object.then); |
| 441 | + return obj && typeof obj == 'object' && is_function(obj.then); |
421 | 442 | }
|
422 | 443 | // -------------------------------------------------------------------------
|
423 | 444 | function is_function(o) {
|
|
477 | 498 | });
|
478 | 499 | }
|
479 | 500 | // -------------------------------------------------------------------------
|
480 |
| - function trigger(arr, ...args) { |
| 501 | + function trigger(arr) { |
| 502 | + var args = Array.prototype.slice.call(arguments, 1); |
481 | 503 | arr.forEach(function(fn) {
|
482 | 504 | fn.apply(null, args);
|
483 | 505 | });
|
|
498 | 520 | // -------------------------------------------------------------------------
|
499 | 521 | function set(key, value) {
|
500 | 522 | // storage event is not fired when value is set first time
|
501 |
| - log({set: key, value}); |
| 523 | + log({set: key, value: value}); |
502 | 524 | if (id == 0) {
|
503 | 525 | ls().setItem(make_internal(key), random_value);
|
504 | 526 | }
|
|
679 | 701 | sysend.track('open', function(data) {
|
680 | 702 | if (data.id !== sysend.id) {
|
681 | 703 | list.push(data);
|
682 |
| - log({ list, action: 'open' }); |
| 704 | + log({ list: list, action: 'open' }); |
683 | 705 | update();
|
684 | 706 | }
|
685 | 707 | }, true);
|
|
688 | 710 | list = list.filter(function(tab) {
|
689 | 711 | return data.id !== tab.id;
|
690 | 712 | });
|
691 |
| - log({ list, action: 'close' }); |
| 713 | + log({ list: list, action: 'close' }); |
692 | 714 | update();
|
693 | 715 | }, true);
|
694 | 716 |
|
|
697 | 719 | // -------------------------------------------------------------------------
|
698 | 720 | function setup_channel() {
|
699 | 721 | if (sa_handle) {
|
700 |
| - if (sa_handle.hasOwnProperty('BroadcastChannel')) { |
| 722 | + var hasOwnProperty = Object.prototype.hasOwnProperty; |
| 723 | + if (hasOwnProperty.call(sa_handle, 'BroadcastChannel')) { |
701 | 724 | channel = new sa_handle.BroadcastChannel(uniq_prefix);
|
702 | 725 | }
|
703 | 726 | } else {
|
|
811 | 834 | });
|
812 | 835 |
|
813 | 836 | sysend.on(make_internal('__window__'), function(data) {
|
814 |
| - log('__window__', { data }) |
| 837 | + log('__window__', { data: data }) |
815 | 838 | sysend.broadcast(make_internal('__window_ack__'), {
|
816 | 839 | id: target_id,
|
817 | 840 | origin: data.id,
|
|
865 | 888 | // -------------------------------------------------------------------------
|
866 | 889 | function init() {
|
867 | 890 | if (is_function(window.BroadcastChannel)) {
|
868 |
| - const ssa = document.requestStorageAccess && 'hasUnpartitionedCookieAccess' in document; |
| 891 | + var ssa = document.requestStorageAccess && 'hasUnpartitionedCookieAccess' in document; |
869 | 892 | if (is_secured_iframe() && ssa) {
|
870 | 893 | document.requestStorageAccess({
|
871 | 894 | all: true
|
|
0 commit comments