@webiny/plugins
Advanced tools
Comparing version
@@ -1,8 +0,3 @@ | ||
import { Plugin } from "./types"; | ||
import { PluginsContainer } from "./PluginsContainer"; | ||
declare const plugins: PluginsContainer; | ||
declare const registerPlugins: (...args: any) => void; | ||
declare const getPlugins: <T extends Plugin<Record<string, any>> = Plugin<Record<string, any>>>(type?: string) => T[]; | ||
declare const getPlugin: <T extends Plugin<Record<string, any>> = Plugin<Record<string, any>>>(name: string) => T; | ||
declare const unregisterPlugin: (name: string) => void; | ||
export { PluginsContainer, plugins, registerPlugins, getPlugins, getPlugin, unregisterPlugin }; | ||
export { PluginsContainer, plugins }; |
30
index.js
@@ -12,3 +12,3 @@ "use strict"; | ||
}); | ||
exports.unregisterPlugin = exports.getPlugin = exports.getPlugins = exports.registerPlugins = exports.plugins = void 0; | ||
exports.plugins = void 0; | ||
@@ -19,30 +19,2 @@ var _PluginsContainer = require("./PluginsContainer"); | ||
exports.plugins = plugins; | ||
const registerPlugins = (...args) => { | ||
plugins.register(...args); | ||
}; | ||
exports.registerPlugins = registerPlugins; | ||
const getPlugins = type => { | ||
if (!type) { | ||
return plugins.all(); | ||
} | ||
return plugins.byType(type); | ||
}; | ||
exports.getPlugins = getPlugins; | ||
const getPlugin = name => { | ||
return plugins.byName(name); | ||
}; | ||
exports.getPlugin = getPlugin; | ||
const unregisterPlugin = name => { | ||
return plugins.unregister(name); | ||
}; | ||
exports.unregisterPlugin = unregisterPlugin; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@webiny/plugins", | ||
"version": "5.0.0-beta.1", | ||
"version": "5.0.0-beta.2", | ||
"main": "index.js", | ||
@@ -32,3 +32,3 @@ "repository": { | ||
}, | ||
"gitHead": "1258532e122a172df782b55cf080a2922687b95f" | ||
"gitHead": "d3bb8397c2cd0be78e9fe8cd441b4e13b72fffe6" | ||
} |
import { Plugin } from "./types"; | ||
export declare class PluginsContainer { | ||
plugins: Record<string, Plugin>; | ||
private plugins; | ||
private _byTypeCache; | ||
constructor(...args: any[]); | ||
@@ -12,2 +13,3 @@ byName<T extends Plugin = Plugin>(name: string): T; | ||
unregister(name: string): void; | ||
private findByType; | ||
} |
@@ -52,2 +52,3 @@ "use strict"; | ||
(0, _defineProperty2.default)(this, "plugins", {}); | ||
(0, _defineProperty2.default)(this, "_byTypeCache", {}); | ||
this.register(...args); | ||
@@ -61,3 +62,9 @@ } | ||
byType(type) { | ||
return Object.values(this.plugins).filter(pl => pl.type === type); | ||
if (this._byTypeCache[type]) { | ||
return Array.from(this._byTypeCache[type]); | ||
} | ||
const plugins = this.findByType(type); | ||
this._byTypeCache[type] = plugins; | ||
return Array.from(plugins); | ||
} | ||
@@ -90,2 +97,4 @@ | ||
register(...args) { | ||
// reset the cache when adding new plugins | ||
this._byTypeCache = {}; | ||
const [plugins, options] = normalizeArgs(args); | ||
@@ -96,5 +105,11 @@ assign(plugins, options, this.plugins); | ||
unregister(name) { | ||
// reset the cache when removing a plugin | ||
this._byTypeCache = {}; | ||
delete this.plugins[name]; | ||
} | ||
findByType(type) { | ||
return Object.values(this.plugins).filter(pl => pl.type === type); | ||
} | ||
} | ||
@@ -101,0 +116,0 @@ |
@@ -30,6 +30,6 @@ # @webiny/plugins | ||
``` | ||
import { registerPlugins } from "@webiny/plugins"; | ||
import { plugins } from "@webiny/plugins"; | ||
// Add a plugin | ||
registerPlugins({ | ||
plugins.register({ | ||
name: "my-plugin", | ||
@@ -40,3 +40,3 @@ type: "say-hi", | ||
registerPlugins({ | ||
plugins.register({ | ||
name: "my-second-plugin", | ||
@@ -51,6 +51,6 @@ type: "say-hi", | ||
// anywhere in your app | ||
import { getPlugins } from "@webiny/plugins"; | ||
import { plugins } from "@webiny/plugins"; | ||
const plugins = getPlugins("say-hi"); | ||
plugins.forEach(plugin => { | ||
const pluginList = plugins.byType("say-hi"); | ||
pluginList.forEach(plugin => { | ||
// Call "salute" function | ||
@@ -64,5 +64,5 @@ plugin.salute(); | ||
// anywhere in your app | ||
import { getPlugin } from "@webiny/plugins"; | ||
import { plugins } from "@webiny/plugins"; | ||
const plugin = getPlugin("my-plugin"); | ||
const plugin = plugins.byName("my-plugin"); | ||
// Call "salute" function | ||
@@ -75,5 +75,5 @@ plugin.salute(); | ||
// anywhere in your app | ||
import { unregisterPlugin } from "@webiny/plugins"; | ||
import { plugins } from "@webiny/plugins"; | ||
unregisterPlugin("my-plugin"); | ||
plugins.unregister("my-plugin"); | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
15291
-4.29%133
-6.34%