Skip to content

Commit

Permalink
fix(core): create major context at demand
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 20, 2020
1 parent d7ff349 commit 3ae7e70
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
35 changes: 24 additions & 11 deletions packages/koishi-core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,17 @@ export class App extends Context {
atMeRE: RegExp
prefixRE: RegExp
nicknameRE: RegExp
users: MajorContext
groups: MajorContext
discusses: MajorContext
status = Status.closed

_commands: Command[] = []
_commandMap: Record<string, Command> = {}
_shortcuts: ShortcutConfig[] = []
_shortcutMap: Record<string, Command> = {}
_middlewares: [Context, Middleware][] = []

private status = Status.closed
private _users: MajorContext
private _groups: MajorContext
private _discusses: MajorContext
private _isReady = false
private _middlewareCounter = 0
private _middlewareSet = new Set<number>()
Expand Down Expand Up @@ -126,14 +126,27 @@ export class App extends Context {
this.receiver.on('before-user', Command.attachUserFields)
this.receiver.on('before-group', Command.attachGroupFields)
this.middleware(this._preprocess)
}

get users () {
if (this._users) return this._users
const users = this.createContext([[null, []], [[], null], [[], null]]) as MajorContext
users.except = (...ids) => this.createContext([[null, ids], [[], null], [[], null]])
return this._users = users
}

get groups () {
if (this._groups) return this._groups
const groups = this.createContext([[[], null], [null, []], [[], null]]) as MajorContext
groups.except = (...ids) => this.createContext([[[], null], [null, ids], [[], null]])
return this._groups = groups
}

// create built-in contexts
this.users = this.createContext([[null, []], [[], null], [[], null]]) as MajorContext
this.groups = this.createContext([[[], null], [null, []], [[], null]]) as MajorContext
this.discusses = this.createContext([[[], null], [[], null], [null, []]]) as MajorContext
this.users.except = (...ids) => this.createContext([[null, ids], [[], null], [[], null]])
this.groups.except = (...ids) => this.createContext([[[], null], [null, ids], [[], null]])
this.discusses.except = (...ids) => this.createContext([[[], null], [[], null], [null, ids]])
get discusses () {
if (this._discusses) return this._discusses
const discusses = this.createContext([[[], null], [[], null], [null, []]]) as MajorContext
discusses.except = (...ids) => this.createContext([[[], null], [[], null], [null, ids]])
return this._discusses = discusses
}

get selfId () {
Expand Down
6 changes: 3 additions & 3 deletions packages/koishi-core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ export class Context {
plugin <U> (plugin: PluginObject<this, U>, options?: U): this
plugin <U> (plugin: Plugin<this, U>, options: any) {
if (options === false) return
const app = Object.create(this)
const ctx = Object.create(this)
if (typeof plugin === 'function') {
plugin(app, options)
plugin(ctx, options)
} else if (plugin && typeof plugin === 'object' && typeof plugin.apply === 'function') {
plugin.apply(app, options)
plugin.apply(ctx, options)
} else {
throw new Error(errors.INVALID_PLUGIN)
}
Expand Down

0 comments on commit 3ae7e70

Please sign in to comment.