From 63fdff3816886fceae80eab88bc1878c94380a16 Mon Sep 17 00:00:00 2001 From: Shigma Date: Sun, 1 Dec 2024 23:25:06 +0800 Subject: [PATCH] refa: upgrade cordis api --- packages/client/app/index.ts | 2 - packages/client/app/styles/index.scss | 12 ++--- packages/client/client/plugins/action.ts | 2 +- packages/client/client/plugins/i18n.ts | 2 +- packages/client/client/plugins/loader.ts | 16 +++--- packages/client/client/plugins/router.ts | 65 +++++++++++++---------- packages/client/client/plugins/setting.ts | 2 +- packages/client/client/plugins/theme.ts | 2 +- packages/client/client/utils.ts | 4 +- plugins/insight/src/index.ts | 4 +- plugins/manager/client/index.ts | 11 +--- plugins/manager/client/routes/main.vue | 2 +- plugins/notifier/src/index.ts | 26 +++++---- plugins/webui/src/shared/entry.ts | 6 +-- plugins/webui/src/shared/index.ts | 11 ++-- 15 files changed, 79 insertions(+), 88 deletions(-) diff --git a/packages/client/app/index.ts b/packages/client/app/index.ts index 72ca496..4fd00f3 100644 --- a/packages/client/app/index.ts +++ b/packages/client/app/index.ts @@ -16,8 +16,6 @@ root.plugin(status) root.plugin(styles) root.plugin(theme) -root.start() - if (!global.static) { const endpoint = new URL(global.endpoint, location.origin).toString() connect(root, () => new WebSocket(endpoint.replace(/^http/, 'ws'))) diff --git a/packages/client/app/styles/index.scss b/packages/client/app/styles/index.scss index 8b9c4cb..310f2d4 100644 --- a/packages/client/app/styles/index.scss +++ b/packages/client/app/styles/index.scss @@ -42,9 +42,9 @@ $white: #ffffff; @mixin apply-color($name, $base) { --k-color-#{$name}: #{$base}; - --k-color-#{$name}-shade: #{mix($base, $black, 90%)}; - --k-color-#{$name}-tint: #{mix($base, $white, 80%)}; - --k-color-#{$name}-fade: #{fade-out($base, 0.9)}; + --k-color-#{$name}-shade: #{color.mix($base, $black, 90%)}; + --k-color-#{$name}-tint: #{color.mix($base, $white, 80%)}; + --k-color-#{$name}-fade: #{color.scale($base, $alpha: -90%)}; } @include apply-color(primary, #409eff); @@ -89,9 +89,9 @@ html.dark, .theme-root.dark { @mixin apply-color($name, $base) { --k-color-#{$name}: #{$base}; - --k-color-#{$name}-shade: #{mix($base, $black, 80%)}; - --k-color-#{$name}-tint: #{mix($base, $white, 90%)}; - --k-color-#{$name}-fade: #{fade-out($base, 0.9)}; + --k-color-#{$name}-shade: #{color.mix($base, $black, 80%)}; + --k-color-#{$name}-tint: #{color.mix($base, $white, 90%)}; + --k-color-#{$name}-fade: #{color.scale($base, $alpha: -90%)}; } @include apply-color(primary, #7459ff); diff --git a/packages/client/client/plugins/action.ts b/packages/client/client/plugins/action.ts index f900be4..91c7416 100644 --- a/packages/client/client/plugins/action.ts +++ b/packages/client/client/plugins/action.ts @@ -77,7 +77,7 @@ export function useMenu(id: K) { export default class ActionService extends Service { constructor(ctx: Context) { - super(ctx, '$action', true) + super(ctx, '$action') ctx.mixin('$action', ['action', 'menu', 'define']) ctx.internal.scope = shallowReactive({}) diff --git a/packages/client/client/plugins/i18n.ts b/packages/client/client/plugins/i18n.ts index a8f114c..1dffc64 100644 --- a/packages/client/client/plugins/i18n.ts +++ b/packages/client/client/plugins/i18n.ts @@ -19,7 +19,7 @@ export default class I18nService extends Service { }) constructor(ctx: Context) { - super(ctx, '$i18n', true) + super(ctx, '$i18n') ctx.effect(() => watchEffect(() => { this.i18n.global.locale.value = config.value.locale diff --git a/packages/client/client/plugins/loader.ts b/packages/client/client/plugins/loader.ts index f2ab764..cf33e05 100644 --- a/packages/client/client/plugins/loader.ts +++ b/packages/client/client/plugins/loader.ts @@ -1,7 +1,7 @@ import { Ref, ref, shallowReactive } from 'vue' import { Context } from '../context' import { Service } from '../utils' -import { ForkScope } from 'cordis' +import { EffectScope } from 'cordis' import { defineProperty, Dict } from 'cosmokit' import { clientId } from '../data' @@ -23,7 +23,7 @@ export function unwrapExports(module: any) { return module?.default || module } -type LoaderFactory = (ctx: Context, url: string) => Promise +type LoaderFactory = (ctx: Context, url: string) => Promise function jsLoader(ctx: Context, exports: {}) { return ctx.plugin(unwrapExports(exports), ctx.$entry.data) @@ -48,7 +48,7 @@ const loaders: Dict = { const link = document.createElement('link') link.rel = 'stylesheet' link.href = url - return ctx.plugin(cssLoader, link) + return ctx.plugin(cssLoader, link as any) }, async [``](ctx, url) { const exports = await import(/* @vite-ignore */ url) @@ -57,8 +57,8 @@ const loaders: Dict = { } export interface LoadState { - forks: ForkScope[] - paths: string[] + forks: EffectScope[] + entryId?: string done: Ref data: Ref } @@ -69,7 +69,7 @@ export default class LoaderService extends Service { public entries: Dict = shallowReactive({}) constructor(ctx: Context) { - super(ctx, '$loader', true) + super(ctx, '$loader') ctx.on('entry:update', ({ id, data }) => { const entry = this.entries[id] @@ -113,11 +113,11 @@ export default class LoaderService extends Service { return } - const { files, paths = [], data } = body + const { files, entryId, data } = body const ctx = this.ctx.isolate('$entry') ctx.$entry = this.entries[key] = { done: ref(false), - paths, + entryId, data: ref(data), forks: [], } diff --git a/packages/client/client/plugins/router.ts b/packages/client/client/plugins/router.ts index e6a4b6e..cba35ff 100644 --- a/packages/client/client/plugins/router.ts +++ b/packages/client/client/plugins/router.ts @@ -1,4 +1,4 @@ -import { createRouter, createWebHistory, START_LOCATION } from 'vue-router' +import { createRouter, createWebHistory, RouteLocation, START_LOCATION } from 'vue-router' import { Context } from '../context' import { insert, Service } from '../utils' import { Component, MaybeRefOrGetter, reactive, ref, toValue } from 'vue' @@ -50,19 +50,31 @@ function getActivityId(path: string) { export const redirectTo = ref() export class Activity { - id: string + id!: string _disposables: Disposable[] = [] constructor(public ctx: Context, public options: Activity.Options) { options.order ??= 0 options.position ??= 'top' Object.assign(this, omit(options, ['icon', 'name', 'desc', 'disabled'])) - const { path, id = getActivityId(path), component } = options - this._disposables.push(ctx.$router.router.addRoute({ path, name: id, component, meta: { activity: this } })) + } + + *setup() { + const { path, id = getActivityId(path), component } = this.options + yield this.ctx.$router.router.addRoute({ path, name: id, component, meta: { activity: this } }) this.id ??= id - this.handleUpdate() this.authority ??= 0 - ctx.$router.pages[this.id] = this + this.ctx.$router.pages[this.id] = this + yield () => delete this.ctx.$router.pages[this.id] + this.handleUpdate() + yield () => { + const { meta, fullPath } = this.ctx.$router.router.currentRoute.value + this._disposables.forEach(dispose => dispose()) + if (meta?.activity === this) { + redirectTo.value = fullPath + this.ctx.$router.router.replace(this.ctx.$router.cache['home'] || '/') + } + } } handleUpdate() { @@ -91,16 +103,6 @@ export class Activity { if (this.ctx.bail('activity', this)) return true if (this.options.disabled?.()) return true } - - dispose() { - const { meta, fullPath } = this.ctx.$router.router.currentRoute.value - this._disposables.forEach(dispose => dispose()) - if (meta?.activity === this) { - redirectTo.value = fullPath - this.ctx.$router.router.replace(this.ctx.$router.cache['home'] || '/') - } - return delete this.ctx.$router.pages[this.id] - } } export default class RouterService extends Service { @@ -114,20 +116,26 @@ export default class RouterService extends Service { }) constructor(ctx: Context) { - super(ctx, '$router', true) + super(ctx, '$router') ctx.mixin('$router', ['slot', 'page']) - const initialTitle = document.title - ctx.effect(() => this.router.afterEach((route) => { - const { name, fullPath } = this.router.currentRoute.value - this.cache[name!] = fullPath - if (route.meta.activity) { - document.title = `${route.meta.activity.name}` - if (initialTitle) document.title += ` | ${initialTitle}` + ctx.effect(() => { + const initialTitle = document.title + const dispose = this.router.afterEach((route) => { + const { name, fullPath } = this.router.currentRoute.value + this.cache[name!] = fullPath + if (route.meta.activity) { + document.title = `${route.meta.activity.name}` + if (initialTitle) document.title += ` | ${initialTitle}` + } + }) + return () => { + document.title = initialTitle + dispose() } - })) + }) - this.router.beforeEach(async (to, from) => { + ctx.effect(() => this.router.beforeEach(async (to: RouteLocation, from) => { if (to.matched.length) { if (to.matched[0].path !== '/') { redirectTo.value = undefined @@ -145,7 +153,7 @@ export default class RouterService extends Service { const result = this.cache['home'] || '/' if (result === to.fullPath) return return result - }) + })) } slot(options: SlotOptions) { @@ -164,7 +172,8 @@ export default class RouterService extends Service { page(options: Activity.Options) { options.component = this.ctx.wrapComponent(options.component) return this.ctx.effect(() => { - return new Activity(this.ctx, options) + const activity = new Activity(this.ctx, options) + return activity.setup() }) } } diff --git a/packages/client/client/plugins/setting.ts b/packages/client/client/plugins/setting.ts index bd5dcaa..6b4ee49 100644 --- a/packages/client/client/plugins/setting.ts +++ b/packages/client/client/plugins/setting.ts @@ -60,7 +60,7 @@ export const useConfig = (useOriginal = false) => useOriginal ? original : resol export default class SettingService extends Service { constructor(ctx: Context) { - super(ctx, '$setting', true) + super(ctx, '$setting') ctx.mixin('$setting', { settings: 'settings', extendSchema: 'schema', diff --git a/packages/client/client/plugins/theme.ts b/packages/client/client/plugins/theme.ts index 2253130..10663d1 100644 --- a/packages/client/client/plugins/theme.ts +++ b/packages/client/client/plugins/theme.ts @@ -51,7 +51,7 @@ export const useColorMode = () => colorMode export default class ThemeService extends Service { constructor(ctx: Context) { - super(ctx, '$theme', true) + super(ctx, '$theme') ctx.mixin('$theme', ['theme']) ctx.internal.themes = reactive({}) diff --git a/packages/client/client/utils.ts b/packages/client/client/utils.ts index 9e33703..3a6d6b3 100644 --- a/packages/client/client/utils.ts +++ b/packages/client/client/utils.ts @@ -2,7 +2,7 @@ import { markRaw } from 'vue' import * as cordis from 'cordis' import { Context } from './context' -export abstract class Service extends cordis.Service {} +export abstract class Service extends cordis.Service {} export interface Ordered { order?: number @@ -10,7 +10,7 @@ export interface Ordered { export function insert(list: T[], item: T) { markRaw(item) - const index = list.findIndex(a => a.order < item.order) + const index = list.findIndex(a => a.order! < item.order!) if (index >= 0) { list.splice(index, 0, item) } else { diff --git a/plugins/insight/src/index.ts b/plugins/insight/src/index.ts index 1765f54..4f1ad41 100644 --- a/plugins/insight/src/index.ts +++ b/plugins/insight/src/index.ts @@ -1,4 +1,4 @@ -import { Context, EffectScope, ForkScope, Plugin, Schema, ScopeStatus } from 'cordis' +import { Context, EffectScope, Plugin, Schema, ScopeStatus } from 'cordis' import { camelize, capitalize } from 'cosmokit' import {} from '@cordisjs/plugin-webui' import {} from 'cordis/loader' @@ -35,7 +35,7 @@ function getName(plugin: Plugin) { return format(plugin.name) } -function getSourceId(child: ForkScope) { +function getSourceId(child: EffectScope) { const { scope } = child.parent if (scope.runtime.isForkable) { return scope.uid diff --git a/plugins/manager/client/index.ts b/plugins/manager/client/index.ts index cb5bc35..2d0f7c7 100644 --- a/plugins/manager/client/index.ts +++ b/plugins/manager/client/index.ts @@ -58,10 +58,6 @@ export interface SubRoute { } export default class Manager extends Service { - static inject = { - optional: ['manager'], - } - changes = reactive>>({}) _dialogFork = ref() @@ -187,7 +183,7 @@ export default class Manager extends Service { routes = reactive([]) constructor(ctx: Context, public data: Ref) { - super(ctx, 'manager', true) + super(ctx, 'manager') watch(data, (value) => { const old = { ...this.changes } @@ -551,11 +547,6 @@ export default class Manager extends Service { setService(name, info) } - // check reusability - if (local.runtime?.id && !local.runtime?.forkable) { - result.warning = true - } - // check schema if (!local.runtime?.schema) { result.warning = true diff --git a/plugins/manager/client/routes/main.vue b/plugins/manager/client/routes/main.vue index 082ee8f..0e9028a 100644 --- a/plugins/manager/client/routes/main.vue +++ b/plugins/manager/client/routes/main.vue @@ -47,7 +47,7 @@