Skip to content

Commit

Permalink
Move requested bit to the pluginSet.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbatiste committed Nov 23, 2023
1 parent 5c10a01 commit 5435b6c
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions helpers/plugins.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
const pluginSets = new Map();
const pluginsRequested = new Set();

function getPluginSet(setKey) {
let pluginSet = pluginSets.get(setKey);
if (pluginSet) return pluginSet;

pluginSet = { plugins: [], requested: false, requiresSorting: false };
pluginSets.set(setKey, pluginSet);
return pluginSet;
}

export function getPlugins(setKey) {
if (!pluginsRequested.has(setKey)) pluginsRequested.add(setKey);
const pluginSet = pluginSets.get(setKey);
if (!pluginSet) return [];
const pluginSet = getPluginSet(setKey);
pluginSet.requested = true;
if (pluginSet.requiresSorting) {
pluginSet.plugins.sort((item1, item2) => item1.options.sort - item2.options.sort);
pluginSet.requiresSorting = false;
Expand All @@ -13,14 +20,10 @@ export function getPlugins(setKey) {
}

export function registerPlugin(setKey, plugin, options) {
if (pluginsRequested.has(setKey)) {
throw new Error(`Plugin Set "${setKey}" has already been requested. Additional plugin registrations would result in stale consumer plugins.`);
}
const pluginSet = getPluginSet(setKey);

let pluginSet = pluginSets.get(setKey);
if (!pluginSet) {
pluginSet = { plugins: [], requiresSorting: false };
pluginSets.set(setKey, pluginSet);
if (pluginSet.requested) {
throw new Error(`Plugin Set "${setKey}" has already been requested. Additional plugin registrations would result in stale consumer plugins.`);
} else if (options?.key !== undefined) {
if (pluginSet.plugins.find(registeredPlugin => registeredPlugin.options.key === options?.key)) {
throw new Error(`Plugin Set "${setKey}" already has a plugin with the key "${options.key}".`);
Expand All @@ -34,7 +37,6 @@ export function registerPlugin(setKey, plugin, options) {
// Do not import! Testing only!!
export function resetPlugins() {
pluginSets.clear();
pluginsRequested.clear();
}

export function tryGetPluginByKey(setKey, pluginKey) {
Expand Down

0 comments on commit 5435b6c

Please sign in to comment.