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

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
@@ -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,7 +4644,7 @@ 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
  */
@@ -4705,6 +4706,17 @@ class QuerySubscriptionProxy {
4705
4706
  // Start the first keepalive loop
4706
4707
  __classPrivateFieldSet$a(this, _QuerySubscriptionProxy_keepaliveTimer, setTimeout(keepaliveLoop, 30000), "f");
4707
4708
  }
4709
+ /** Get the subscription ID for this query subscription
4710
+ *
4711
+ * This is a unique identifier assigned when the subscription was created.
4712
+ * It can be used to reference this specific subscription, for example when
4713
+ * sending keepalive signals.
4714
+ *
4715
+ * @returns The subscription ID string
4716
+ */
4717
+ get id() {
4718
+ return __classPrivateFieldGet$a(this, _QuerySubscriptionProxy_subscriptionId, "f");
4719
+ }
4708
4720
  /** Promise that resolves when the subscription has received its first result
4709
4721
  * through the subscription channel. This ensures the subscription is fully
4710
4722
  * set up before allowing access to results or updates.
@@ -4768,11 +4780,16 @@ class QuerySubscriptionProxy {
4768
4780
  if (__classPrivateFieldGet$a(this, _QuerySubscriptionProxy_unsubscribe, "f")) {
4769
4781
  __classPrivateFieldGet$a(this, _QuerySubscriptionProxy_unsubscribe, "f").call(this);
4770
4782
  }
4783
+ __classPrivateFieldGet$a(this, _QuerySubscriptionProxy_callbacks, "f").clear();
4771
4784
  if (__classPrivateFieldGet$a(this, _QuerySubscriptionProxy_initTimeoutId, "f")) {
4772
4785
  clearTimeout(__classPrivateFieldGet$a(this, _QuerySubscriptionProxy_initTimeoutId, "f"));
4773
4786
  __classPrivateFieldSet$a(this, _QuerySubscriptionProxy_initTimeoutId, undefined, "f");
4774
4787
  }
4775
- __classPrivateFieldGet$a(this, _QuerySubscriptionProxy_callbacks, "f").clear();
4788
+ // Tell the backend to dispose of the subscription
4789
+ if (__classPrivateFieldGet$a(this, _QuerySubscriptionProxy_subscriptionId, "f")) {
4790
+ __classPrivateFieldGet$a(this, _QuerySubscriptionProxy_client, "f").disposeQuerySubscription(__classPrivateFieldGet$a(this, _QuerySubscriptionProxy_uuid, "f"), __classPrivateFieldGet$a(this, _QuerySubscriptionProxy_subscriptionId, "f"))
4791
+ .catch(e => console.error('Error disposing query subscription:', e));
4792
+ }
4776
4793
  }
4777
4794
  }
4778
4795
  _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) {
@@ -5581,6 +5598,10 @@ class PerspectiveProxy {
5581
5598
  * The returned subscription is guaranteed to be ready to receive updates,
5582
5599
  * as this method waits for the initialization process to complete.
5583
5600
  *
5601
+ * The subscription will be automatically cleaned up on both frontend and backend
5602
+ * when dispose() is called. Make sure to call dispose() when you're done to
5603
+ * prevent memory leaks and ensure proper cleanup of resources.
5604
+ *
5584
5605
  * @param query - Prolog query string
5585
5606
  * @returns Initialized QuerySubscriptionProxy instance
5586
5607
  *
@@ -5599,6 +5620,9 @@ class PerspectiveProxy {
5599
5620
  * subscription.onResult((todos) => {
5600
5621
  * console.log("Active todos:", todos);
5601
5622
  * });
5623
+ *
5624
+ * // Clean up subscription when done
5625
+ * subscription.dispose();
5602
5626
  * ```
5603
5627
  */
5604
5628
  async subscribeInfer(query) {
@@ -5804,6 +5828,15 @@ class PerspectiveClient {
5804
5828
  }));
5805
5829
  return perspectiveKeepAliveQuery;
5806
5830
  }
