From 53b7dea0969b9af723e410fc33da9883b1ff325e Mon Sep 17 00:00:00 2001 From: Shigma Date: Wed, 19 Jun 2024 20:22:09 +0800 Subject: [PATCH] refa: use new cordis API also revert 6ffd2b7feddf5bce0903455cff18c24de14ec9bb --- package.json | 8 +++---- packages/client/app/settings/index.ts | 2 +- packages/client/client/context.ts | 5 ++--- packages/client/client/plugins/action.ts | 6 ++--- packages/client/client/plugins/router.ts | 12 +++++----- packages/client/client/plugins/setting.ts | 27 +++++++++-------------- packages/client/client/plugins/theme.ts | 5 ++--- packages/console/src/index.ts | 4 +--- packages/online/src/dev.ts | 22 +++++++++--------- plugins/explorer/client/index.ts | 2 +- plugins/insight/src/index.ts | 3 ++- plugins/notifier/src/index.ts | 2 +- 12 files changed, 43 insertions(+), 55 deletions(-) diff --git a/package.json b/package.json index c611d782..05d4fa44 100644 --- a/package.json +++ b/package.json @@ -28,8 +28,8 @@ "devDependencies": { "@cordisjs/eslint-config": "^1.1.1", "@koishijs/plugin-database-memory": "^3.4.0", - "@koishijs/plugin-help": "^2.4.3", - "@koishijs/plugin-mock": "^2.6.5", + "@koishijs/plugin-help": "^2.4.4", + "@koishijs/plugin-mock": "^2.6.6", "@sinonjs/fake-timers": "^6.0.1", "@types/chai": "^4.3.14", "@types/chai-as-promised": "^7.1.8", @@ -43,7 +43,7 @@ "esbuild-register": "^3.5.0", "eslint": "^8.57.0", "eslint-plugin-mocha": "^10.4.1", - "koishi": "^4.17.8", + "koishi": "^4.17.9", "mocha": "^9.2.2", "shx": "^0.3.4", "tsx": "^4.7.1", @@ -51,7 +51,7 @@ "yakumo": "^1.0.0-beta.16", "yakumo-esbuild": "^1.0.0-beta.6", "yakumo-mocha": "^1.0.0-beta.2", - "yakumo-tsc": "^1.0.0-beta.3", + "yakumo-tsc": "^1.0.0-beta.4", "yml-register": "^1.2.5" } } diff --git a/packages/client/app/settings/index.ts b/packages/client/app/settings/index.ts index c347f699..fe319fab 100644 --- a/packages/client/app/settings/index.ts +++ b/packages/client/app/settings/index.ts @@ -12,7 +12,7 @@ export default function (ctx: Context) { component: Settings, }) - ctx.schema.component({ + ctx.schema({ type: 'string', role: 'theme', component: Theme, diff --git a/packages/client/client/context.ts b/packages/client/client/context.ts index cd113b9d..13fd7d4c 100644 --- a/packages/client/client/context.ts +++ b/packages/client/client/context.ts @@ -76,10 +76,9 @@ export class Context extends cordis.Context { wrapComponent(component: Component) { if (!component) return - const caller = this[Context.current] || this - if (!caller.extension) return component + if (!this.extension) return component return defineComponent((props, { slots }) => { - provide('cordis', caller) + provide('cordis', this) return () => h(component, props, slots) }) } diff --git a/packages/client/client/plugins/action.ts b/packages/client/client/plugins/action.ts index c89a651d..4ea46d90 100644 --- a/packages/client/client/plugins/action.ts +++ b/packages/client/client/plugins/action.ts @@ -118,14 +118,14 @@ export default class ActionService extends Service { action(id: string, options: ActionOptions) { markRaw(options) - return this[Context.current].effect(() => { + return this.ctx.effect(() => { this.ctx.internal.actions[id] = options return () => delete this.ctx.internal.actions[id] }) } menu(id: string, items: MenuItem[]) { - return this[Context.current].effect(() => { + return this.ctx.effect(() => { const list = this.ctx.internal.menus[id] ||= [] items.forEach(item => insert(list, item)) return () => { @@ -136,7 +136,7 @@ export default class ActionService extends Service { } define(key: K, value: MaybeRefOrGetter) { - return this[Context.current].effect(() => { + return this.ctx.effect(() => { this.ctx.internal.scope[key] = value as any return () => delete this.ctx.internal.scope[key] }) diff --git a/packages/client/client/plugins/router.ts b/packages/client/client/plugins/router.ts index 9f7c4033..3672b09a 100644 --- a/packages/client/client/plugins/router.ts +++ b/packages/client/client/plugins/router.ts @@ -161,11 +161,10 @@ export default class RouterService extends Service { } slot(options: SlotOptions) { - const caller = this[Context.current] options.order ??= 0 - options.component = caller.wrapComponent(options.component) + options.component = this.ctx.wrapComponent(options.component) if (options.when) options.disabled = () => !options.when() - return caller.effect(() => { + return this.ctx.effect(() => { const list = this.views[options.type] ||= [] insert(list, options) return () => { @@ -176,10 +175,9 @@ export default class RouterService extends Service { } page(options: Activity.Options) { - const caller = this[Context.current] - options.component = caller.wrapComponent(options.component) - return caller.effect(() => { - const activity = new Activity(caller, options) + options.component = this.ctx.wrapComponent(options.component) + return this.ctx.effect(() => { + const activity = new Activity(this.ctx, options) return () => activity.dispose() }) } diff --git a/packages/client/client/plugins/setting.ts b/packages/client/client/plugins/setting.ts index 6610a967..edff0e40 100644 --- a/packages/client/client/plugins/setting.ts +++ b/packages/client/client/plugins/setting.ts @@ -6,15 +6,10 @@ import { Dict, remove } from 'cosmokit' import { Component, computed, markRaw, reactive, ref, watch } from 'vue' import { Config } from '..' -declare module '@cordisjs/schema' { - interface SchemaService { - component(extension: SchemaBase.Extension): () => void - } -} - declare module '../context' { interface Context { $setting: SettingService + schema(extension: SchemaBase.Extension): () => void settings(options: SettingOptions): () => void } @@ -79,8 +74,8 @@ export default class SettingService extends Service { constructor(ctx: Context) { super(ctx, '$setting', true) ctx.mixin('$setting', { - 'schema': 'schema.component', - 'settings': 'settings', + settings: 'settings', + extendSchema: 'schema', }) ctx.internal.settings = reactive({}) @@ -129,10 +124,9 @@ export default class SettingService extends Service { ctx.effect(() => watch(schema, update)) } - schema(extension: SchemaBase.Extension) { - const caller = this[Context.current] - extension.component = caller.wrapComponent(extension.component) - return caller.effect(() => { + extendSchema(extension: SchemaBase.Extension) { + extension.component = this.ctx.wrapComponent(extension.component) + return this.ctx.effect(() => { SchemaBase.extensions.add(extension) return () => SchemaBase.extensions.delete(extension) }) @@ -140,15 +134,14 @@ export default class SettingService extends Service { settings(options: SettingOptions) { markRaw(options) - const caller = this[Context.current] options.order ??= 0 - options.component = caller.wrapComponent(options.component) - return caller.effect(() => { - const list = caller.internal.settings[options.id] ||= [] + options.component = this.ctx.wrapComponent(options.component) + return this.ctx.effect(() => { + const list = this.ctx.internal.settings[options.id] ||= [] insert(list, options) return () => { remove(list, options) - if (!list.length) delete caller.internal.settings[options.id] + if (!list.length) delete this.ctx.internal.settings[options.id] } }) } diff --git a/packages/client/client/plugins/theme.ts b/packages/client/client/plugins/theme.ts index c3a49d01..bb62d7c8 100644 --- a/packages/client/client/plugins/theme.ts +++ b/packages/client/client/plugins/theme.ts @@ -87,15 +87,14 @@ export default class ThemeService extends Service { theme(options: ThemeOptions) { markRaw(options) - const caller = this[Context.current] for (const [type, component] of Object.entries(options.components || {})) { - caller.slot({ + this.ctx.slot({ type, disabled: () => config.value.theme[colorMode.value] !== options.id, component, }) } - return caller.effect(() => { + return this.ctx.effect(() => { this.ctx.internal.themes[options.id] = options return () => delete this.ctx.internal.themes[options.id] }) diff --git a/packages/console/src/index.ts b/packages/console/src/index.ts index 867cac70..b59ba7f8 100644 --- a/packages/console/src/index.ts +++ b/packages/console/src/index.ts @@ -21,8 +21,6 @@ declare module 'koishi' { } } -export interface Console extends Console.Services {} - export interface Listener extends DataService.Options { callback(this: Client, ...args: any[]): Awaitable } @@ -95,7 +93,7 @@ export abstract class Console extends Service { protected abstract resolveEntry(files: Entry.Files, key: string): string[] addEntry(files: Entry.Files, data?: () => T) { - return new Entry(this[Context.current], files, data) + return new Entry(this.ctx, files, data) } addListener(event: K, callback: Events[K], options?: DataService.Options) { diff --git a/packages/online/src/dev.ts b/packages/online/src/dev.ts index 7fe09e49..79c18ad2 100644 --- a/packages/online/src/dev.ts +++ b/packages/online/src/dev.ts @@ -22,30 +22,30 @@ const root = resolve(require.resolve('@koishijs/online/package.json'), '../app') let vite: ViteDevServer -router.get(uiPath + '(/.+)*', async (ctx, next) => { +router.get(uiPath + '(/.+)*', async (koa, next) => { await next() - if (ctx.body || ctx.response.body) return + if (koa.body || koa.response.body) return // add trailing slash and redirect - if (ctx.path === uiPath && !uiPath.endsWith('/')) { - return ctx.redirect(ctx.path + '/') + if (koa.path === uiPath && !uiPath.endsWith('/')) { + return koa.redirect(koa.path + '/') } - const name = ctx.path.slice(uiPath.length).replace(/^\/+/, '') + const name = koa.path.slice(uiPath.length).replace(/^\/+/, '') const sendFile = (filename: string) => { - ctx.type = extname(filename) - return ctx.body = createReadStream(filename) + koa.type = extname(filename) + return koa.body = createReadStream(filename) } const filename = resolve(root, name) if (!filename.startsWith(root) && !filename.includes('node_modules')) { - return ctx.status = 403 + return koa.status = 403 } const stats = await stat(filename).catch(noop) if (stats?.isFile()) return sendFile(filename) const ext = extname(filename) - if (ext && ext !== '.html') return ctx.status = 404 + if (ext && ext !== '.html') return koa.status = 404 const template = await readFile(resolve(root, 'index.html'), 'utf8') - ctx.type = 'html' - ctx.body = await transformHtml(template) + koa.type = 'html' + koa.body = await transformHtml(template) }) const scanner = new LocalScanner(__dirname) diff --git a/plugins/explorer/client/index.ts b/plugins/explorer/client/index.ts index 5f479440..04b71bcb 100644 --- a/plugins/explorer/client/index.ts +++ b/plugins/explorer/client/index.ts @@ -11,7 +11,7 @@ import 'virtual:uno.css' import './editor.scss' export default (ctx: Context) => { - ctx.schema.component({ + ctx.schema({ type: 'string', role: 'path', component: FilePicker, diff --git a/plugins/insight/src/index.ts b/plugins/insight/src/index.ts index ed7b2007..8a5c747f 100644 --- a/plugins/insight/src/index.ts +++ b/plugins/insight/src/index.ts @@ -105,7 +105,8 @@ class Insight extends DataService { } const addDeps = (state: EffectScope) => { - for (const name of runtime.using) { + for (const [name, meta] of Object.entries(runtime.inject)) { + if (!meta.required) continue const instance = this.ctx.get(name) if (!(instance instanceof Object)) continue const ctx: Context = Reflect.getOwnPropertyDescriptor(instance, Context.current)?.value diff --git a/plugins/notifier/src/index.ts b/plugins/notifier/src/index.ts index 9a1f7cc0..0b4725f0 100644 --- a/plugins/notifier/src/index.ts +++ b/plugins/notifier/src/index.ts @@ -131,7 +131,7 @@ class NotifierService extends Service { } create(options?: h.Fragment | Notifier.Options) { - return new Notifier(this[Context.current], options) + return new Notifier(this.ctx, options) } }