@coasys/ad4m 0.10.1-release-candidate-2 → 0.10.1-release-candidate-2-test-2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.cjs CHANGED
@@ -4614,7 +4614,7 @@ var __classPrivateFieldGet$a = (undefined && undefined.__classPrivateFieldGet) |
4614
4614
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4615
4615
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
4616
4616
  };
4617
- var _QuerySubscriptionProxy_instances, _QuerySubscriptionProxy_uuid, _QuerySubscriptionProxy_subscriptionId, _QuerySubscriptionProxy_client, _QuerySubscriptionProxy_callbacks, _QuerySubscriptionProxy_keepaliveTimer, _QuerySubscriptionProxy_unsubscribe, _QuerySubscriptionProxy_latestResult, _QuerySubscriptionProxy_disposed, _QuerySubscriptionProxy_initialized, _QuerySubscriptionProxy_initTimeoutId, _QuerySubscriptionProxy_notifyCallbacks, _PerspectiveProxy_handle, _PerspectiveProxy_client, _PerspectiveProxy_perspectiveLinkAddedCallbacks, _PerspectiveProxy_perspectiveLinkRemovedCallbacks, _PerspectiveProxy_perspectiveLinkUpdatedCallbacks, _PerspectiveProxy_perspectiveSyncStateChangeCallbacks;
4617
+ var _QuerySubscriptionProxy_instances, _QuerySubscriptionProxy_uuid, _QuerySubscriptionProxy_subscriptionId, _QuerySubscriptionProxy_client, _QuerySubscriptionProxy_callbacks, _QuerySubscriptionProxy_keepaliveTimer, _QuerySubscriptionProxy_unsubscribe, _QuerySubscriptionProxy_latestResult, _QuerySubscriptionProxy_disposed, _QuerySubscriptionProxy_initialized, _QuerySubscriptionProxy_initTimeoutId, _QuerySubscriptionProxy_query, _QuerySubscriptionProxy_notifyCallbacks, _PerspectiveProxy_handle, _PerspectiveProxy_client, _PerspectiveProxy_perspectiveLinkAddedCallbacks, _PerspectiveProxy_perspectiveLinkRemovedCallbacks, _PerspectiveProxy_perspectiveLinkUpdatedCallbacks, _PerspectiveProxy_perspectiveSyncStateChangeCallbacks;
4618
4618
  /** Proxy object for a subscribed Prolog query that provides real-time updates
4619
4619
  *
4620
4620
  * This class handles:
@@ -4623,10 +4623,11 @@ var _QuerySubscriptionProxy_instances, _QuerySubscriptionProxy_uuid, _QuerySubsc
4623
4623
  * - Subscribing to query updates via GraphQL subscriptions
4624
4624
  * - Maintaining the latest query result
4625
4625
  * - Ensuring subscription is fully initialized before allowing access
4626
+ * - Cleaning up resources when disposed
4626
4627
  *
4627
4628
  * The subscription will remain active as long as keepalive signals are sent.
4628
4629
  * Make sure to call dispose() when you're done with the subscription to clean up
4629
- * resources and stop keepalive signals.
4630
+ * resources, stop keepalive signals, and notify the backend to remove the subscription.
4630
4631
  *
4631
4632
  * The subscription goes through an initialization process where it waits for the first
4632
4633
  * result to come through the subscription channel. You can await the `initialized`
@@ -4643,18 +4644,17 @@ var _QuerySubscriptionProxy_instances, _QuerySubscriptionProxy_uuid, _QuerySubsc
4643
4644
  * console.log("New result:", result);
4644
4645
  * });
4645
4646
  *
4646
- * // Later: clean up
4647
+ * // Later: clean up subscription and notify backend
4647
4648
  * subscription.dispose();
4648
4649
  * ```
4649
4650
  */
