Skip to content

Commit 0af7a40

Browse files
committed
fix making primary when primary is closed #24
When there is more then 2 windows and primary is closed Then first that is visible become primary
1 parent 348a92b commit 0af7a40

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ sysend object:
112112
* `serializer(to_string, from_string)` - add serializer and deserializer functions (new in 1.4.0).
113113
* `post(<window_id>, [, object])` - send any data to other window (new in 1.6.0).
114114
* `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).
115-
* `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:
115+
* `track(event, callback)` - track specific event (new in 1.6.0), avilable events: `"open"`, `"close"`, `"primary"`, `"secondary"`, `"message"`, callback is a function that accepts single object as argument:
116116
* `"open"`: `{count, primary, id}`.
117117
* `"close"`: `{count, primary, id, self}`.
118118
* `"primary"` and `"secondary"` no argument is given.

sysend.js

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
var serializer = {};
2727
// object with user events as keys and values arrays of callback functions
2828
var callbacks = {};
29+
var has_primary;
2930
var iframes = [];
3031
var index = 0;
3132
var channel;
@@ -46,9 +47,9 @@
4647
close: [],
4748
open: [],
4849
secondary: [],
49-
message: []
50+
message: [],
51+
visbility: []
5052
};
51-
5253
var events = Object.keys(handlers);
5354
// -------------------------------------------------------------------------
5455
var serialize = make_process(serializer, 'to');
@@ -358,6 +359,26 @@
358359
}
359360
}
360361
// -------------------------------------------------------------------------
362+
function init_visiblity() {
363+
// ref: https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API
364+
var hidden, visibilityChange;
365+
if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support
366+
hidden = "hidden";
367+
visibilityChange = "visibilitychange";
368+
} else if (typeof document.msHidden !== "undefined") {
369+
hidden = "msHidden";
370+
visibilityChange = "msvisibilitychange";
371+
} else if (typeof document.webkitHidden !== "undefined") {
372+
hidden = "webkitHidden";
373+
visibilityChange = "webkitvisibilitychange";
374+
}
375+
if (typeof document.addEventListener === 'function' && hidden) {
376+
document.addEventListener(visibilityChange, function() {
377+
trigger(handlers.visbility, !document[hidden]);
378+
}, false);
379+
}
380+
}
381+
// -------------------------------------------------------------------------
361382
function init() {
362383
if (typeof window.BroadcastChannel === 'function') {
363384
channel = new window.BroadcastChannel(uniq_prefix);
@@ -416,6 +437,19 @@
416437
}
417438
});
418439
} else {
440+
init_visiblity();
441+
442+
sysend.track('visbility', function(visible) {
443+
if (visible && !has_primary) {
444+
primary = true;
445+
trigger(handlers.primary);
446+
sysend.emit('__primary__');
447+
}
448+
});
449+
450+
sysend.on('__primary__', function() {
451+
has_primary = true;
452+
});
419453

420454
sysend.on('__open__', function(data) {
421455
var id = data.id;
@@ -438,12 +472,18 @@
438472

439473
sysend.on('__close__', function(data) {
440474
--target_count;
441-
if (target_count === 1) {
475+
var last = target_count === 1;
476+
if (data.wasPrimary && !primary) {
477+
has_primary = false;
478+
}
479+
if (last) {
442480
primary = true;
481+
has_primary = true;
443482
}
444483
var payload = {
445484
id: data.id,
446485
count: target_count,
486+
wasPrimary: data.wasPrimary,
447487
primary: primary,
448488
self: data.id === target_id
449489
};
@@ -468,15 +508,19 @@
468508
});
469509

470510
addEventListener('beforeunload', function() {
471-
sysend.emit('__close__', { id: target_id });
511+
sysend.emit('__close__', { id: target_id, wasPrimary: primary });
472512
}, { capture: true });
473513

474514
onLoad().then(function() {
475515
sysend.list().then(function(list) {
476516
target_count = list.length;
477517
primary = list.length === 0;
478-
console.log([...list]);
479-
console.log(primary);
518+
var found = list.find(function(item) {
519+
return item.primary;
520+
});
521+
if (found || primary) {
522+
has_primary = true;
523+
}
480524
sysend.emit('__open__', {
481525
id: target_id,
482526
primary: primary

0 commit comments

Comments
 (0)