Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e24140a

Browse files
committedOct 12, 2024·
chore: clean plugin class
1 parent 2977a71 commit e24140a

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed
 

‎src/mad_hatter/plugin.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export const CatPlugin = Object.freeze({
6060
* @param fn The function to execute when the event is triggered.
6161
* @returns the event instance
6262
*/
63-
on: <T extends EventNames>(event: T, fn: PluginEvents[T]) => ({ name: event, fn } as PluginEvent<T>),
63+
on: <T extends EventNames>(event: T, fn: PluginEvents[T]): PluginEvent<T> => ({ name: event, fn }),
6464
})
6565

6666
export class Plugin<
@@ -142,7 +142,7 @@ export class Plugin<
142142
id: this.id,
143143
active: this.active,
144144
manifest: this.manifest,
145-
upgradable: false,
145+
upgradable: false, // TODO: Add registry support for upgrading
146146
forms: this.forms.map(({ name, description, active }) => ({ name, description, active })),
147147
tools: this.tools.map(({ name, description, active }) => ({ name, description, active })),
148148
hooks: this.hooks.map(({ name, priority }) => ({ name, priority })),
@@ -161,7 +161,7 @@ export class Plugin<
161161
return this._settings
162162
}
163163

164-
set settings(settings: Record<string, any>) {
164+
set settings(settings: S) {
165165
this._settings = this.schema.parse(settings) as S
166166
const settingsPath = join(this.path, 'settings.json')
167167
Bun.write(settingsPath, JSON.stringify(this._settings, null, 2))
@@ -172,17 +172,16 @@ export class Plugin<
172172
* @param event The name of the event to trigger.
173173
*/
174174
triggerEvent(event: EventNames) {
175-
const callback = this.events[event]
175+
const callback = this.events[event] as (value: Record<string, any>) => void
176176
if (callback) {
177177
const timeStart = performance.now()
178-
// TODO: Improve this check
179178
if (event === 'installed' || event === 'removed') callback(this.manifest)
180-
else callback(this.settings as TODO)
179+
else if (event === 'enabled' || event === 'disabled') callback(this.settings)
181180
const timeEnd = performance.now()
182181
const eventTime = (timeEnd - timeStart).toFixed(2)
183182
log.tag('bgCyanBright', 'PLUGIN', event, `event of ${this.id} executed in ${eventTime}ms`)
184183
}
185-
else log.info(`Plugin ${this.id} ${event}`)
184+
else log.debug(`Plugin ${this.id} ${event}`)
186185
}
187186

188187
private async loadManifest() {
@@ -206,7 +205,7 @@ export class Plugin<
206205
const settingsPath = join(this.path, 'settings.json')
207206
if (existsSync(settingsPath)) {
208207
try {
209-
const json = destr<Record<string, any>>(await Bun.file(settingsPath).text())
208+
const json = destr(await Bun.file(settingsPath).text())
210209
this._settings = this.schema.parse(json) as S
211210
}
212211
catch (err) {
@@ -233,7 +232,6 @@ export class Plugin<
233232

234233
private async importAll(files: Dirent[]) {
235234
log.debug(`Importing plugin features...`)
236-
// TODO: Improve plugin methods import (maybe with the Function class (?), ECMAScript parser or AST parser)
237235
for (const file of files) {
238236
const normalizedPath = relative(process.cwd(), file.path)
239237
const content = await Bun.file(normalizedPath).text()

0 commit comments

Comments
 (0)
Please sign in to comment.