Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ykforerlang/repure
Memoized selector library for Redux
https://github.com/ykforerlang/repure
memoized-selectors redux reselect
Last synced: about 2 months ago
JSON representation
Memoized selector library for Redux
- Host: GitHub
- URL: https://github.com/ykforerlang/repure
- Owner: ykforerlang
- Created: 2018-03-10T02:28:02.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-03-11T15:27:11.000Z (almost 7 years ago)
- Last Synced: 2024-03-23T10:41:42.261Z (10 months ago)
- Topics: memoized-selectors, redux, reselect
- Language: JavaScript
- Homepage:
- Size: 12.7 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# repure
[中文文档](./README_zh.md)A replacement for [reselect](https://github.com/reactjs/reselect.git) 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.### Get Start
**install** :`npm install repure````javascript
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```
### API
repure provide 3 API: repureCreator(cacheSize, equalityCheck), repure, repureOneCacheCreator(equalityCheck)#### repureCreator(cacheSize, 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:
```javascript
function defaultEqualityCheck(a, b) {
return a === b
}
```Another common equalityCheck is shallowEqual: `import { shallowEqual } from 'repure'`
#### repure
In most cases, you should use this method. cacheSize = 1 and equalityCheck is `===`. the same with `createSelector` provided by [reselct](https://github.com/reactjs/reselect.git)#### repureOneCacheCreator(equalityCheck)
repureOneCacheCreator is optimized version of repureCreator when cacheSize is 1.Also provide a method of batch operation: batchRepure(obj, repure)
```javascript
import * as fobj from './xx' // file export funcconst fobj2 = batchRepure(obj, repure)
```### Composing
```javascript
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)
```