|
| 1 | +--- |
| 2 | +id: DeduplicatedLoadSubset |
| 3 | +title: DeduplicatedLoadSubset |
| 4 | +--- |
| 5 | + |
| 6 | +# Class: DeduplicatedLoadSubset |
| 7 | + |
| 8 | +Defined in: [packages/db/src/query/subset-dedupe.ts:34](https://github.com/TanStack/db/blob/main/packages/db/src/query/subset-dedupe.ts#L34) |
| 9 | + |
| 10 | +Deduplicated wrapper for a loadSubset function. |
| 11 | +Tracks what data has been loaded and avoids redundant calls by applying |
| 12 | +subset logic to predicates. |
| 13 | + |
| 14 | +## Param |
| 15 | + |
| 16 | +The options for the DeduplicatedLoadSubset |
| 17 | + |
| 18 | +## Param |
| 19 | + |
| 20 | +The underlying loadSubset function to wrap |
| 21 | + |
| 22 | +## Param |
| 23 | + |
| 24 | +An optional callback function that is invoked when a loadSubset call is deduplicated. |
| 25 | + If the call is deduplicated because the requested data is being loaded by an inflight request, |
| 26 | + then this callback is invoked when the inflight request completes successfully and the data is fully loaded. |
| 27 | + This callback is useful if you need to track rows per query, in which case you can't ignore deduplicated calls |
| 28 | + because you need to know which rows were loaded for each query. |
| 29 | + |
| 30 | +## Example |
| 31 | + |
| 32 | +```ts |
| 33 | +const dedupe = new DeduplicatedLoadSubset({ loadSubset: myLoadSubset, onDeduplicate: (opts) => console.log(`Call was deduplicated:`, opts) }) |
| 34 | + |
| 35 | +// First call - fetches data |
| 36 | +await dedupe.loadSubset({ where: gt(ref('age'), val(10)) }) |
| 37 | + |
| 38 | +// Second call - subset of first, returns true immediately |
| 39 | +await dedupe.loadSubset({ where: gt(ref('age'), val(20)) }) |
| 40 | + |
| 41 | +// Clear state to start fresh |
| 42 | +dedupe.reset() |
| 43 | +``` |
| 44 | + |
| 45 | +## Constructors |
| 46 | + |
| 47 | +### Constructor |
| 48 | + |
| 49 | +```ts |
| 50 | +new DeduplicatedLoadSubset(opts): DeduplicatedLoadSubset; |
| 51 | +``` |
| 52 | + |
| 53 | +Defined in: [packages/db/src/query/subset-dedupe.ts:67](https://github.com/TanStack/db/blob/main/packages/db/src/query/subset-dedupe.ts#L67) |
| 54 | + |
| 55 | +#### Parameters |
| 56 | + |
| 57 | +##### opts |
| 58 | + |
| 59 | +###### loadSubset |
| 60 | + |
| 61 | +(`options`) => `true` \| `Promise`\<`void`\> |
| 62 | + |
| 63 | +###### onDeduplicate? |
| 64 | + |
| 65 | +(`options`) => `void` |
| 66 | + |
| 67 | +#### Returns |
| 68 | + |
| 69 | +`DeduplicatedLoadSubset` |
| 70 | + |
| 71 | +## Methods |
| 72 | + |
| 73 | +### loadSubset() |
| 74 | + |
| 75 | +```ts |
| 76 | +loadSubset(options): true | Promise<void>; |
| 77 | +``` |
| 78 | + |
| 79 | +Defined in: [packages/db/src/query/subset-dedupe.ts:85](https://github.com/TanStack/db/blob/main/packages/db/src/query/subset-dedupe.ts#L85) |
| 80 | + |
| 81 | +Load a subset of data, with automatic deduplication based on previously |
| 82 | +loaded predicates and in-flight requests. |
| 83 | + |
| 84 | +This method is auto-bound, so it can be safely passed as a callback without |
| 85 | +losing its `this` context (e.g., `loadSubset: dedupe.loadSubset` in a sync config). |
| 86 | + |
| 87 | +#### Parameters |
| 88 | + |
| 89 | +##### options |
| 90 | + |
| 91 | +[`LoadSubsetOptions`](../../type-aliases/LoadSubsetOptions.md) |
| 92 | + |
| 93 | +The predicate options (where, orderBy, limit) |
| 94 | + |
| 95 | +#### Returns |
| 96 | + |
| 97 | +`true` \| `Promise`\<`void`\> |
| 98 | + |
| 99 | +true if data is already loaded, or a Promise that resolves when data is loaded |
| 100 | + |
| 101 | +*** |
| 102 | + |
| 103 | +### reset() |
| 104 | + |
| 105 | +```ts |
| 106 | +reset(): void; |
| 107 | +``` |
| 108 | + |
| 109 | +Defined in: [packages/db/src/query/subset-dedupe.ts:198](https://github.com/TanStack/db/blob/main/packages/db/src/query/subset-dedupe.ts#L198) |
| 110 | + |
| 111 | +Reset all tracking state. |
| 112 | +Clears the history of loaded predicates and in-flight calls. |
| 113 | +Use this when you want to start fresh, for example after clearing the underlying data store. |
| 114 | + |
| 115 | +Note: Any in-flight requests will still complete, but they will not update the tracking |
| 116 | +state after the reset. This prevents old requests from repopulating cleared state. |
| 117 | + |
| 118 | +#### Returns |
| 119 | + |
| 120 | +`void` |
0 commit comments