5831
+ async disposeQuerySubscription(uuid, subscriptionId) {
5832
+ const { perspectiveDisposeQuerySubscription } = unwrapApolloResult(await __classPrivateFieldGet$9(this, _PerspectiveClient_apolloClient, "f").mutate({
5833
+ mutation: gql `mutation perspectiveDisposeQuerySubscription($uuid: String!, $subscriptionId: String!) {
5834
+ perspectiveDisposeQuerySubscription(uuid: $uuid, subscriptionId: $subscriptionId)
5835
+ }`,
5836
+ variables: { uuid, subscriptionId }
5837
+ }));
5838
+ return perspectiveDisposeQuerySubscription;
5839
+ }
5807
5840
  async add(name) {
5808
5841
  const { perspectiveAdd } = unwrapApolloResult(await __classPrivateFieldGet$9(this, _PerspectiveClient_apolloClient, "f").mutate({
5809
5842
  mutation: gql `mutation perspectiveAdd($name: String!) {
@@ -16537,6 +16570,7 @@ type Mutation {
16537
16570
  perspectiveCommitBatch(batchId: String!, uuid: String!): LinkExpressionMutations!
16538
16571
  perspectiveCreateBatch(uuid: String!): String!
16539
16572
  perspectiveCreateSubject(batchId: String, expressionAddress: String!, initialValues: String, subjectClass: String!, uuid: String!): Boolean!
16573
+ perspectiveDisposeQuerySubscription(subscriptionId: String!, uuid: String!): Boolean!
16540
16574
  perspectiveExecuteCommands(batchId: String, commands: String!, expression: String!, parameters: String, uuid: String!): Boolean!
16541
16575
  perspectiveGetSubjectData(expressionAddress: String!, subjectClass: String!, uuid: String!): String!
16542
16576
  perspectiveKeepAliveQuery(subscriptionId: String!, uuid: String!): Boolean!
@@ -18143,6 +18177,24 @@ class ModelQueryBuilder {
18143
18177
  if (query)
18144
18178
  this.queryParams = query;
18145
18179
  }
18180
+ /**
18181
+ * Disposes of the current subscription if one exists.
18182
+ *
18183
+ * This method:
18184
+ * 1. Stops the keepalive signals to the subscription
18185
+ * 2. Unsubscribes from GraphQL subscription updates
18186
+ * 3. Notifies the backend to clean up subscription resources
18187
+ * 4. Clears the subscription reference
18188
+ *
18189
+ * You should call this method when you're done with a subscription
18190
+ * to prevent memory leaks and ensure proper cleanup.
18191
+ */
18192
+ dispose() {
18193
+ if (this.currentSubscription) {
18194
+ this.currentSubscription.dispose();
18195
+ this.currentSubscription = undefined;
18196
+ }
18197
+ }
18146
18198
  /**
18147
18199
  * Adds where conditions to the query.
18148
18200
  *
@@ -18283,35 +18335,36 @@ class ModelQueryBuilder {
18283
18335
  * 2. Sets up the callback to process future updates
18284
18336
  * 3. Returns the initial results immediately
18285
18337
  *
18286
- * The subscription is guaranteed to be ready when this method returns,
18287
- * as it waits for the initialization process to complete.
18338
+ * Remember to call dispose() when you're done with the subscription
18339
+ * to clean up resources.
18288
18340
  *
18289
18341
  * @param callback - Function to call with updated results
18290
18342
  * @returns Initial results array
18291
18343
  *
18292
18344
  * @example
18293
18345
  * ```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
- * });
18346
+ * const builder = Recipe.query(perspective)
18347
+ * .where({ status: "cooking" });
18301
18348
  *
18302
- * // initialRecipes contains the current state
18303
- * console.log("Initial recipes:", initialRecipes);
18349
+ * const initialRecipes = await builder.subscribe(recipes => {
18350
+ * console.log("Updated recipes:", recipes);
18351
+ * });
18352
+ *
18353
+ * // When done with subscription:
18354
+ * builder.dispose();
18304
18355
  * ```
18305
18356
  */
18306
18357
  async subscribe(callback) {
18358
+ // Clean up any existing subscription
18359
+ this.dispose();
18307
18360
  const query = await this.ctor.queryToProlog(this.perspective, this.queryParams, this.modelClassName);
18308
- const subscription = await this.perspective.subscribeInfer(query);
18361
+ this.currentSubscription = await this.perspective.subscribeInfer(query);
18309
18362
  const processResults = async (result) => {
18310
18363
  const { results } = await this.ctor.instancesFromPrologResult(this.perspective, this.queryParams, result);
18311
18364
  callback(results);
18312
18365
  };
18313
- subscription.onResult(processResults);
18314
- const { results } = await this.ctor.instancesFromPrologResult(this.perspective, this.queryParams, subscription.result);
18366
+ this.currentSubscription.onResult(processResults);
18367
+ const { results } = await this.ctor.instancesFromPrologResult(this.perspective, this.queryParams, this.currentSubscription.result);
18315
18368
  return results;
18316
18369
  }
18317
18370
  /**
@@ -18339,35 +18392,36 @@ class ModelQueryBuilder {
18339
18392
  * 2. Sets up the callback to process future count updates
18340
18393
  * 3. Returns the initial count immediately
18341
18394
  *
18342
- * The subscription is guaranteed to be ready when this method returns,
18343
- * as it waits for the initialization process to complete.
18395
+ * Remember to call dispose() when you're done with the subscription
18396
+ * to clean up resources.
18344
18397
  *
18345
18398
  * @param callback - Function to call with updated count
18346
18399
  * @returns Initial count
18347
18400
  *
18348
18401
  * @example
18349
18402
  * ```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
- * });
18403
+ * const builder = Recipe.query(perspective)
18404
+ * .where({ status: "active" });
18405
+ *
18406
+ * const initialCount = await builder.countSubscribe(count => {
18407
+ * console.log("Active items:", count);
18408
+ * });
18357
18409
  *
