@osmium/events v0.11.3
Osmium Events - Async event dispatcher (readme in progress)
About
@osmium/events - is a TypeScript/JS library of universal event dispatcher with support for asynchrony, middlewares,
event mapping between different library instances and many other things. Support nodejs & browser (has bundled js.min version with all depends).
This project is released under MIT license.
How to use?
...
// Example hereEvents handlers and emitters
...
// Example hereMiddlewares
...
// Example hereEvent mappings
...
// Example hereAPI
Classes
new Events
Events<EventNameType = string | number | symbol, MiddlewareMetadataType extends Record<string, unknown>>(config: Events.ConfigOptionable = {}, mapEmitOnce: Events<EventNameType> | null = null)
Creates an event dispatcher instance.
<EventNameType> - defines the type of event names used. Any types can be used (actually the event name is the key in Map<EventNameType,...>).
By default is string | number | symbol
config - Instance configuration
Events.ConfigOptionable
{
metadata? : MiddlewareMetadata; //
defaultChain? : boolean; //
instanceIdPrefix?: string; //
instanceIdMask? : string; //
eventIdPrefix? : string; //
eventIdPrefixMW? : string; //
eventIdMask? : string; //
}mapEmitOnce - ...
API - Event handlers
on
on<ArgsType extends any[] = any[], ReturnType = any, ThisExtType extends object = {}>(name: Events.EventName<EventNameType>, cb: Events.EventCallback<EventNameType, ArgsType, ReturnType, ThisExtType>): Events.EventId
Register event handler
// Example hereonce
once<ArgsType extends any[] = any[], ReturnType = any, ThisExtType extends object = {}>(name: Events.EventName<EventNameType>, cb: Events.EventCallback<EventNameType, ArgsType, ReturnType, ThisExtType>): Events.EventId
Register event and self-remove after first call
// Example herewait
wait<ReturnType extends [] = []>(name: Events.EventName<EventNameType>, timeout: number = -1): Promise<ReturnType | null>
Await event emit
// Example hereoffById
offById(targetId: Events.EventId | Events.EventIds): Events.AffectedEventId | Events.AffectedEventIds
Remove event by EventId or array of EventId
// Example hereoff
off(name: Events.EventName<EventNameType>): Events.AffectedEventIds | null
Remove event by EventName
// Example hereAPI - Event emits
emit
async emit<ArgsType extends any[] = any[], ReturnType = unknown>(name: Events.EventName<EventNameType>, ...args: ArgsType): Promise<Events.EmitResult<ReturnType>>
Event emit (call)
// Example hereemitParallel
async emitParallel<ArgsType extends any[] = any[], ReturnType = unknown>(name: Events.EventName<EventNameType>, ...args: ArgsType): Promise<Events.EmitResult<ReturnType>>
Event emit (call) as parallel (via Promise.all)
// Example hereemitChain
async emitChain<ArgsType extends any[] = any[], ReturnType = unknown>(name: Events.EventName<EventNameType>, ...args: ArgsType): Promise<Events.EmitResult<ReturnType>>
Event emit (call) as chain (one by one)
// Example hereemitOnce
async emitOnce<ArgsType extends any[] = any[], ReturnType = unknown>(name: Events.EventName<EventNameType>, ...args: ArgsType): Promise<ReturnType | undefined>
Event emit (call) and return first value
// Example hereemitEx
async emitEx<ArgsType extends any[] = any[], ReturnType = unknown>
Advanced event emit
// Example hereAPI - Tools/others
clear
clear(): void
Clear all events
new e = new Events();
e.on('test',()=>{
console.log('Test called');
});
e.emit('test'); // 'Test called' in console
e.clear();
e.emit('test'); // Nopereset
reset(): void
Reset all (events, mappings, middlewares), like new Events().
// Example heregetEvents
getEvents(findStr: string | RegExp | null = null): Events.EventNames<EventNameType>
Get events names by RegExp pattern.
Will try to cast the event name to a string type (so that RegExp can work) using ?.toString(), if that doesn't work, it will skip that event name for out list.
// Example hereexists
exists(what: Events.EventName<EventNameType> | RegExp, inMappingsToo: boolean = false): boolean
Check event exists
// Example hereAPI - Middlewares
useBefore
useBefore(handler: Events.MiddlewareBeforeCallback<EventNameType>, position: number | null = null): number
Add 'before' middleware
// Example hereunUseBefore
unUseBefore(handlerPosition: number): void
Remove 'before' middleware by MiddlewareBefore position
// Example hereuseAfter
useAfter(handler: Events.MiddlewareAfterCallback<EventNameType>, position: number | null = null): number
Add 'after' middleware
// Example hereunUseAfter
unUseAfter(handlerPosition: number): void
Remove 'after' middleware by MiddlewareAfter id
// Example hereAPI - Event mapping
mapEventsBefore
mapEventsBefore(target: Events<EventNameType>, list: Events.EventNames<EventNameType> | null = null): void
Map events 'before' by list, or all if not defined
// Example hereunMapEventsBefore
unMapEventsBefore(target: Events<EventNameType>, list: Events.EventNames<EventNameType> | null = null): void
Remove 'before' event mapper by list, or all if not defined
// Example heremapEventsAfter
mapEventsAfter(target: Events<EventNameType>, list: Events.EventNames<EventNameType> | null = null): void
Map events 'after' by list, or all if not defined
// Example hereunMapEventsAfter
unMapEventsAfter(target: Events<EventNameType>, list: Events.EventNames<EventNameType> | null = null): void
Remove 'after' event mapper by list, or all if not defined
// Example here2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago