Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/model.mixins.pager/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]...@profiscience/[email protected]) (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/[email protected]...@profiscience/[email protected]) (2020-10-09)

**Note:** Version bump only for package @profiscience/knockout-contrib-model-mixins-pager
Expand Down
20 changes: 12 additions & 8 deletions packages/model.mixins.pager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export function PagerMixin<
property: keyof TData,
strategy: PaginationStrategy<TParams> = ((page: number) => ({
page,
})) as PaginationStrategy<any>
})) as PaginationStrategy<any>,
primeSecondPage: boolean = false
) {
return <
T extends new (...args: any[]) => DataModelConstructorBuilder<
Expand All @@ -47,34 +48,36 @@ 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<boolean> {
this.loading(true)
const { done } = await this[PAGER].next()
this.loading(false)
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<void> {
let page = 2
let page = 2 // Page 1 already loaded by base model

Object.assign(this.params, strategy(page))

Expand All @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion packages/model.mixins.pager/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down