Skip to content

Commit b772346

Browse files
committed
4.18.1
1 parent 0e63523 commit b772346

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1300
-3801
lines changed

.github/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ A BroadcastChannel that allows you to send data between different browser-tabs o
2929

3030
This behaves similar to the [BroadcastChannel-API](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API) which is currently only featured in [some browsers](https://caniuse.com/#feat=broadcastchannel).
3131

32+
33+
# Sponsored by
34+
35+
<p align="center">
36+
<a href="https://rxdb.info/?utm_source=github&utm_medium=repo&utm_campaign=github-broadcast-channel-readme">
37+
<img
38+
src="https://github.com/pubkey/rxdb/raw/master/docs-src/files/logo/logo_text.svg"
39+
alt="Sponsored by RxDB"
40+
width="300"
41+
/>
42+
<br />
43+
<br />
44+
<span>The <b>JavaScript Database</b></span>
45+
</a>
46+
</p>
47+
3248
## Using the BroadcastChannel
3349

3450
```bash

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
## X.X.X (comming soon)
55

6+
## 4.18.1 (31 October 2022)
7+
8+
- Updated dependencies
9+
610
## 4.18.0 (6 October 2022)
711

812

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
<p align="center">
2424
<a href="https://twitter.com/pubkeypubkey">
2525
<img src="https://img.shields.io/twitter/follow/pubkeypubkey.svg?style=social&logo=twitter"
26-
alt="follow on Twitter"></a>
26+
alt="follow on Twitter" />
27+
</a>
2728
</p>
2829

2930
![demo.gif](docs/files/demo.gif)
@@ -34,3 +35,19 @@ A BroadcastChannel that allows you to send data between different browser-tabs o
3435
And a LeaderElection over the channels.
3536

3637
# [Read the full documentation on github](https://github.com/pubkey/broadcast-channel)
38+
39+
40+
# Sponsored by
41+
42+
<p align="center">
43+
<a href="https://rxdb.info/?utm_source=github&utm_medium=repo&utm_campaign=github-broadcast-channel-npm">
44+
<img
45+
src="https://github.com/pubkey/rxdb/raw/master/docs-src/files/logo/logo_text.svg"
46+
alt="Sponsored by RxDB"
47+
width="300"
48+
/>
49+
<br />
50+
<br />
51+
<span>The <b>JavaScript Database</b></span>
52+
</a>
53+
</p>

dist/es5node/broadcast-channel.js

Lines changed: 24 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,91 +6,83 @@ Object.defineProperty(exports, "__esModule", {
66
exports.OPEN_BROADCAST_CHANNELS = exports.BroadcastChannel = void 0;
77
exports.clearNodeFolder = clearNodeFolder;
88
exports.enforceOptions = enforceOptions;
9-
109
var _util = require("./util.js");
11-
1210
var _methodChooser = require("./method-chooser.js");
13-
1411
var _options = require("./options.js");
15-
1612
/**
1713
* Contains all open channels,
1814
* used in tests to ensure everything is closed.
1915
*/
2016
var OPEN_BROADCAST_CHANNELS = new Set();
2117
exports.OPEN_BROADCAST_CHANNELS = OPEN_BROADCAST_CHANNELS;
2218
var lastId = 0;
23-
2419
var BroadcastChannel = function BroadcastChannel(name, options) {
2520
// identifier of the channel to debug stuff
2621
this.id = lastId++;
2722
OPEN_BROADCAST_CHANNELS.add(this);
2823
this.name = name;
29-
3024
if (ENFORCED_OPTIONS) {
3125
options = ENFORCED_OPTIONS;
3226
}
33-
3427
this.options = (0, _options.fillOptionsWithDefaults)(options);
35-
this.method = (0, _methodChooser.chooseMethod)(this.options); // isListening
28+
this.method = (0, _methodChooser.chooseMethod)(this.options);
3629

30+
// isListening
3731
this._iL = false;
32+
3833
/**
3934
* _onMessageListener
4035
* setting onmessage twice,
4136
* will overwrite the first listener
4237
*/
43-
4438
this._onML = null;
39+
4540
/**
4641
* _addEventListeners
4742
*/
48-
4943
this._addEL = {
5044
message: [],
5145
internal: []
5246
};
47+
5348
/**
5449
* Unsend message promises
5550
* where the sending is still in progress
5651
* @type {Set<Promise>}
5752
*/
58-
5953
this._uMP = new Set();
54+
6055
/**
6156
* _beforeClose
6257
* array of promises that will be awaited
6358
* before the channel is closed
6459
*/
65-
6660
this._befC = [];
61+
6762
/**
6863
* _preparePromise
6964
*/
70-
7165
this._prepP = null;
72-
7366
_prepareChannel(this);
74-
}; // STATICS
67+
};
68+
69+
// STATICS
7570

7671
/**
7772
* used to identify if someone overwrites
7873
* window.BroadcastChannel with this
7974
* See methods/native.js
8075
*/
81-
82-
8376
exports.BroadcastChannel = BroadcastChannel;
8477
BroadcastChannel._pubkey = true;
78+
8579
/**
8680
* clears the tmp-folder if is node
8781
* @return {Promise<boolean>} true if has run, false if not node
8882
*/
89-
9083
function clearNodeFolder(options) {
9184
options = (0, _options.fillOptionsWithDefaults)(options);
9285
var method = (0, _methodChooser.chooseMethod)(options);
93-
9486
if (method.type === 'node') {
9587
return method.clearNodeFolder().then(function () {
9688
return true;
@@ -99,19 +91,17 @@ function clearNodeFolder(options) {
9991
return _util.PROMISE_RESOLVED_FALSE;
10092
}
10193
}
94+
10295
/**
10396
* if set, this method is enforced,
10497
* no mather what the options are
10598
*/
106-
107-
10899
var ENFORCED_OPTIONS;
109-
110100
function enforceOptions(options) {
111101
ENFORCED_OPTIONS = options;
112-
} // PROTOTYPE
113-
102+
}
114103

104+
// PROTOTYPE
115105
BroadcastChannel.prototype = {
116106
postMessage: function postMessage(msg) {
117107
if (this.closed) {
@@ -123,87 +113,77 @@ BroadcastChannel.prototype = {
123113
*/
124114
JSON.stringify(msg));
125115
}
126-
127116
return _post(this, 'message', msg);
128117
},
129118
postInternal: function postInternal(msg) {
130119
return _post(this, 'internal', msg);
131120
},
132-
133121
set onmessage(fn) {
134122
var time = this.method.microSeconds();
135123
var listenObj = {
136124
time: time,
137125
fn: fn
138126
};
139-
140127
_removeListenerObject(this, 'message', this._onML);
141-
142128
if (fn && typeof fn === 'function') {
143129
this._onML = listenObj;
144-
145130
_addListenerObject(this, 'message', listenObj);
146131
} else {
147132
this._onML = null;
148133
}
149134
},
150-
151135
addEventListener: function addEventListener(type, fn) {
152136
var time = this.method.microSeconds();
153137
var listenObj = {
154138
time: time,
155139
fn: fn
156140
};
157-
158141
_addListenerObject(this, type, listenObj);
159142
},
160143
removeEventListener: function removeEventListener(type, fn) {
161144
var obj = this._addEL[type].find(function (obj) {
162145
return obj.fn === fn;
163146
});
164-
165147
_removeListenerObject(this, type, obj);
166148
},
167149
close: function close() {
168150
var _this = this;
169-
170151
if (this.closed) {
171152
return;
172153
}
173-
174154
OPEN_BROADCAST_CHANNELS["delete"](this);
175155
this.closed = true;
176156
var awaitPrepare = this._prepP ? this._prepP : _util.PROMISE_RESOLVED_VOID;
177157
this._onML = null;
178158
this._addEL.message = [];
179-
return awaitPrepare // wait until all current sending are processed
159+
return awaitPrepare
160+
// wait until all current sending are processed
180161
.then(function () {
181162
return Promise.all(Array.from(_this._uMP));
182-
}) // run before-close hooks
163+
})
164+
// run before-close hooks
183165
.then(function () {
184166
return Promise.all(_this._befC.map(function (fn) {
185167
return fn();
186168
}));
187-
}) // close the channel
169+
})
170+
// close the channel
188171
.then(function () {
189172
return _this.method.close(_this._state);
190173
});
191174
},
192-
193175
get type() {
194176
return this.method.type;
195177
},
196-
197178
get isClosed() {
198179
return this.closed;
199180
}
200-
201181
};
182+
202183
/**
203184
* Post a message over the channel
204185
* @returns {Promise} that resolved when the message sending is done
205186
*/
206-
207187
function _post(broadcastChannel, type, msg) {
208188
var time = broadcastChannel.method.microSeconds();
209189
var msgObj = {
@@ -213,25 +193,22 @@ function _post(broadcastChannel, type, msg) {
213193
};
214194
var awaitPrepare = broadcastChannel._prepP ? broadcastChannel._prepP : _util.PROMISE_RESOLVED_VOID;
215195
return awaitPrepare.then(function () {
216-
var sendPromise = broadcastChannel.method.postMessage(broadcastChannel._state, msgObj); // add/remove to unsend messages list
196+
var sendPromise = broadcastChannel.method.postMessage(broadcastChannel._state, msgObj);
217197

198+
// add/remove to unsend messages list
218199
broadcastChannel._uMP.add(sendPromise);
219-
220200
sendPromise["catch"]().then(function () {
221201
return broadcastChannel._uMP["delete"](sendPromise);
222202
});
223203
return sendPromise;
224204
});
225205
}
226-
227206
function _prepareChannel(channel) {
228207
var maybePromise = channel.method.create(channel.name, channel.options);
229-
230208
if ((0, _util.isPromise)(maybePromise)) {
231209
channel._prepP = maybePromise;
232210
maybePromise.then(function (s) {
233211
// used in tests to simulate slow runtime
234-
235212
/*if (channel.options.prepareDelay) {
236213
await new Promise(res => setTimeout(res, this.options.prepareDelay));
237214
}*/
@@ -241,30 +218,25 @@ function _prepareChannel(channel) {
241218
channel._state = maybePromise;
242219
}
243220
}
244-
245221
function _hasMessageListeners(channel) {
246222
if (channel._addEL.message.length > 0) return true;
247223
if (channel._addEL.internal.length > 0) return true;
248224
return false;
249225
}
250-
251226
function _addListenerObject(channel, type, obj) {
252227
channel._addEL[type].push(obj);
253-
254228
_startListening(channel);
255229
}
256-
257230
function _removeListenerObject(channel, type, obj) {
258231
channel._addEL[type] = channel._addEL[type].filter(function (o) {
259232
return o !== obj;
260233
});
261-
262234
_stopListening(channel);
263235
}
264-
265236
function _startListening(channel) {
266237
if (!channel._iL && _hasMessageListeners(channel)) {
267238
// someone is listening, start subscribing
239+
268240
var listenerFn = function listenerFn(msgObj) {
269241
channel._addEL[msgObj.type].forEach(function (listenerObject) {
270242
/**
@@ -278,15 +250,12 @@ function _startListening(channel) {
278250
*/
279251
var hundredMsInMicro = 100 * 1000;
280252
var minMessageTime = listenerObject.time - hundredMsInMicro;
281-
282253
if (msgObj.time >= minMessageTime) {
283254
listenerObject.fn(msgObj.data);
284255
}
285256
});
286257
};
287-
288258
var time = channel.method.microSeconds();
289-
290259
if (channel._prepP) {
291260
channel._prepP.then(function () {
292261
channel._iL = true;
@@ -298,7 +267,6 @@ function _startListening(channel) {
298267
}
299268
}
300269
}
301-
302270
function _stopListening(channel) {
303271
if (channel._iL && !_hasMessageListeners(channel)) {
304272
// noone is listening, stop subscribing

dist/es5node/browserify.index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"use strict";
22

33
var _module = require('./index.es5.js');
4-
54
var BroadcastChannel = _module.BroadcastChannel;
65
var createLeaderElection = _module.createLeaderElection;
76
window['BroadcastChannel2'] = BroadcastChannel;

dist/es5node/index.es5.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"use strict";
22

33
var _index = require("./index.js");
4-
54
/**
65
* because babel can only export on default-attribute,
76
* we use this for the non-module-build
@@ -10,6 +9,7 @@ var _index = require("./index.js");
109
* but
1110
* var BroadcastChannel = require('broadcast-channel');
1211
*/
12+
1313
module.exports = {
1414
BroadcastChannel: _index.BroadcastChannel,
1515
createLeaderElection: _index.createLeaderElection,

0 commit comments

Comments
 (0)