18358
- * // initialCount contains the current count
18359
- * console.log("Initial count:", initialCount);
18410
+ * // When done with subscription:
18411
+ * builder.dispose();
18360
18412
  * ```
18361
18413
  */
18362
18414
  async countSubscribe(callback) {
18415
+ // Clean up any existing subscription
18416
+ this.dispose();
18363
18417
  const query = await this.ctor.countQueryToProlog(this.perspective, this.queryParams, this.modelClassName);
18364
- const subscription = await this.perspective.subscribeInfer(query);
18418
+ this.currentSubscription = await this.perspective.subscribeInfer(query);
18365
18419
  const processResults = async (result) => {
18366
18420
  const newCount = result?.[0]?.TotalCount || 0;
18367
18421
  callback(newCount);
18368
18422
  };
18369
- subscription.onResult(processResults);
18370
- return subscription.result?.[0]?.TotalCount || 0;
18423
+ this.currentSubscription.onResult(processResults);
18424
+ return this.currentSubscription.result?.[0]?.TotalCount || 0;
18371
18425
  }
18372
18426
  /**
18373
18427
  * Gets a page of results with pagination metadata.
@@ -18399,8 +18453,8 @@ class ModelQueryBuilder {
18399
18453
  * 2. Sets up the callback to process future page updates
18400
18454
  * 3. Returns the initial page immediately
18401
18455
  *
18402
- * The subscription is guaranteed to be ready when this method returns,
18403
- * as it waits for the initialization process to complete.
18456
+ * Remember to call dispose() when you're done with the subscription
18457
+ * to clean up resources.
18404
18458
  *
18405
18459
  * @param pageSize - Number of items per page
18406
18460
  * @param pageNumber - Which page to retrieve (1-based)
@@ -18409,28 +18463,29 @@ class ModelQueryBuilder {
18409
18463
  *
18410
18464
  * @example
18411
18465
  * ```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
- * });
18466
+ * const builder = Recipe.query(perspective)
18467
+ * .where({ category: "Main" });
18468
+ *
18469
+ * const initialPage = await builder.paginateSubscribe(10, 1, page => {
18470
+ * console.log("Updated page:", page.results);
18471
+ * });
18419
18472
  *
