Skip to content

Commit

Permalink
refa: use ctx.schema.component for schema extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 3, 2024
1 parent 94d415a commit 0badd5e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
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({
ctx.schema.component({
type: 'string',
role: 'theme',
component: Theme,
Expand Down
6 changes: 4 additions & 2 deletions packages/client/client/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as cordis from 'cordis'
import {
App, Component, createApp, defineComponent, h, inject, InjectionKey,
App, Component, createApp, DefineComponent, defineComponent, h, inject, InjectionKey,
markRaw, onBeforeUnmount, onErrorCaptured, provide, Ref, resolveComponent,
} from 'vue'
import ActionService from './plugins/action'
Expand Down Expand Up @@ -75,8 +75,10 @@ export class Context extends cordis.Context {
})
}

wrapComponent(component: Component): DefineComponent
wrapComponent(component?: Component): DefineComponent | undefined
wrapComponent(component: Component) {
if (!component) return null!
if (!component) return undefined
const caller = this[Context.current] || this
if (!caller.$entry) return component
return markRaw(defineComponent((props, { slots }) => {
Expand Down
31 changes: 11 additions & 20 deletions packages/client/client/plugins/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ 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 All @@ -26,7 +31,7 @@ interface SettingOptions extends Ordered {
component?: Component
}

export let useStorage = <T extends object>(key: string, version: number, fallback?: () => T): RemovableRef<T> => {
export let useStorage = <T extends object>(key: string, version?: number, fallback?: () => T): RemovableRef<T> => {
const initial = fallback ? fallback() : {} as T
initial['__version__'] = version
const storage = useLocalStorage('koishi.console.' + key, initial)
Expand All @@ -40,23 +45,6 @@ export function provideStorage(factory: typeof useStorage) {
useStorage = factory
}

interface StorageData<T> {
version: number
data: T
}

/** @deprecated use `useConfig` instead */
export function createStorage<T extends object>(key: string, version: number, fallback?: () => T) {
const storage = useLocalStorage('koishi.console.' + key, {} as StorageData<T>)
const initial = fallback ? fallback() : {} as T
if (storage.value.version !== version) {
storage.value = { version, data: initial }
} else if (!Array.isArray(storage.value.data)) {
storage.value.data = { ...initial, ...storage.value.data }
}
return reactive<T>(storage.value['data'])
}

export const original = useStorage<Config>('config', undefined, () => ({
theme: {
mode: 'auto',
Expand All @@ -73,7 +61,10 @@ export const useConfig = (useOriginal = false) => useOriginal ? original : resol
export default class SettingService extends Service {
constructor(ctx: Context) {
super(ctx, '$setting', true)
ctx.mixin('$setting', ['schema', 'settings'])
ctx.mixin('$setting', {
'schema': 'schema.component',
'settings': 'settings',
})

ctx.internal.settings = reactive({})

Expand Down

0 comments on commit 0badd5e

Please sign in to comment.