diff --git a/packages/model.mixins.pager/CHANGELOG.md b/packages/model.mixins.pager/CHANGELOG.md index 6f7688d3..5bc17779 100644 --- a/packages/model.mixins.pager/CHANGELOG.md +++ b/packages/model.mixins.pager/CHANGELOG.md @@ -3,6 +3,16 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [3.0.0](https://github.com/Profiscience/knockout-contrib/compare/@profiscience/knockout-contrib-model-mixins-pager@2.1.11...@profiscience/knockout-contrib-model-mixins-pager@3.0.0) (2025-05-05) + +### ✅ Summary of Changes: + +- **Added** a `primeSecondPage` optional parameter to `PagerMixin`, defaulting to `false`. +- **Modified** the `PRIME_PAGER` method to conditionally fetch page 2 based on `primeSecondPage`. +- **Updated** `constructor` and `update()` to pass `primeSecondPage` to `PRIME_PAGER`. +- **⚠️ Breaking Change**: Previously, page 2 was always loaded during initialization. This change **defers the loading of page 2** unless `primeSecondPage` is explicitly set to `true`. + → **This is designed to reduce server load and improve perceived performance** on first load. + ## [2.1.11](https://github.com/Profiscience/knockout-contrib/compare/@profiscience/knockout-contrib-model-mixins-pager@2.1.10...@profiscience/knockout-contrib-model-mixins-pager@2.1.11) (2020-10-09) **Note:** Version bump only for package @profiscience/knockout-contrib-model-mixins-pager diff --git a/packages/model.mixins.pager/index.ts b/packages/model.mixins.pager/index.ts index bea60e34..e3a205c1 100644 --- a/packages/model.mixins.pager/index.ts +++ b/packages/model.mixins.pager/index.ts @@ -27,7 +27,8 @@ export function PagerMixin< property: keyof TData, strategy: PaginationStrategy = ((page: number) => ({ page, - })) as PaginationStrategy + })) as PaginationStrategy, + primeSecondPage: boolean = false ) { return < T extends new (...args: any[]) => DataModelConstructorBuilder< @@ -47,18 +48,18 @@ export function PagerMixin< Object.assign(args[0], strategy(1)) super(...args) - this[PAGER] = this[CREATE_PAGER]() + this[PAGER] = this[CREATE_PAGER]() // Setup pager generator this.hasMore = ko.observable(true) - // ensure method is called with correct "this" value this.getMore = this.getMore.bind(this) const initialized = this[INITIALIZED] - this[INITIALIZED] = initialized - // @TODO unchain this when proper error handling is implemented - .then(() => this[PRIME_PAGER]()) + this[INITIALIZED] = primeSecondPage + ? initialized.then(() => this[PRIME_PAGER]()) + : initialized } + /** Manually fetch the next page of data */ public async getMore(): Promise { this.loading(true) const { done } = await this[PAGER].next() @@ -66,15 +67,17 @@ export function PagerMixin< return !done } + /** Resets pager and reloads starting from page 1 */ protected update() { Object.assign(this.params, strategy(1)) this[PAGER] = this[CREATE_PAGER]() const p = super.update() - return p.then(() => this[PRIME_PAGER]()) + return p.then(() => primeSecondPage ? this[PRIME_PAGER]() : undefined) } + /** Generator to yield control between page loads */ private async *[CREATE_PAGER](): AsyncIterableIterator { - let page = 2 + let page = 2 // Page 1 already loaded by base model Object.assign(this.params, strategy(page)) @@ -98,6 +101,7 @@ export function PagerMixin< } while (this.hasMore()) } + /** Loads the first yield from the pager (usually page 2) */ private async [PRIME_PAGER]() { await this[PAGER].next() } diff --git a/packages/model.mixins.pager/package.json b/packages/model.mixins.pager/package.json index 6598e03e..088ee6cc 100644 --- a/packages/model.mixins.pager/package.json +++ b/packages/model.mixins.pager/package.json @@ -1,6 +1,6 @@ { "name": "@profiscience/knockout-contrib-model-mixins-pager", - "version": "2.1.11", + "version": "3.0.0", "license": "WTFPL", "author": "Casey Webb (https://caseyWebb.xyz)", "homepage": "https://profiscience.github.io/knockout-contrib/packages/model.mixins.pager",