API Reference
classes
Querysubscriptionproxy

@coasys/ad4m / Exports / QuerySubscriptionProxy

Class: QuerySubscriptionProxy

Proxy object for a subscribed Prolog query that provides real-time updates

This class handles:

  • Keeping the subscription alive by sending periodic keepalive signals
  • Managing callbacks for result updates
  • Subscribing to query updates via GraphQL subscriptions
  • Maintaining the latest query result

The subscription will remain active as long as keepalive signals are sent. Make sure to call dispose() when you're done with the subscription to clean up resources and stop keepalive signals.

Example usage:

const subscription = await perspective.subscribeInfer("my_query(X)");
console.log("Initial result:", subscription.result);
 
// Set up callback for updates
const removeCallback = subscription.onResult(result => {
    console.log("New result:", result);
});
 
// Later: clean up
subscription.dispose();

Table of contents

Constructors

Properties

Accessors

Methods

Constructors

constructor

new QuerySubscriptionProxy(uuid, subscriptionId, initialResult, client)

Creates a new query subscription

Parameters

NameTypeDescription
uuidstringThe UUID of the perspective
subscriptionIdstringThe ID returned by the subscription mutation
initialResultAllInstancesResultThe initial query result
clientPerspectiveClientThe PerspectiveClient instance to use for communication

Defined in

perspectives/PerspectiveProxy.ts:66

Properties

#callbacks

Private #callbacks: Set<QueryCallback>

Defined in

perspectives/PerspectiveProxy.ts:54


#client

Private #client: PerspectiveClient

Defined in

perspectives/PerspectiveProxy.ts:53


#disposed

Private #disposed: boolean = false

Defined in

perspectives/PerspectiveProxy.ts:58


#keepaliveTimer

Private #keepaliveTimer: number

Defined in

perspectives/PerspectiveProxy.ts:55


#latestResult

Private #latestResult: AllInstancesResult

Defined in

perspectives/PerspectiveProxy.ts:57


#subscriptionId

Private #subscriptionId: string

Defined in

perspectives/PerspectiveProxy.ts:52


#unsubscribe

Private Optional #unsubscribe: () => void

Type declaration

▸ (): void

Returns

void

Defined in

perspectives/PerspectiveProxy.ts:56


#uuid

Private #uuid: string

Defined in

perspectives/PerspectiveProxy.ts:51

Accessors

result

get result(): AllInstancesResult

Get the latest query result

This returns the most recent result from the query, which could be either:

  • The initial result from when the subscription was created
  • The latest update received through the subscription

Returns

AllInstancesResult

The latest query result as a string (usually a JSON array of bindings)

Defined in

perspectives/PerspectiveProxy.ts:113

Methods

#notifyCallbacks

Private #notifyCallbacks(result): void

Internal method to notify all callbacks of a new result

Parameters

NameType
resultAllInstancesResult

Returns

void

Defined in

perspectives/PerspectiveProxy.ts:142


dispose

dispose(): void

Clean up the subscription and stop keepalive signals

This method:

  1. Stops the keepalive timer
  2. Unsubscribes from GraphQL subscription updates
  3. Clears all registered callbacks

After calling this method, the subscription is no longer active and will not receive any more updates. The instance should be discarded.

Returns

void

Defined in

perspectives/PerspectiveProxy.ts:162


onResult

onResult(callback): () => void

Add a callback that will be called whenever new results arrive

The callback will be called immediately with the current result, and then again each time the query results change.

Parameters

NameTypeDescription
callbackQueryCallbackFunction that takes a result string and processes it

Returns

fn

A function that can be called to remove this callback

Example:

const removeCallback = subscription.onResult(result => {
    const bindings = JSON.parse(result);
    console.log("New bindings:", bindings);
});
 
// Later: stop receiving updates
removeCallback();

▸ (): void

Add a callback that will be called whenever new results arrive

The callback will be called immediately with the current result, and then again each time the query results change.

Returns

void

A function that can be called to remove this callback

Example:

const removeCallback = subscription.onResult(result => {
    const bindings = JSON.parse(result);
    console.log("New bindings:", bindings);
});
 
// Later: stop receiving updates
removeCallback();

Defined in

perspectives/PerspectiveProxy.ts:136