QuerySubscription is a utility class that allows you to subscribe to a query and receive updates when the query result changes.
It is useful for polling operations where you want to receive updates when the result of a query changes, such as when a process instance is created or updated.
It uses a predicate function to determine whether to emit an update event. When using the Orchestration Cluster API, the default predicate checks if the result has new items compared to the previous state.
The predicate function receives the previous state and the current state of the query result and should return a value that indicates whether to emit an update.
If the predicate returns true, the current state is emitted. If it returns an object, that object is emitted as the update.
If it returns false, no update is emitted.
This is an experimental feature and may change in the future.
It is not yet stable and may have breaking changes in future releases. We're still working on it, and we welcome feedback.
Please use it with caution and be prepared for potential changes.
constsubscription = QuerySubscription({ query, predicate: (previous, current) => { // This is the default predicate, shown here for clarity constpreviousItems = (previous?.items ?? []) asArray<unknown> constcurrentItems = current.items.filter( (item) => !previousItems.some((prevItem) =>isDeepStrictEqual(prevItem, item)) ) if (currentItems.length > 0) { return { ...current, items:currentItems, page: { ...current.page, totalItems:currentItems.length }, } } returnfalse// No new items, do not emit }, interval:500, }) subscription.on('update', (data) => { console.log('Received new processes:', data.items) }) // After some time subscription.stop() // Stop polling when no longer needed, you can also call `start()` to resume polling subscription.cancel() // Or cancel the subscription to free resources
See
PollingOperation for a simpler polling operation that does a single query.
Description
QuerySubscription is a utility class that allows you to subscribe to a query and receive updates when the query result changes. It is useful for polling operations where you want to receive updates when the result of a query changes, such as when a process instance is created or updated. It uses a predicate function to determine whether to emit an update event. When using the Orchestration Cluster API, the default predicate checks if the result has new items compared to the previous state. The predicate function receives the previous state and the current state of the query result and should return a value that indicates whether to emit an update. If the predicate returns
true
, the current state is emitted. If it returns an object, that object is emitted as the update. If it returnsfalse
, no update is emitted. This is an experimental feature and may change in the future. It is not yet stable and may have breaking changes in future releases. We're still working on it, and we welcome feedback. Please use it with caution and be prepared for potential changes.Example
See
PollingOperation for a simpler polling operation that does a single query.