From 0d8f60c5a8cd11277f740e4c30e7973055e77344 Mon Sep 17 00:00:00 2001 From: Shigma Date: Wed, 27 Dec 2023 00:41:00 +0800 Subject: [PATCH] feat(console): drop data service access in favor of console.refresh API --- packages/console/src/index.ts | 8 ++++++++ plugins/admin/src/index.ts | 20 ++++++++++---------- plugins/logger/src/index.ts | 2 +- plugins/market/src/node/deps.ts | 10 ---------- plugins/market/src/node/index.ts | 6 +++--- plugins/market/src/node/installer.ts | 18 ++++++++++++++---- 6 files changed, 36 insertions(+), 28 deletions(-) diff --git a/packages/console/src/index.ts b/packages/console/src/index.ts index 1dd8b238..82c52292 100644 --- a/packages/console/src/index.ts +++ b/packages/console/src/index.ts @@ -110,6 +110,14 @@ export abstract class Console extends Service { client.socket.send(data) })) } + + refresh(type: K) { + return this.ctx.get(`console.${type}`)?.refresh() + } + + patch(type: K, value: Console.Services[K] extends DataService ? T : never) { + return this.ctx.get(`console.${type}`)?.patch(value as any) + } } export interface Events { diff --git a/plugins/admin/src/index.ts b/plugins/admin/src/index.ts index 55a5b8f8..f47c6dff 100644 --- a/plugins/admin/src/index.ts +++ b/plugins/admin/src/index.ts @@ -81,7 +81,7 @@ export class Admin extends Service { const item = await this.ctx.database.create('perm_track', { name }) item.dispose = this.track(item.permissions) this.tracks.push(item) - this.ctx.get('console.admin')?.refresh() + this.ctx.get('console')?.refresh('admin') return item.id } @@ -91,14 +91,14 @@ export class Admin extends Service { if (item.name === name) return item.name = name await this.ctx.database.set('perm_track', id, { name }) - this.ctx.get('console.admin')?.refresh() + this.ctx.get('console')?.refresh('admin') } async deleteTrack(id: number) { const index = this.tracks.findIndex(track => track.id === id) if (index < 0) throw new Error('track not found') this.tracks.splice(index, 1) - this.ctx.get('console.admin')?.refresh() + this.ctx.get('console')?.refresh('admin') await this.ctx.database.remove('perm_track', id) } @@ -109,7 +109,7 @@ export class Admin extends Service { item.dispose!() item.dispose = this.track(permissions) await this.ctx.database.set('perm_track', id, { permissions }) - this.ctx.get('console.admin')?.refresh() + this.ctx.get('console')?.refresh('admin') } async createGroup(name: string) { @@ -117,7 +117,7 @@ export class Admin extends Service { item.count = 0 item.dispose = this.ctx.permissions.define('group.' + item.id, []) this.groups.push(item) - this.ctx.get('console.admin')?.refresh() + this.ctx.get('console')?.refresh('admin') return item.id } @@ -127,7 +127,7 @@ export class Admin extends Service { if (item.name === name) return item.name = name await this.ctx.database.set('group', id, { name }) - this.ctx.get('console.admin')?.refresh() + this.ctx.get('console')?.refresh('admin') } async deleteGroup(id: number) { @@ -146,7 +146,7 @@ export class Admin extends Service { }) await this.ctx.database.upsert('group', updates) await this.ctx.database.remove('group', id) - this.ctx.get('console.admin')?.refresh() + this.ctx.get('console')?.refresh('admin') } async updateGroup(id: number, permissions: string[]) { @@ -156,7 +156,7 @@ export class Admin extends Service { item.dispose!() item.dispose = this.ctx.permissions.define('group.' + item.id, permissions) await this.ctx.database.set('group', id, { permissions }) - this.ctx.get('console.admin')?.refresh() + this.ctx.get('console')?.refresh('admin') } async addUser(id: number, platform: string, aid: string) { @@ -168,7 +168,7 @@ export class Admin extends Service { data.permissions.push('group.' + item.id) item.count!++ await this.ctx.database.set('user', data.id, { permissions: data.permissions }) - this.ctx.get('console.admin')?.refresh() + this.ctx.get('console')?.refresh('admin') } } @@ -180,7 +180,7 @@ export class Admin extends Service { if (remove(data.permissions, 'group.' + item.id)) { item.count!-- await this.ctx.database.set('user', data.id, { permissions: data.permissions }) - this.ctx.get('console.admin')?.refresh() + this.ctx.get('console')?.refresh('admin') } } } diff --git a/plugins/logger/src/index.ts b/plugins/logger/src/index.ts index e4fe2da2..e09df78c 100644 --- a/plugins/logger/src/index.ts +++ b/plugins/logger/src/index.ts @@ -92,7 +92,7 @@ export async function apply(ctx: Context, config: Config) { const update = throttle(100, () => { // Be very careful about accessing service in this callback, // because undeclared service access may cause infinite loop. - ctx.get('console.logs')?.patch(buffer) + ctx.get('console')?.patch('logs', buffer) buffer = [] }) diff --git a/plugins/market/src/node/deps.ts b/plugins/market/src/node/deps.ts index d973ae2f..ead2d8e7 100644 --- a/plugins/market/src/node/deps.ts +++ b/plugins/market/src/node/deps.ts @@ -2,7 +2,6 @@ import { Context, Dict } from 'koishi' import { DataService } from '@koishijs/console' import { DependencyMetaKey, RemotePackage } from '@koishijs/registry' import { Dependency } from './installer' -import { throttle } from 'throttle-debounce' class DependencyProvider extends DataService> { constructor(public ctx: Context) { @@ -19,15 +18,6 @@ class RegistryProvider extends DataService { - this.ctx.console.broadcast('market/registry', this.ctx.installer.tempCache) - this.ctx.installer.tempCache = {} - }) - async get() { return this.ctx.installer.fullCache } diff --git a/plugins/market/src/node/index.ts b/plugins/market/src/node/index.ts index 4911eb46..9b6064ca 100644 --- a/plugins/market/src/node/index.ts +++ b/plugins/market/src/node/index.ts @@ -157,9 +157,9 @@ export function apply(ctx: Context, config: Config) { ctx.console.addListener('market/install', async (deps, forced) => { const code = await ctx.installer.install(deps, forced) - ctx.get('console.dependencies')?.refresh() - ctx.get('console.registry')?.refresh() - ctx.get('console.packages')?.refresh() + ctx.get('console')?.refresh('dependencies') + ctx.get('console')?.refresh('registry') + ctx.get('console')?.refresh('packages') return code }, { authority: 4 }) diff --git a/plugins/market/src/node/installer.ts b/plugins/market/src/node/installer.ts index 77616d9a..97ff2a73 100644 --- a/plugins/market/src/node/installer.ts +++ b/plugins/market/src/node/installer.ts @@ -3,6 +3,7 @@ import Scanner, { DependencyMetaKey, PackageJson, Registry, RemotePackage } from import { resolve } from 'path' import { promises as fsp, readFileSync } from 'fs' import { compare, satisfies, valid } from 'semver' +import { throttle } from 'throttle-debounce' import {} from '@koishijs/console' import {} from '@koishijs/loader' import getRegistry from 'get-registry' @@ -67,6 +68,15 @@ class Installer extends Service { this.manifest = loadManifest(this.cwd) } + stop() { + this.flushData.cancel() + } + + flushData = throttle(500, () => { + this.ctx.get('console')?.broadcast('market/registry', this.tempCache) + this.tempCache = {} + }) + get cwd() { return this.ctx.baseDir } @@ -105,7 +115,7 @@ class Installer extends Service { this.fullCache[name] = this.tempCache[name] = getVersions(Object.values(registry.versions).filter((remote) => { return !Scanner.isPlugin(name) || Scanner.isCompatible('4', remote) })) - this.ctx.get('console.registry')?.flushData() + this.flushData() return this.fullCache[name] } catch (e) { logger.warn(e.message) @@ -114,7 +124,7 @@ class Installer extends Service { setPackage(name: string, versions: RemotePackage[]) { this.fullCache[name] = this.tempCache[name] = getVersions(versions) - this.ctx.get('console.registry')?.flushData() + this.flushData() this.pkgTasks[name] = Promise.resolve(this.fullCache[name]) } @@ -150,8 +160,8 @@ class Installer extends Service { } refreshData() { - this.ctx.get('console.registry')?.refresh() - this.ctx.get('console.packages')?.refresh() + this.ctx.get('console')?.refresh('registry') + this.ctx.get('console')?.refresh('packages') } refresh(refresh = false) {