Skip to content

Commit

Permalink
refa: use new cordis API
Browse files Browse the repository at this point in the history
also revert 6ffd2b7
  • Loading branch information
shigma committed Jun 19, 2024
1 parent e5d3dec commit 53b7dea
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 55 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -43,15 +43,15 @@
"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",
"typescript": "^5.4.3",
"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"
}
}
2 changes: 1 addition & 1 deletion packages/client/app/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function (ctx: Context) {
component: Settings,
})

ctx.schema.component({
ctx.schema({
type: 'string',
role: 'theme',
component: Theme,
Expand Down
5 changes: 2 additions & 3 deletions packages/client/client/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand Down
6 changes: 3 additions & 3 deletions packages/client/client/plugins/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -136,7 +136,7 @@ export default class ActionService extends Service {
}

define<K extends keyof ActionContext>(key: K, value: MaybeRefOrGetter<ActionContext[K]>) {
return this[Context.current].effect(() => {
return this.ctx.effect(() => {
this.ctx.internal.scope[key] = value as any
return () => delete this.ctx.internal.scope[key]
})
Expand Down
12 changes: 5 additions & 7 deletions packages/client/client/plugins/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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()
})
}
Expand Down
27 changes: 10 additions & 17 deletions packages/client/client/plugins/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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({})
Expand Down Expand Up @@ -129,26 +124,24 @@ 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)
})
}

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]
}
})
}
Expand Down
5 changes: 2 additions & 3 deletions packages/client/client/plugins/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
})
Expand Down
4 changes: 1 addition & 3 deletions packages/console/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>
}
Expand Down Expand Up @@ -95,7 +93,7 @@ export abstract class Console extends Service {
protected abstract resolveEntry(files: Entry.Files, key: string): string[]

addEntry<T>(files: Entry.Files, data?: () => T) {
return new Entry(this[Context.current], files, data)
return new Entry(this.ctx, files, data)
}

addListener<K extends keyof Events>(event: K, callback: Events[K], options?: DataService.Options) {
Expand Down
22 changes: 11 additions & 11 deletions packages/online/src/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Stats>(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)
Expand Down
2 changes: 1 addition & 1 deletion plugins/explorer/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion plugins/insight/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ class Insight extends DataService<Insight.Payload> {
}

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
Expand Down
2 changes: 1 addition & 1 deletion plugins/notifier/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 53b7dea

Please sign in to comment.