UNPKG

broadcast-channel

Version:

A BroadcastChannel that works in New Browsers, Old Browsers, WebWorkers, NodeJs, Deno and iframes

57 lines (52 loc) 2.4 kB
"use strict"; 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 _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t2 in e) "default" !== _t2 && {}.hasOwnProperty.call(e, _t2) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t2)) && (i.get || i.set) ? o(f, _t2, i) : f[_t2] = e[_t2]); return f; })(e, t); } // 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; } }