18420
- * // initialPage contains the current page state
18421
- * console.log("Initial page:", initialPage.results);
18473
+ * // When done with subscription:
18474
+ * builder.dispose();
18422
18475
  * ```
18423
18476
  */
18424
18477
  async paginateSubscribe(pageSize, pageNumber, callback) {
18478
+ // Clean up any existing subscription
18479
+ this.dispose();
18425
18480
  const paginationQuery = { ...(this.queryParams || {}), limit: pageSize, offset: pageSize * (pageNumber - 1), count: true };
18426
18481
  const prologQuery = await this.ctor.queryToProlog(this.perspective, paginationQuery, this.modelClassName);
18427
- const subscription = await this.perspective.subscribeInfer(prologQuery);
18482
+ this.currentSubscription = await this.perspective.subscribeInfer(prologQuery);
18428
18483
  const processResults = async (r) => {
18429
18484
  const { results, totalCount } = (await this.ctor.instancesFromPrologResult(this.perspective, this.queryParams, r));
18430
18485
  callback({ results, totalCount, pageSize, pageNumber });
18431
18486
  };
18432
- subscription.onResult(processResults);
18433
- const { results, totalCount } = (await this.ctor.instancesFromPrologResult(this.perspective, paginationQuery, subscription.result));
18487
+ this.currentSubscription.onResult(processResults);
18488
+ const { results, totalCount } = (await this.ctor.instancesFromPrologResult(this.perspective, paginationQuery, this.currentSubscription.result));
18434
18489
  return { results, totalCount, pageSize, pageNumber };
18435
18490
  }
18436
18491
  }
package/lib/index.js CHANGED
@@ -4619,10 +4619,11 @@ var _QuerySubscriptionProxy_instances, _QuerySubscriptionProxy_uuid, _QuerySubsc
4619
4619
  * - Subscribing to query updates via GraphQL subscriptions
4620
4620
  * - Maintaining the latest query result
4621
4621
  * - Ensuring subscription is fully initialized before allowing access
4622
+ * - Cleaning up resources when disposed
4622
4623
  *
4623
4624
  * The subscription will remain active as long as keepalive signals are sent.
4624
4625
  * Make sure to call dispose() when you're done with the subscription to clean up
4625
- * resources and stop keepalive signals.
4626
+ * resources, stop keepalive signals, and notify the backend to remove the subscription.
4626
4627
  *
4627
4628
  * The subscription goes through an initialization process where it waits for the first
4628
4629
  * result to come through the subscription channel. You can await the `initialized`
@@ -4639,7 +4640,7 @@ var _QuerySubscriptionProxy_instances, _QuerySubscriptionProxy_uuid, _QuerySubsc
4639
4640
  * console.log("New result:", result);
4640
4641
  * });
4641
4642
  *
4642
- * // Later: clean up
4643
+ * // Later: clean up subscription and notify backend
4643
4644
  * subscription.dispose();
4644
4645
  * ```
4645
4646
  */
@@ -4701,6 +4702,17 @@ class QuerySubscriptionProxy {
4701
4702
  // Start the first keepalive loop
4702
4703
  __classPrivateFieldSet$a(this, _QuerySubscriptionProxy_keepaliveTimer, setTimeout(keepaliveLoop, 30000), "f");
4703
4704
  }
