@toruslabs/broadcast-channel
Version:
A BroadcastChannel that works in New Browsers, Old Browsers, WebWorkers
78 lines (69 loc) • 2.59 kB
JavaScript
import loglevel from 'loglevel';
// import Bowser from 'bowser';
/**
* returns true if the given object is a promise
*/
function isPromise(obj) {
if (obj && typeof obj.then === "function") {
return true;
}
return false;
}
Promise.resolve(false);
Promise.resolve(true);
const PROMISE_RESOLVED_VOID = Promise.resolve();
function sleep(time, resolveWith) {
if (!time) time = 0;
return new Promise(resolve => {
setTimeout(() => resolve(resolveWith), time);
});
}
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
/**
* https://stackoverflow.com/a/8084248
*/
function generateRandomId() {
return Math.random().toString(36).substring(2);
}
let lastMs = 0;
/**
* returns the current time in micro-seconds,
* WARNING: This is a pseudo-function
* Performance.now is not reliable in webworkers, so we just make sure to never return the same time.
* This is enough in browsers, and this function will not be used in nodejs.
* The main reason for this hack is to ensure that BroadcastChannel behaves equal to production when it is used in fast-running unit tests.
*/
function microSeconds() {
let ret = Date.now() * 1000; // milliseconds to microseconds
if (ret <= lastMs) {
ret = lastMs + 1;
}
lastMs = ret;
return ret;
}
// the problem is only in iframes. we should default to server in case of iframes.
// storage scoping is present in all browsers now
// Safari and other browsers support native Broadcast channel now. It's in LS.
// test here: https://pubkey.github.io/broadcast-channel/e2e.html?methodType=native
// https://caniuse.com/broadcastchannel
// export function are3PCSupported() {
// if (typeof navigator === 'undefined') return false;
// const browserInfo = Bowser.parse(navigator.userAgent);
// log.info(JSON.stringify(browserInfo), 'current browser info');
// let thirdPartyCookieSupport = true;
// // brave
// if (navigator.brave) {
// thirdPartyCookieSupport = false;
// }
// // All webkit & gecko engine instances use itp (intelligent tracking prevention -
// // https://webkit.org/tracking-prevention/#intelligent-tracking-prevention-itp)
// if (browserInfo.engine.name === Bowser.ENGINE_MAP.WebKit || browserInfo.engine.name === Bowser.ENGINE_MAP.Gecko) {
// thirdPartyCookieSupport = false;
// }
// return thirdPartyCookieSupport;
// }
const log = loglevel.getLogger("broadcast-channel");
log.setLevel("error");
export { PROMISE_RESOLVED_VOID, generateRandomId, isPromise, log, microSeconds, randomInt, sleep };