@camunda8/sdk
    Preparing search index...

    Class _QuerySubscription<T>Experimental

    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.

    const query = () =>
    c8.searchProcessInstances({
    filter: {
    processDefinitionKey: key,
    state: 'ACTIVE',
    },
    sort: [{ field: 'startDate', order: 'ASC' }],
    })

    const subscription = QuerySubscription({
    query,
    predicate: (previous, current) => { // This is the default predicate, shown here for clarity
    const previousItems = (previous?.items ?? []) as Array<unknown>
    const currentItems = current.items.filter(
    (item) =>
    !previousItems.some((prevItem) => isDeepStrictEqual(prevItem, item))
    )
    if (currentItems.length > 0) {
    return {
    ...current,
    items: currentItems,
    page: { ...current.page, totalItems: currentItems.length },
    }
    }
    return false // 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

    PollingOperation for a simpler polling operation that does a single query.

    Type Parameters

    • T
    Index

    Constructors

    Properties

    listeners: <E extends "update">(event: E) => QuerySubscriptionEvents<T>[E][]
    off: <E extends "update">(
        event: E,
        listener: QuerySubscriptionEvents<T>[E],
    ) => TypedEventEmitter<QuerySubscriptionEvents<T>>
    on: <E extends "update">(
        event: E,
        listener: QuerySubscriptionEvents<T>[E],
    ) => TypedEventEmitter<QuerySubscriptionEvents<T>>
    once: <E extends "update">(
        event: E,
        listener: QuerySubscriptionEvents<T>[E],
    ) => TypedEventEmitter<QuerySubscriptionEvents<T>>
    prependListener: <E extends "update">(
        event: E,
        listener: QuerySubscriptionEvents<T>[E],
    ) => TypedEventEmitter<QuerySubscriptionEvents<T>>
    prependOnceListener: <E extends "update">(
        event: E,
        listener: QuerySubscriptionEvents<T>[E],
    ) => TypedEventEmitter<QuerySubscriptionEvents<T>>
    removeAllListeners: <E extends "update">(
        event?: E,
    ) => TypedEventEmitter<QuerySubscriptionEvents<T>>
    removeListener: <E extends "update">(
        event: E,
        listener: QuerySubscriptionEvents<T>[E],
    ) => TypedEventEmitter<QuerySubscriptionEvents<T>>

    Methods