Skip to content

Commit

Permalink
[FEATURE] Add support for preContent, onRequestNextPage hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcemira committed May 27, 2024
1 parent 1dce72b commit 8b612bd
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 37 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/push-dist.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

name: Push dist

on:
push:
branches:
- main
- master

jobs:
push-dist:
name: Push dist
runs-on: ubuntu-latest
permissions:
contents: write

steps:
- uses: actions/checkout@v4
- uses: NullVoxPopuli/[email protected]
- uses: kategengler/[email protected]
with:
branch: dist
token: ${{ secrets.GITHUB_TOKEN }}
working-directory: ember-infinity
1 change: 1 addition & 0 deletions ember-infinity/src/components/infinity-loader.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
' reached-infinity'
}}"
data-test-infinity-loader
...attributes
>
{{#if (has-block)}}
{{yield this.infinityModelContent}}
Expand Down
29 changes: 29 additions & 0 deletions ember-infinity/src/lib/infinity-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,35 @@ export default class InfinityModel extends DEFAULTS(addEvented(ArrayProxy)) {
return objectAssign(pageParams, this.extraParams);
}

/**
* This hook is useful when you have the need to initialize the infinity model
* with some data. For example, if you want to load the first page of data using ember-fastboot shoebox
* you can do something like this:
*
* onRequestNextPage (infinityModel, modelName, params) {
* if (this.afterRehydration) {
this.set('afterRehydration', false)
this.set('preContent.meta', this.meta)
return Promise.resolve(this.preContent);
* }
*
* return infinityModel.store[infinityModel.storeFindMethod](
* modelName,
* params
* )
* }
*
* Where `this.content` is the data you want to initialize the infinity model with,
* previously stored in the shoebox, deserialized and pushed to the store, peeked in this case.
* With this, you can avoid the first request to the server because we already have the data.
*
* @method onRequestNextPage
* @param {Ember.ArrayProxy} infinityModel (self)
* @param {String} modelName
* @param {Object} params
* @return {Ember.RSVP.Promise} A Promise that resolves the new objects
*/

/**
abstract after-model hook, can be overridden in subclasses
Used to keep shape for optimization
Expand Down
30 changes: 30 additions & 0 deletions ember-infinity/src/services/infinity.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,24 @@ export default class Infinity extends Service {
infinityModelForParamsCheck
);

// preContent, content, and meta are OPTIONAL, These allow to initialize
// the infinity model with a pre-existing array of objects, the best use case
// so far would be the data on a shoebox, coming from ember-fastBoot. This way, we
// avoid having a second request to the server, and we also maintain the logic
// of the infinity model, the current content, the meta, which makes requesting
// the next page, or the previous page, or any other action, CONSISTENT.

const preContent = A(paramsCheck(
'preContent',
options,
infinityModelForParamsCheck
)) || A()
const meta = paramsCheck(
'meta',
options,
infinityModelForParamsCheck
) || {}

// create identifier for use in storing unique cached infinity model
let identifier = stringifyObjectValues(options);

Expand All @@ -316,6 +334,8 @@ export default class Infinity extends Service {
delete options.infinityCache;
delete options.store;
delete options.storeFindMethod;
delete options.preContent;
delete options.meta;

const initParams = {
container: getOwner(this),
Expand All @@ -331,6 +351,8 @@ export default class Infinity extends Service {
store,
storeFindMethod,
content: A(),
preContent,
meta
};

for (let key in initParams) {
Expand Down Expand Up @@ -493,6 +515,14 @@ export default class Infinity extends Service {
const modelName = infinityModel._infinityModelName;
const params = infinityModel.buildParams(increment);

if (infinityModel.onRequestNextPage) {
return infinityModel.onRequestNextPage(
infinityModel,
modelName,
params
);
}

return infinityModel.store[infinityModel.storeFindMethod](
modelName,
params
Expand Down
5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,5 @@
},
"engines": {
"node": ">= 18"
},
"pnpm": {
"patchedDependencies": {
"[email protected]": "patches/[email protected]"
}
}
}
69 changes: 37 additions & 32 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8b612bd

Please sign in to comment.