Skip to content

Commit c6c70bb

Browse files
author
余聪
committed
chore: use usePersistFn
1 parent dca64ad commit c6c70bb

File tree

12 files changed

+165
-18
lines changed

12 files changed

+165
-18
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@ should be named as `use.{{lowerCase}}`, eg. `@rp/use.i18n`.
5353
## Packages
5454

5555
- [@rcp/c.loadingwrapper](packages/c.loadingwrapper) - A component for easy create loading mask
56-
- [@rcp/c.prompt](packages/c.prompt) - Advanced React router prompt support beforeunload
56+
- [@rcp/c.keepalive](packages/c.keepalive) - Keep react component view / store when switched view.
5757
- [@rcp/hoc.i18n](packages/hoc.i18n) - React Component's high order component about internationalization
5858
- [@rcp/hoc.mount](packages/hoc.mount) - The high order component for mounting component
59-
- [@rcp/c.keepalive](packages/c.keepalive) - Keep react component view / store when switched view.
6059
- [@rcp/hoc.uncontrolled](packages/hoc.uncontrolled) - The high order component for creating uncontrolled component
60+
- [@rcp/c.prompt](packages/c.prompt) - Advanced React router prompt support beforeunload
61+
- [@rcp/use.i18n](packages/use.i18n) - A react hook for using i18n
6162
- [@rcp/use.i18ncontext](packages/use.i18ncontext) - A react hook for using i18n provider/consumer
63+
- [@rcp/use.persistfn](packages/use.persistfn) - use persist fn
6264
- [@rcp/use.uncontrolled](packages/use.uncontrolled) - Make props.value piped to state, and exposes `onChange`, make react component is **uncontrolled & controlled**
63-
- [@rcp/use.i18n](packages/use.i18n) - A react hook for using i18n
6465
- [@rcp/use.valuesstate](packages/use.valuesstate) - The useful methods exported for values state
6566
- [@rcp/util.createlogger](packages/util.createlogger) - Create namespace isomorphic logger
6667
- [@rcp/util.createmount](packages/util.createmount) - The utility for creating mountable view

packages/use.persistfn/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
**/*.js
2+
!jest.config.js

packages/use.persistfn/.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
index.ts
2+
index.tsx

packages/use.persistfn/License

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) 余聪 <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

packages/use.persistfn/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# @rcp/use.persistfn
2+
3+
[![NPM version](https://img.shields.io/npm/v/@rcp/use.persistfn.svg?style=flat-square)](https://www.npmjs.com/package/@rcp/use.persistfn)
4+
[![NPM Downloads](https://img.shields.io/npm/dm/@rcp/use.persistfn.svg?style=flat-square&maxAge=43200)](https://www.npmjs.com/package/@rcp/use.persistfn)
5+
6+
use persist fn
7+
8+
## Installation
9+
10+
```bash
11+
npm install @rcp/use.persistfn
12+
# or use yarn
13+
yarn add @rcp/use.persistfn
14+
```
15+
16+
## Usage
17+
18+
```javascript
19+
import usePersistfn from '@rcp/use.persistfn'
20+
```
21+
22+
## API
23+
24+
## Related
25+
26+
## Authors
27+
28+
This library is written and maintained by 余聪, <a href="mailto:[email protected]">[email protected]</a>.
29+
30+
## License
31+
32+
MIT
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @file spec
3+
* @author 余聪
4+
* @description
5+
*/
6+
import usePersistfn from '../'
7+
8+
describe('usePersistfn', function() {
9+
it('should ', () => {})
10+
})

packages/use.persistfn/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @file index.ts
3+
* @author 余聪
4+
*
5+
*/
6+
7+
import { useRef } from 'react'
8+
9+
type Noop = (...args: any[]) => any
10+
const noop: Noop = () => {}
11+
12+
// @ts-ignore
13+
function usePersistFn<T extends Noop>(fn?: T = noop) {
14+
const fnRef = useRef<T>(fn)
15+
fnRef.current = fn
16+
17+
const persistFn = useRef<T>()
18+
if (!persistFn.current) {
19+
persistFn.current = function(...args) {
20+
// @ts-ignore
21+
return fnRef.current!.apply(this, args)
22+
} as T
23+
}
24+
25+
return persistFn.current!
26+
}
27+
28+
export default usePersistFn

packages/use.persistfn/jest.config.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @file jest.config
3+
* @author imcuttle <[email protected]>
4+
* @date 2019/4/17
5+
*
6+
*/
7+
const base = require('../../jest.config.base.js')
8+
9+
module.exports = {
10+
...base,
11+
name: require('./package').name,
12+
displayName: require('./package').name,
13+
testMatch: [`${__dirname}/__tests__/**/*.{spec,test}.ts{x,}`],
14+
rootDir: '../..'
15+
}

packages/use.persistfn/package.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "@rcp/use.persistfn",
3+
"author": "余聪",
4+
"description": "use persist fn",
5+
"publishConfig": {
6+
"access": "public"
7+
},
8+
"license": "MIT",
9+
"module": "dist/es",
10+
"main": "dist/cjs/index.js",
11+
"typings": "dist/es/index.d.ts",
12+
"scripts": {
13+
"dist": "npm run dist:cjs && npm run dist:es",
14+
"dist:cjs": "rm -rf dist/cjs && tsc --module commonjs --outDir dist/cjs",
15+
"dist:es": "rm -rf dist/es && tsc --module ES6 --outDir dist/es",
16+
"test": "npx jest",
17+
"dev": "npm run dist -- -w",
18+
"version": "npm run dist && npm run doc",
19+
"doc": "documentation --markdown-toc=false readme dist/es/index.js -a public -s \"API\" && git add README.md"
20+
},
21+
"repository": {
22+
"directory": "packages/use.persistfn",
23+
"type": "git",
24+
"url": "git+https://github.com/imcuttle/rcp.git"
25+
},
26+
"keywords": [
27+
"余聪",
28+
"use",
29+
"persistfn",
30+
"react",
31+
"rcp"
32+
],
33+
"engines": {
34+
"node": ">=6"
35+
},
36+
"version": "1.0.0"
37+
}

packages/use.persistfn/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": ".",
5+
"jsx": "react",
6+
"sourceMap": true
7+
},
8+
"include": ["index.ts", "index.tsx", "**/*.ts", "**/*.tsx"],
9+
"exclude": ["__tests__"]
10+
}

0 commit comments

Comments
 (0)