Skip to content

Commit db02381

Browse files
committed
fix for ES5 #57
1 parent 1d3bec9 commit db02381

File tree

3 files changed

+75
-40
lines changed

3 files changed

+75
-40
lines changed

eslint.config.mjs

+10
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,15 @@ export default [
1515
globals: globals.browser
1616
}
1717
},
18+
{
19+
rules: {
20+
"no-unused-vars": [
21+
"error",
22+
{
23+
caughtErrors: "none"
24+
}
25+
]
26+
}
27+
},
1828
pluginJs.configs.recommended
1929
];

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"description": "Communication and Synchronization between browser tabs/windows. Works cross-domain.",
55
"main": "sysend.js",
66
"typings": "sysend.d.ts",
7-
"scripts": {},
7+
"scripts": {
8+
"lint": "eslint"
9+
},
810
"repository": {
911
"type": "git",
1012
"url": "git+https://github.com/jcubic/sysend.js.git"

sysend.js

+62-39
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* http://stackoverflow.com/q/24182409/387194
99
*
1010
*/
11-
/* global define, module, exports, localStorage, setTimeout */
11+
/* global define, module, exports, Symbol, Promise */
1212
(function (root, factory) {
1313
if (typeof define === 'function' && define.amd) {
1414
define(['sysend'], factory);
@@ -68,7 +68,7 @@
6868
id: target_id,
6969
broadcast: function(event, data) {
7070
if (channel && !force_ls) {
71-
log('broadcast', { event, data });
71+
log('broadcast', { event: event, data: data });
7272
channel.postMessage({name: event, data: serialize(data)});
7373
} else {
7474
set(event, to_json(data));
@@ -81,7 +81,7 @@
8181
return sysend;
8282
},
8383
emit: function(event, data) {
84-
log('emit', { event, data });
84+
log('emit', { event: event, data: data });
8585
sysend.broadcast(event, data);
8686
invoke(event, data);
8787
return sysend;
@@ -95,7 +95,8 @@
9595
serializer.from = from;
9696
return sysend;
9797
},
98-
proxy: function(...args) {
98+
proxy: function() {
99+
var args = Array.prototype.slice.call(arguments);
99100
args.forEach(function(url) {
100101
if (is_string(url) && host(url) !== window.location.host) {
101102
domains = domains || [];
@@ -153,7 +154,7 @@
153154
callbacks[event].push(fn);
154155
return sysend;
155156
},
156-
off: function(event, fn, internal = false) {
157+
off: function(event, fn, internal) {
157158
if (callbacks[event]) {
158159
if (fn) {
159160
for (var i = callbacks[event].length; i--;) {
@@ -167,7 +168,7 @@
167168
}
168169
return sysend;
169170
},
170-
track: function(event, fn, internal = false) {
171+
track: function(event, fn, internal) {
171172
if (internal) {
172173
fn[Symbol.for(uniq_prefix)] = true;
173174
}
@@ -176,7 +177,7 @@
176177
}
177178
return sysend;
178179
},
179-
untrack: function(event, fn, internal = false) {
180+
untrack: function(event, fn, internal) {
180181
if (events.includes(event) && handlers[event].length) {
181182
if (fn === undefined) {
182183
if (internal) {
@@ -208,7 +209,7 @@
208209
return new Promise(function(resolve) {
209210
var ids = [];
210211
function handler(data) {
211-
log('__window_ack__', { data, marker });
212+
log('__window_ack__', { data: data, marker: marker });
212213
if (data.origin.target === target_id && data.origin.id === id) {
213214
ids.push({
214215
id: data.id,
@@ -219,13 +220,14 @@
219220
sysend.on(make_internal('__window_ack__'), handler);
220221
sysend.broadcast(make_internal('__window__'), { id: marker });
221222
timer().then(function() {
222-
log('timeout', { ids });
223+
log('timeout', { ids: ids });
223224
sysend.off(make_internal('__window_ack__'), handler);
224225
resolve(ids);
225226
});
226227
});
227228
},
228-
channel: function(...args) {
229+
channel: function() {
230+
var args = Array.prototype.slice.call(arguments);
229231
domains = args.map(origin);
230232
return sysend;
231233
},
@@ -241,60 +243,79 @@
241243
},
242244
rpc: function(object) {
243245
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__";
246248
var request_index = 0;
247249
var timeout = 1000;
248-
function request(id, method, args = []) {
250+
function request(id, method, args) {
251+
args = args || [];
249252
var req_id = ++request_index;
250253
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;
252257
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);
257261
} else {
258-
resolve(result);
262+
resolve(data.result);
259263
}
260264
clearTimeout(timer);
261265
sysend.untrack('message', handler);
262266
}
263267
}
264268
}, 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);
266276
var timer = setTimeout(function() {
267277
reject(new Error('Timeout error'));
268278
}, timeout);
269279
});
270280
}
271281

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;
273285
if (data.type == req) {
274-
var { method, args, id } = data;
275286
var type = res;
276-
if (Object.hasOwn(object, method)) {
287+
if (Object.hasOwn(object, data.method)) {
277288
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 });
280292
}, function(error) {
281-
sysend.post(origin, { error: error.message, id, type });
293+
sysend.post(origin, { error: error.message, id: data.id, type: type });
282294
});
283295
} 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+
});
285301
}
286302
} 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+
});
288308

289309
}
290310
}
291311
}, true);
292312
var error_msg = 'You need to specify the target window/tab';
293313
return Object.fromEntries(Object.keys(object).map(function(name) {
294-
return [name, function(id, ...args) {
314+
return [name, function(id) {
295315
if (!id) {
296316
return Promise.reject(new Error(error_msg));
297317
}
318+
var args = Array.prototype.slice.call(arguments, 1);
298319
return request(id, name, args);
299320
}];
300321
}));
@@ -333,10 +354,10 @@
333354
};
334355
})();
335356
// -------------------------------------------------------------------------
336-
function unpromise(obj, callback, error = null) {
357+
function unpromise(obj, callback, error) {
337358
if (is_promise(obj)) {
338359
var ret = obj.then(callback);
339-
if (error === null) {
360+
if (!error) {
340361
return ret;
341362
} else {
342363
return ret.catch(error);
@@ -417,7 +438,7 @@
417438
}
418439
// -------------------------------------------------------------------------
419440
function is_promise(obj) {
420-
return obj && typeof object == 'object' && is_function(object.then);
441+
return obj && typeof obj == 'object' && is_function(obj.then);
421442
}
422443
// -------------------------------------------------------------------------
423444
function is_function(o) {
@@ -477,7 +498,8 @@
477498
});
478499
}
479500
// -------------------------------------------------------------------------
480-
function trigger(arr, ...args) {
501+
function trigger(arr) {
502+
var args = Array.prototype.slice.call(arguments, 1);
481503
arr.forEach(function(fn) {
482504
fn.apply(null, args);
483505
});
@@ -498,7 +520,7 @@
498520
// -------------------------------------------------------------------------
499521
function set(key, value) {
500522
// storage event is not fired when value is set first time
501-
log({set: key, value});
523+
log({set: key, value: value});
502524
if (id == 0) {
503525
ls().setItem(make_internal(key), random_value);
504526
}
@@ -679,7 +701,7 @@
679701
sysend.track('open', function(data) {
680702
if (data.id !== sysend.id) {
681703
list.push(data);
682-
log({ list, action: 'open' });
704+
log({ list: list, action: 'open' });
683705
update();
684706
}
685707
}, true);
@@ -688,7 +710,7 @@
688710
list = list.filter(function(tab) {
689711
return data.id !== tab.id;
690712
});
691-
log({ list, action: 'close' });
713+
log({ list: list, action: 'close' });
692714
update();
693715
}, true);
694716

@@ -697,7 +719,8 @@
697719
// -------------------------------------------------------------------------
698720
function setup_channel() {
699721
if (sa_handle) {
700-
if (sa_handle.hasOwnProperty('BroadcastChannel')) {
722+
var hasOwnProperty = Object.prototype.hasOwnProperty;
723+
if (hasOwnProperty.call(sa_handle, 'BroadcastChannel')) {
701724
channel = new sa_handle.BroadcastChannel(uniq_prefix);
702725
}
703726
} else {
@@ -811,7 +834,7 @@
811834
});
812835

813836
sysend.on(make_internal('__window__'), function(data) {
814-
log('__window__', { data })
837+
log('__window__', { data: data })
815838
sysend.broadcast(make_internal('__window_ack__'), {
816839
id: target_id,
817840
origin: data.id,
@@ -865,7 +888,7 @@
865888
// -------------------------------------------------------------------------
866889
function init() {
867890
if (is_function(window.BroadcastChannel)) {
868-
const ssa = document.requestStorageAccess && 'hasUnpartitionedCookieAccess' in document;
891+
var ssa = document.requestStorageAccess && 'hasUnpartitionedCookieAccess' in document;
869892
if (is_secured_iframe() && ssa) {
870893
document.requestStorageAccess({
871894
all: true

0 commit comments

Comments
 (0)