* Preload the collection data by starting sync if not already started * Multiple concurrent calls will share the same promise
()
| 384 | * Multiple concurrent calls will share the same promise |
| 385 | */ |
| 386 | public preload(): Promise<void> { |
| 387 | if (this.preloadPromise) { |
| 388 | return this.preloadPromise |
| 389 | } |
| 390 | |
| 391 | // Warn when calling preload on an on-demand collection |
| 392 | if (this.syncMode === `on-demand`) { |
| 393 | console.warn( |
| 394 | `${this.id ? `[${this.id}] ` : ``}Calling .preload() on a collection with syncMode "on-demand" is a no-op. ` + |
| 395 | `In on-demand mode, data is only loaded when queries request it. ` + |
| 396 | `Instead, create a live query and call .preload() on that to load the specific data you need. ` + |
| 397 | `See https://tanstack.com/blog/tanstack-db-0.5-query-driven-sync for more details.`, |
| 398 | ) |
| 399 | } |
| 400 | |
| 401 | this.preloadPromise = new Promise<void>((resolve, reject) => { |
| 402 | if (this.lifecycle.status === `ready`) { |
| 403 | resolve() |
| 404 | return |
| 405 | } |
| 406 | |
| 407 | if (this.lifecycle.status === `error`) { |
| 408 | reject(new CollectionIsInErrorStateError()) |
| 409 | return |
| 410 | } |
| 411 | |
| 412 | // Register callback BEFORE starting sync to avoid race condition |
| 413 | this.lifecycle.onFirstReady(() => { |
| 414 | resolve() |
| 415 | }) |
| 416 | |
| 417 | // Start sync if collection hasn't started yet or was cleaned up |
| 418 | if ( |
| 419 | this.lifecycle.status === `idle` || |
| 420 | this.lifecycle.status === `cleaned-up` |
| 421 | ) { |
| 422 | try { |
| 423 | this.startSync() |
| 424 | } catch (error) { |
| 425 | reject(error) |
| 426 | return |
| 427 | } |
| 428 | } |
| 429 | }) |
| 430 | |
| 431 | return this.preloadPromise |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Gets whether the collection is currently loading more data |
nothing calls this directly
no test coverage detected