4705
+ /** Get the subscription ID for this query subscription
4706
+ *
4707
+ * This is a unique identifier assigned when the subscription was created.
4708
+ * It can be used to reference this specific subscription, for example when
4709
+ * sending keepalive signals.
4710
+ *
4711
+ * @returns The subscription ID string
4712
+ */
4713
+ get id() {
4714
+ return __classPrivateFieldGet$a(this, _QuerySubscriptionProxy_subscriptionId, "f");
4715
+ }
4704
4716
  /** Promise that resolves when the subscription has received its first result
4705
4717
  * through the subscription channel. This ensures the subscription is fully
4706
4718
  * set up before allowing access to results or updates.
@@ -4764,11 +4776,16 @@ class QuerySubscriptionProxy {
4764
4776
  if (__classPrivateFieldGet$a(this, _QuerySubscriptionProxy_unsubscribe, "f")) {
4765
4777
  __classPrivateFieldGet$a(this, _QuerySubscriptionProxy_unsubscribe, "f").call(this);
4766
4778
  }
4779
+ __classPrivateFieldGet$a(this, _QuerySubscriptionProxy_callbacks, "f").clear();
4767
4780
  if (__classPrivateFieldGet$a(this, _QuerySubscriptionProxy_initTimeoutId, "f")) {
4768
4781
  clearTimeout(__classPrivateFieldGet$a(this, _QuerySubscriptionProxy_initTimeoutId, "f"));
4769
4782
  __classPrivateFieldSet$a(this, _QuerySubscriptionProxy_initTimeoutId, undefined, "f");
4770
4783
  }
4771
- __classPrivateFieldGet$a(this, _QuerySubscriptionProxy_callbacks, "f").clear();
4784
+ // Tell the backend to dispose of the subscription
4785
+ if (__classPrivateFieldGet$a(this, _QuerySubscriptionProxy_subscriptionId, "f")) {
4786
+ __classPrivateFieldGet$a(this, _QuerySubscriptionProxy_client, "f").disposeQuerySubscription(__classPrivateFieldGet$a(this, _QuerySubscriptionProxy_uuid, "f"), __classPrivateFieldGet$a(this, _QuerySubscriptionProxy_subscriptionId, "f"))
4787
+ .catch(e => console.error('Error disposing query subscription:', e));
4788
+ }
4772
4789
  }
4773
4790
  }
4774
4791
  _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) {
@@ -5577,6 +5594,10 @@ class PerspectiveProxy {
5577
5594
  * The returned subscription is guaranteed to be ready to receive updates,
5578
5595
  * as this method waits for the initialization process to complete.
5579
5596
  *
5597
+ * The subscription will be automatically cleaned up on both frontend and backend
5598
+ * when dispose() is called. Make sure to call dispose() when you're done to
5599
+ * prevent memory leaks and ensure proper cleanup of resources.
5600
+ *
5580
5601
  * @param query - Prolog query string
5581
5602
  * @returns Initialized QuerySubscriptionProxy instance
5582
5603
  *
@@ -5595,6 +5616,9 @@ class PerspectiveProxy {
5595
5616
  * subscription.onResult((todos) => {
5596
5617
  * console.log("Active todos:", todos);
5597
5618
  * });
5619
+ *
5620
+ * // Clean up subscription when done
5621
+ * subscription.dispose();
5598
5622
  * ```
5599
5623
  */
5600
5624
  async subscribeInfer(query) {
@@ -5800,6 +5824,15 @@ class PerspectiveClient {
5800
5824
  }));
5801
5825
  return perspectiveKeepAliveQuery;
5802
5826
  }
