Skip to content

Commit

Permalink
feat: drop throttle-debounce in favor of timer service
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 9, 2024
1 parent ae60519 commit 14215bf
Show file tree
Hide file tree
Showing 16 changed files with 36 additions and 72 deletions.
2 changes: 0 additions & 2 deletions plugins/commands/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@
"@koishijs/plugin-console": "^5.23.4",
"@koishijs/plugin-locales": "^2.5.1",
"@koishijs/plugin-mock": "^2.6.5",
"@types/throttle-debounce": "^2.1.0"
},
"dependencies": {
"@koishijs/console": "^5.23.4",
"throttle-debounce": "^3.0.1"
}
}
5 changes: 2 additions & 3 deletions plugins/commands/src/console.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DataService } from '@koishijs/console'
import { debounce } from 'throttle-debounce'
import { Command, Context, Dict } from 'koishi'
import { resolve } from 'path'
import { CommandManager, CommandState } from '.'
Expand Down Expand Up @@ -31,14 +30,14 @@ export interface CommandData {

export default class CommandProvider extends DataService<CommandData[]> {
cached: CommandData[]
update = debounce(0, () => this.refresh())
update: () => void

constructor(ctx: Context, private manager: CommandManager) {
super(ctx, 'commands', { authority: 4 })

this.update = ctx.debounce(() => this.refresh(), 0)
ctx.on('command-added', this.update)
ctx.on('command-removed', this.update)
ctx.on('dispose', this.update.cancel)

ctx.console.addEntry(process.env.KOISHI_BASE ? [
process.env.KOISHI_BASE + '/dist/index.js',
Expand Down
2 changes: 0 additions & 2 deletions plugins/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@
"devDependencies": {
"@koishijs/client": "^5.23.4",
"@koishijs/plugin-hmr": "^1.2.6",
"@types/throttle-debounce": "^2.1.0"
},
"dependencies": {
"@koishijs/console": "^5.23.4",
"@koishijs/registry": "^7.0.0",
"throttle-debounce": "^3.0.1"
}
}
4 changes: 2 additions & 2 deletions plugins/config/src/shared/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Context, Dict, Logger, MainScope, Plugin, Schema } from 'koishi'
import { ScopeStatus } from 'cordis'
import { DataService } from '@koishijs/console'
import { PackageJson, SearchObject, SearchResult } from '@koishijs/registry'
import { debounce } from 'throttle-debounce'
import {} from '@koishijs/plugin-hmr'

declare module '@koishijs/loader' {
Expand All @@ -21,11 +20,12 @@ const logger = new Logger('config')

export abstract class PackageProvider extends DataService<Dict<PackageProvider.Data>> {
cache: Dict<PackageProvider.RuntimeData> = {}
debouncedRefresh = debounce(0, this.refresh.bind(this, false))
debouncedRefresh: () => void

constructor(public ctx: Context) {
super(ctx, 'packages', { authority: 4 })

this.debouncedRefresh = ctx.debounce(() => this.refresh(false), 0)
ctx.on('internal/runtime', scope => this.update(scope.runtime.plugin))
ctx.on('internal/fork', scope => this.update(scope.runtime.plugin))
ctx.on('internal/status', scope => this.update(scope.runtime.plugin))
Expand Down
2 changes: 0 additions & 2 deletions plugins/insight/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@
"devDependencies": {
"@koishijs/client": "^5.23.4",
"@types/d3-force": "^3.0.9",
"@types/throttle-debounce": "^2.1.0",
"d3-force": "^3.0.0"
},
"dependencies": {
"@koishijs/console": "^5.23.4",
"throttle-debounce": "^3.0.1"
}
}
18 changes: 5 additions & 13 deletions plugins/insight/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { camelize, capitalize, Context, EffectScope, ForkScope, Plugin, Schema, ScopeStatus } from 'koishi'
import { debounce } from 'throttle-debounce'
import { DataService } from '@koishijs/console'
import { resolve } from 'path'
import {} from '@koishijs/loader'
Expand Down Expand Up @@ -46,20 +45,13 @@ class Insight extends DataService<Insight.Payload> {
prod: resolve(__dirname, '../dist'),
})

ctx.on('internal/fork', this.update)
ctx.on('internal/runtime', this.update)
ctx.on('internal/service', this.update)
ctx.on('internal/status', this.update)
const update = ctx.debounce(() => this.refresh(), 0)
ctx.on('internal/fork', update)
ctx.on('internal/runtime', update)
ctx.on('internal/service', update)
ctx.on('internal/status', update)
}

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

private update = debounce(0, () => {
this.refresh()
})

async get() {
const nodes: Insight.Node[] = []
const edges: Insight.Link[] = []
Expand Down
2 changes: 0 additions & 2 deletions plugins/locales/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,9 @@
"@koishijs/plugin-console": "^5.23.4",
"@koishijs/plugin-mock": "^2.6.5",
"@types/js-yaml": "^4.0.9",
"@types/throttle-debounce": "^2.1.0"
},
"dependencies": {
"@koishijs/console": "^5.23.4",
"js-yaml": "^4.1.0",
"throttle-debounce": "^3.0.1"
}
}
6 changes: 2 additions & 4 deletions plugins/locales/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Context, Dict, I18n, Logger, Schema } from 'koishi'
import { DataService } from '@koishijs/console'
import { mkdir, readdir, readFile, writeFile } from 'fs/promises'
import { resolve } from 'path'
import { debounce } from 'throttle-debounce'
import { dump, load } from 'js-yaml'

declare module '@koishijs/console' {
Expand All @@ -20,12 +19,11 @@ declare module '@koishijs/console' {
const logger = new Logger('locales')

class LocaleProvider extends DataService<Dict<I18n.Store>> {
update = debounce(0, () => this.refresh())

constructor(ctx: Context, private config: Config) {
super(ctx, 'locales', { authority: 4 })

ctx.on('internal/i18n', this.update)
const update = ctx.debounce(() => this.refresh(), 0)
ctx.on('internal/i18n', update)

ctx.console.addEntry(process.env.KOISHI_BASE ? [
process.env.KOISHI_BASE + '/dist/index.js',
Expand Down
2 changes: 0 additions & 2 deletions plugins/logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,11 @@
},
"devDependencies": {
"@koishijs/client": "^5.23.4",
"@types/throttle-debounce": "^2.1.0",
"ansi_up": "^5.2.1",
"koishi": "^4.16.3",
"reggol": "^1.6.3"
},
"dependencies": {
"@koishijs/console": "^5.23.4",
"throttle-debounce": "^3.0.1"
}
}
5 changes: 2 additions & 3 deletions plugins/logger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { DataService } from '@koishijs/plugin-console'
import { resolve } from 'path'
import { mkdir, readdir, rm } from 'fs/promises'
import { FileWriter } from './file'
import { throttle } from 'throttle-debounce'
import zhCN from './locales/zh-CN.yml'

declare module '@koishijs/console' {
Expand Down Expand Up @@ -89,12 +88,12 @@ export async function apply(ctx: Context, config: Config) {
createFile(date, Math.max(...files[date] ?? [0]) + 1)

let buffer: Logger.Record[] = []
const update = throttle(100, () => {
const update = ctx.throttle(() => {
// Be very careful about accessing service in this callback,
// because undeclared service access may cause infinite loop.
ctx.get('console')?.patch('logs', buffer)
buffer = []
})
}, 100)

const loader = ctx.get('loader')
const target: Logger.Target = {
Expand Down
2 changes: 0 additions & 2 deletions plugins/market/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
"@koishijs/market": "^4.2.0",
"@koishijs/plugin-config": "^2.8.1",
"@types/semver": "^7.5.6",
"@types/throttle-debounce": "^2.1.0",
"@types/which-pm-runs": "^1.0.2"
},
"dependencies": {
Expand All @@ -82,7 +81,6 @@
"get-registry": "^1.1.0",
"ns-require": "^1.1.4",
"semver": "^7.5.4",
"throttle-debounce": "^3.0.1",
"which-pm-runs": "^1.1.0"
}
}
1 change: 1 addition & 0 deletions plugins/market/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ declare module '@koishijs/console' {
}

export const name = 'market'
export const inject = ['http']

export interface Config {
registry?: Installer.Config
Expand Down
15 changes: 5 additions & 10 deletions plugins/market/src/node/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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 @@ -62,21 +61,17 @@ class Installer extends Service {
private agent = which()?.name || 'npm'
private manifest: PackageJson
private depTask: Promise<Dict<Dependency>>
private flushData: () => void

constructor(public ctx: Context, public config: Installer.Config) {
super(ctx, 'installer')
this.manifest = loadManifest(this.cwd)
this.flushData = ctx.throttle(() => {
ctx.get('console')?.broadcast('market/registry', this.tempCache)
this.tempCache = {}
}, 500)
}

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
25 changes: 10 additions & 15 deletions plugins/market/src/node/market.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import { Context, Dict, Quester, Schema, Time } from 'koishi'
import Scanner, { SearchObject, SearchResult } from '@koishijs/registry'
import { MarketProvider as BaseMarketProvider } from '../shared'
import { throttle } from 'throttle-debounce'

class MarketProvider extends BaseMarketProvider {
private http: Quester
private failed: string[] = []
private scanner: Scanner
private fullCache: Dict<SearchObject> = {}
private tempCache: Dict<SearchObject> = {}
private flushData: () => void

constructor(ctx: Context, public config: MarketProvider.Config) {
super(ctx)
if (config.endpoint) this.http = ctx.http.extend(config)
this.flushData = ctx.throttle(() => {
ctx.console.broadcast('market/patch', {
data: this.tempCache,
failed: this.failed.length,
total: this.scanner.total,
progress: this.scanner.progress,
})
this.tempCache = {}
}, 500)
}

async start(refresh = false) {
Expand All @@ -24,20 +33,6 @@ class MarketProvider extends BaseMarketProvider {
super.start()
}

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

flushData = throttle(500, () => {
this.ctx.console.broadcast('market/patch', {
data: this.tempCache,
failed: this.failed.length,
total: this.scanner.total,
progress: this.scanner.progress,
})
this.tempCache = {}
})

async collect() {
const { timeout } = this.config
const registry = this.ctx.installer.http
Expand Down
2 changes: 0 additions & 2 deletions plugins/status/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@
"@koishijs/plugin-analytics": "^2.0.5",
"@koishijs/plugin-config": "^2.8.1",
"@types/envinfo": "^7.8.3",
"@types/throttle-debounce": "^2.1.0",
"koishi": "^4.16.3"
},
"dependencies": {
"@koishijs/console": "^5.23.4",
"envinfo": "^7.11.0",
"throttle-debounce": "^3.0.1",
"which-pm-runs": "^1.1.0"
}
}
15 changes: 7 additions & 8 deletions plugins/status/src/profile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Bot, Context, Dict, Schema, Time, Universal } from 'koishi'
import { cpus, freemem, totalmem } from 'os'
import { DataService } from '@koishijs/console'
import { debounce } from 'throttle-debounce'
import zhCN from './locales/zh-CN.yml'

declare module 'koishi' {
Expand Down Expand Up @@ -81,8 +80,6 @@ function updateCpuUsage() {
class ProfileProvider extends DataService<ProfileProvider.Payload> {
cached: ProfileProvider.Payload

update = debounce(0, async () => this.refresh())

constructor(ctx: Context, private config: ProfileProvider.Config) {
super(ctx, 'status')

Expand All @@ -97,28 +94,30 @@ class ProfileProvider extends DataService<ProfileProvider.Payload> {
})

ctx.any().before('send', (session) => {
session.bot._messageSent.add(1)
session.bot._messageSent?.add(1)
})

ctx.any().on('message', (session) => {
session.bot._messageReceived.add(1)
session.bot._messageReceived?.add(1)
})

ctx.bots.forEach(bot => TickCounter.initialize(bot, ctx))

const update = ctx.debounce(() => this.refresh(), 0)

ctx.on('login-added', ({ bot }) => {
TickCounter.initialize(bot, ctx)
this.update()
update()
})

ctx.on('login-removed', ({ bot }) => {
bot._messageSent.stop()
bot._messageReceived.stop()
this.update()
update()
})

ctx.on('login-updated', () => {
this.update()
update()
})

ctx.command('status')
Expand Down

0 comments on commit 14215bf

Please sign in to comment.