broadcast-channel
Version:
A BroadcastChannel that works in New Browsers, Old Browsers, WebWorkers, NodeJs, Deno and iframes
58 lines (53 loc) • 2.56 kB
JavaScript
;
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.chooseMethod = chooseMethod;
var _native = require("./methods/native.js");
var _indexedDb = require("./methods/indexed-db.js");
var _localstorage = require("./methods/localstorage.js");
var _simulate = require("./methods/simulate.js");
var NodeMethod = _interopRequireWildcard(require("./methods/node.js"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
// the line below will be removed from es5/browser builds
// order is important
var METHODS = [_native.NativeMethod,
// fastest
_indexedDb.IndexedDBMethod, _localstorage.LocalstorageMethod];
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 _simulate.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;
}
}