5827
+ async disposeQuerySubscription(uuid, subscriptionId) {
5828
+ const { perspectiveDisposeQuerySubscription } = unwrapApolloResult(await __classPrivateFieldGet$9(this, _PerspectiveClient_apolloClient, "f").mutate({
5829
+ mutation: gql `mutation perspectiveDisposeQuerySubscription($uuid: String!, $subscriptionId: String!) {
5830
+ perspectiveDisposeQuerySubscription(uuid: $uuid, subscriptionId: $subscriptionId)
5831
+ }`,
5832
+ variables: { uuid, subscriptionId }
5833
+ }));
5834
+ return perspectiveDisposeQuerySubscription;
5835
+ }
5803
5836
  async add(name) {
5804
5837
  const { perspectiveAdd } = unwrapApolloResult(await __classPrivateFieldGet$9(this, _PerspectiveClient_apolloClient, "f").mutate({
5805
5838
  mutation: gql `mutation perspectiveAdd($name: String!) {
@@ -16533,6 +16566,7 @@ type Mutation {
16533
16566
  perspectiveCommitBatch(batchId: String!, uuid: String!): LinkExpressionMutations!
16534
16567
  perspectiveCreateBatch(uuid: String!): String!
16535
16568
  perspectiveCreateSubject(batchId: String, expressionAddress: String!, initialValues: String, subjectClass: String!, uuid: String!): Boolean!
16569
+ perspectiveDisposeQuerySubscription(subscriptionId: String!, uuid: String!): Boolean!
16536
16570
  perspectiveExecuteCommands(batchId: String, commands: String!, expression: String!, parameters: String, uuid: String!): Boolean!
16537
16571
  perspectiveGetSubjectData(expressionAddress: String!, subjectClass: String!, uuid: String!): String!
16538
16572
  perspectiveKeepAliveQuery(subscriptionId: String!, uuid: String!): Boolean!
@@ -18139,6 +18173,24 @@ class ModelQueryBuilder {
18139
18173
  if (query)
18140
18174
  this.queryParams = query;
18141
18175
  }
18176
+ /**
18177
+ * Disposes of the current subscription if one exists.
18178
+ *
18179
+ * This method:
18180
+ * 1. Stops the keepalive signals to the subscription
18181
+ * 2. Unsubscribes from GraphQL subscription updates
18182
+ * 3. Notifies the backend to clean up subscription resources
18183
+ * 4. Clears the subscription reference
18184
+ *
18185
+ * You should call this method when you're done with a subscription
18186
+ * to prevent memory leaks and ensure proper cleanup.
18187
+ */
18188
+ dispose() {
18189
+ if (this.currentSubscription) {
18190
+ this.currentSubscription.dispose();
18191
+ this.currentSubscription = undefined;
18192
+ }
18193
+ }
18142
18194
  /**
18143
18195
  * Adds where conditions to the query.
18144
18196
  *
@@ -18279,35 +18331,36 @@ class ModelQueryBuilder {
18279
18331
  * 2. Sets up the callback to process future updates
18280
18332
  * 3. Returns the initial results immediately
18281
18333
  *
18282
- * The subscription is guaranteed to be ready when this method returns,
18283
- * as it waits for the initialization process to complete.
18334
+ * Remember to call dispose() when you're done with the subscription
18335
+ * to clean up resources.
18284
18336
  *
18285
18337
  * @param callback - Function to call with updated results
18286
18338
  * @returns Initial results array
18287
18339
  *
18288
18340
  * @example
18289
18341
  * ```typescript
18290
- * // Subscribe to cooking recipes
18291
- * const initialRecipes = await Recipe.query(perspective)
18292
- * .where({ status: "cooking" })
18293
- * .subscribe(recipes => {
18294
- * // This callback will be called with future updates
18295
- * console.log("Currently cooking:", recipes);
18296
- * });
18342
+ * const builder = Recipe.query(perspective)
18343
+ * .where({ status: "cooking" });
18297
18344
  *
18298
- * // initialRecipes contains the current state
18299
- * console.log("Initial recipes:", initialRecipes);
18345
+ * const initialRecipes = await builder.subscribe(recipes => {
18346
+ * console.log("Updated recipes:", recipes);
18347
+ * });
18348
+ *
18349
+ * // When done with subscription:
18350
+ * builder.dispose();
18300
18351
  * ```
18301
18352
  */
18302
18353
  async subscribe(callback) {
18354
+ // Clean up any existing subscription
18355
+ this.dispose();
18303
18356
  const query = await this.ctor.queryToProlog(this.perspective, this.queryParams, this.modelClassName);
18304
- const subscription = await this.perspective.subscribeInfer(query);
18357
+ this.currentSubscription = await this.perspective.subscribeInfer(query);
18305
18358
  const processResults = async (result) => {
18306
18359
  const { results } = await this.ctor.instancesFromPrologResult(this.perspective, this.queryParams, result);
18307
18360
  callback(results);
18308
18361
  };
18309
- subscription.onResult(processResults);
18310
- const { results } = await this.ctor.instancesFromPrologResult(this.perspective, this.queryParams, subscription.result);
18362
+ this.currentSubscription.onResult(processResults);
18363
+ const { results } = await this.ctor.instancesFromPrologResult(this.perspective, this.queryParams, this.currentSubscription.result);
18311
18364
  return results;
18312
18365
  }
18313
18366
  /**
@@ -18335,35 +18388,36 @@ class ModelQueryBuilder {
18335
18388
  * 2. Sets up the callback to process future count updates
18336
18389
  * 3. Returns the initial count immediately
18337
18390
  *
18338
- * The subscription is guaranteed to be ready when this method returns,
18339
- * as it waits for the initialization process to complete.
18391
+ * Remember to call dispose() when you're done with the subscription
18392
+ * to clean up resources.
18340
18393
  *
18341
18394
  * @param callback - Function to call with updated count
18342
18395
  * @returns Initial count
18343
18396
  *
18344
18397
  * @example
18345
18398
  * ```typescript
18346
- * // Subscribe to active recipe count
18347
- * const initialCount = await Recipe.query(perspective)
18348
- * .where({ status: "active" })
18349
- * .countSubscribe(count => {
18350
- * // This callback will be called when the count changes
18351
- * console.log("Active items:", count);
18352
- * });
18399
+ * const builder = Recipe.query(perspective)
18400
+ * .where({ status: "active" });
18401
+ *
18402
+ * const initialCount = await builder.countSubscribe(count => {
18403
+ * console.log("Active items:", count);
18404
+ * });
18353
18405
  *
18354
- * // initialCount contains the current count
18355
- * console.log("Initial count:", initialCount);
18406
+ * // When done with subscription:
18407
+ * builder.dispose();
18356
18408
  * ```
18357
18409
  */
18358
18410
  async countSubscribe(callback) {
18411
+ // Clean up any existing subscription
18412
+ this.dispose();
18359
18413
  const query = await this.ctor.countQueryToProlog(this.perspective, this.queryParams, this.modelClassName);
18360
- const subscription = await this.perspective.subscribeInfer(query);
18414
+ this.currentSubscription = await this.perspective.subscribeInfer(query);
18361
18415
  const processResults = async (result) => {
18362
18416
  const newCount = result?.[0]?.TotalCount || 0;
18363
18417
  callback(newCount);
18364
18418
  };
18365
- subscription.onResult(processResults);
18366
- return subscription.result?.[0]?.TotalCount || 0;
18419
+ this.currentSubscription.onResult(processResults);
18420
+ return this.currentSubscription.result?.[0]?.TotalCount || 0;
18367
18421
  }
18368
18422
  /**
18369
18423
  * Gets a page of results with pagination metadata.
@@ -18395,8 +18449,8 @@ class ModelQueryBuilder {
18395
18449
  * 2. Sets up the callback to process future page updates
18396
18450
  * 3. Returns the initial page immediately
18397
18451
  *
18398
- * The subscription is guaranteed to be ready when this method returns,
18399
- * as it waits for the initialization process to complete.
18452
+ * Remember to call dispose() when you're done with the subscription
18453
+ * to clean up resources.
18400
18454
  *
18401
18455
  * @param pageSize - Number of items per page
18402
18456
  * @param pageNumber - Which page to retrieve (1-based)
@@ -18405,28 +18459,29 @@ class ModelQueryBuilder {
18405
18459
  *
18406
18460
  * @example
18407
18461
  * ```typescript
18408
- * // Subscribe to first page of main recipes
18409
- * const initialPage = await Recipe.query(perspective)
18410
- * .where({ category: "Main" })
18411
- * .paginateSubscribe(10, 1, page => {
18412
- * // This callback will be called when the page content changes
18413
- * console.log("Updated page:", page.results);
18414
- * });
18462
+ * const builder = Recipe.query(perspective)
18463
+ * .where({ category: "Main" });
18464
+ *
18465
+ * const initialPage = await builder.paginateSubscribe(10, 1, page => {
18466
+ * console.log("Updated page:", page.results);
18467
+ * });
18415
18468
  *
18416
- * // initialPage contains the current page state
18417
- * console.log("Initial page:", initialPage.results);
18469
+ * // When done with subscription:
18470
+ * builder.dispose();
18418
18471
  * ```
18419
18472
  */
18420
18473
  async paginateSubscribe(pageSize, pageNumber, callback) {
18474
+ // Clean up any existing subscription
18475
+ this.dispose();
18421
18476
  const paginationQuery = { ...(this.queryParams || {}), limit: pageSize, offset: pageSize * (pageNumber - 1), count: true };
18422
18477
  const prologQuery = await this.ctor.queryToProlog(this.perspective, paginationQuery, this.modelClassName);
18423
- const subscription = await this.perspective.subscribeInfer(prologQuery);
18478
+ this.currentSubscription = await this.perspective.subscribeInfer(prologQuery);
18424
18479
  const processResults = async (r) => {
18425
18480
  const { results, totalCount } = (await this.ctor.instancesFromPrologResult(this.perspective, this.queryParams, r));
18426
18481
  callback({ results, totalCount, pageSize, pageNumber });
18427
18482
  };
18428
- subscription.onResult(processResults);
18429
- const { results, totalCount } = (await this.ctor.instancesFromPrologResult(this.perspective, paginationQuery, subscription.result));
18483
+ this.currentSubscription.onResult(processResults);
18484
+ const { results, totalCount } = (await this.ctor.instancesFromPrologResult(this.perspective, paginationQuery, this.currentSubscription.result));
18430
18485
  return { results, totalCount, pageSize, pageNumber };
18431
18486
  }
18432
18487
  }