Skip to content

Commit c185982

Browse files
committed
refactor(plugins): Pass arguments directly to plugin functions
1 parent 18f83e9 commit c185982

File tree

2 files changed

+44
-31
lines changed

2 files changed

+44
-31
lines changed

frontend/src/stores/plugins.ts

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,11 @@ export const usePluginsStore = defineStore('plugins', () => {
335335

336336
const getPluginCodefromCache = (id: string) => PluginsCache[id]?.code
337337

338-
const onSubscribeTrigger = async (proxies: Record<string, any>[], subscription: Subscription) => {
338+
const onSubscribeTrigger = async (proxies: Recordable[], subscription: Subscription) => {
339339
const { fnName, observers } = PluginsTriggerMap[PluginTrigger.OnSubscribe]
340+
if (observers.length === 0) return proxies
340341

341-
let result = proxies
342+
subscription = deepClone(subscription)
342343

343344
for (const observer of observers) {
344345
const cache = PluginsCache[observer]
@@ -347,21 +348,23 @@ export const usePluginsStore = defineStore('plugins', () => {
347348

348349
const metadata = getPluginMetadata(cache.plugin)
349350
try {
350-
const fn = new window.AsyncFunction(`const Plugin = ${JSON.stringify(metadata)};
351-
${cache.code};
352-
return await ${fnName}(${JSON.stringify(result)}, ${JSON.stringify(subscription)})
353-
`) as <T>(params: T) => Promise<T>
354-
result = await fn(result)
351+
const fn = new window.AsyncFunction(
352+
'Plugin',
353+
'proxies',
354+
'subscription',
355+
`${cache.code}; return await ${fnName}(proxies, subscription)`,
356+
)
357+
proxies = await fn(metadata, proxies, subscription)
355358
} catch (error: any) {
356359
throw `${cache.plugin.name} : ` + (error.message || error)
357360
}
358361

359-
if (!Array.isArray(result)) {
362+
if (!Array.isArray(proxies)) {
360363
throw `${cache.plugin.name} : Wrong result`
361364
}
362365
}
363366

364-
return result
367+
return proxies
365368
}
366369

367370
const noParamsTrigger = async (trigger: PluginTrigger, interruptOnError = false) => {
@@ -375,10 +378,8 @@ export const usePluginsStore = defineStore('plugins', () => {
375378

376379
const metadata = getPluginMetadata(cache.plugin)
377380
try {
378-
const fn = new window.AsyncFunction(
379-
`const Plugin = ${JSON.stringify(metadata)}; ${cache.code}; return await ${fnName}()`,
380-
)
381-
const exitCode = await fn()
381+
const fn = new window.AsyncFunction('Plugin', `${cache.code}; return await ${fnName}()`)
382+
const exitCode = await fn(metadata)
382383
if (isNumber(exitCode) && exitCode !== cache.plugin.status) {
383384
cache.plugin.status = exitCode
384385
editPlugin(cache.plugin.id, cache.plugin)
@@ -391,12 +392,13 @@ export const usePluginsStore = defineStore('plugins', () => {
391392
console.error(msg)
392393
}
393394
}
394-
return
395395
}
396396

397-
const onGenerateTrigger = async (params: Record<string, any>, profile: ProfileType) => {
397+
const onGenerateTrigger = async (config: Recordable, profile: ProfileType) => {
398398
const { fnName, observers } = PluginsTriggerMap[PluginTrigger.OnGenerate]
399-
if (observers.length === 0) return params
399+
if (observers.length === 0) return config
400+
401+
profile = deepClone(profile)
400402

401403
for (const observer of observers) {
402404
const cache = PluginsCache[observer]
@@ -406,23 +408,28 @@ export const usePluginsStore = defineStore('plugins', () => {
406408
const metadata = getPluginMetadata(cache.plugin)
407409
try {
408410
const fn = new window.AsyncFunction(
409-
`const Plugin = ${JSON.stringify(metadata)}; ${cache.code}; return await ${fnName}(${JSON.stringify(params)}, ${JSON.stringify(profile)})`,
411+
'Plugin',
412+
'config',
413+
'profile',
414+
`${cache.code}; return await ${fnName}(config, profile)`,
410415
)
411-
params = await fn()
416+
config = await fn(metadata, config, profile)
412417
} catch (error: any) {
413418
throw `${cache.plugin.name} : ` + (error.message || error)
414419
}
415420

416-
if (!params) throw `${cache.plugin.name} : Wrong result`
421+
if (!config) throw `${cache.plugin.name} : Wrong result`
417422
}
418423

419-
return params as Record<string, any>
424+
return config
420425
}
421426

422-
const onBeforeCoreStartTrigger = async (params: Record<string, any>, profile: ProfileType) => {
427+
const onBeforeCoreStartTrigger = async (params: Recordable, profile: ProfileType) => {
423428
const { fnName, observers } = PluginsTriggerMap[PluginTrigger.OnBeforeCoreStart]
424429
if (observers.length === 0) return params
425430

431+
profile = deepClone(profile)
432+
426433
for (const observer of observers) {
427434
const cache = PluginsCache[observer]
428435

@@ -431,17 +438,20 @@ export const usePluginsStore = defineStore('plugins', () => {
431438
const metadata = getPluginMetadata(cache.plugin)
432439
try {
433440
const fn = new window.AsyncFunction(
434-
`const Plugin = ${JSON.stringify(metadata)}; ${cache.code}; return await ${fnName}(${JSON.stringify(params)}, ${JSON.stringify(profile)})`,
441+
'Plugin',
442+
'config',
443+
'profile',
444+
`${cache.code}; return await ${fnName}(config, profile)`,
435445
)
436-
params = await fn()
446+
params = await fn(metadata, params, profile)
437447
} catch (error: any) {
438448
throw `${cache.plugin.name} : ` + (error.message || error)
439449
}
440450

441451
if (!params) throw `${cache.plugin.name} : Wrong result`
442452
}
443453

444-
return params as Record<string, any>
454+
return params
445455
}
446456

447457
const manualTrigger = async (id: string, event: PluginTriggerEvent, ...args: any[]) => {
@@ -451,14 +461,15 @@ export const usePluginsStore = defineStore('plugins', () => {
451461
if (!cache) throw `${plugin.name} is Missing source code`
452462
if (cache.plugin.disabled) throw `${plugin.name} is Disabled`
453463
const metadata = getPluginMetadata(plugin)
454-
const _args = args.map((arg) => JSON.stringify(arg))
464+
args = deepClone(args)
455465
try {
456466
const fn = new window.AsyncFunction(
457-
`const Plugin = ${JSON.stringify(metadata)};
458-
${cache.code};
459-
return await ${event}(${_args.join(',')})`,
467+
'Plugin',
468+
'...args',
469+
`${cache.code}; return await ${event}(...args)`,
460470
)
461-
const exitCode = await fn()
471+
472+
const exitCode = await fn(metadata, ...args)
462473
if (isNumber(exitCode) && exitCode !== plugin.status) {
463474
plugin.status = exitCode
464475
editPlugin(id, plugin)
@@ -471,6 +482,7 @@ export const usePluginsStore = defineStore('plugins', () => {
471482

472483
const onTrayUpdateTrigger = async (tray: TrayContent, menus: MenuItem[]) => {
473484
const { fnName, observers } = PluginsTriggerMap[PluginTrigger.OnTrayUpdate]
485+
if (observers.length === 0) return [tray, menus] as const
474486

475487
let finalTray = tray
476488
let finalMenus = menus

frontend/src/utils/generator.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,12 @@ export const generateConfig = async (originalProfile: ProfileType) => {
352352

353353
// step 4
354354
const fn = new window.AsyncFunction(
355-
`${profile.scriptConfig.code};return await onGenerate(${JSON.stringify(_config)})`,
355+
'config',
356+
`${originalProfile.scriptConfig.code}; return await onGenerate(config)`,
356357
)
357358
let result
358359
try {
359-
result = await fn()
360+
result = await fn(_config)
360361
} catch (error: any) {
361362
throw error.message || error
362363
}

0 commit comments

Comments
 (0)