Skip to content

Commit

Permalink
v4.0.0; ES2018 refactor; use fetch & Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
desandro committed Jan 1, 2021
1 parent 82910c6 commit 7e3840e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
26 changes: 16 additions & 10 deletions dist/infinite-scroll.pkgd.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Infinite Scroll PACKAGED v4.0.0-beta.0
* Infinite Scroll PACKAGED v4.0.0
* Automatically add next page
*
* Licensed GPLv3 for open source use
Expand Down Expand Up @@ -844,11 +844,10 @@ proto.loadNextPage = function() {
this.isLoading = true;
if ( typeof fetchOptions == 'function' ) fetchOptions = fetchOptions();

// TODO add fetch options
let fetchPromise = fetch( path, fetchOptions ).then( ( response ) => {
if ( !response.ok ) {
let error = new Error( response.statusText );
this.onPageError( error, path );
this.onPageError( error, path, response );
return { response };
}

Expand All @@ -866,7 +865,7 @@ proto.loadNextPage = function() {
} );
} );

this.dispatchEvent( 'request', null, [ path ] );
this.dispatchEvent( 'request', null, [ path, fetchPromise ] );

return fetchPromise;
};
Expand All @@ -883,18 +882,25 @@ proto.onPageLoad = function( body, path, response ) {
};

proto.appendNextPage = function( body, path, response ) {
let { append, responseBody } = this.options;
let { append, responseBody, domParseResponse } = this.options;
// do not append json
let isDocument = responseBody == 'text';
let isDocument = responseBody == 'text' && domParseResponse;
if ( !isDocument || !append ) return { body, response };

let items = body.querySelectorAll( append );
let promiseValue = { body, response, items };
// last page hit if no items. #840
if ( !items || !items.length ) {
this.lastPageReached( body, path );
return promiseValue;
}

let fragment = getItemsFragment( items );
let appendReady = () => {
this.appendItems( items, fragment );
this.isLoading = false;
this.dispatchEvent( 'append', null, [ body, path, items ] );
return { body, response, items };
this.dispatchEvent( 'append', null, [ body, path, items, response ] );
return promiseValue;
};

// TODO add hook for option to trigger appendReady
Expand Down Expand Up @@ -1000,10 +1006,10 @@ proto.lastPageReached = function( body, path ) {

// ----- error ----- //

proto.onPageError = function( error, path ) {
proto.onPageError = function( error, path, response ) {
this.isLoading = false;
this.canLoad = false;
this.dispatchEvent( 'error', null, [ error, path ] );
this.dispatchEvent( 'error', null, [ error, path, response ] );
return error;
};

Expand Down
2 changes: 1 addition & 1 deletion dist/infinite-scroll.pkgd.min.js

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

2 changes: 1 addition & 1 deletion js/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Infinite Scroll v4.0.0-beta.0
* Infinite Scroll v4.0.0
* Automatically add next page
*
* Licensed GPLv3 for open source use
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "infinite-scroll",
"version": "4.0.0-beta.0",
"version": "4.0.0",
"description": "Automatically add next page",
"main": "js/index.js",
"files": [
Expand Down

0 comments on commit 7e3840e

Please sign in to comment.