diff --git a/__tests__/plugins/any-of.test.js b/__tests__/plugins/any-of.test.js index 56cdba7..bd53b52 100644 --- a/__tests__/plugins/any-of.test.js +++ b/__tests__/plugins/any-of.test.js @@ -54,32 +54,39 @@ describe("plugin: any-of", () => { beforeEach(() => { Object.assign(global, makeServiceWorkerEnv()); jest.resetModules(); - self.LibResilientPlugins = new Array() - self.LibResilientConfig = { - plugins: { - 'any-of': { - plugins: { - 'fetch': {}, - 'cache': {} - } - } + global.LibResilientPluginConstructors = new Map() + LR = { + log: (component, ...items)=>{ + console.debug(component + ' :: ', ...items) } } + require("../../plugins/fetch.js"); + init = { + name: 'any-of', + plugins: [ + LibResilientPluginConstructors.get('fetch')(LR), + { + name: 'reject-all', + description: 'Rejects all', + version: '0.0.1', + fetch: url=>Promise.reject('Reject All!') + } + ] + } self.log = function(component, ...items) { console.debug(component + ' :: ', ...items) } }) - test("it should register in LibResilientPlugins", () => { + test("it should register in LibResilientPluginConstructors", () => { require("../../plugins/any-of.js"); - expect(self.LibResilientPlugins[0].name).toEqual('any-of'); + expect(LibResilientPluginConstructors.get('any-of')(LR, init).name).toEqual('any-of'); }); test("it should return data from fetch()", async () => { require("../../plugins/any-of.js"); - require("../../plugins/fetch.js"); - - const response = await self.LibResilientPlugins[0].fetch('https://resilient.is/test.json'); + + const response = await LibResilientPluginConstructors.get('any-of')(LR, init).fetch('https://resilient.is/test.json'); expect(fetch).toHaveBeenCalled(); expect(await response.json()).toEqual({test: "success"}) @@ -103,11 +110,10 @@ describe("plugin: any-of", () => { }); require("../../plugins/any-of.js"); - require("../../plugins/fetch.js"); expect.assertions(2); try { - await self.LibResilientPlugins[0].fetch('https://resilient.is/test.json') + await LibResilientPluginConstructors.get('any-of')(LR, init).fetch('https://resilient.is/test.json') } catch (e) { if (e instanceof Array) { expect(e[0].toString()).toMatch('Error') diff --git a/plugins/any-of.js b/plugins/any-of.js index ab9d8bd..c30de11 100644 --- a/plugins/any-of.js +++ b/plugins/any-of.js @@ -7,49 +7,48 @@ */ // no polluting of the global namespace please -(function () { +(function(LRPC){ + // this never changes + const pluginName = "any-of" + LRPC.set(pluginName, (LR, init={})=>{ - /* - * plugin config settings - */ - - // sane defaults - let defaultConfig = { - // name of this plugin - // should not be changed - name: "any-of", - // list of plugins to run simultaneously - plugins: { - "alt-fetch": {}, - "gun-ipfs": {} + /* + * plugin config settings + */ + + // sane defaults + let defaultConfig = { + // list of plugins to run simultaneously + plugins: [{ + name: "alt-fetch" + },{ + name: "gun-ipfs" + }] + } + + // merge the defaults with settings from LibResilientConfig + let config = {...defaultConfig, ...init} + + /** + * getting content using regular HTTP(S) fetch() + */ + let fetchContent = (url) => { + LR.log(pluginName, `using: [${config.plugins.map(p=>p.name).join(', ')}]!`) + return Promise.any( + config.plugins.map(p=>p.fetch(url)) + ) + } + + // and add ourselves to it + // with some additional metadata + return { + name: pluginName, + description: `Running simultaneously: [${config.plugins.map(p=>p.name).join(', ')}]`, + version: 'COMMIT_UNKNOWN', + fetch: fetchContent, + uses: config.plugins } - } - - // merge the defaults with settings from LibResilientConfig - let config = {...defaultConfig, ...self.LibResilientConfig.plugins[defaultConfig.name]} - - /** - * getting content using regular HTTP(S) fetch() - */ - let fetchContent = (url) => { - self.log(config.name, `using: [${Object.keys(config.plugins).join(', ')}]!`) - return Promise.any( - self - .LibResilientPlugins - .filter(p=>Object.keys(config.plugins).includes(p.name)) - .map(p=>p.fetch(url)) - ) - } - // and add ourselves to it - // with some additional metadata - self.LibResilientPlugins.push({ - name: config.name, - description: `Running simultaneously: [${Object.keys(config.plugins).join(', ')}]`, - version: 'COMMIT_UNKNOWN', - fetch: fetchContent, - uses: config.plugins }) - // done with not polluting the global namespace -})() +})(LibResilientPluginConstructors)