Add loadSubset State Tracking and On-Demand Sync Mode #669
Mergedsamwillis(GitHub profile, opens in a new tab)merged 15 commits into
main fromrefs/pull/669/headmerged by samwillis(GitHub profile, opens in a new tab) samwillis(GitHub profile, opens in a new tab) changed the title
- @@ -242,9 +300,18 @@300+242301 /**243302 * Requests the sync layer to load more data.244303 * @param options Options to control what data is being loaded245304 * @returns If data loading is asynchronous, this method returns a promise that resolves when the data is loaded.246305 * If data loading is synchronous, the data is loaded when the method returns.306+ * Returns undefined if no sync function is configured or if syncMode is 'eager'.247307 */248- public syncMore(options: OnLoadMoreOptions): void | Promise<void> {249- if (this.syncOnLoadMoreFn) {250- return this.syncOnLoadMoreFn(options)308+ public loadSubset(options: LoadSubsetOptions): Promise<void> | undefined {309+ // Bypass loadSubset when syncMode is 'eager'310+ if (this.syncMode === `eager`) {311+ return undefined312+ }313+314+ if (this.syncLoadSubsetFn) {315+ const result = this.syncLoadSubsetFn(options)316+317+ // If the result is void (synchronous), wrap in Promise.resolve()packages/db/tests/query/live-query-collection.test.ts line 1014Outdated@@ -939,0 +994,21 @@994+ sync: {995+ sync: ({ markReady, begin, write, commit }) => {996+ begin()997+ write({ type: `insert`, value: { id: `1`, value: 1 } })998+ commit()999+ markReady()1000+ return {1001+ loadSubset: () => loadSubsetPromise,1002+ }1003+ },1004+ },1005+ })1006+1007+ const liveQuery = createLiveQueryCollection({1008+ query: (q) => q.from({ item: sourceCollection }),1009+ startSync: true,1010+ })1011+1012+ await liveQuery.preload()1013+1014+ // Calling loadSubset directly on source collection sets its own isLoadingMore
kevin-dp(GitHub profile, opens in a new tab) approved these changes
@@ -239,3 +256,20 @@239256 return this.preloadPromise240257 }241258259+ /**260+ * Gets whether the collection is currently loading more data261+ */262+ public get isLoadingMore(): boolean {263+ return this.pendingLoadSubsetPromises.size > 0264+ }265+266+ /**267+ * Tracks a load promise for isLoadingMore state.268+ * @internal This is for internal coordination (e.g., live-query glue code), not for general use.269+ */270+ public trackLoadPromise(promise: Promise<void>): void {271+ const wasLoading = this.isLoadingMore272+ this.pendingLoadSubsetPromises.add(promise)273+ const isLoadingNow = this.isLoadingMore274+275+ if (!wasLoading && isLoadingNow) {@@ -241,0 +259,21 @@259+ /**260+ * Gets whether the collection is currently loading more data261+ */262+ public get isLoadingMore(): boolean {263+ return this.pendingLoadSubsetPromises.size > 0264+ }265+266+ /**267+ * Tracks a load promise for isLoadingMore state.268+ * @internal This is for internal coordination (e.g., live-query glue code), not for general use.269+ */270+ public trackLoadPromise(promise: Promise<void>): void {271+ const wasLoading = this.isLoadingMore272+ this.pendingLoadSubsetPromises.add(promise)273+ const isLoadingNow = this.isLoadingMore274+275+ if (!wasLoading && isLoadingNow) {276+ this._events.emit(`loadingMore:change`, {277+ type: `loadingMore:change`,278+ collection: this.collection,279+ isLoadingMore: true,@@ -241,0 +270,21 @@270+ public trackLoadPromise(promise: Promise<void>): void {271+ const wasLoading = this.isLoadingMore272+ this.pendingLoadSubsetPromises.add(promise)273+ const isLoadingNow = this.isLoadingMore274+275+ if (!wasLoading && isLoadingNow) {276+ this._events.emit(`loadingMore:change`, {277+ type: `loadingMore:change`,278+ collection: this.collection,279+ isLoadingMore: true,280+ previousIsLoadingMore: false,281+ })282+ }283+284+ promise.finally(() => {285+ // Check loading state BEFORE removing the promise286+ const wasLoadingBeforeRemoval = this.isLoadingMore287+ this.pendingLoadSubsetPromises.delete(promise)288+ const stillLoading = this.isLoadingMore289+290+ if (wasLoadingBeforeRemoval && !stillLoading) {@@ -241,0 +274,21 @@274+275+ if (!wasLoading && isLoadingNow) {276+ this._events.emit(`loadingMore:change`, {277+ type: `loadingMore:change`,278+ collection: this.collection,279+ isLoadingMore: true,280+ previousIsLoadingMore: false,281+ })282+ }283+284+ promise.finally(() => {285+ // Check loading state BEFORE removing the promise286+ const wasLoadingBeforeRemoval = this.isLoadingMore287+ this.pendingLoadSubsetPromises.delete(promise)288+ const stillLoading = this.isLoadingMore289+290+ if (wasLoadingBeforeRemoval && !stillLoading) {291+ this._events.emit(`loadingMore:change`, {292+ type: `loadingMore:change`,293+ collection: this.collection,294+ isLoadingMore: false,packages/db/src/collection/index.ts line 372@@ -364,9 +372,6 @@364372 }365373366374 /**367- * Requests the sync layer to load more data.368- * @param options Options to control what data is being loaded369- * @returns If data loading is asynchronous, this method returns a promise that resolves when the data is loaded.370- * If data loading is synchronous, the data is loaded when the method returns.375+ * Tracks a load promise for isLoadingMore state.376+ * @internal This is for internal coordination (e.g., live-query glue code), not for general use.371377 */372- public syncMore(options: OnLoadMoreOptions): void | Promise<void> {packages/db/src/collection/subscription.ts line 183Outdated@@ -121,7 +174,10 @@121174122175 // Request the sync layer to load more data123176 // don't await it, we will load the data into the collection when it comes in124- this.collection.syncMore({177+ const syncResult = this.collection._sync.loadSubset({125178 where: stateOpts.where,179+ subscription: this,126180 })127181182+ // Track the promise if it's actually a promise (async work)183+ if (syncResult instanceof Promise) {@@ -215,8 +282,12 @@215282216283 // Request the sync layer to load more data217284 // don't await it, we will load the data into the collection when it comes in218- this.collection.syncMore({285+ const syncResult = this.collection._sync.loadSubset({219286 where: whereWithValueFilter,220287 limit,221288 orderBy,289+ subscription: this,222290 })291+292+ // Track the promise if it's actually a promise (async work)293+ if (syncResult instanceof Promise) {@@ -313,3 +380,4 @@313380 /** Collection has been cleaned up and resources freed */314381 | `cleaned-up`315382383+/**samwillis(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: f31a67e4f6e7ee26d1f1f42df7cbd2a651909c69
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 12 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR