Skip to content

Commit

Permalink
feat(utils): new helper function: remove
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 16, 2021
1 parent bd33be6 commit e500178
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
8 changes: 3 additions & 5 deletions packages/koishi-core/src/command.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Logger, coerce, Time, template } from 'koishi-utils'
import { Logger, coerce, Time, template, remove } from 'koishi-utils'
import { Argv, Domain } from './parser'
import { Context, NextFunction } from './context'
import { User, Channel } from './database'
Expand Down Expand Up @@ -245,11 +245,9 @@ export class Command<U extends User.Field = never, G extends Channel.Field = nev
}
this.app._shortcuts = this.app._shortcuts.filter(s => s.command !== this)
this._aliases.forEach(name => delete this.app._commandMap[name])
const index = this.app._commands.indexOf(this)
this.app._commands.splice(index, 1)
remove(this.app._commands, this)
if (this.parent) {
const index = this.parent.children.indexOf(this)
this.parent.children.splice(index, 1)
remove(this.parent.children, this)
}
}
}
Expand Down
18 changes: 5 additions & 13 deletions packages/koishi-core/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Logger, defineProperty } from 'koishi-utils'
import { Logger, defineProperty, remove } from 'koishi-utils'
import { Command } from './command'
import { Session } from './session'
import { User, Channel, Database } from './database'
Expand Down Expand Up @@ -139,14 +139,6 @@ export class Context {
return this.app.registry.get(this._plugin)
}

private removeDisposable(listener: Disposable) {
const index = this.state.disposables.indexOf(listener)
if (index >= 0) {
this.state.disposables.splice(index, 1)
return true
}
}

addSideEffect(state = this.state) {
while (state && !state.sideEffect) {
state.sideEffect = true
Expand Down Expand Up @@ -208,6 +200,7 @@ export class Context {
...state.disposables.map(dispose => dispose()),
]).finally(() => {
this.app.registry.delete(plugin)
remove(state.parent.children, plugin)
const index = state.parent.children.indexOf(plugin)
if (index >= 0) state.parent.children.splice(index, 1)
this.emit('registry', this.app.registry)
Expand Down Expand Up @@ -292,7 +285,7 @@ export class Context {
return _listener(), () => false
} else if (name === 'before-disconnect') {
this.state.disposables[method](_listener)
return () => this.removeDisposable(_listener)
return () => remove(this.state.disposables, _listener)
} else if (name === 'before-connect') {
// before-connect is side effect
this.addSideEffect()
Expand Down Expand Up @@ -342,7 +335,7 @@ export class Context {
private createTimerDispose(timer: NodeJS.Timeout) {
const dispose = () => {
clearTimeout(timer)
return this.removeDisposable(dispose)
return remove(this.state.disposables, dispose)
}
this.state.disposables.push(dispose)
return dispose
Expand Down Expand Up @@ -502,8 +495,7 @@ Router.prototype.register = function (this: Router, ...args) {
const layer = register.apply(this, args)
const context: Context = this['_koishiContext']
context.state.disposables.push(() => {
const index = this.stack.indexOf(layer)
if (index) this.stack.splice(index, 1)
remove(this.stack, layer)
})
return layer
}
5 changes: 2 additions & 3 deletions packages/koishi-core/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import LruCache from 'lru-cache'
import { distance } from 'fastest-levenshtein'
import { User, Channel, TableType, Tables } from './database'
import { Command } from './command'
import { contain, observe, Logger, defineProperty, Random, template } from 'koishi-utils'
import { contain, observe, Logger, defineProperty, Random, template, remove } from 'koishi-utils'
import { Argv } from './parser'
import { Middleware, NextFunction } from './context'
import { App } from './app'
Expand Down Expand Up @@ -169,8 +169,7 @@ export class Session<
const hook = () => {
resolve()
clearTimeout(timer)
const index = this._hooks.indexOf(hook)
if (index >= 0) this._hooks.splice(index, 1)
remove(this._hooks, hook)
}
this._hooks.push(hook)
const timer = setTimeout(async () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/koishi-utils/src/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ export function union<T>(array1: readonly T[], array2: readonly T[]) {
export function deduplicate<T>(array: readonly T[]) {
return [...new Set(array)]
}

export function remove<T>(list: T[], item: T) {
const index = list.indexOf(item)
if (index >= 0) {
list.splice(index, 1)
return true
}
}

0 comments on commit e500178

Please sign in to comment.