Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): support event / middleware decorators #1141

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/core/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ export class SessionError extends Error {
export type Next = (next?: Next.Callback) => Promise<void | Fragment>
export type Middleware = (session: Session, next: Next) => Awaitable<void | Fragment>

export const Middleware = (prepend?: boolean) => <T extends Middleware>(value: T, meta: ClassMethodDecoratorContext, ...args) => {
if (meta.kind !== 'method') return value
meta.addInitializer(function () {
(this[Context.current] as Context).middleware(value.bind(this), prepend)
})
return value
}

export namespace Next {
export const MAX_DEPTH = 64

Expand Down Expand Up @@ -80,7 +88,6 @@ export class Processor {
defineProperty(this, Context.current, ctx)

// bind built-in event listeners
this.middleware(this._process.bind(this), true)
ctx.on('message', this._handleMessage.bind(this))

ctx.on('interaction/command', (session) => {
Expand Down Expand Up @@ -219,6 +226,7 @@ export class Processor {
}
}

@Middleware(true)
private async _process(session: Session, next: Next) {
let atSelf = false, appel = false
let content = session.content.trim()
Expand Down