From bf3281d6ed149e0237a12ca731de24147e5a4a49 Mon Sep 17 00:00:00 2001 From: Shigma Date: Sat, 6 Jan 2024 00:14:25 +0800 Subject: [PATCH] fix(console): use ref to fix reactivity issue --- packages/client/app/status/loading.vue | 2 +- packages/client/client/context.ts | 4 ++-- packages/client/client/loader.ts | 14 +++++++------- plugins/admin/client/group.vue | 8 ++++---- plugins/admin/package.json | 1 - plugins/admin/src/index.ts | 2 +- 6 files changed, 15 insertions(+), 16 deletions(-) diff --git a/packages/client/app/status/loading.vue b/packages/client/app/status/loading.vue index 8e370416..951dff19 100644 --- a/packages/client/app/status/loading.vue +++ b/packages/client/app/status/loading.vue @@ -15,7 +15,7 @@ const ctx = useContext() const progress = computed(() => { const states = Object.values(ctx.internal.extensions) - return states.filter(state => state.done).length / states.length + return states.filter(state => state.done.value).length / states.length }) diff --git a/packages/client/client/context.ts b/packages/client/client/context.ts index 0046c8c5..cda9c7f8 100644 --- a/packages/client/client/context.ts +++ b/packages/client/client/context.ts @@ -3,7 +3,7 @@ import { Schema, SchemaBase } from '@koishijs/components' import { Dict, Intersect, remove } from 'cosmokit' import { App, Component, createApp, defineComponent, h, inject, markRaw, MaybeRefOrGetter, - onBeforeUnmount, provide, reactive, resolveComponent, shallowReactive, toValue, + onBeforeUnmount, provide, reactive, Ref, resolveComponent, shallowReactive, toValue, } from 'vue' import { activities, Activity } from './activity' import { SlotOptions } from './components' @@ -41,7 +41,7 @@ export function useContext() { return fork.ctx } -export function useRpc(): T { +export function useRpc(): Ref { const parent = inject('cordis') as Context return parent.extension?.data } diff --git a/packages/client/client/loader.ts b/packages/client/client/loader.ts index a1b87b4b..dc909d25 100644 --- a/packages/client/client/loader.ts +++ b/packages/client/client/loader.ts @@ -1,4 +1,4 @@ -import { reactive, watch } from 'vue' +import { ref, Ref, shallowReactive, watch } from 'vue' import { Context, Dict, receive, root, store } from '.' import { EffectScope } from 'cordis' @@ -35,12 +35,12 @@ const loaders: Dict<(ctx: Context, url: string) => Promise> = { export interface LoadResult { scope: EffectScope - done: boolean paths: string[] - data: any + done: Ref + data: Ref } -export const extensions: Dict = reactive({}) +export const extensions: Dict = shallowReactive({}) let backendId: any @@ -62,14 +62,14 @@ export const initTask = new Promise((resolve) => { await Promise.all(Object.entries(rest).map(([key, { files, paths, data }]) => { if (extensions[key]) return const scope = root.isolate(['extension']).plugin(() => {}) - scope.ctx.extension = extensions[key] = { done: false, scope, paths, data } + scope.ctx.extension = extensions[key] = { done: ref(false), scope, paths, data: ref(data) } const task = Promise.all(files.map((url) => { for (const ext in loaders) { if (!url.endsWith(ext)) continue return loaders[ext](scope.ctx, url) } })) - task.finally(() => extensions[key].done = true) + task.finally(() => extensions[key].done.value = true) })) if (!oldValue) { @@ -83,5 +83,5 @@ receive('entry-data', ({ id, data }) => { const entry = store.entry?.[id] if (!entry) return entry.data = data - extensions[id].data = data + extensions[id].data.value = data }) diff --git a/plugins/admin/client/group.vue b/plugins/admin/client/group.vue index 756e7456..777adf3e 100644 --- a/plugins/admin/client/group.vue +++ b/plugins/admin/client/group.vue @@ -135,12 +135,12 @@ interface Active { const active = computed(() => { if (route.path.startsWith('/admin/group/')) { const id = route.path.slice(13) - if (id in data.group) { + if (id in data.value.group) { return { type: 'group', id } } } else if (route.path.startsWith('/admin/track/')) { const id = route.path.slice(13) - if (id in data.track) { + if (id in data.value.track) { return { type: 'track', id } } } @@ -153,7 +153,7 @@ const activeGroup = computed({ return active.value.id }, set(id) { - if (!(id in data.group)) id = '' + if (!(id in data.value.group)) id = '' router.replace('/admin/group/' + id) }, }) @@ -164,7 +164,7 @@ const activeTrack = computed({ return active.value.id }, set(id) { - if (!(id in data.track)) id = '' + if (!(id in data.value.track)) id = '' router.replace('/admin/track/' + id) }, }) diff --git a/plugins/admin/package.json b/plugins/admin/package.json index 3b7a4af7..9bd6289c 100644 --- a/plugins/admin/package.json +++ b/plugins/admin/package.json @@ -66,7 +66,6 @@ "@types/throttle-debounce": "^2.1.0" }, "dependencies": { - "@koishijs/console": "^5.23.4", "throttle-debounce": "^3.0.1" } } diff --git a/plugins/admin/src/index.ts b/plugins/admin/src/index.ts index bd5f632a..581b0a20 100644 --- a/plugins/admin/src/index.ts +++ b/plugins/admin/src/index.ts @@ -1,5 +1,5 @@ import { $, Context, Dict, remove, Schema, Service } from 'koishi' -import { Entry } from '@koishijs/console' +import type { Entry } from '@koishijs/console' import { resolve } from 'path' import command from './command' import zhCN from './locales/zh-CN.yml'