Skip to content

Commit

Permalink
4.18.1
Browse files Browse the repository at this point in the history
  • Loading branch information
pubkey committed Oct 31, 2022
1 parent 0e63523 commit b772346
Show file tree
Hide file tree
Showing 59 changed files with 1,300 additions and 3,801 deletions.
16 changes: 16 additions & 0 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ A BroadcastChannel that allows you to send data between different browser-tabs o

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).


# Sponsored by

<p align="center">
<a href="https://rxdb.info/?utm_source=github&utm_medium=repo&utm_campaign=github-broadcast-channel-readme">
<img
src="https://github.com/pubkey/rxdb/raw/master/docs-src/files/logo/logo_text.svg"
alt="Sponsored by RxDB"
width="300"
/>
<br />
<br />
<span>The <b>JavaScript Database</b></span>
</a>
</p>

## Using the BroadcastChannel

```bash
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

## X.X.X (comming soon)

## 4.18.1 (31 October 2022)

- Updated dependencies

## 4.18.0 (6 October 2022)


Expand Down
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
<p align="center">
<a href="https://twitter.com/pubkeypubkey">
<img src="https://img.shields.io/twitter/follow/pubkeypubkey.svg?style=social&logo=twitter"
alt="follow on Twitter"></a>
alt="follow on Twitter" />
</a>
</p>

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

# [Read the full documentation on github](https://github.com/pubkey/broadcast-channel)


# Sponsored by

<p align="center">
<a href="https://rxdb.info/?utm_source=github&utm_medium=repo&utm_campaign=github-broadcast-channel-npm">
<img
src="https://github.com/pubkey/rxdb/raw/master/docs-src/files/logo/logo_text.svg"
alt="Sponsored by RxDB"
width="300"
/>
<br />
<br />
<span>The <b>JavaScript Database</b></span>
</a>
</p>
80 changes: 24 additions & 56 deletions dist/es5node/broadcast-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,91 +6,83 @@ Object.defineProperty(exports, "__esModule", {
exports.OPEN_BROADCAST_CHANNELS = exports.BroadcastChannel = void 0;
exports.clearNodeFolder = clearNodeFolder;
exports.enforceOptions = enforceOptions;

var _util = require("./util.js");

var _methodChooser = require("./method-chooser.js");

var _options = require("./options.js");

/**
* Contains all open channels,
* used in tests to ensure everything is closed.
*/
var OPEN_BROADCAST_CHANNELS = new Set();
exports.OPEN_BROADCAST_CHANNELS = OPEN_BROADCAST_CHANNELS;
var lastId = 0;

var BroadcastChannel = function BroadcastChannel(name, options) {
// identifier of the channel to debug stuff
this.id = lastId++;
OPEN_BROADCAST_CHANNELS.add(this);
this.name = name;

if (ENFORCED_OPTIONS) {
options = ENFORCED_OPTIONS;
}

this.options = (0, _options.fillOptionsWithDefaults)(options);
this.method = (0, _methodChooser.chooseMethod)(this.options); // isListening
this.method = (0, _methodChooser.chooseMethod)(this.options);

// isListening
this._iL = false;

/**
* _onMessageListener
* setting onmessage twice,
* will overwrite the first listener
*/

this._onML = null;

/**
* _addEventListeners
*/

this._addEL = {
message: [],
internal: []
};

/**
* Unsend message promises
* where the sending is still in progress
* @type {Set<Promise>}
*/

this._uMP = new Set();

/**
* _beforeClose
* array of promises that will be awaited
* before the channel is closed
*/

this._befC = [];

/**
* _preparePromise
*/

this._prepP = null;

_prepareChannel(this);
}; // STATICS
};

// STATICS

/**
* used to identify if someone overwrites
* window.BroadcastChannel with this
* See methods/native.js
*/


exports.BroadcastChannel = BroadcastChannel;
BroadcastChannel._pubkey = true;

/**
* clears the tmp-folder if is node
* @return {Promise<boolean>} true if has run, false if not node
*/

function clearNodeFolder(options) {
options = (0, _options.fillOptionsWithDefaults)(options);
var method = (0, _methodChooser.chooseMethod)(options);

if (method.type === 'node') {
return method.clearNodeFolder().then(function () {
return true;
Expand All @@ -99,19 +91,17 @@ function clearNodeFolder(options) {
return _util.PROMISE_RESOLVED_FALSE;
}
}

/**
* if set, this method is enforced,
* no mather what the options are
*/


var ENFORCED_OPTIONS;

function enforceOptions(options) {
ENFORCED_OPTIONS = options;
} // PROTOTYPE

}

// PROTOTYPE
BroadcastChannel.prototype = {
postMessage: function postMessage(msg) {
if (this.closed) {
Expand All @@ -123,87 +113,77 @@ BroadcastChannel.prototype = {
*/
JSON.stringify(msg));
}

return _post(this, 'message', msg);
},
postInternal: function postInternal(msg) {
return _post(this, 'internal', msg);
},

set onmessage(fn) {
var time = this.method.microSeconds();
var listenObj = {
time: time,
fn: fn
};

_removeListenerObject(this, 'message', this._onML);

if (fn && typeof fn === 'function') {
this._onML = listenObj;

_addListenerObject(this, 'message', listenObj);
} else {
this._onML = null;
}
},

addEventListener: function addEventListener(type, fn) {
var time = this.method.microSeconds();
var listenObj = {
time: time,
fn: fn
};

_addListenerObject(this, type, listenObj);
},
removeEventListener: function removeEventListener(type, fn) {
var obj = this._addEL[type].find(function (obj) {
return obj.fn === fn;
});

_removeListenerObject(this, type, obj);
},
close: function close() {
var _this = this;

if (this.closed) {
return;
}

OPEN_BROADCAST_CHANNELS["delete"](this);
this.closed = true;
var awaitPrepare = this._prepP ? this._prepP : _util.PROMISE_RESOLVED_VOID;
this._onML = null;
this._addEL.message = [];
return awaitPrepare // wait until all current sending are processed
return awaitPrepare
// wait until all current sending are processed
.then(function () {
return Promise.all(Array.from(_this._uMP));
}) // run before-close hooks
})
// run before-close hooks
.then(function () {
return Promise.all(_this._befC.map(function (fn) {
return fn();
}));
}) // close the channel
})
// close the channel
.then(function () {
return _this.method.close(_this._state);
});
},

get type() {
return this.method.type;
},

get isClosed() {
return this.closed;
}

};

/**
* Post a message over the channel
* @returns {Promise} that resolved when the message sending is done
*/

function _post(broadcastChannel, type, msg) {
var time = broadcastChannel.method.microSeconds();
var msgObj = {
Expand All @@ -213,25 +193,22 @@ function _post(broadcastChannel, type, msg) {
};
var awaitPrepare = broadcastChannel._prepP ? broadcastChannel._prepP : _util.PROMISE_RESOLVED_VOID;
return awaitPrepare.then(function () {
var sendPromise = broadcastChannel.method.postMessage(broadcastChannel._state, msgObj); // add/remove to unsend messages list
var sendPromise = broadcastChannel.method.postMessage(broadcastChannel._state, msgObj);

// add/remove to unsend messages list
broadcastChannel._uMP.add(sendPromise);

sendPromise["catch"]().then(function () {
return broadcastChannel._uMP["delete"](sendPromise);
});
return sendPromise;
});
}

function _prepareChannel(channel) {
var maybePromise = channel.method.create(channel.name, channel.options);

if ((0, _util.isPromise)(maybePromise)) {
channel._prepP = maybePromise;
maybePromise.then(function (s) {
// used in tests to simulate slow runtime

/*if (channel.options.prepareDelay) {
await new Promise(res => setTimeout(res, this.options.prepareDelay));
}*/
Expand All @@ -241,30 +218,25 @@ function _prepareChannel(channel) {
channel._state = maybePromise;
}
}

function _hasMessageListeners(channel) {
if (channel._addEL.message.length > 0) return true;
if (channel._addEL.internal.length > 0) return true;
return false;
}

function _addListenerObject(channel, type, obj) {
channel._addEL[type].push(obj);

_startListening(channel);
}

function _removeListenerObject(channel, type, obj) {
channel._addEL[type] = channel._addEL[type].filter(function (o) {
return o !== obj;
});

_stopListening(channel);
}

function _startListening(channel) {
if (!channel._iL && _hasMessageListeners(channel)) {
// someone is listening, start subscribing

var listenerFn = function listenerFn(msgObj) {
channel._addEL[msgObj.type].forEach(function (listenerObject) {
/**
Expand All @@ -278,15 +250,12 @@ function _startListening(channel) {
*/
var hundredMsInMicro = 100 * 1000;
var minMessageTime = listenerObject.time - hundredMsInMicro;

if (msgObj.time >= minMessageTime) {
listenerObject.fn(msgObj.data);
}
});
};

var time = channel.method.microSeconds();

if (channel._prepP) {
channel._prepP.then(function () {
channel._iL = true;
Expand All @@ -298,7 +267,6 @@ function _startListening(channel) {
}
}
}

function _stopListening(channel) {
if (channel._iL && !_hasMessageListeners(channel)) {
// noone is listening, stop subscribing
Expand Down
1 change: 0 additions & 1 deletion dist/es5node/browserify.index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use strict";

var _module = require('./index.es5.js');

var BroadcastChannel = _module.BroadcastChannel;
var createLeaderElection = _module.createLeaderElection;
window['BroadcastChannel2'] = BroadcastChannel;
Expand Down
2 changes: 1 addition & 1 deletion dist/es5node/index.es5.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use strict";

var _index = require("./index.js");

/**
* because babel can only export on default-attribute,
* we use this for the non-module-build
Expand All @@ -10,6 +9,7 @@ var _index = require("./index.js");
* but
* var BroadcastChannel = require('broadcast-channel');
*/

module.exports = {
BroadcastChannel: _index.BroadcastChannel,
createLeaderElection: _index.createLeaderElection,
Expand Down
Loading

0 comments on commit b772346

Please sign in to comment.