-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow a handler to subscribe to multiple events (#179)
<!-- Thank you for your pull request. Please review below requirements. Bug fixes and new features should include tests and possibly benchmarks. Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md 感谢您贡献代码。请确认下列 checklist 的完成情况。 Bug 修复和新功能必须包含测试,必要时请附上性能测试。 Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md --> ##### Checklist <!-- Remove items that do not apply. For completed items, change [ ] to [x]. --> - [x] `npm test` passes - [x] tests and/or benchmarks are included - [x] documentation is changed or added - [x] commit message follows commit guidelines ##### Affected core subsystem(s) <!-- Provide affected core subsystem(s). --> `plugin/eventbus` `eventbus-decorator` `eventbus-runtime` ##### Description of change <!-- Provide a description of the change below this comment. --> allow a handler to subscribe to multiple events <!-- - any feature? - close https://github.com/eggjs/egg/ISSUE_URL -->
- Loading branch information
Showing
19 changed files
with
356 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// use @eggjs/tegg as namespace | ||
// eslint-disable-next-line import/no-unresolved | ||
import type { Events } from '@eggjs/tegg'; | ||
import { EggProtoImplClass } from '@eggjs/core-decorator'; | ||
import assert from 'assert'; | ||
import { EventInfoUtil } from './EventInfoUtil'; | ||
|
||
export interface IEventContext{ | ||
eventName: keyof Events | ||
} | ||
|
||
export function EventContext() { | ||
return function(target: any, propertyKey: PropertyKey, parameterIndex: number) { | ||
assert(propertyKey === 'handle', | ||
`[eventHandler ${target.name}] expect method name be handle, but now is ${String(propertyKey)}`); | ||
assert(parameterIndex === 0, | ||
`[eventHandler ${target.name}] expect param EventContext be the first param`); | ||
const clazz = target.constructor as EggProtoImplClass; | ||
EventInfoUtil.setEventHandlerContextInject(true, clazz); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class EmptyHandler{} |
28 changes: 28 additions & 0 deletions
28
core/eventbus-decorator/test/fixtures/event-handle-with-context.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Inject, SingletonProto } from '@eggjs/core-decorator'; | ||
import {EventBus, Event, IEventContext, EventContext} from '../..'; | ||
|
||
|
||
declare module '@eggjs/tegg' { | ||
interface Events { | ||
ctxEvent: (msg: string) => void; | ||
} | ||
} | ||
|
||
@SingletonProto() | ||
export class EventContextProducer { | ||
@Inject() | ||
private readonly eventBus: EventBus; | ||
|
||
trigger() { | ||
this.eventBus.emit('ctxEvent', 'hello'); | ||
} | ||
} | ||
|
||
@Event('ctxEvent') | ||
export class EventContextHandler { | ||
handle(@EventContext() ctx: IEventContext, msg: string): void { | ||
console.log('ctx: ', ctx); | ||
console.log('msg: ', msg); | ||
} | ||
} | ||
|
29 changes: 29 additions & 0 deletions
29
core/eventbus-decorator/test/fixtures/multiple-events-handle.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Inject, SingletonProto } from '@eggjs/core-decorator'; | ||
import { EventBus, Event } from '../..'; | ||
|
||
|
||
declare module '@eggjs/tegg' { | ||
interface Events { | ||
hello: (msg: string) => void; | ||
hi: (msg: string) => void; | ||
} | ||
} | ||
|
||
@SingletonProto() | ||
export class MultiProducer { | ||
@Inject() | ||
private readonly eventBus: EventBus; | ||
|
||
trigger() { | ||
this.eventBus.emit('hello', 'Ydream'); | ||
} | ||
} | ||
|
||
@Event('hello') | ||
@Event('hi') | ||
export class MultiHandler { | ||
handle(msg: string): void { | ||
console.log('msg: ', msg); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.