From f631856db30f4228839cf220e7301ce184c04540 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Tue, 1 Sep 2020 23:02:50 +0800 Subject: [PATCH] feat(core): adjust bail/serial behavior --- packages/koishi-core/src/context.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/koishi-core/src/context.ts b/packages/koishi-core/src/context.ts index 3c99234b2c..637b59b6a0 100644 --- a/packages/koishi-core/src/context.ts +++ b/packages/koishi-core/src/context.ts @@ -33,6 +33,10 @@ function matchScope(base: ScopeSet, id: number) { return !id || !(base.positive ^ base.includes(id)) } +function isBailed(value: any) { + return value !== null && value !== false && value !== undefined +} + export class Context { static readonly MIDDLEWARE_EVENT = Symbol('mid') @@ -140,7 +144,7 @@ export class Context { for (const [context, callback] of this.app._hooks[name] || []) { if (!context.match(session)) continue const result = await callback.apply(this, args) - if (result !== undefined) return result + if (isBailed(result)) return result } } @@ -153,7 +157,7 @@ export class Context { for (const [context, callback] of this.app._hooks[name] || []) { if (!context.match(session)) continue const result = callback.apply(this, args) - if (result !== undefined) return result + if (isBailed(result)) return result } }