mirror of
https://github.com/ragestudio/comty.js.git
synced 2025-06-08 18:14:18 +00:00
added addons support
This commit is contained in:
parent
2f77c50635
commit
243ce4731c
23
src/addons.js
Normal file
23
src/addons.js
Normal file
@ -0,0 +1,23 @@
|
||||
export default class AddonsManager {
|
||||
addons = new Map()
|
||||
|
||||
register(name, addon) {
|
||||
this.addons.set(name, addon)
|
||||
}
|
||||
|
||||
get(name) {
|
||||
return this.addons.get(name)
|
||||
}
|
||||
|
||||
// search all addons registered, and find all addons that has a addon[operation] function
|
||||
getByOperation(operation) {
|
||||
return Array.from(this.addons.values())
|
||||
.filter((addon) => addon[operation])
|
||||
.map((addon) => {
|
||||
return {
|
||||
id: addon.constructor.id,
|
||||
fn: addon[operation],
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
30
src/helpers/processWithAddons.js
Normal file
30
src/helpers/processWithAddons.js
Normal file
@ -0,0 +1,30 @@
|
||||
export default async function processAddons({
|
||||
operation,
|
||||
initialData,
|
||||
fnArguments,
|
||||
normalizeAddonResult,
|
||||
}) {
|
||||
const addons = __comty_shared_state.addons.getByOperation(operation)
|
||||
|
||||
let processedData = initialData
|
||||
|
||||
if (typeof fnArguments === "undefined") {
|
||||
fnArguments = []
|
||||
}
|
||||
|
||||
for (const addon of addons) {
|
||||
try {
|
||||
const addonResult = await addon.fn(...fnArguments)
|
||||
|
||||
processedData = normalizeAddonResult({
|
||||
operation,
|
||||
currentData: processedData,
|
||||
addonResult,
|
||||
})
|
||||
} catch (error) {
|
||||
console.error(`Error in [${operation}] addon:`, error)
|
||||
}
|
||||
}
|
||||
|
||||
return processedData
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user