4650
4651
  class QuerySubscriptionProxy {
4651
4652
  /** Creates a new query subscription
4652
4653
  * @param uuid - The UUID of the perspective
4653
- * @param subscriptionId - The ID returned by the subscription mutation
4654
- * @param initialResult - The initial query result
4654
+ * @param query - The Prolog query to subscribe to
4655
4655
  * @param client - The PerspectiveClient instance to use for communication
4656
4656
  */
4657
- constructor(uuid, subscriptionId, initialResult, client) {
4657
+ constructor(uuid, query, client) {
4658
4658
  _QuerySubscriptionProxy_instances.add(this);
4659
4659
  _QuerySubscriptionProxy_uuid.set(this, void 0);
4660
4660
  _QuerySubscriptionProxy_subscriptionId.set(this, void 0);
@@ -4666,11 +4666,17 @@ class QuerySubscriptionProxy {
4666
4666
  _QuerySubscriptionProxy_disposed.set(this, false);
4667
4667
  _QuerySubscriptionProxy_initialized.set(this, void 0);
4668
4668
  _QuerySubscriptionProxy_initTimeoutId.set(this, void 0);
4669
+ _QuerySubscriptionProxy_query.set(this, void 0);
4669
4670
  __classPrivateFieldSet$a(this, _QuerySubscriptionProxy_uuid, uuid, "f");
4670
- __classPrivateFieldSet$a(this, _QuerySubscriptionProxy_subscriptionId, subscriptionId, "f");
4671
+ __classPrivateFieldSet$a(this, _QuerySubscriptionProxy_query, query, "f");
4671
4672
  __classPrivateFieldSet$a(this, _QuerySubscriptionProxy_client, client, "f");
4672
4673
  __classPrivateFieldSet$a(this, _QuerySubscriptionProxy_callbacks, new Set(), "f");
4673
- __classPrivateFieldSet$a(this, _QuerySubscriptionProxy_latestResult, initialResult, "f");
4674
+ }
4675
+ async subscribe() {
4676
+ // initialize the query subscription
4677
+ const result = await __classPrivateFieldGet$a(this, _QuerySubscriptionProxy_client, "f").subscribeQuery(__classPrivateFieldGet$a(this, _QuerySubscriptionProxy_uuid, "f"), __classPrivateFieldGet$a(this, _QuerySubscriptionProxy_query, "f"));
4678
+ __classPrivateFieldSet$a(this, _QuerySubscriptionProxy_subscriptionId, result.subscriptionId, "f");
4679
+ // Subscribe to query updates
4674
4680
  __classPrivateFieldSet$a(this, _QuerySubscriptionProxy_initialized, new Promise((resolve, reject) => {
4675
4681
  // Add timeout to prevent hanging promises
4676
4682
  __classPrivateFieldSet$a(this, _QuerySubscriptionProxy_initTimeoutId, setTimeout(() => {
@@ -4696,6 +4702,10 @@ class QuerySubscriptionProxy {
4696
4702
  }
4697
4703
  catch (e) {
4698
4704
  console.error('Error in keepalive:', e);
4705
+ // try to reinitialize the subscription
4706
+ console.log('Reinitializing subscription for query:', __classPrivateFieldGet$a(this, _QuerySubscriptionProxy_query, "f"));
4707
+ await this.subscribe();
4708
+ console.log('Subscription reinitialized');
4699
4709
  }
4700
4710
  // Schedule next keepalive if not disposed
4701
4711
  if (!__classPrivateFieldGet$a(this, _QuerySubscriptionProxy_disposed, "f")) {
@@ -4705,6 +4715,17 @@ class QuerySubscriptionProxy {
4705
4715
  // Start the first keepalive loop
4706
4716
  __classPrivateFieldSet$a(this, _QuerySubscriptionProxy_keepaliveTimer, setTimeout(keepaliveLoop, 30000), "f");
4707
4717
  }
4718
+ /** Get the subscription ID for this query subscription
4719
+ *
4720
+ * This is a unique identifier assigned when the subscription was created.
4721
+ * It can be used to reference this specific subscription, for example when
4722
+ * sending keepalive signals.
4723
+ *
4724
+ * @returns The subscription ID string
4725
+ */
4726
+ get id() {
4727
+ return __classPrivateFieldGet$a(this, _QuerySubscriptionProxy_subscriptionId, "f");
4728
+ }
4708
4729
  /** Promise that resolves when the subscription has received its first result
4709
4730
  * through the subscription channel. This ensures the subscription is fully
4710
4731
  * set up before allowing access to results or updates.
@@ -4768,14 +4789,19 @@ class QuerySubscriptionProxy {
4768
4789
  if (__classPrivateFieldGet$a(this, _QuerySubscriptionProxy_unsubscribe, "f")) {
4769
4790
  __classPrivateFieldGet$a(this, _QuerySubscriptionProxy_unsubscribe, "f").call(this);
4770
4791
  }
4792
+ __classPrivateFieldGet$a(this, _QuerySubscriptionProxy_callbacks, "f").clear();
4771
4793
  if (__classPrivateFieldGet$a(this, _QuerySubscriptionProxy_initTimeoutId, "f")) {
4772
4794
  clearTimeout(__classPrivateFieldGet$a(this, _QuerySubscriptionProxy_initTimeoutId, "f"));
4773
4795
  __classPrivateFieldSet$a(this, _QuerySubscriptionProxy_initTimeoutId, undefined, "f");
4774
4796
  }
4775
- __classPrivateFieldGet$a(this, _QuerySubscriptionProxy_callbacks, "f").clear();
4797
+ // Tell the backend to dispose of the subscription
4798
+ if (__classPrivateFieldGet$a(this, _QuerySubscriptionProxy_subscriptionId, "f")) {
4799
+ __classPrivateFieldGet$a(this, _QuerySubscriptionProxy_client, "f").disposeQuerySubscription(__classPrivateFieldGet$a(this, _QuerySubscriptionProxy_uuid, "f"), __classPrivateFieldGet$a(this, _QuerySubscriptionProxy_subscriptionId, "f"))
4800
+ .catch(e => console.error('Error disposing query subscription:', e));
4801
+ }
4776
4802
  }
4777
4803
  }
4778
- _QuerySubscriptionProxy_uuid = new WeakMap(), _QuerySubscriptionProxy_subscriptionId = new WeakMap(), _QuerySubscriptionProxy_client = new WeakMap(), _QuerySubscriptionProxy_callbacks = new WeakMap(), _QuerySubscriptionProxy_keepaliveTimer = new WeakMap(), _QuerySubscriptionProxy_unsubscribe = new WeakMap(), _QuerySubscriptionProxy_latestResult = new WeakMap(), _QuerySubscriptionProxy_disposed = new WeakMap(), _QuerySubscriptionProxy_initialized = new WeakMap(), _QuerySubscriptionProxy_initTimeoutId = new WeakMap(), _QuerySubscriptionProxy_instances = new WeakSet(), _QuerySubscriptionProxy_notifyCallbacks = function _QuerySubscriptionProxy_notifyCallbacks(result) {
4804
+ _QuerySubscriptionProxy_uuid = new WeakMap(), _QuerySubscriptionProxy_subscriptionId = new WeakMap(), _QuerySubscriptionProxy_client = new WeakMap(), _QuerySubscriptionProxy_callbacks = new WeakMap(), _QuerySubscriptionProxy_keepaliveTimer = new WeakMap(), _QuerySubscriptionProxy_unsubscribe = new WeakMap(), _QuerySubscriptionProxy_latestResult = new WeakMap(), _QuerySubscriptionProxy_disposed = new WeakMap(), _QuerySubscriptionProxy_initialized = new WeakMap(), _QuerySubscriptionProxy_initTimeoutId = new WeakMap(), _QuerySubscriptionProxy_query = new WeakMap(), _QuerySubscriptionProxy_instances = new WeakSet(), _QuerySubscriptionProxy_notifyCallbacks = function _QuerySubscriptionProxy_notifyCallbacks(result) {
4779
4805
  for (const callback of __classPrivateFieldGet$a(this, _QuerySubscriptionProxy_callbacks, "f")) {
4780
4806
  try {
4781
4807
  callback(result);
@@ -5581,6 +5607,10 @@ class PerspectiveProxy {
5581
5607
  * The returned subscription is guaranteed to be ready to receive updates,
5582
5608
  * as this method waits for the initialization process to complete.
5583
5609
  *
5610
+ * The subscription will be automatically cleaned up on both frontend and backend
5611
+ * when dispose() is called. Make sure to call dispose() when you're done to
5612
+ * prevent memory leaks and ensure proper cleanup of resources.
5613
+ *
5584
5614
  * @param query - Prolog query string
5585
5615
  * @returns Initialized QuerySubscriptionProxy instance
5586
5616
  *
@@ -5599,12 +5629,15 @@ class PerspectiveProxy {
5599
5629
  * subscription.onResult((todos) => {
5600
5630
  * console.log("Active todos:", todos);
5601
5631
  * });
5632
+ *
5633
+ * // Clean up subscription when done
5634
+ * subscription.dispose();
5602
5635
  * ```
5603
5636
  */
5604
5637
  async subscribeInfer(query) {
5638
+ const subscriptionProxy = new QuerySubscriptionProxy(this.uuid, query, __classPrivateFieldGet$a(this, _PerspectiveProxy_client, "f"));
5605
5639
  // Start the subscription on the Rust side first to get the real subscription ID
5606
- const result = await __classPrivateFieldGet$a(this, _PerspectiveProxy_client, "f").subscribeQuery(this.uuid, query);
5607
- const subscriptionProxy = new QuerySubscriptionProxy(this.uuid, result.subscriptionId, result.result, __classPrivateFieldGet$a(this, _PerspectiveProxy_client, "f"));
5640
+ await subscriptionProxy.subscribe();
5608
5641
  // Wait for the initial result
5609
5642
  await subscriptionProxy.initialized;
5610
5643
  return subscriptionProxy;
@@ -5804,6 +5837,15 @@ class PerspectiveClient {
5804
5837
  }));
5805
5838
  return perspectiveKeepAliveQuery;
5806
5839
  }
5840
+ async disposeQuerySubscription(uuid, subscriptionId) {
5841
+ const { perspectiveDisposeQuerySubscription } = unwrapApolloResult(await __classPrivateFieldGet$9(this, _PerspectiveClient_apolloClient, "f").mutate({
5842
+ mutation: gql `mutation perspectiveDisposeQuerySubscription($uuid: String!, $subscriptionId: String!) {
5843
+ perspectiveDisposeQuerySubscription(uuid: $uuid, subscriptionId: $subscriptionId)
5844
+ }`,
5845
+ variables: { uuid, subscriptionId }
5846
+ }));
5847
+ return perspectiveDisposeQuerySubscription;
5848
+ }
5807
5849
  async add(name) {
5808
5850
  const { perspectiveAdd } = unwrapApolloResult(await __classPrivateFieldGet$9(this, _PerspectiveClient_apolloClient, "f").mutate({
5809
5851
  mutation: gql `mutation perspectiveAdd($name: String!) {
@@ -16537,6 +16579,7 @@ type Mutation {
16537
16579
  perspectiveCommitBatch(batchId: String!, uuid: String!): LinkExpressionMutations!
16538
16580
  perspectiveCreateBatch(uuid: String!): String!
16539
16581
  perspectiveCreateSubject(batchId: String, expressionAddress: String!, initialValues: String, subjectClass: String!, uuid: String!): Boolean!
16582
+ perspectiveDisposeQuerySubscription(subscriptionId: String!, uuid: String!): Boolean!
16540
16583
  perspectiveExecuteCommands(batchId: String, commands: String!, expression: String!, parameters: String, uuid: String!): Boolean!
16541
16584
  perspectiveGetSubjectData(expressionAddress: String!, subjectClass: String!, uuid: String!): String!
16542
16585
  perspectiveKeepAliveQuery(subscriptionId: String!, uuid: String!): Boolean!
@@ -18143,6 +18186,24 @@ class ModelQueryBuilder {
18143
18186
  if (query)
18144
18187
  this.queryParams = query;
18145
18188
  }
18189
+ /**
18190
+ * Disposes of the current subscription if one exists.
18191
+ *
18192
+ * This method:
18193
+ * 1. Stops the keepalive signals to the subscription
18194
+ * 2. Unsubscribes from GraphQL subscription updates
18195
+ * 3. Notifies the backend to clean up subscription resources
18196
+ * 4. Clears the subscription reference
18197
+ *
18198
+ * You should call this method when you're done with a subscription
18199
+ * to prevent memory leaks and ensure proper cleanup.
18200
+ */
18201
+ dispose() {
18202
+ if (this.currentSubscription) {
18203
+ this.currentSubscription.dispose();
18204
+ this.currentSubscription = undefined;
18205
+ }
18206
+ }
18146
18207
  /**
18147
18208
  * Adds where conditions to the query.
18148
18209
  *
@@ -18283,35 +18344,36 @@ class ModelQueryBuilder {
18283
18344
  * 2. Sets up the callback to process future updates
18284
18345
  * 3. Returns the initial results immediately
18285
18346
  *
18286
- * The subscription is guaranteed to be ready when this method returns,
18287
- * as it waits for the initialization process to complete.
18347
+ * Remember to call dispose() when you're done with the subscription
18348
+ * to clean up resources.
18288
18349
  *
18289
18350
  * @param callback - Function to call with updated results
18290
18351
  * @returns Initial results array
18291
18352
  *
18292
18353
  * @example
18293
18354
  * ```typescript
18294
- * // Subscribe to cooking recipes
18295
- * const initialRecipes = await Recipe.query(perspective)
18296
- * .where({ status: "cooking" })
18297
- * .subscribe(recipes => {
18298
- * // This callback will be called with future updates
18299
- * console.log("Currently cooking:", recipes);
18300
- * });
18355
+ * const builder = Recipe.query(perspective)
18356
+ * .where({ status: "cooking" });
18301
18357
  *
18302
- * // initialRecipes contains the current state
18303
- * console.log("Initial recipes:", initialRecipes);
18358
+ * const initialRecipes = await builder.subscribe(recipes => {
18359
+ * console.log("Updated recipes:", recipes);
18360
+ * });
18361
+ *
18362
+ * // When done with subscription:
18363
+ * builder.dispose();
18304
18364
  * ```
18305
18365
  */
18306
18366
  async subscribe(callback) {
18367
+ // Clean up any existing subscription
18368
+ this.dispose();
18307
18369
  const query = await this.ctor.queryToProlog(this.perspective, this.queryParams, this.modelClassName);
18308
- const subscription = await this.perspective.subscribeInfer(query);
18370
+ this.currentSubscription = await this.perspective.subscribeInfer(query);
18309
18371
  const processResults = async (result) => {
18310
18372
  const { results } = await this.ctor.instancesFromPrologResult(this.perspective, this.queryParams, result);
18311
18373
  callback(results);
18312
18374
  };
18313
- subscription.onResult(processResults);
18314
- const { results } = await this.ctor.instancesFromPrologResult(this.perspective, this.queryParams, subscription.result);
18375
+ this.currentSubscription.onResult(processResults);
18376
+ const { results } = await this.ctor.instancesFromPrologResult(this.perspective, this.queryParams, this.currentSubscription.result);
18315
18377
  return results;
18316
18378
  }
18317
18379
  /**
@@ -18339,35 +18401,36 @@ class ModelQueryBuilder {
18339
18401
  * 2. Sets up the callback to process future count updates
18340
18402
  * 3. Returns the initial count immediately
18341
18403
  *
18342
- * The subscription is guaranteed to be ready when this method returns,
18343
- * as it waits for the initialization process to complete.
18404
+ * Remember to call dispose() when you're done with the subscription
18405
+ * to clean up resources.
18344
18406
  *
18345
18407
  * @param callback - Function to call with updated count
18346
18408
  * @returns Initial count
18347
18409
  *
18348
18410
  * @example
18349
18411
  * ```typescript
18350
- * // Subscribe to active recipe count
18351
- * const initialCount = await Recipe.query(perspective)
18352
- * .where({ status: "active" })
18353
- * .countSubscribe(count => {
18354
- * // This callback will be called when the count changes
18355
- * console.log("Active items:", count);
18356
- * });
18412
+ * const builder = Recipe.query(perspective)
18413
+ * .where({ status: "active" });
18414
+ *
18415
+ * const initialCount = await builder.countSubscribe(count => {
18416
+ * console.log("Active items:", count);
18417
+ * });
18357
18418
  *
18358
- * // initialCount contains the current count
18359
- * console.log("Initial count:", initialCount);
18419
+ * // When done with subscription:
18420
+ * builder.dispose();
18360
18421
  * ```
18361
18422
  */
18362
18423
  async countSubscribe(callback) {
18424
+ // Clean up any existing subscription
18425
+ this.dispose();
18363
18426
  const query = await this.ctor.countQueryToProlog(this.perspective, this.queryParams, this.modelClassName);
18364
- const subscription = await this.perspective.subscribeInfer(query);
18427
+ this.currentSubscription = await this.perspective.subscribeInfer(query);
18365
18428
  const processResults = async (result) => {
18366
18429
  const newCount = result?.[0]?.TotalCount || 0;
18367
18430
  callback(newCount);
18368
18431
  };
18369
- subscription.onResult(processResults);
18370
- return subscription.result?.[0]?.TotalCount || 0;
18432
+ this.currentSubscription.onResult(processResults);
18433
+ return this.currentSubscription.result?.[0]?.TotalCount || 0;
18371
18434
  }
18372
18435
  /**
18373
18436
  * Gets a page of results with pagination metadata.
@@ -18399,8 +18462,8 @@ class ModelQueryBuilder {
18399
18462
  * 2. Sets up the callback to process future page updates
18400
18463
  * 3. Returns the initial page immediately
18401
18464
  *
18402
- * The subscription is guaranteed to be ready when this method returns,
18403
- * as it waits for the initialization process to complete.
18465
+ * Remember to call dispose() when you're done with the subscription
18466
+ * to clean up resources.
18404
18467
  *
18405
18468
  * @param pageSize - Number of items per page
18406
18469
  * @param pageNumber - Which page to retrieve (1-based)
@@ -18409,28 +18472,29 @@ class ModelQueryBuilder {
18409
18472
  *
18410
18473
  * @example
18411
18474
  * ```typescript
18412
- * // Subscribe to first page of main recipes
18413
- * const initialPage = await Recipe.query(perspective)
18414
- * .where({ category: "Main" })
18415
- * .paginateSubscribe(10, 1, page => {
18416
- * // This callback will be called when the page content changes
18417
- * console.log("Updated page:", page.results);
18418
- * });
18475
+ * const builder = Recipe.query(perspective)
18476
+ * .where({ category: "Main" });
18477
+ *
18478
+ * const initialPage = await builder.paginateSubscribe(10, 1, page => {
18479
+ * console.log("Updated page:", page.results);
18480
+ * });
18419
18481
  *
18420
- * // initialPage contains the current page state
18421
- * console.log("Initial page:", initialPage.results);
18482
+ * // When done with subscription:
18483
+ * builder.dispose();
18422
18484
  * ```
18423
18485
  */
18424
18486
  async paginateSubscribe(pageSize, pageNumber, callback) {
18487
+ // Clean up any existing subscription
18488
+ this.dispose();
18425
18489
  const paginationQuery = { ...(this.queryParams || {}), limit: pageSize, offset: pageSize * (pageNumber - 1), count: true };
18426
18490
  const prologQuery = await this.ctor.queryToProlog(this.perspective, paginationQuery, this.modelClassName);
18427
- const subscription = await this.perspective.subscribeInfer(prologQuery);
18491
+ this.currentSubscription = await this.perspective.subscribeInfer(prologQuery);
18428
18492
  const processResults = async (r) => {
18429
18493
  const { results, totalCount } = (await this.ctor.instancesFromPrologResult(this.perspective, this.queryParams, r));
18430
18494
  callback({ results, totalCount, pageSize, pageNumber });
18431
18495
  };
18432
- subscription.onResult(processResults);
18433
- const { results, totalCount } = (await this.ctor.instancesFromPrologResult(this.perspective, paginationQuery, subscription.result));
18496
+ this.currentSubscription.onResult(processResults);
18497
+ const { results, totalCount } = (await this.ctor.instancesFromPrologResult(this.perspective, paginationQuery, this.currentSubscription.result));
18434
18498
  return { results, totalCount, pageSize, pageNumber };
18435
18499
  }
18436
18500
  }