Skip to content

Commit

Permalink
feat(core): support session.$uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Sep 4, 2020
1 parent e3357ce commit af6a7c0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 22 deletions.
12 changes: 5 additions & 7 deletions packages/koishi-core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@ export class App extends Context {

_database: Database
_commands: Command[]
_sessions: Record<string, Session> = {}
_commandMap: Record<string, Command>
_hooks: Record<keyof any, [Context, (...args: any[]) => any][]>
_userCache: LruCache<number, Observed<Partial<User>, Promise<void>>>
_groupCache: LruCache<number, Observed<Partial<Group>, Promise<void>>>

private _nameRE: RegExp
private _prefixRE: RegExp
private _middlewareCounter = 0
private _middlewareSet = new Set<number>()
private _getSelfIdsPromise: Promise<any>

static defaultConfig: AppOptions = {
Expand Down Expand Up @@ -206,8 +205,7 @@ export class App extends Context {

private async _receive(session: Session) {
// preparation
const counter = this._middlewareCounter++
this._middlewareSet.add(counter)
this._sessions[session.$uuid] = session
const middlewares: Middleware[] = this._hooks[Context.MIDDLEWARE_EVENT as any]
.filter(([context]) => context.match(session))
.map(([, middleware]) => middleware)
Expand All @@ -225,7 +223,7 @@ export class App extends Context {
}

try {
if (!this._middlewareSet.has(counter)) {
if (!this._sessions[session.$uuid]) {
throw new Error('isolated next function detected')
}
if (fallback) middlewares.push((_, next) => fallback(next))
Expand All @@ -241,8 +239,8 @@ export class App extends Context {
}
await next()

// update middleware set
this._middlewareSet.delete(counter)
// update session map
delete this._sessions[session.$uuid]
this.emit(session, 'after-middleware', session)

// flush user & group data
Expand Down
3 changes: 2 additions & 1 deletion packages/koishi-core/src/session.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { User, Group } from './database'
import { ExecuteArgv, ParsedArgv, Command } from './command'
import { isInteger, contain, observe, noop, Logger, defineProperty } from 'koishi-utils'
import { isInteger, contain, observe, noop, Logger, defineProperty, Random } from 'koishi-utils'
import { NextFunction } from './context'
import { App } from './app'

Expand Down Expand Up @@ -77,6 +77,7 @@ export class Session<U extends User.Field = never, G extends Group.Field = never
$prefix?: string = null
$parsed?: string
$reply?: number
$uuid = Random.uuid()

private _delay?: number
private _queued = Promise.resolve()
Expand Down
16 changes: 2 additions & 14 deletions packages/plugin-eval/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Context, Session } from 'koishi-core'
import { CQCode, Logger, defineProperty, Random } from 'koishi-utils'
import { Context } from 'koishi-core'
import { CQCode, Logger, defineProperty } from 'koishi-utils'
import { EvalWorker, attachTraps, EvalConfig, Config } from './main'

export * from './main'

declare module 'koishi-core/dist/app' {
interface App {
_sessions: Record<string, Session>
worker: EvalWorker
}
}
Expand All @@ -19,7 +18,6 @@ declare module 'koishi-core/dist/command' {

declare module 'koishi-core/dist/session' {
interface Session {
$uuid: string
_isEval: boolean
_sendCount: number
}
Expand All @@ -43,18 +41,8 @@ export function apply(ctx: Context, config: Config = {}) {
const { prefix } = config = { ...defaultConfig, ...config }
const { app } = ctx
const worker = new EvalWorker(app, config)
defineProperty(app, '_sessions', {})
defineProperty(app, 'worker', worker)

app.prependMiddleware((session, next) => {
app._sessions[session.$uuid = Random.uuid()] = session
return next()
})

app.on('after-middleware', (session) => {
delete app._sessions[session.$uuid]
})

app.on('before-connect', () => {
return worker.start()
})
Expand Down

0 comments on commit af6a7c0

Please sign in to comment.