broadcast-channel
Advanced tools
Comparing version 1.1.0 to 1.2.0
var BroadcastChannel = require('./index.es5.js'); | ||
var LeaderElection = require('./leader-election/index.es5.js'); | ||
window['BroadcastChannel2'] = BroadcastChannel; | ||
window['BroadcastChannel2'] = BroadcastChannel; | ||
window['LeaderElection'] = LeaderElection; |
@@ -25,2 +25,8 @@ import { isPromise } from './util.js'; | ||
/** | ||
* array of promises that will be awaited | ||
* before the channel is closed | ||
*/ | ||
this._beforeClose = []; | ||
this._preparePromise = null; | ||
@@ -31,3 +37,3 @@ _prepareChannel(this); | ||
BroadcastChannel.prototype = { | ||
postMessage: function postMessage(msg) { | ||
_post: function _post(type, msg) { | ||
var _this = this; | ||
@@ -37,10 +43,6 @@ | ||
time: new Date().getTime(), | ||
type: 'message', | ||
type: type, | ||
data: msg | ||
}; | ||
if (this.closed) { | ||
throw new Error('BroadcastChannel.postMessage(): ' + 'Cannot post message after channel has closed'); | ||
} | ||
var awaitPrepare = this._preparePromise ? this._preparePromise : Promise.resolve(); | ||
@@ -51,2 +53,11 @@ return awaitPrepare.then(function () { | ||
}, | ||
postMessage: function postMessage(msg) { | ||
if (this.closed) { | ||
throw new Error('BroadcastChannel.postMessage(): ' + 'Cannot post message after channel has closed'); | ||
} | ||
return this._post('message', msg); | ||
}, | ||
postInternal: function postInternal(msg) { | ||
return this._post('internal', msg); | ||
}, | ||
@@ -85,2 +96,3 @@ set onmessage(fn) { | ||
if (this.closed) return; | ||
this.closed = true; | ||
@@ -93,2 +105,6 @@ var awaitPrepare = this._preparePromise ? this._preparePromise : Promise.resolve(); | ||
return awaitPrepare.then(function () { | ||
return Promise.all(_this2._beforeClose.map(function (fn) { | ||
return fn(); | ||
})); | ||
}).then(function () { | ||
return _this2.method.close(_this2._state); | ||
@@ -95,0 +111,0 @@ }); |
@@ -261,2 +261,6 @@ /** | ||
return true; | ||
}; | ||
}; | ||
export function averageResponseTime(options) { | ||
return options.idb.fallbackInterval * 1.5; | ||
} |
@@ -138,2 +138,6 @@ /** | ||
return true; | ||
}; | ||
}; | ||
export function averageResponseTime() { | ||
return 120; | ||
} |
@@ -42,2 +42,6 @@ var isNode = require('detect-node'); | ||
if (typeof BroadcastChannel === 'function') return true; | ||
}; | ||
}; | ||
export function averageResponseTime() { | ||
return 100; | ||
} |
@@ -765,3 +765,3 @@ import _regeneratorRuntime from 'babel-runtime/regenerator'; | ||
}).forEach(function (readerUuid) { | ||
channelState.otherReaderClients[readerUuid].close(); | ||
channelState.otherReaderClients[readerUuid].destroy(); | ||
delete channelState.otherReaderClients[readerUuid]; | ||
@@ -921,7 +921,16 @@ }); | ||
_context20.next = 7; | ||
_context20.prev = 5; | ||
_context20.next = 8; | ||
return unlink(channelState.infoFilePath); | ||
case 7: | ||
case 8: | ||
_context20.next = 12; | ||
break; | ||
case 10: | ||
_context20.prev = 10; | ||
_context20.t0 = _context20['catch'](5); | ||
case 12: | ||
Object.values(channelState.otherReaderClients).forEach(function (client) { | ||
@@ -931,3 +940,3 @@ return client.destroy(); | ||
case 8: | ||
case 13: | ||
case 'end': | ||
@@ -937,3 +946,3 @@ return _context20.stop(); | ||
} | ||
}, _callee19, this); | ||
}, _callee19, this, [[5, 10]]); | ||
})); | ||
@@ -948,2 +957,6 @@ | ||
return isNode; | ||
}; | ||
}; | ||
export function averageResponseTime() { | ||
return 50; | ||
} |
'use strict'; | ||
var BroadcastChannel = require('./index.es5.js'); | ||
var LeaderElection = require('./leader-election/index.es5.js'); | ||
window['BroadcastChannel2'] = BroadcastChannel; | ||
window['BroadcastChannel2'] = BroadcastChannel; | ||
window['LeaderElection'] = LeaderElection; |
@@ -31,2 +31,8 @@ 'use strict'; | ||
/** | ||
* array of promises that will be awaited | ||
* before the channel is closed | ||
*/ | ||
this._beforeClose = []; | ||
this._preparePromise = null; | ||
@@ -37,3 +43,3 @@ _prepareChannel(this); | ||
BroadcastChannel.prototype = { | ||
postMessage: function postMessage(msg) { | ||
_post: function _post(type, msg) { | ||
var _this = this; | ||
@@ -43,10 +49,6 @@ | ||
time: new Date().getTime(), | ||
type: 'message', | ||
type: type, | ||
data: msg | ||
}; | ||
if (this.closed) { | ||
throw new Error('BroadcastChannel.postMessage(): ' + 'Cannot post message after channel has closed'); | ||
} | ||
var awaitPrepare = this._preparePromise ? this._preparePromise : Promise.resolve(); | ||
@@ -57,2 +59,11 @@ return awaitPrepare.then(function () { | ||
}, | ||
postMessage: function postMessage(msg) { | ||
if (this.closed) { | ||
throw new Error('BroadcastChannel.postMessage(): ' + 'Cannot post message after channel has closed'); | ||
} | ||
return this._post('message', msg); | ||
}, | ||
postInternal: function postInternal(msg) { | ||
return this._post('internal', msg); | ||
}, | ||
@@ -91,2 +102,3 @@ set onmessage(fn) { | ||
if (this.closed) return; | ||
this.closed = true; | ||
@@ -99,2 +111,6 @@ var awaitPrepare = this._preparePromise ? this._preparePromise : Promise.resolve(); | ||
return awaitPrepare.then(function () { | ||
return Promise.all(_this2._beforeClose.map(function (fn) { | ||
return fn(); | ||
})); | ||
}).then(function () { | ||
return _this2.method.close(_this2._state); | ||
@@ -101,0 +117,0 @@ }); |
@@ -20,2 +20,3 @@ 'use strict'; | ||
exports.canBeUsed = canBeUsed; | ||
exports.averageResponseTime = averageResponseTime; | ||
@@ -282,2 +283,6 @@ var _util = require('../util.js'); | ||
return true; | ||
}; | ||
}; | ||
function averageResponseTime(options) { | ||
return options.idb.fallbackInterval * 1.5; | ||
} |
@@ -16,2 +16,3 @@ 'use strict'; | ||
exports.canBeUsed = canBeUsed; | ||
exports.averageResponseTime = averageResponseTime; | ||
@@ -155,2 +156,6 @@ var _options = require('../options'); | ||
return true; | ||
}; | ||
}; | ||
function averageResponseTime() { | ||
return 120; | ||
} |
@@ -11,2 +11,3 @@ 'use strict'; | ||
exports.canBeUsed = canBeUsed; | ||
exports.averageResponseTime = averageResponseTime; | ||
var isNode = require('detect-node'); | ||
@@ -53,2 +54,6 @@ | ||
if (typeof BroadcastChannel === 'function') return true; | ||
}; | ||
}; | ||
function averageResponseTime() { | ||
return 100; | ||
} |
@@ -710,3 +710,3 @@ 'use strict'; | ||
}).forEach(function (readerUuid) { | ||
channelState.otherReaderClients[readerUuid].close(); | ||
channelState.otherReaderClients[readerUuid].destroy(); | ||
delete channelState.otherReaderClients[readerUuid]; | ||
@@ -858,7 +858,16 @@ }); | ||
_context20.next = 7; | ||
_context20.prev = 5; | ||
_context20.next = 8; | ||
return unlink(channelState.infoFilePath); | ||
case 7: | ||
case 8: | ||
_context20.next = 12; | ||
break; | ||
case 10: | ||
_context20.prev = 10; | ||
_context20.t0 = _context20['catch'](5); | ||
case 12: | ||
Object.values(channelState.otherReaderClients).forEach(function (client) { | ||
@@ -868,3 +877,3 @@ return client.destroy(); | ||
case 8: | ||
case 13: | ||
case 'end': | ||
@@ -874,3 +883,3 @@ return _context20.stop(); | ||
} | ||
}, _callee19, this); | ||
}, _callee19, this, [[5, 10]]); | ||
})); | ||
@@ -890,2 +899,3 @@ | ||
exports.canBeUsed = canBeUsed; | ||
exports.averageResponseTime = averageResponseTime; | ||
@@ -1016,2 +1026,6 @@ var _util = require('util'); | ||
return _detectNode2['default']; | ||
}; | ||
}; | ||
function averageResponseTime() { | ||
return 50; | ||
} |
{ | ||
"name": "broadcast-channel", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A BroadcastChannel implementation that works with new browsers, older browsers and Node.js", | ||
@@ -15,3 +15,4 @@ "homepage": "https://github.com/pubkey/broadcast-channel#readme", | ||
"crosstab", | ||
"ipc" | ||
"ipc", | ||
"leader-election" | ||
], | ||
@@ -46,3 +47,4 @@ "repository": { | ||
"build:iframe": "browserify test_tmp/scripts/iframe.js > docs/iframe.js", | ||
"build": "npm run clear && concurrently \"npm run build:es6\" \"npm run build:es5\" \"npm run build:test\" && concurrently \"npm run build:browser\" \"npm run build:worker\" \"npm run build:iframe\"", | ||
"build:leader-iframe": "browserify test_tmp/scripts/leader-iframe.js > docs/leader-iframe.js", | ||
"build": "npm run clear && concurrently \"npm run build:es6\" \"npm run build:es5\" \"npm run build:test\" && concurrently \"npm run build:browser\" \"npm run build:worker\" \"npm run build:iframe\" \"npm run build:leader-iframe\"", | ||
"build:min": "uglifyjs --compress --mangle --output dist/lib/browserify.min.js -- dist/lib/browserify.index.js", | ||
@@ -61,3 +63,3 @@ "docs:only": "http-server ./docs", | ||
"js-sha3": "0.7.0", | ||
"unload": "1.3.6" | ||
"unload": "1.3.9" | ||
}, | ||
@@ -70,4 +72,4 @@ "devDependencies": { | ||
"babel-core": "6.26.3", | ||
"babel-eslint": "8.2.3", | ||
"babel-loader": "7.1.4", | ||
"babel-eslint": "8.2.5", | ||
"babel-loader": "7.1.5", | ||
"babel-plugin-transform-async-to-generator": "6.24.1", | ||
@@ -92,10 +94,10 @@ "babel-plugin-transform-class-properties": "6.24.1", | ||
"clone": "2.1.1", | ||
"concurrently": "3.5.1", | ||
"concurrently": "3.6.0", | ||
"convert-hrtime": "2.0.0", | ||
"copyfiles": "2.0.0", | ||
"cross-env": "5.1.6", | ||
"cross-env": "5.2.0", | ||
"eslint": "4.19.1", | ||
"gzip-size-cli": "^2.1.0", | ||
"gzip-size-cli": "3.0.0", | ||
"http-server": "0.11.1", | ||
"karma": "2.0.2", | ||
"karma": "2.0.4", | ||
"karma-babel-preprocessor": "7.0.0", | ||
@@ -117,7 +119,7 @@ "karma-browserify": "5.3.0", | ||
"rimraf": "2.6.2", | ||
"testcafe": "0.20.3", | ||
"testcafe-hammerhead": "14.0.0", | ||
"ts-node": "6.1.2", | ||
"testcafe": "0.20.4", | ||
"testcafe-hammerhead": "14.1.1", | ||
"ts-node": "7.0.0", | ||
"typescript": "2.9.2" | ||
} | ||
} |
@@ -11,2 +11,4 @@ | ||
<strong>A BroadcastChannel that works in old browsers, new browsers, WebWorkers and NodeJs</strong> | ||
<br/> | ||
<span>+ LeaderElection over the channels</span> | ||
</p> | ||
@@ -32,3 +34,3 @@ | ||
## Usage | ||
## Using the BroadcastChannel | ||
@@ -113,2 +115,36 @@ This API behaves similar to the [javascript-standard](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API). | ||
## Using the LeaderElection | ||
This module also comes with a leader-election which can be used so elect a leader between different BroadcastChannels. | ||
For example if you have a stable connection from the frontend to your server, you can use the LeaderElection to save server-side performance by only connecting once, even if the user has opened your website in multiple tabs. | ||
Create a channel and an elector. | ||
```js | ||
const BroadcastChannel = require('broadcast-channel'); | ||
const LeaderElection = require('leader-election'); | ||
const channel = new BroadcastChannel('foobar'); | ||
const elector = LeaderElection.create(channel); | ||
``` | ||
Wait until the elector becomes leader. | ||
```js | ||
const LeaderElection = require('leader-election'); | ||
const elector = LeaderElection.create(channel); | ||
elector.awaitLeadership().then(()=> { | ||
console.log('this tab is now leader'); | ||
}) | ||
``` | ||
Let the leader die. (automatically happens if the tab is closed or the process exits). | ||
```js | ||
const elector = LeaderElection.create(channel); | ||
await elector.die(); | ||
``` | ||
## What this is | ||
@@ -115,0 +151,0 @@ |
const BroadcastChannel = require('./index.es5.js'); | ||
const LeaderElection = require('./leader-election/index.es5.js'); | ||
window['BroadcastChannel2'] = BroadcastChannel; | ||
window['LeaderElection'] = LeaderElection; |
@@ -33,2 +33,8 @@ import { | ||
/** | ||
* array of promises that will be awaited | ||
* before the channel is closed | ||
*/ | ||
this._beforeClose = []; | ||
this._preparePromise = null; | ||
@@ -39,16 +45,9 @@ _prepareChannel(this); | ||
BroadcastChannel.prototype = { | ||
postMessage(msg) { | ||
_post(type, msg) { | ||
const msgObj = { | ||
time: new Date().getTime(), | ||
type: 'message', | ||
type, | ||
data: msg | ||
}; | ||
if (this.closed) { | ||
throw new Error( | ||
'BroadcastChannel.postMessage(): ' + | ||
'Cannot post message after channel has closed' | ||
); | ||
} | ||
const awaitPrepare = this._preparePromise ? this._preparePromise : Promise.resolve(); | ||
@@ -62,2 +61,14 @@ return awaitPrepare.then(() => { | ||
}, | ||
postMessage(msg) { | ||
if (this.closed) { | ||
throw new Error( | ||
'BroadcastChannel.postMessage(): ' + | ||
'Cannot post message after channel has closed' | ||
); | ||
} | ||
return this._post('message', msg); | ||
}, | ||
postInternal(msg) { | ||
return this._post('internal', msg); | ||
}, | ||
set onmessage(fn) { | ||
@@ -92,2 +103,3 @@ const time = new Date().getTime() - 5; | ||
close() { | ||
if (this.closed) return; | ||
this.closed = true; | ||
@@ -99,7 +111,9 @@ const awaitPrepare = this._preparePromise ? this._preparePromise : Promise.resolve(); | ||
return awaitPrepare.then(() => { | ||
return this.method.close( | ||
this._state | ||
); | ||
}); | ||
return awaitPrepare | ||
.then(() => Promise.all(this._beforeClose.map(fn => fn()))) | ||
.then(() => { | ||
return this.method.close( | ||
this._state | ||
); | ||
}); | ||
}, | ||
@@ -106,0 +120,0 @@ get type() { |
@@ -264,1 +264,5 @@ /** | ||
}; | ||
export function averageResponseTime(options){ | ||
return options.idb.fallbackInterval * 1.5; | ||
} |
@@ -151,1 +151,6 @@ /** | ||
}; | ||
export function averageResponseTime(){ | ||
return 120; | ||
} |
@@ -43,1 +43,6 @@ const isNode = require('detect-node'); | ||
}; | ||
export function averageResponseTime(){ | ||
return 100; | ||
} |
@@ -411,3 +411,3 @@ /** | ||
.forEach(readerUuid => { | ||
channelState.otherReaderClients[readerUuid].close(); | ||
channelState.otherReaderClients[readerUuid].destroy(); | ||
delete channelState.otherReaderClients[readerUuid]; | ||
@@ -484,3 +484,5 @@ }); | ||
await unlink(channelState.infoFilePath); | ||
try { | ||
await unlink(channelState.infoFilePath); | ||
} catch (err) { } | ||
@@ -495,1 +497,6 @@ Object.values(channelState.otherReaderClients) | ||
}; | ||
export function averageResponseTime(){ | ||
return 50; | ||
} |
201283
46
4670
168
+ Addedunload@1.3.9(transitive)
- Removedunload@1.3.6(transitive)
Updatedunload@1.3.9