vbstyle-event-models-js
v1.0.1
Published
A thread-safe library providing event models; a pure JavaScript port of "vbstyle-event-models" (TypeScript version).
Maintainers
Readme
vbstyle-event-models-js - A Pure JavaScript Port of vbstyle-event-models
Description
This library a pure JavaScript port of vbstyle-event-models, which was originally written in TypeScript. It provides a set of event handling and message routing utilities designed to be thread-safe and easy to use, and includes:
BasicEventModel: A simple event model for registering and invoking event handlersMessageRouter: A singleton message router for pub/sub pattern messagingQueueEventScheduler: An event scheduler that processes events in a queueMutexLock: A simple mutex lock implementation for thread safety
Installation & Usage
Install the library using NPM (current version 1.0.1):
npm install vbstyle-event-models-jsExamples of usage are the same as the "Usage" section in vbstyle-event-models (see this link: vbstyle-event-models#usage).
API Reference
BasicEventModel
addHandler(handler): Add an event handlerremoveHandler(handler): Remove an event handlerraiseEvent(sender, e): Raise the event, invoking all handlers
MessageRouter
static instance: Get the singleton instanceasync subscribe(messageType, handler): Subscribe to a message typeasync send(messageType, content): Send a messageasync unsubscribe(messageType, handler): Unsubscribe from a message typeasync clearSubscribers(messageType): Clear all subscribers for a message typesubscriberCount: Get total number of subscribers
QueueEventScheduler
async scheduleEvent(event): Schedule an eventasync processEvents(sender, e): Process all eventsasync clearEvents(): Clear all pending eventspendingEventCount: Get number of pending events
MutexLock
async acquireLock(): Acquire the lockreleaseLock(): Release the lockisLocked: Check if the lock is heldwaiterCount: Get number of waiters
Thread Safety
All classes in this library are designed to be thread-safe, using the MutexLock class to ensure safe access to shared resources.
License
BSD 3-Clause License
