Services
Overview
This module provides helper functions to manage recurring tasks on services like binding and unbinding event listeners to track real-time events such as created, updated, patched, and removed.
Functions
listenToServiceEvents(service, options, listeners)
Binds event listeners to a service and stores them in an object.
- Parameters:
service(string | Object): The service instance or name.options*(string | Object, optional)**:context(Object, optional): Context for retrieving the service ifserviceis a string.created(Function, optional): Listener for thecreatedevent.updated(Function, optional): Listener for theupdatedevent.patched(Function, optional): Listener for thepatchedevent.removed(Function, optional): Listener for theremovedevent.all(Function, optional): Listener for all events when no other is given (created,updated,patched,removed).
listeners(Object, optional): The previous object returned fromlistenToServiceEvents, containing event handlers to unbind first.
- Returns:
- An object containing the service and the provided listeners.
Notes
- If
serviceis a string, it is resolved usingapi.getService(service, context). - The
alloption applies the same function to all event types.
unlistenToServiceEvents(listeners)
Unbinds previously stored listeners from a service.
After calling unlistenToServiceEvents, the service will no longer trigger the specified event listeners.
- Parameters:
listeners(Object): The object returned fromlistenToServiceEvents, containing event handlers.
Note
Calling unlistenToServiceEvents is necessary to prevent memory leaks when event listeners are no longer needed.
Usage
javascript
import { listenToServiceEvents, unlistenToServiceEvents } from './utils'
const listeners = listenToServiceEvents('users', {
created: (data) => console.log('User created:', data),
updated: (data) => console.log('User updated:', data)
})
// Later, when no longer needed
unlistenToServiceEvents(listeners)