Skip to content

Commit

Permalink
refactor: execute plugins atleast one time even when registered later
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 24, 2023
1 parent b5ac2f3 commit ee7a38d
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/edge/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@ export class Edge {
return new Edge(options)
}

#executedPlugins = false

/**
* An array of registered plugins
*/
#plugins: {
fn: PluginFn<any>
executed: boolean
options?: any
}[] = []

Expand Down Expand Up @@ -126,22 +125,20 @@ export class Edge {
}

/**
* Execute plugins. Since plugins are meant to be called only
* once we empty out the array after first call
* Execute plugins
*/
#executePlugins() {
if (this.#executedPlugins) {
this.#plugins
.filter(({ options }) => options && options.recurring)
.forEach(({ fn, options }) => {
fn(this, false, options)
})
} else {
this.#executedPlugins = true
this.#plugins.forEach(({ fn, options }) => {
fn(this, true, options)
this.#plugins
.filter(({ options, executed }) => {
if (options && options.recurring) {
return true
}
return !executed
})
.forEach((plugin) => {
plugin.fn(this, !plugin.executed, plugin.options)
plugin.executed = true
})
}
}

/**
Expand All @@ -151,6 +148,7 @@ export class Edge {
use<T extends any>(pluginFn: PluginFn<T>, options?: T): this {
this.#plugins.push({
fn: pluginFn,
executed: false,
options,
})
return this
Expand Down

0 comments on commit ee7a38d

Please sign in to comment.