broadcast-channel
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,3 +0,3 @@ | ||
var BroadcastChannel = require('./index.js'); | ||
var BroadcastChannel = require('./index.es5.js'); | ||
window['BroadcastChannel2'] = BroadcastChannel; |
@@ -7,84 +7,81 @@ import { isPromise } from './util.js'; | ||
module.exports = function () { | ||
var BroadcastChannel = function BroadcastChannel(name, options) { | ||
this.name = name; | ||
this.options = fillOptionsWithDefaults(options); | ||
this.method = chooseMethod(this.options); | ||
var BroadcastChannel = function BroadcastChannel(name, options) { | ||
this.name = name; | ||
this.options = fillOptionsWithDefaults(options); | ||
this.method = chooseMethod(this.options); | ||
this._preparePromise = null; | ||
_prepareChannel(this); | ||
}; | ||
this._preparePromise = null; | ||
_prepareChannel(this); | ||
}; | ||
BroadcastChannel.prototype = { | ||
postMessage: function postMessage(msg) { | ||
var _this = this; | ||
BroadcastChannel.prototype = { | ||
postMessage: function postMessage(msg) { | ||
var _this = this; | ||
var msgObj = { | ||
time: new Date().getTime(), | ||
data: msg | ||
}; | ||
var msgObj = { | ||
time: new Date().getTime(), | ||
data: msg | ||
}; | ||
if (this.closed) { | ||
throw new Error('BroadcastChannel.postMessage(): ' + 'Cannot post message after channel has closed'); | ||
} | ||
if (this.closed) { | ||
throw new Error('BroadcastChannel.postMessage(): ' + 'Cannot post message after channel has closed'); | ||
} | ||
var awaitPrepare = this._preparePromise ? this._preparePromise : Promise.resolve(); | ||
return awaitPrepare.then(function () { | ||
return _this.method.postMessage(_this._state, msgObj); | ||
}); | ||
}, | ||
var awaitPrepare = this._preparePromise ? this._preparePromise : Promise.resolve(); | ||
return awaitPrepare.then(function () { | ||
return _this.method.postMessage(_this._state, msgObj); | ||
set onmessage(fn) { | ||
var _this2 = this; | ||
var time = new Date().getTime() - 5; | ||
if (this._preparePromise) { | ||
this._preparePromise.then(function () { | ||
_this2.method.onMessage(_this2._state, messageHandler(fn, time), time); | ||
}); | ||
}, | ||
} else { | ||
this.method.onMessage(this._state, messageHandler(fn, time), time); | ||
} | ||
}, | ||
close: function close() { | ||
var _this3 = this; | ||
set onmessage(fn) { | ||
var _this2 = this; | ||
this.closed = true; | ||
var awaitPrepare = this._preparePromise ? this._preparePromise : Promise.resolve(); | ||
return awaitPrepare.then(function () { | ||
return _this3.method.close(_this3._state); | ||
}); | ||
}, | ||
var time = new Date().getTime() - 5; | ||
if (this._preparePromise) { | ||
this._preparePromise.then(function () { | ||
_this2.method.onMessage(_this2._state, messageHandler(fn, time), time); | ||
}); | ||
} else { | ||
this.method.onMessage(this._state, messageHandler(fn, time), time); | ||
} | ||
}, | ||
close: function close() { | ||
var _this3 = this; | ||
get type() { | ||
return this.method.type; | ||
} | ||
}; | ||
this.closed = true; | ||
var awaitPrepare = this._preparePromise ? this._preparePromise : Promise.resolve(); | ||
return awaitPrepare.then(function () { | ||
return _this3.method.close(_this3._state); | ||
}); | ||
}, | ||
function _prepareChannel(channel) { | ||
var maybePromise = channel.method.create(channel.name, channel.options); | ||
if (isPromise(maybePromise)) { | ||
channel._preparePromise = 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)); | ||
}*/ | ||
channel._state = s; | ||
}); | ||
} else { | ||
channel._state = maybePromise; | ||
} | ||
} | ||
get type() { | ||
return this.method.type; | ||
function messageHandler(fn, minTime) { | ||
return function (msgObj) { | ||
if (msgObj.time >= minTime) { | ||
fn(msgObj.data); | ||
} | ||
}; | ||
}; | ||
function _prepareChannel(channel) { | ||
var maybePromise = channel.method.create(channel.name, channel.options); | ||
if (isPromise(maybePromise)) { | ||
channel._preparePromise = 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)); | ||
}*/ | ||
channel._state = s; | ||
}); | ||
} else { | ||
channel._state = maybePromise; | ||
} | ||
} | ||
function messageHandler(fn, minTime) { | ||
return function (msgObj) { | ||
if (msgObj.time >= minTime) { | ||
fn(msgObj.data); | ||
} | ||
}; | ||
}; | ||
return BroadcastChannel; | ||
}(); | ||
export default BroadcastChannel; |
'use strict'; | ||
var BroadcastChannel = require('./index.js'); | ||
var BroadcastChannel = require('./index.es5.js'); | ||
window['BroadcastChannel2'] = BroadcastChannel; |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _util = require('./util.js'); | ||
@@ -9,84 +13,81 @@ | ||
module.exports = function () { | ||
var BroadcastChannel = function BroadcastChannel(name, options) { | ||
this.name = name; | ||
this.options = (0, _options.fillOptionsWithDefaults)(options); | ||
this.method = (0, _methodChooser.chooseMethod)(this.options); | ||
var BroadcastChannel = function BroadcastChannel(name, options) { | ||
this.name = name; | ||
this.options = (0, _options.fillOptionsWithDefaults)(options); | ||
this.method = (0, _methodChooser.chooseMethod)(this.options); | ||
this._preparePromise = null; | ||
_prepareChannel(this); | ||
}; | ||
this._preparePromise = null; | ||
_prepareChannel(this); | ||
}; | ||
BroadcastChannel.prototype = { | ||
postMessage: function postMessage(msg) { | ||
var _this = this; | ||
BroadcastChannel.prototype = { | ||
postMessage: function postMessage(msg) { | ||
var _this = this; | ||
var msgObj = { | ||
time: new Date().getTime(), | ||
data: msg | ||
}; | ||
var msgObj = { | ||
time: new Date().getTime(), | ||
data: msg | ||
}; | ||
if (this.closed) { | ||
throw new Error('BroadcastChannel.postMessage(): ' + 'Cannot post message after channel has closed'); | ||
} | ||
if (this.closed) { | ||
throw new Error('BroadcastChannel.postMessage(): ' + 'Cannot post message after channel has closed'); | ||
} | ||
var awaitPrepare = this._preparePromise ? this._preparePromise : Promise.resolve(); | ||
return awaitPrepare.then(function () { | ||
return _this.method.postMessage(_this._state, msgObj); | ||
}); | ||
}, | ||
var awaitPrepare = this._preparePromise ? this._preparePromise : Promise.resolve(); | ||
return awaitPrepare.then(function () { | ||
return _this.method.postMessage(_this._state, msgObj); | ||
set onmessage(fn) { | ||
var _this2 = this; | ||
var time = new Date().getTime() - 5; | ||
if (this._preparePromise) { | ||
this._preparePromise.then(function () { | ||
_this2.method.onMessage(_this2._state, messageHandler(fn, time), time); | ||
}); | ||
}, | ||
} else { | ||
this.method.onMessage(this._state, messageHandler(fn, time), time); | ||
} | ||
}, | ||
close: function close() { | ||
var _this3 = this; | ||
set onmessage(fn) { | ||
var _this2 = this; | ||
this.closed = true; | ||
var awaitPrepare = this._preparePromise ? this._preparePromise : Promise.resolve(); | ||
return awaitPrepare.then(function () { | ||
return _this3.method.close(_this3._state); | ||
}); | ||
}, | ||
var time = new Date().getTime() - 5; | ||
if (this._preparePromise) { | ||
this._preparePromise.then(function () { | ||
_this2.method.onMessage(_this2._state, messageHandler(fn, time), time); | ||
}); | ||
} else { | ||
this.method.onMessage(this._state, messageHandler(fn, time), time); | ||
} | ||
}, | ||
close: function close() { | ||
var _this3 = this; | ||
get type() { | ||
return this.method.type; | ||
} | ||
}; | ||
this.closed = true; | ||
var awaitPrepare = this._preparePromise ? this._preparePromise : Promise.resolve(); | ||
return awaitPrepare.then(function () { | ||
return _this3.method.close(_this3._state); | ||
}); | ||
}, | ||
function _prepareChannel(channel) { | ||
var maybePromise = channel.method.create(channel.name, channel.options); | ||
if ((0, _util.isPromise)(maybePromise)) { | ||
channel._preparePromise = 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)); | ||
}*/ | ||
channel._state = s; | ||
}); | ||
} else { | ||
channel._state = maybePromise; | ||
} | ||
} | ||
get type() { | ||
return this.method.type; | ||
function messageHandler(fn, minTime) { | ||
return function (msgObj) { | ||
if (msgObj.time >= minTime) { | ||
fn(msgObj.data); | ||
} | ||
}; | ||
}; | ||
function _prepareChannel(channel) { | ||
var maybePromise = channel.method.create(channel.name, channel.options); | ||
if ((0, _util.isPromise)(maybePromise)) { | ||
channel._preparePromise = 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)); | ||
}*/ | ||
channel._state = s; | ||
}); | ||
} else { | ||
channel._state = maybePromise; | ||
} | ||
} | ||
function messageHandler(fn, minTime) { | ||
return function (msgObj) { | ||
if (msgObj.time >= minTime) { | ||
fn(msgObj.data); | ||
} | ||
}; | ||
}; | ||
return BroadcastChannel; | ||
}(); | ||
exports['default'] = BroadcastChannel; |
{ | ||
"name": "broadcast-channel", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A BroadcastChannel implementation that works with new browsers, older browsers and Node.js", | ||
@@ -26,3 +26,3 @@ "homepage": "https://github.com/pubkey/broadcast-channel#readme", | ||
}, | ||
"main": "./dist/lib/index.js", | ||
"main": "./dist/lib/index.es5.js", | ||
"jsnext:main": "./dist/es/index.js", | ||
@@ -29,0 +29,0 @@ "module": "./dist/es/index.js", |
@@ -1,3 +0,3 @@ | ||
const BroadcastChannel = require('./index.js'); | ||
const BroadcastChannel = require('./index.es5.js'); | ||
window['BroadcastChannel2'] = BroadcastChannel; |
148
src/index.js
@@ -14,47 +14,38 @@ import { | ||
module.exports = (() => { | ||
const BroadcastChannel = function (name, options) { | ||
this.name = name; | ||
this.options = fillOptionsWithDefaults(options); | ||
this.method = chooseMethod(this.options); | ||
const BroadcastChannel = function (name, options) { | ||
this.name = name; | ||
this.options = fillOptionsWithDefaults(options); | ||
this.method = chooseMethod(this.options); | ||
this._preparePromise = null; | ||
_prepareChannel(this); | ||
}; | ||
this._preparePromise = null; | ||
_prepareChannel(this); | ||
}; | ||
BroadcastChannel.prototype = { | ||
postMessage(msg) { | ||
const msgObj = { | ||
time: new Date().getTime(), | ||
data: msg | ||
}; | ||
BroadcastChannel.prototype = { | ||
postMessage(msg) { | ||
const msgObj = { | ||
time: new Date().getTime(), | ||
data: msg | ||
}; | ||
if (this.closed) { | ||
throw new Error( | ||
'BroadcastChannel.postMessage(): ' + | ||
'Cannot post message after channel has closed' | ||
); | ||
} | ||
if (this.closed) { | ||
throw new Error( | ||
'BroadcastChannel.postMessage(): ' + | ||
'Cannot post message after channel has closed' | ||
); | ||
} | ||
const awaitPrepare = this._preparePromise ? this._preparePromise : Promise.resolve(); | ||
return awaitPrepare.then(() => { | ||
return this.method.postMessage( | ||
this._state, | ||
msgObj | ||
); | ||
}); | ||
}, | ||
set onmessage(fn) { | ||
const time = new Date().getTime() - 5; | ||
if (this._preparePromise) { | ||
this._preparePromise.then(() => { | ||
this.method.onMessage( | ||
this._state, | ||
messageHandler(fn, time), | ||
time | ||
); | ||
}); | ||
} else { | ||
const awaitPrepare = this._preparePromise ? this._preparePromise : Promise.resolve(); | ||
return awaitPrepare.then(() => { | ||
return this.method.postMessage( | ||
this._state, | ||
msgObj | ||
); | ||
}); | ||
}, | ||
set onmessage(fn) { | ||
const time = new Date().getTime() - 5; | ||
if (this._preparePromise) { | ||
this._preparePromise.then(() => { | ||
this.method.onMessage( | ||
@@ -65,45 +56,50 @@ this._state, | ||
); | ||
} | ||
}, | ||
close() { | ||
this.closed = true; | ||
const awaitPrepare = this._preparePromise ? this._preparePromise : Promise.resolve(); | ||
return awaitPrepare.then(() => { | ||
return this.method.close( | ||
this._state | ||
); | ||
}); | ||
}, | ||
get type() { | ||
return this.method.type; | ||
} | ||
}; | ||
function _prepareChannel(channel){ | ||
const maybePromise = channel.method.create(channel.name, channel.options); | ||
if (isPromise(maybePromise)) { | ||
channel._preparePromise = maybePromise; | ||
maybePromise.then(s => { | ||
// used in tests to simulate slow runtime | ||
/*if (channel.options.prepareDelay) { | ||
await new Promise(res => setTimeout(res, this.options.prepareDelay)); | ||
}*/ | ||
channel._state = s; | ||
}); | ||
} else { | ||
channel._state = maybePromise; | ||
this.method.onMessage( | ||
this._state, | ||
messageHandler(fn, time), | ||
time | ||
); | ||
} | ||
}, | ||
close() { | ||
this.closed = true; | ||
const awaitPrepare = this._preparePromise ? this._preparePromise : Promise.resolve(); | ||
return awaitPrepare.then(() => { | ||
return this.method.close( | ||
this._state | ||
); | ||
}); | ||
}, | ||
get type() { | ||
return this.method.type; | ||
} | ||
}; | ||
function messageHandler(fn, minTime) { | ||
return msgObj => { | ||
if (msgObj.time >= minTime) { | ||
fn(msgObj.data); | ||
} | ||
}; | ||
function _prepareChannel(channel) { | ||
const maybePromise = channel.method.create(channel.name, channel.options); | ||
if (isPromise(maybePromise)) { | ||
channel._preparePromise = maybePromise; | ||
maybePromise.then(s => { | ||
// used in tests to simulate slow runtime | ||
/*if (channel.options.prepareDelay) { | ||
await new Promise(res => setTimeout(res, this.options.prepareDelay)); | ||
}*/ | ||
channel._state = s; | ||
}); | ||
} else { | ||
channel._state = maybePromise; | ||
} | ||
} | ||
function messageHandler(fn, minTime) { | ||
return msgObj => { | ||
if (msgObj.time >= minTime) { | ||
fn(msgObj.data); | ||
} | ||
}; | ||
}; | ||
return BroadcastChannel; | ||
})(); | ||
export default BroadcastChannel; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
167201
38
3776