broadcast-channel
Version:
A BroadcastChannel that works in New Browsers, Old Browsers, WebWorkers, NodeJs, Deno and iframes
49 lines (45 loc) • 1.54 kB
JavaScript
import { NativeMethod } from './methods/native.js';
import { IndexedDBMethod } from './methods/indexed-db.js';
import { LocalstorageMethod } from './methods/localstorage.js';
import { SimulateMethod } from './methods/simulate.js';
// the line below will be removed from es5/browser builds
import * as NodeMethod from './methods/node.js';
// order is important
var METHODS = [NativeMethod,
// fastest
IndexedDBMethod, LocalstorageMethod];
export function chooseMethod(options) {
var chooseMethods = [].concat(options.methods, METHODS).filter(Boolean);
// the line below will be removed from es5/browser builds
chooseMethods.push(NodeMethod);
// directly chosen
if (options.type) {
if (options.type === 'simulate') {
// only use simulate-method if directly chosen
return SimulateMethod;
}
var ret = chooseMethods.find(function (m) {
return m.type === options.type;
});
if (!ret) throw new Error('method-type ' + options.type + ' not found');else return ret;
}
/**
* if no webworker support is needed,
* remove idb from the list so that localstorage will be chosen
*/
if (!options.webWorkerSupport) {
chooseMethods = chooseMethods.filter(function (m) {
return m.type !== 'idb';
});
}
var useMethod = chooseMethods.find(function (method) {
return method.canBeUsed();
});
if (!useMethod) {
throw new Error("No usable method found in " + JSON.stringify(METHODS.map(function (m) {
return m.type;
})));
} else {
return useMethod;
}
}