A replacement for reselect and providing a more simple, more natural way of writing.
Same parameters will get same result, so the function which wrapped by repure
will cache the result. If the same parameter is transmitted next time, the cached results will be used.
install :npm install repure
import repure from 'repure'
function f(a, b) {
console.log('invoke f, will takes 5 seconds')
return a + b
}
const rf = repure(f)
rf(1, 1) // takes 5 seconds
rf(1, 1) // takes 0 seconds
rf(1, 1) // takes 0 seconds
rf(1, 1) // takes 0 seconds
rf(2, 2) // takes 5 seconds
repure provide 3 API: repureCreator(cacheSize, equalityCheck), repure, repureOneCacheCreator(equalityCheck)
the result of repureCreator is a repure and the wrapped functions could cache cacheSize results. we will use equalityCheck to compare whether or not the parameters are equal.
Default equalityCheck:
function defaultEqualityCheck(a, b) {
return a === b
}
Another common equalityCheck is shallowEqual: import { shallowEqual } from 'repure'
In most cases, you should use this method. cacheSize = 1 and equalityCheck is ===
. the same with createSelector
provided by reselct
repureOneCacheCreator is optimized version of repureCreator when cacheSize is 1.
Also provide a method of batch operation: batchRepure(obj, repure)
import * as fobj from './xx' // file export func
const fobj2 = batchRepure(obj, repure)
function f(a, b) {
...
}
const rf = repure(f)
function g(a, b, c){
const r = rf(a, b)
return r * c
}
const rg = repure(g)
rg(1, 1, 2)
rg(1, 1, 2)
rg(1, 1, 3)