Skip to content

Commit

Permalink
feat(console): drop data service access in favor of console.refresh API
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 26, 2023
1 parent 56e9979 commit 0d8f60c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 28 deletions.
8 changes: 8 additions & 0 deletions packages/console/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ export abstract class Console extends Service {
client.socket.send(data)
}))
}

refresh<K extends keyof Console.Services>(type: K) {
return this.ctx.get(`console.${type}`)?.refresh()
}

patch<K extends keyof Console.Services>(type: K, value: Console.Services[K] extends DataService<infer T> ? T : never) {
return this.ctx.get(`console.${type}`)?.patch(value as any)
}
}

export interface Events {
Expand Down
20 changes: 10 additions & 10 deletions plugins/admin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
}

Expand All @@ -109,15 +109,15 @@ 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) {
const item = await this.ctx.database.create('group', { name })
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
}

Expand All @@ -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) {
Expand All @@ -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[]) {
Expand All @@ -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) {
Expand All @@ -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')
}
}

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

Expand Down
10 changes: 0 additions & 10 deletions plugins/market/src/node/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Dict<Dependency>> {
constructor(public ctx: Context) {
Expand All @@ -19,15 +18,6 @@ class RegistryProvider extends DataService<Dict<Dict<Pick<RemotePackage, Depende
super(ctx, 'registry', { authority: 4 })
}

stop() {
this.flushData.cancel()
}

flushData = throttle(500, () => {
this.ctx.console.broadcast('market/registry', this.ctx.installer.tempCache)
this.ctx.installer.tempCache = {}
})

async get() {
return this.ctx.installer.fullCache
}
Expand Down
6 changes: 3 additions & 3 deletions plugins/market/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })

Expand Down
18 changes: 14 additions & 4 deletions plugins/market/src/node/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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])
}

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 0d8f60c

Please sign in to comment.