Package Exports
- @eggjs/eventbus-decorator
- @eggjs/eventbus-decorator/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@eggjs/eventbus-decorator) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@eggjs/eventbus-decorator
Usage
emit event
import { EventBus } from '@eggjs/eventbus-decorator'
// Define event first.
// Ts can check event and args type for you.
declare module '@eggjs/eventbus-decorator' {
interface Events {
hello: (msg: string) => Promise<void>;
}
}
class Foo {
@Inject()
private readonly eventBus: EventBus;
bar() {
this.eventBus.emit('hello', '01');
}
}cork events
Cache events in memory until uncork.
class Foo {
@Inject()
private readonly eventBus: ContextEventBus;
bar() {
this.eventBus.cork();
// ...do something
this.eventBus.emit('hello', '01');
// ...do other things
// emit all cached events
this.eventBus.uncork();
}
}handle event
@Event('hello')
export class Foo {
async handle(msg: string): Promise<void> {
console.log('msg: ', msg);
}
}