# new ObserverHandler()
The ObserverHandler class implements the Observable interface and provides a centralized way to manage multiple observers. It allows registering observers with optional filters to control which events they receive notifications for, and handles the process of notifying all relevant observers when database events occur.
Manages a collection of observers for database events
Example
```typescript
// Create an observer handler
const handler = new ObserverHandler();
// Register an observer
const myObserver = {
refresh: async (table, event, id) => {
console.log(`Change in ${table}: ${event} for ID ${id}`);
}
};
// Add observer with a filter for only user table events
handler.observe(myObserver, (table, event, id) => table === 'users');
// Notify observers about an event
await handler.updateObservers(logger, 'users', 'CREATE', 123);
// Remove an observer when no longer needed
handler.unObserve(myObserver);
```
Members
Methods
# count() → {number}
Returns the count of observers currently registered with this handler
Gets the number of registered observers
The number of registered observers
number
# observe(observer, filteropt) → {void}
Adds an observer to the collection with an optional filter function
Registers a new observer
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
observer |
Observer
|
The observer to register |
|
filter |
ObserverFilter
|
<optional> |
Optional filter function to determine which events the observer receives |
void
# unObserve(observer) → {void}
Removes an observer from the collection
Unregisters an observer
Parameters:
Name | Type | Description |
---|---|---|
observer |
Observer
|
The observer to unregister |
void
# async updateObservers(log, table, event, id, …args) → {Promise.<void>}
Filters observers based on their filter functions and calls refresh on each matching observer
Notifies all relevant observers about a database event
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
log |
Logger
|
Logger for recording notification activities |
|
table |
string
|
The name of the table where the event occurred |
|
event |
OperationKeys
|
BulkCrudOperationKeys
|
string
|
The type of operation that occurred |
|
id |
EventIds
|
The identifier(s) of the affected record(s) |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments to pass to the observers |
A promise that resolves when all observers have been notified
Promise.<void>