-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Daniel Jonathan
committed
Jul 14, 2024
1 parent
7f55219
commit ea2ecdb
Showing
5 changed files
with
281 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/** | ||
* BSD 3-Clause License | ||
* | ||
* Copyright © 2023, Daniel Jonathan <daniel at cosmicmind dot com> | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* 3. Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES LOSS OF USE, DATA, OR PROFITS OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
import { | ||
it, | ||
expect, | ||
describe, | ||
} from 'vitest' | ||
|
||
import { | ||
Plugin, | ||
PluginManager, | ||
} from '@/index' | ||
|
||
type Data = { | ||
prop: number | ||
} | ||
|
||
class PluginA extends Plugin<Data> { | ||
get name(): string { | ||
return 'Plugin A' | ||
} | ||
|
||
execute(data: Data): void { | ||
++data.prop | ||
} | ||
} | ||
|
||
class PluginB extends Plugin<Data> { | ||
get name(): string { | ||
return 'Plugin B' | ||
} | ||
|
||
execute(data: Data): void { | ||
data.prop += 2 | ||
} | ||
} | ||
|
||
describe('Plugin', () => { | ||
it('plugin execution', () => { | ||
const pm = new PluginManager<Data>() | ||
|
||
const a = new PluginA() | ||
const b = new PluginB() | ||
|
||
const data = { | ||
prop: 0, | ||
} | ||
|
||
expect(pm.register(a)).toBeTruthy() | ||
expect(pm.register(a)).toBeFalsy() | ||
|
||
expect(pm.register(b)).toBeTruthy() | ||
expect(pm.register(b)).toBeFalsy() | ||
|
||
expect(pm.deregister('bogus plugin')).toBeFalsy() | ||
|
||
pm.execute(data) | ||
|
||
expect(data.prop).toBe(3) | ||
}) | ||
|
||
it('plugin removal', () => { | ||
const pm = new PluginManager<Data>() | ||
|
||
const a = new PluginA() | ||
const b = new PluginB() | ||
|
||
const data = { | ||
prop: 0, | ||
} | ||
|
||
expect(pm.register(a)).toBeTruthy() | ||
expect(pm.register(a)).toBeFalsy() | ||
|
||
expect(pm.register(b)).toBeTruthy() | ||
expect(pm.register(b)).toBeFalsy() | ||
|
||
expect(pm.deregister(a)).toBeTruthy() | ||
expect(pm.deregister(a.name)).toBeFalsy() | ||
|
||
pm.execute(data) | ||
|
||
expect(data.prop).toBe(2) | ||
|
||
expect(pm.deregister(b.name)).toBeTruthy() | ||
expect(pm.deregister(a)).toBeFalsy() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/** | ||
* BSD 3-Clause License | ||
* | ||
* Copyright © 2023, Daniel Jonathan <daniel at cosmicmind dot com> | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* 3. Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES LOSS OF USE, DATA, OR PROFITS OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
/** | ||
* @module Plugin | ||
*/ | ||
|
||
export abstract class Plugin<T> { | ||
/** | ||
* Retrieves the name. | ||
* | ||
* @returns {string} The name. | ||
*/ | ||
abstract get name(): string | ||
|
||
/** | ||
* Executes the function with the given arguments. | ||
* | ||
* @param {...T} args - The arguments to be passed to the function. | ||
* @return {void} | ||
*/ | ||
abstract execute(...args: T[]): void | ||
} | ||
|
||
export class PluginManager<T> { | ||
protected plugins: Plugin<T>[] | ||
|
||
constructor() { | ||
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) | ||
|
||
if (-1 === i) { | ||
this.plugins.push(plugin) | ||
return true | ||
} | ||
|
||
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) | ||
|
||
if (-1 < i) { | ||
this.plugins.splice(i, 1) | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
/** | ||
* Executes the `execute` method of all plugins. | ||
* | ||
* @param {...T} args - The arguments to pass to the `execute` method. | ||
* @return {void} | ||
*/ | ||
execute(...args: T[]): void { | ||
for (const p of this.plugins) { | ||
p.execute(...args) | ||
} | ||
} | ||
|
||
/** | ||
* Finds the index of the specified plugin in the plugins array. | ||
* | ||
* @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 { | ||
const plugins = this.plugins | ||
const name = 'string' === typeof plugin ? plugin : plugin.name | ||
|
||
for (let i = plugins.length - 1; i >= 0; --i) { | ||
if (name === plugins[i].name) { | ||
return i | ||
} | ||
} | ||
|
||
return -1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.