react-instantsearch-core
Advanced tools
Comparing version 6.5.0 to 6.6.0
@@ -39,2 +39,35 @@ "use strict"; | ||
} | ||
function getStateWithoutPage(state) { | ||
var _ref = state || {}, | ||
page = _ref.page, | ||
rest = (0, _objectWithoutProperties2.default)(_ref, ["page"]); | ||
return rest; | ||
} | ||
function getInMemoryCache() { | ||
var cachedHits = undefined; | ||
var cachedState = undefined; | ||
return { | ||
read: function read(_ref2) { | ||
var state = _ref2.state; | ||
return (0, _reactFastCompare.default)(cachedState, getStateWithoutPage(state)) ? cachedHits : null; | ||
}, | ||
write: function write(_ref3) { | ||
var state = _ref3.state, | ||
hits = _ref3.hits; | ||
cachedState = getStateWithoutPage(state); | ||
cachedHits = hits; | ||
} | ||
}; | ||
} | ||
function extractHitsFromCachedHits(cachedHits) { | ||
return Object.keys(cachedHits).map(Number).sort(function (a, b) { | ||
return a - b; | ||
}).reduce(function (acc, page) { | ||
return acc.concat(cachedHits[page]); | ||
}, []); | ||
} | ||
/** | ||
@@ -61,8 +94,14 @@ * InfiniteHits connector provides the logic to create connected | ||
}); | ||
this._allResults = this._allResults || []; | ||
this._prevState = this._prevState || {}; | ||
var cache = props.cache || getInMemoryCache(); | ||
if (this._cachedHits === undefined) { | ||
this._cachedHits = cache.read({ | ||
state: searchState | ||
}) || {}; | ||
} | ||
if (!results) { | ||
return { | ||
hits: [], | ||
hits: extractHitsFromCachedHits(this._cachedHits), | ||
hasPrevious: false, | ||
@@ -87,29 +126,38 @@ hasMore: false, | ||
if (this._firstReceivedPage === undefined || !(0, _reactFastCompare.default)(currentState, this._prevState)) { | ||
this._allResults = (0, _toConsumableArray2.default)(hitsWithPositionsAndQueryID); | ||
this._firstReceivedPage = page; | ||
this._lastReceivedPage = page; | ||
} else if (this._lastReceivedPage < page) { | ||
this._allResults = [].concat((0, _toConsumableArray2.default)(this._allResults), (0, _toConsumableArray2.default)(hitsWithPositionsAndQueryID)); | ||
this._lastReceivedPage = page; | ||
} else if (this._firstReceivedPage > page) { | ||
this._allResults = [].concat((0, _toConsumableArray2.default)(hitsWithPositionsAndQueryID), (0, _toConsumableArray2.default)(this._allResults)); | ||
this._firstReceivedPage = page; | ||
if (!(0, _reactFastCompare.default)(currentState, this._prevState)) { | ||
this._cachedHits = cache.read({ | ||
state: searchState | ||
}) || {}; | ||
} | ||
if (this._cachedHits[page] === undefined) { | ||
this._cachedHits[page] = hitsWithPositionsAndQueryID; | ||
cache.write({ | ||
state: searchState, | ||
hits: this._cachedHits | ||
}); | ||
} | ||
this._prevState = currentState; | ||
var hasPrevious = this._firstReceivedPage > 0; | ||
/* | ||
Math.min() and Math.max() returns Infinity or -Infinity when no argument is given. | ||
But there is always something in this point because of `this._cachedHits[page]`. | ||
*/ | ||
var firstReceivedPage = Math.min.apply(Math, (0, _toConsumableArray2.default)(Object.keys(this._cachedHits).map(Number))); | ||
var lastReceivedPage = Math.max.apply(Math, (0, _toConsumableArray2.default)(Object.keys(this._cachedHits).map(Number))); | ||
var hasPrevious = firstReceivedPage > 0; | ||
var lastPageIndex = nbPages - 1; | ||
var hasMore = page < lastPageIndex; | ||
var hasMore = lastReceivedPage < lastPageIndex; | ||
var refinePrevious = function refinePrevious(event) { | ||
return _this.refine(event, _this._firstReceivedPage - 1); | ||
return _this.refine(event, firstReceivedPage - 1); | ||
}; | ||
var refineNext = function refineNext(event) { | ||
return _this.refine(event, _this._lastReceivedPage + 1); | ||
return _this.refine(event, lastReceivedPage + 1); | ||
}; | ||
return { | ||
hits: this._allResults, | ||
hits: extractHitsFromCachedHits(this._cachedHits), | ||
hasPrevious: hasPrevious, | ||
@@ -130,4 +178,8 @@ hasMore: hasMore, | ||
refine: function refine(props, searchState, event, index) { | ||
if (index === undefined && this._lastReceivedPage !== undefined) { | ||
index = this._lastReceivedPage + 1; | ||
var pages = Object.keys(this._cachedHits || {}).map(Number); | ||
var lastReceivedPage = pages.length === 0 ? undefined : Math.max.apply(Math, (0, _toConsumableArray2.default)(pages)); // If there is no key in `this._cachedHits`, | ||
// then `lastReceivedPage` should be `undefined`. | ||
if (index === undefined && lastReceivedPage !== undefined) { | ||
index = lastReceivedPage + 1; | ||
} else if (index === undefined) { | ||
@@ -134,0 +186,0 @@ index = getCurrentRefinement(props, searchState, { |
@@ -7,3 +7,3 @@ "use strict"; | ||
exports.default = void 0; | ||
var _default = '6.5.0'; | ||
var _default = '6.6.0'; | ||
exports.default = _default; |
@@ -24,2 +24,35 @@ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; | ||
} | ||
function getStateWithoutPage(state) { | ||
var _ref = state || {}, | ||
page = _ref.page, | ||
rest = _objectWithoutProperties(_ref, ["page"]); | ||
return rest; | ||
} | ||
function getInMemoryCache() { | ||
var cachedHits = undefined; | ||
var cachedState = undefined; | ||
return { | ||
read: function read(_ref2) { | ||
var state = _ref2.state; | ||
return isEqual(cachedState, getStateWithoutPage(state)) ? cachedHits : null; | ||
}, | ||
write: function write(_ref3) { | ||
var state = _ref3.state, | ||
hits = _ref3.hits; | ||
cachedState = getStateWithoutPage(state); | ||
cachedHits = hits; | ||
} | ||
}; | ||
} | ||
function extractHitsFromCachedHits(cachedHits) { | ||
return Object.keys(cachedHits).map(Number).sort(function (a, b) { | ||
return a - b; | ||
}).reduce(function (acc, page) { | ||
return acc.concat(cachedHits[page]); | ||
}, []); | ||
} | ||
/** | ||
@@ -46,8 +79,14 @@ * InfiniteHits connector provides the logic to create connected | ||
}); | ||
this._allResults = this._allResults || []; | ||
this._prevState = this._prevState || {}; | ||
var cache = props.cache || getInMemoryCache(); | ||
if (this._cachedHits === undefined) { | ||
this._cachedHits = cache.read({ | ||
state: searchState | ||
}) || {}; | ||
} | ||
if (!results) { | ||
return { | ||
hits: [], | ||
hits: extractHitsFromCachedHits(this._cachedHits), | ||
hasPrevious: false, | ||
@@ -74,29 +113,38 @@ hasMore: false, | ||
if (this._firstReceivedPage === undefined || !isEqual(currentState, this._prevState)) { | ||
this._allResults = _toConsumableArray(hitsWithPositionsAndQueryID); | ||
this._firstReceivedPage = page; | ||
this._lastReceivedPage = page; | ||
} else if (this._lastReceivedPage < page) { | ||
this._allResults = [].concat(_toConsumableArray(this._allResults), _toConsumableArray(hitsWithPositionsAndQueryID)); | ||
this._lastReceivedPage = page; | ||
} else if (this._firstReceivedPage > page) { | ||
this._allResults = [].concat(_toConsumableArray(hitsWithPositionsAndQueryID), _toConsumableArray(this._allResults)); | ||
this._firstReceivedPage = page; | ||
if (!isEqual(currentState, this._prevState)) { | ||
this._cachedHits = cache.read({ | ||
state: searchState | ||
}) || {}; | ||
} | ||
if (this._cachedHits[page] === undefined) { | ||
this._cachedHits[page] = hitsWithPositionsAndQueryID; | ||
cache.write({ | ||
state: searchState, | ||
hits: this._cachedHits | ||
}); | ||
} | ||
this._prevState = currentState; | ||
var hasPrevious = this._firstReceivedPage > 0; | ||
/* | ||
Math.min() and Math.max() returns Infinity or -Infinity when no argument is given. | ||
But there is always something in this point because of `this._cachedHits[page]`. | ||
*/ | ||
var firstReceivedPage = Math.min.apply(Math, _toConsumableArray(Object.keys(this._cachedHits).map(Number))); | ||
var lastReceivedPage = Math.max.apply(Math, _toConsumableArray(Object.keys(this._cachedHits).map(Number))); | ||
var hasPrevious = firstReceivedPage > 0; | ||
var lastPageIndex = nbPages - 1; | ||
var hasMore = page < lastPageIndex; | ||
var hasMore = lastReceivedPage < lastPageIndex; | ||
var refinePrevious = function refinePrevious(event) { | ||
return _this.refine(event, _this._firstReceivedPage - 1); | ||
return _this.refine(event, firstReceivedPage - 1); | ||
}; | ||
var refineNext = function refineNext(event) { | ||
return _this.refine(event, _this._lastReceivedPage + 1); | ||
return _this.refine(event, lastReceivedPage + 1); | ||
}; | ||
return { | ||
hits: this._allResults, | ||
hits: extractHitsFromCachedHits(this._cachedHits), | ||
hasPrevious: hasPrevious, | ||
@@ -117,4 +165,8 @@ hasMore: hasMore, | ||
refine: function refine(props, searchState, event, index) { | ||
if (index === undefined && this._lastReceivedPage !== undefined) { | ||
index = this._lastReceivedPage + 1; | ||
var pages = Object.keys(this._cachedHits || {}).map(Number); | ||
var lastReceivedPage = pages.length === 0 ? undefined : Math.max.apply(Math, _toConsumableArray(pages)); // If there is no key in `this._cachedHits`, | ||
// then `lastReceivedPage` should be `undefined`. | ||
if (index === undefined && lastReceivedPage !== undefined) { | ||
index = lastReceivedPage + 1; | ||
} else if (index === undefined) { | ||
@@ -121,0 +173,0 @@ index = getCurrentRefinement(props, searchState, { |
@@ -1,1 +0,1 @@ | ||
export default '6.5.0'; | ||
export default '6.6.0'; |
{ | ||
"name": "react-instantsearch-core", | ||
"version": "6.5.0", | ||
"version": "6.6.0", | ||
"description": "⚡ Lightning-fast search for React, by Algolia", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2059458
19802