|
1 | 1 | /**@license
|
2 |
| - * sysend.js - send messages between browser windows/tabs |
3 |
| - * Copyright (C) 2014 Jakub Jankiewicz <http://jcubic.pl> |
| 2 | + * sysend.js - send messages between browser windows/tabs version 1.2.0 |
4 | 3 | *
|
| 4 | + * Copyright (C) 2014-2018 Jakub Jankiewicz <http://jcubic.pl/me> |
5 | 5 | * Released under the MIT license
|
6 | 6 | *
|
7 |
| - * The idea for this implementation came from this StackOverflow question: |
| 7 | + * The idea for localStorage implementation came from this StackOverflow question: |
8 | 8 | * http://stackoverflow.com/q/24182409/387194
|
| 9 | + * |
9 | 10 | */
|
| 11 | +/* global define, module, exports, localStorage, setTimeout */ |
10 | 12 | (function (root, factory) {
|
11 | 13 | if (typeof define === 'function' && define.amd) {
|
12 | 14 | define(['sysend'], factory);
|
|
60 | 62 | // object with user events as keys and values arrays of callback functions
|
61 | 63 | var callbacks = {};
|
62 | 64 | var index = 0;
|
63 |
| - window.addEventListener('storage', function(e) { |
64 |
| - // prevent event to be executed on remove in IE |
65 |
| - if (e.key.match(re) && index++ % 2 === 0) { |
66 |
| - var key = e.key.replace(re, ''); |
67 |
| - if (callbacks[key]) { |
68 |
| - var value = e.newValue || get(key); |
69 |
| - if (value && value != random_value) { |
70 |
| - var obj = JSON.parse(value); |
71 |
| - if (obj && obj[1] != random_value) { |
72 |
| - // don't call on remove |
73 |
| - callbacks[key].forEach(function(fn) { |
74 |
| - fn(obj[2], key); |
75 |
| - }); |
| 65 | + var channel; |
| 66 | + if (typeof window.BroadcastChannel === 'function') { |
| 67 | + channel = new window.BroadcastChannel(uniq_prefix); |
| 68 | + channel.addEventListener('message', function(event) { |
| 69 | + if (event.target.name === uniq_prefix) { |
| 70 | + var key = event.data && event.data.name; |
| 71 | + if (callbacks[key]) { |
| 72 | + callbacks[key].forEach(function(fn) { |
| 73 | + fn(event.data.data, key); |
| 74 | + }); |
| 75 | + } |
| 76 | + } |
| 77 | + }); |
| 78 | + } else { |
| 79 | + window.addEventListener('storage', function(e) { |
| 80 | + // prevent event to be executed on remove in IE |
| 81 | + if (e.key.match(re) && index++ % 2 === 0) { |
| 82 | + var key = e.key.replace(re, ''); |
| 83 | + if (callbacks[key]) { |
| 84 | + var value = e.newValue || get(key); |
| 85 | + if (value && value != random_value) { |
| 86 | + var obj = JSON.parse(value); |
| 87 | + if (obj && obj[1] != random_value) { |
| 88 | + // don't call on remove |
| 89 | + callbacks[key].forEach(function(fn) { |
| 90 | + fn(obj[2], key); |
| 91 | + }); |
| 92 | + } |
76 | 93 | }
|
77 | 94 | }
|
78 | 95 | }
|
79 |
| - } |
80 |
| - }, false); |
| 96 | + }, false); |
| 97 | + } |
81 | 98 | return {
|
82 | 99 | broadcast: function(event, message) {
|
83 |
| - set(event, to_json(message)); |
84 |
| - // clean up localstorage |
85 |
| - setTimeout(function() { |
86 |
| - remove(event); |
87 |
| - }, 0); |
| 100 | + if (channel) { |
| 101 | + channel.postMessage({name: event, data: message}); |
| 102 | + } else { |
| 103 | + set(event, to_json(message)); |
| 104 | + // clean up localstorage |
| 105 | + setTimeout(function() { |
| 106 | + remove(event); |
| 107 | + }, 0); |
| 108 | + } |
88 | 109 | },
|
89 | 110 | on: function(event, fn) {
|
90 | 111 | if (!callbacks[event]) {
|
|
0 commit comments