From 7e3840ef75093aa56a6b374ec396c0c2c9afcdac Mon Sep 17 00:00:00 2001 From: Dave DeSandro Date: Fri, 1 Jan 2021 17:27:04 -0500 Subject: [PATCH] v4.0.0; ES2018 refactor; use fetch & Promise --- dist/infinite-scroll.pkgd.js | 26 ++++++++++++++++---------- dist/infinite-scroll.pkgd.min.js | 2 +- js/index.js | 2 +- package-lock.json | 2 +- package.json | 2 +- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/dist/infinite-scroll.pkgd.js b/dist/infinite-scroll.pkgd.js index 25a7001..7a3053c 100644 --- a/dist/infinite-scroll.pkgd.js +++ b/dist/infinite-scroll.pkgd.js @@ -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 @@ -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 }; } @@ -866,7 +865,7 @@ proto.loadNextPage = function() { } ); } ); - this.dispatchEvent( 'request', null, [ path ] ); + this.dispatchEvent( 'request', null, [ path, fetchPromise ] ); return fetchPromise; }; @@ -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 @@ -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; }; diff --git a/dist/infinite-scroll.pkgd.min.js b/dist/infinite-scroll.pkgd.min.js index a7bea70..1c18735 100644 --- a/dist/infinite-scroll.pkgd.min.js +++ b/dist/infinite-scroll.pkgd.min.js @@ -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 diff --git a/js/index.js b/js/index.js index 66b3d23..cab4422 100644 --- a/js/index.js +++ b/js/index.js @@ -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 diff --git a/package-lock.json b/package-lock.json index b1b77e1..1dac75d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "infinite-scroll", - "version": "4.0.0-beta.0", + "version": "4.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 841748f..4cda6a3 100644 --- a/package.json +++ b/package.json @@ -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": [