Skip to content

Commit

Permalink
updated Plugin pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-jonathan committed Jul 28, 2024
1 parent 8c7ce0b commit 9a1ccfd
Showing 1 changed file with 14 additions and 26 deletions.
40 changes: 14 additions & 26 deletions src/behavioral/Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,38 +75,26 @@ export class PluginManager<T> {
this.plugins = []
}

/**
* Registers a plugin.
*
* @param {Plugin<T>} plugin - The plugin to register.
* @return {boolean} - Returns true if the plugin is registered successfully, otherwise returns false.
*/
register(plugin: Plugin<T>): boolean {
const i = this.indexOf(plugin)
register(...plugins: Plugin<T>[]): void {
for (const plugin of plugins) {
const i = this.indexOf(plugin)

if (-1 === i) {
this.plugins.push(plugin)
return true
if (-1 === i) {
this.plugins.push(plugin)
}
}

return false
}

/**
* Deregisters a plugin from the system.
*
* @param {Plugin<T> | string} plugin - The plugin or the name of the plugin to be deregistered.
* @return {boolean} - Returns true if the plugin was successfully deregistered, otherwise false.
*/
deregister(plugin: Plugin<T> | string): boolean {
const i = this.indexOf(plugin)
deregister(...plugins: Plugin<T>[]): void {
for (const plugin of plugins) {
const i = this.indexOf(plugin)

if (-1 < i) {
this.plugins.splice(i, 1)
return true
if (-1 < i) {
this.plugins.splice(i, 1)
}
}

return false
}

/**
Expand All @@ -127,9 +115,9 @@ export class PluginManager<T> {
* @param {Plugin<T> | string} plugin - The plugin to search for. Can be either a Plugin object or a string representing the plugin name.
* @return {number} - The index of the plugin in the plugins array. Returns -1 if the plugin is not found.
*/
protected indexOf(plugin: Plugin<T> | string): number {
protected indexOf(plugin: Plugin<T>): number {
const plugins = this.plugins
const name = 'string' === typeof plugin ? plugin : plugin.name
const name = plugin.name

for (let i = plugins.length - 1; i >= 0; --i) {
if (name === plugins[i].name) {
Expand Down

0 comments on commit 9a1ccfd

Please sign in to comment.