Pattern-matching event emitter for route-like event names.
import { ExpressEmitter } from '@stackpress/lib';Use ExpressEmitter when event names carry parameters or wildcard segments. It extends EventEmitter with string-pattern and RegExp listeners plus extracted params and args.
const emitter = new ExpressEmitter('/');separator defaults to '/'.
| Name | Type | Notes |
|---|---|---|
separator |
string |
Segment separator used when building route fragments. |
expressions |
Map<string, EventExpression> |
Registered pattern expressions. |
| Method | Returns | Notes |
|---|---|---|
match(event) |
Map<string, EventMatch> |
Includes exact matches and computed expression matches. |
on(eventOrRegExp, action, priority = 0) |
this |
Accepts exact strings, :params, *, **, or raw RegExp. |
use(emitter) |
this |
Merges expression metadata and listeners. |
:namecaptures one segment intoevent.data.params.name*captures one segment intoevent.data.args**captures multiple segments intoevent.data.argsRegExplisteners keeppatternempty and expose match groups throughdata.args
The constants VARIABLE_NAME and EVENT_PATTERNS are also exported from the module for pattern parsing.
import { ExpressEmitter } from '@stackpress/lib';
type Events = {
[key: string]: [string];
};
const emitter = new ExpressEmitter<Events>();
emitter.on('user/:id', async value => {
console.log(value);
});
await emitter.emit('user/42', 'loaded');
const match = emitter.match('user/42').values().next().value;
match?.data.params.id;