Comparing version 1.6.2 to 1.7.0
{ | ||
"name": "sysend", | ||
"version": "1.6.2", | ||
"version": "1.7.0", | ||
"description": "Send messages to other tabs/windows in the same browser", | ||
@@ -5,0 +5,0 @@ "main": "sysend.js", |
@@ -5,4 +5,4 @@ <p align="center"> | ||
[![npm](https://img.shields.io/badge/npm-1.6.2-blue.svg)](https://www.npmjs.com/package/sysend) | ||
![bower](https://img.shields.io/badge/bower-1.6.2-yellow.svg) | ||
[![npm](https://img.shields.io/badge/npm-1.7.0-blue.svg)](https://www.npmjs.com/package/sysend) | ||
![bower](https://img.shields.io/badge/bower-1.7.0-yellow.svg) | ||
![downloads](https://img.shields.io/npm/dt/sysend.svg) | ||
@@ -115,3 +115,3 @@ [![jsdelivr](https://img.shields.io/jsdelivr/npm/hm/sysend)](https://www.jsdelivr.com/package/npm/sysend) | ||
* `post(<window_id>, [, object])` - send any data to other window (new in 1.6.0). | ||
* `list()` - function return Promise of UUID for other windows, you can use those to send message with `post()` (new in 1.6.0). | ||
* `list()` - function return Promise of objects `{id:<UUID>, primary}` for other windows, you can use those to send message with `post()` (new in 1.6.0). | ||
* `track(event, callback)` - track specific event (new in 1.6.0), avilable events: `"open"`, `"close"`, `"primary"`, `"secondary"`, callback is a function that accepts single object as argument: | ||
@@ -118,0 +118,0 @@ * `"open"`: `{count, primary, id}`. |
@@ -23,3 +23,3 @@ /**@license | ||
untrack(event: 'open' | 'close' | 'primary' | 'secondary' | 'message', fn?: (data?: any) => void): void; | ||
list(): Promise<Array<String>>; | ||
list(): Promise<Array<{ id: string, primary: boolean }>>; | ||
post(target: string, data?: any): void; | ||
@@ -26,0 +26,0 @@ } |
249
sysend.js
/**@license | ||
* sysend.js - send messages between browser windows/tabs version 1.6.2 | ||
* sysend.js - send messages between browser windows/tabs version 1.7.0 | ||
* | ||
@@ -21,3 +21,3 @@ * Copyright (C) 2014-2021 Jakub T. Jankiewicz <https://jakub.jankiewicz.org> | ||
})(typeof window !== "undefined" ? window : this, function() { | ||
var target_collecting_timeout = 400; | ||
var collecting_timeout = 400; | ||
// we use prefix so `foo' event don't collide with `foo' locaStorage value | ||
@@ -38,2 +38,4 @@ var uniq_prefix = '___sysend___'; | ||
var id = 0; | ||
// identifier for making each call to list unique | ||
var list_id = 0; | ||
@@ -157,14 +159,19 @@ // id of the window/tab | ||
list: function() { | ||
var id = list_id++; | ||
var marker = { target: target_id, id: id }; | ||
var timer = delay(sysend.timeout); | ||
return new Promise(function(resolve) { | ||
var ids = []; | ||
sysend.on('__window_ack__', function(data) { | ||
if (data.origin === target_id) { | ||
ids.push(data.id); | ||
if (data.origin.target === target_id && data.origin.id === id) { | ||
ids.push({ | ||
id: data.id, | ||
primary: data.primary | ||
}); | ||
} | ||
}); | ||
sysend.broadcast('__window__', { id: target_id }); | ||
setTimeout(function() { | ||
sysend.off('__window_ack__'); | ||
sysend.broadcast('__window__', { id: marker }); | ||
timer().then(function() { | ||
resolve(ids); | ||
}, target_collecting_timeout); | ||
}); | ||
}); | ||
@@ -177,4 +184,60 @@ }, | ||
// ------------------------------------------------------------------------- | ||
Object.defineProperty(sysend, 'timeout', { | ||
enumerable: true, | ||
get: function() { | ||
return collecting_timeout; | ||
}, | ||
set: function(value) { | ||
if (typeof value === 'number' && !isNaN(value)) { | ||
collecting_timeout = value; | ||
} | ||
} | ||
}); | ||
// ------------------------------------------------------------------------- | ||
var host = (function() { | ||
if (typeof URL !== 'undefined') { | ||
return function(url) { | ||
url = new URL(url); | ||
return url.host; | ||
}; | ||
} | ||
var a = document.createElement('a'); | ||
return function(url) { | ||
a.href = url; | ||
return a.host; | ||
}; | ||
})(); | ||
// ------------------------------------------------------------------------- | ||
init(); | ||
// ------------------------------------------------------------------------- | ||
function delay(time) { | ||
return function() { | ||
return new Promise(function(resolve) { | ||
setTimeout(resolve, time); | ||
}); | ||
}; | ||
} | ||
// ------------------------------------------------------------------------- | ||
function onLoad() { | ||
return new Promise(function(resolve) { | ||
window.addEventListener('load', resolve, true); | ||
}).then(iframeLoaded); | ||
} | ||
// ------------------------------------------------------------------------- | ||
function iframeLoaded() { | ||
var iframes = Array.from(document.querySelectorAll('iframe')); | ||
return Promise.all(iframes.filter(function(iframe) { | ||
return iframe.src; | ||
}).map(function(iframe) { | ||
return new Promise(function(resolve, reject) { | ||
iframe.addEventListener('load', () => { | ||
resolve(); | ||
}, true); | ||
iframe.addEventListener('error', reject, true); | ||
}); | ||
})).then(delay(sysend.timeout)); | ||
// delay is required, something with browser is not intitled properly | ||
// the number was pick by experimentation | ||
} | ||
// ------------------------------------------------------------------------- | ||
// ref: https://stackoverflow.com/a/8809472/387194 | ||
@@ -286,16 +349,2 @@ // license: Public Domain/MIT | ||
// ------------------------------------------------------------------------- | ||
var host = (function() { | ||
if (typeof URL !== 'undefined') { | ||
return function(url) { | ||
url = new URL(url); | ||
return url.host; | ||
}; | ||
} | ||
var a = document.createElement('a'); | ||
return function(url) { | ||
a.href = url; | ||
return a.host; | ||
}; | ||
})(); | ||
// ------------------------------------------------------------------------- | ||
function invoke(key, data) { | ||
@@ -318,10 +367,2 @@ callbacks[key].forEach(function(fn) { | ||
function init() { | ||
// we need to clean up localStorage if broadcast called on unload | ||
// because setTimeout will never fire, even setTimeout 0 | ||
var re = new RegExp('^' + uniq_prefix); | ||
for(var key in localStorage) { | ||
if (key.match(re)) { | ||
localStorage.removeItem(key); | ||
} | ||
} | ||
if (typeof window.BroadcastChannel === 'function') { | ||
@@ -342,2 +383,10 @@ channel = new window.BroadcastChannel(uniq_prefix); | ||
} else { | ||
// we need to clean up localStorage if broadcast called on unload | ||
// because setTimeout will never fire, even setTimeout 0 | ||
var re = new RegExp('^' + uniq_prefix); | ||
for(var key in localStorage) { | ||
if (key.match(re)) { | ||
localStorage.removeItem(key); | ||
} | ||
} | ||
window.addEventListener('storage', function(e) { | ||
@@ -361,9 +410,25 @@ // prevent event to be executed on remove in IE | ||
sysend.on('__open__', function(data) { | ||
var id = data.id; | ||
target_count++; | ||
if (primary) { | ||
sysend.broadcast('__ack__'); | ||
} | ||
setTimeout(function() { | ||
if (is_iframe()) { | ||
window.addEventListener('message', function(e) { | ||
if (typeof e.data === 'string' && e.data.match(prefix_re)) { | ||
try { | ||
var payload = JSON.parse(e.data); | ||
if (payload && payload.name === uniq_prefix) { | ||
var data = unserialize(payload.data); | ||
sysend.broadcast(payload.key, data); | ||
} | ||
} catch(e) { | ||
// ignore wrong JSON, the message don't came from Sysend | ||
// even that the string have unix_prefix, this is just in case | ||
} | ||
} | ||
}); | ||
} else { | ||
sysend.on('__open__', function(data) { | ||
var id = data.id; | ||
target_count++; | ||
if (primary) { | ||
sysend.broadcast('__ack__'); | ||
} | ||
trigger(handlers.open, { | ||
@@ -374,68 +439,60 @@ count: target_count, | ||
}); | ||
}, 100); | ||
}); | ||
}); | ||
sysend.on('__ack__', function() { | ||
if (!primary) { | ||
trigger(handlers.secondary); | ||
} | ||
}); | ||
sysend.on('__ack__', function() { | ||
if (!primary) { | ||
trigger(handlers.secondary); | ||
} | ||
}); | ||
sysend.on('__close__', function(data) { | ||
--target_count; | ||
if (target_count === 1) { | ||
primary = true; | ||
} | ||
var payload = { | ||
id: data.id, | ||
count: target_count, | ||
primary: primary, | ||
self: data.id === target_id | ||
}; | ||
trigger(handlers.close, payload); | ||
if (primary) { | ||
trigger(handlers.primary); | ||
} | ||
}); | ||
sysend.on('__close__', function(data) { | ||
--target_count; | ||
if (target_count === 1) { | ||
primary = true; | ||
} | ||
var payload = { | ||
id: data.id, | ||
count: target_count, | ||
primary: primary, | ||
self: data.id === target_id | ||
}; | ||
trigger(handlers.close, payload); | ||
if (primary) { | ||
trigger(handlers.primary); | ||
} | ||
}); | ||
sysend.on('__window__', function(data) { | ||
sysend.broadcast('__window_ack__', { id: target_id, origin: data.id }); | ||
}); | ||
sysend.on('__window__', function(data) { | ||
sysend.broadcast('__window_ack__', { | ||
id: target_id, | ||
origin: data.id, | ||
primary: primary | ||
}); | ||
}); | ||
sysend.on('__message__', function(data) { | ||
if (data.target === target_id) { | ||
trigger(handlers.message, data); | ||
} | ||
}); | ||
sysend.on('__message__', function(data) { | ||
if (data.target === target_id) { | ||
trigger(handlers.message, data); | ||
} | ||
}); | ||
addEventListener('beforeunload', function() { | ||
sysend.emit('__close__', { id: target_id }); | ||
}, { capture: true }); | ||
addEventListener('beforeunload', function() { | ||
sysend.emit('__close__', { id: target_id }); | ||
}, { capture: true }); | ||
sysend.list().then(function(list) { | ||
target_count = list.length; | ||
primary = list.length === 0; | ||
sysend.emit('__open__', { | ||
id: target_id, | ||
primary: primary | ||
onLoad().then(function() { | ||
sysend.list().then(function(list) { | ||
target_count = list.length; | ||
primary = list.length === 0; | ||
console.log([...list]); | ||
console.log(primary); | ||
sysend.emit('__open__', { | ||
id: target_id, | ||
primary: primary | ||
}); | ||
if (primary) { | ||
trigger(handlers.primary); | ||
} | ||
}); | ||
}); | ||
if (primary) { | ||
trigger(handlers.primary); | ||
} | ||
}); | ||
if (is_iframe()) { | ||
window.addEventListener('message', function(e) { | ||
if (typeof e.data === 'string' && e.data.match(prefix_re)) { | ||
try { | ||
var payload = JSON.parse(e.data); | ||
if (payload && payload.name === uniq_prefix) { | ||
sysend.broadcast(payload.key, unserialize(payload.data)); | ||
} | ||
} catch(e) { | ||
// ignore wrong JSON, the message don't came from Sysend | ||
// even that the string have unix_prefix, this is just in case | ||
} | ||
} | ||
}); | ||
} | ||
@@ -442,0 +499,0 @@ } |
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
27315
507