Comparing version 1.17.1 to 1.17.2
{ | ||
"name": "sysend", | ||
"version": "1.17.1", | ||
"version": "1.17.2", | ||
"description": "Communication and Synchronization between browser tabs/windows. Works cross-domain.", | ||
@@ -5,0 +5,0 @@ "main": "sysend.js", |
@@ -5,4 +5,4 @@ <p align="center"> | ||
[![npm](https://img.shields.io/badge/npm-1.17.1-blue.svg)](https://www.npmjs.com/package/sysend) | ||
![bower](https://img.shields.io/badge/bower-1.17.1-yellow.svg) | ||
[![npm](https://img.shields.io/badge/npm-1.17.2-blue.svg)](https://www.npmjs.com/package/sysend) | ||
![bower](https://img.shields.io/badge/bower-1.17.2-yellow.svg) | ||
![downloads](https://img.shields.io/npm/dt/sysend.svg) | ||
@@ -9,0 +9,0 @@ [![jsdelivr](https://img.shields.io/jsdelivr/npm/hm/sysend)](https://www.jsdelivr.com/package/npm/sysend) |
/**@license | ||
* sysend.js - send messages between browser windows/tabs version 1.17.1 | ||
* sysend.js - send messages between browser windows/tabs version 1.17.2 | ||
* | ||
@@ -25,3 +25,3 @@ * Copyright (C) 2014-2023 Jakub T. Jankiewicz <https://jcubic.pl/me> | ||
var uniq_prefix = '___sysend___'; | ||
var prefix_re = new RegExp(uniq_prefix); | ||
var prefix_re = new RegExp(uniq_prefix, 'g'); | ||
var random_value = Math.random(); | ||
@@ -177,3 +177,3 @@ var serializer = {}; | ||
} else { | ||
handlers[event] = handlers[event].filter(fn => { | ||
handlers[event] = handlers[event].filter(function(fn) { | ||
return !fn[Symbol.for(uniq_prefix)]; | ||
@@ -203,3 +203,3 @@ }); | ||
var ids = []; | ||
sysend.on(make_internal('__window_ack__'), function(data) { | ||
function handler(data) { | ||
log('__window_ack__', { data, marker }); | ||
@@ -212,6 +212,8 @@ if (data.origin.target === target_id && data.origin.id === id) { | ||
} | ||
}); | ||
} | ||
sysend.on(make_internal('__window_ack__'), handler); | ||
sysend.broadcast(make_internal('__window__'), { id: marker }); | ||
timer().then(function() { | ||
log('timeout', { ids }); | ||
sysend.off(make_internal('__window_ack__'), handler); | ||
resolve(ids); | ||
@@ -243,3 +245,3 @@ }); | ||
var req_id = ++request_index; | ||
return new Promise((resolve, reject) => { | ||
return new Promise(function(resolve, reject) { | ||
sysend.track('message', function handler({data, origin}) { | ||
@@ -260,3 +262,3 @@ if (data.type === res) { | ||
sysend.post(id, { method, id: req_id, type: req, args }); | ||
var timer = setTimeout(() => { | ||
var timer = setTimeout(function() { | ||
reject(new Error('Timeout error')); | ||
@@ -288,4 +290,4 @@ }, timeout); | ||
var error_msg = 'You need to specify the target window/tab'; | ||
return Object.fromEntries(Object.keys(object).map(name => { | ||
return [name, (id, ...args) => { | ||
return Object.fromEntries(Object.keys(object).map(function(name) { | ||
return [name, function(id, ...args) { | ||
if (!id) { | ||
@@ -490,3 +492,3 @@ return Promise.reject(new Error(error_msg)); | ||
} | ||
ls().setItem(uniq_prefix + key, value); | ||
ls().setItem(make_internal(key), value); | ||
} | ||
@@ -496,3 +498,3 @@ // ------------------------------------------------------------------------- | ||
log({remove: key}); | ||
ls().removeItem(uniq_prefix + key); | ||
ls().removeItem(make_internal(key)); | ||
} | ||
@@ -624,4 +626,2 @@ // ------------------------------------------------------------------------- | ||
// ------------------------------------------------------------------------- | ||
setup_update_tracking(); | ||
// ------------------------------------------------------------------------- | ||
function setup_ls() { | ||
@@ -642,4 +642,8 @@ log('setup_ls()'); | ||
var key = e.key.replace(re, ''); | ||
log('__key__', e.key + ' ==> ' + key, { | ||
callbacks: callbacks[key], | ||
again: callbacks[key.replace(re, '')] | ||
}); | ||
if (callbacks[key]) { | ||
var value = e.newValue || get(key); | ||
var value = e.newValue || get(e.key); | ||
if (value && value != random_value) { | ||
@@ -657,5 +661,7 @@ var obj = from_json(value); | ||
// ------------------------------------------------------------------------- | ||
function setup_update_tracking() { | ||
var list = []; | ||
function setup_update_tracking(init_list) { | ||
var list = init_list || []; | ||
update(); | ||
function update() { | ||
@@ -665,3 +671,3 @@ trigger(handlers.update, list); | ||
sysend.track('open', data => { | ||
sysend.track('open', function(data) { | ||
if (data.id !== sysend.id) { | ||
@@ -674,4 +680,6 @@ list.push(data); | ||
sysend.track('close', data => { | ||
list = list.filter(tab => data.id !== tab.id); | ||
sysend.track('close', function(data) { | ||
list = list.filter(function(tab) { | ||
return data.id !== tab.id; | ||
}); | ||
log({ list, action: 'close' }); | ||
@@ -681,11 +689,3 @@ update(); | ||
sysend.track('ready', () => { | ||
setTimeout(function() { | ||
sysend.list().then(tabs => { | ||
list = tabs; | ||
log({ list, action: 'ready' }); | ||
update(); | ||
}); | ||
}, 0); | ||
}, true); | ||
} | ||
@@ -836,20 +836,23 @@ // ------------------------------------------------------------------------- | ||
iframe_loaded().then(function() { | ||
sysend.list().then(function(list) { | ||
log('sysend.list()', list); | ||
target_count = list.length; | ||
primary = list.length === 0; | ||
var found = list.find(function(item) { | ||
return item.primary; | ||
setTimeout(function() { | ||
sysend.list().then(function(list) { | ||
log('sysend.list()', list); | ||
target_count = list.length; | ||
primary = list.length === 0; | ||
var found = list.find(function(item) { | ||
return item.primary; | ||
}); | ||
if (found || primary) { | ||
has_primary = true; | ||
} | ||
sysend.emit(make_internal('__open__'), { | ||
id: target_id, | ||
primary: primary | ||
}); | ||
if (primary) { | ||
trigger(handlers.primary); | ||
} | ||
setup_update_tracking(list); | ||
}); | ||
if (found || primary) { | ||
has_primary = true; | ||
} | ||
sysend.emit(make_internal('__open__'), { | ||
id: target_id, | ||
primary: primary | ||
}); | ||
if (primary) { | ||
trigger(handlers.primary); | ||
} | ||
}); | ||
}, 0); | ||
}); | ||
@@ -856,0 +859,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51104
879