From 3ae7e7044d06daa2ea76e24af15358c5764b9887 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Mon, 20 Jan 2020 16:10:07 +0800 Subject: [PATCH] fix(core): create major context at demand --- packages/koishi-core/src/app.ts | 35 ++++++++++++++++++++--------- packages/koishi-core/src/context.ts | 6 ++--- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/packages/koishi-core/src/app.ts b/packages/koishi-core/src/app.ts index f3eabb4b22..239a4a0b48 100644 --- a/packages/koishi-core/src/app.ts +++ b/packages/koishi-core/src/app.ts @@ -85,9 +85,7 @@ export class App extends Context { atMeRE: RegExp prefixRE: RegExp nicknameRE: RegExp - users: MajorContext - groups: MajorContext - discusses: MajorContext + status = Status.closed _commands: Command[] = [] _commandMap: Record = {} @@ -95,7 +93,9 @@ export class App extends Context { _shortcutMap: Record = {} _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() @@ -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 () { diff --git a/packages/koishi-core/src/context.ts b/packages/koishi-core/src/context.ts index 9cde0e2c11..18c616e773 100644 --- a/packages/koishi-core/src/context.ts +++ b/packages/koishi-core/src/context.ts @@ -131,11 +131,11 @@ export class Context { plugin (plugin: PluginObject, options?: U): this plugin (plugin: Plugin, 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) }