Skip to content

Commit 2e19a84

Browse files
author
Jaeho Lee
authored
Add rescript-use-debounce (#44)
* Add use-debounce binding * version packages
1 parent bc7bd39 commit 2e19a84

File tree

6 files changed

+171
-0
lines changed

6 files changed

+171
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@greenlabs/rescript-use-debounce": patch
3+
---
4+
5+
add rescript-use-debounce
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
**/__tests__/**
2+
.bsb.lock
3+
*.bs.js
4+
.gitattributes
5+
.github/
6+
.graphql_ppx_cache/
7+
.merlin
8+
.vscode/
9+
assets/
10+
CONTRIBUTING.md
11+
CODE_OF_CONDUCT.md
12+
documentation/
13+
documentationWebsite/
14+
docs/
15+
EXAMPLES/
16+
lib/
17+
node_modules/
18+
package-lock.json
19+
TODO
20+
yarn.lock
21+
yarn-error.log
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# `rescript-use-debounce`
2+
3+
Binding of [`use-debounce`](https://github.com/xnimorz/use-debounce) package
4+
5+
## Install
6+
7+
```bash
8+
npm i @greenlabs/rescript-use-debounce
9+
or
10+
yarn add @greenlabs/rescript-use-debounce
11+
```
12+
13+
```json
14+
"bs-dependencies": [
15+
"@greenlabs/rescript-use-debounce"
16+
]
17+
```
18+
19+
## Usage
20+
21+
```rescript
22+
let test = () => {
23+
let (debouncedCallback, debouncedCallbackMethods) = useDebouncedCallback(e => {
24+
e->ReactEvent.Synthetic.preventDefault
25+
}, 500, ~options={maxWait: 500})
26+
27+
(debouncedCallback, debouncedCallbackMethods.isPending())
28+
}
29+
```
30+
31+
## Note
32+
33+
`flush()`'s type signature is `unit => ReturnType<callback>`, but couldn't find a way to achieve it in ReScript so left the return type as unit.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "@greenlabs/rescript-use-debounce",
3+
"reason": { "react-jsx": 3 },
4+
"package-specs": {
5+
"module": "es6",
6+
"in-source": true
7+
},
8+
"suffix": ".bs.js",
9+
"sources": [
10+
{
11+
"dir": "./src",
12+
"subdirs": false
13+
}
14+
],
15+
"bsc-flags": ["-bs-no-version-header"],
16+
"warnings": {
17+
"error": true
18+
},
19+
"bs-dependencies": []
20+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "@greenlabs/rescript-use-debounce",
3+
"description": "ReScript bindings for rescript-use-debounce",
4+
"version": "0.0.0",
5+
"license": "MIT",
6+
"author": "Greenlabs Dev <developer@greenlabs.co.kr>",
7+
"scripts": {
8+
"start": "rescript build -w",
9+
"build": "rescript build -with-deps"
10+
},
11+
"keywords": [
12+
"ReScript",
13+
"rescript-use-debounce"
14+
],
15+
"publishConfig": {
16+
"access": "public"
17+
},
18+
"bugs": "https://github.com/green-labs/rescript-bindings/issues",
19+
"repository": {
20+
"type": "git",
21+
"url": "https://github.com/green-labs/rescript-bindings.git",
22+
"directory": "packages/rescript-use-debounce"
23+
},
24+
"dependencies": {
25+
"use-debounce": "^9.0.3"
26+
},
27+
"devDependencies": {},
28+
"peerDependencies": {}
29+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
type debounceOptions<'a> = {
2+
maxWait?: int,
3+
leading?: bool,
4+
trailing?: bool,
5+
equalityFn?: (. 'a, 'a) => bool,
6+
}
7+
8+
type debouncedReturn<'a> = {@as("0") value: 'a} // TODO: add second position
9+
10+
/**
11+
```rescript
12+
let debounced = useDebounce(someValue, 500)
13+
debounced.value->Js.log
14+
````
15+
*/
16+
@module("use-debounce")
17+
external useDebounce: ('a, int) => debouncedReturn<'a> = "useDebounce"
18+
19+
@module("use-debounce")
20+
external useDebounceWithOptions: ('a, int, debounceOptions<'a>) => debouncedReturn<'a> =
21+
"useDebounce"
22+
23+
type debounceCallbackOptions = {
24+
maxWait?: int,
25+
leading?: bool,
26+
trailing?: bool,
27+
}
28+
29+
@module("use-debounce")
30+
external useDebouncedCallbackOriginal: ('a, int, ~options: debounceCallbackOptions=?) => 'a =
31+
"useDebouncedCallback"
32+
33+
// FIXME: flush()'s type signature is `unit => ReturnType<callback>`, but couldn't find a way
34+
// to achieve it in ReScript so left the return type as unit.
35+
type methods = {cancel: unit => unit, isPending: unit => int, flush: unit => unit}
36+
%%private(external getMethods: 'a => methods = "%identity")
37+
38+
/**
39+
```rescript
40+
let (debouncedCallback, _debouncedCallbackMethods) = useDebouncedCallback(e => {
41+
e->ReactEvent.Synthetic.preventDefault
42+
}, 500, ~options={maxWait: 500})
43+
<input onClick={debouncedCallback} />
44+
````
45+
*/
46+
let useDebouncedCallback = (callback, timeout, ~options) => {
47+
let deboucedCallback = useDebouncedCallbackOriginal(callback, timeout, ~options)
48+
(deboucedCallback, getMethods(deboucedCallback))
49+
}
50+
51+
type throttleCallbackOptions = {
52+
leading?: bool,
53+
trailing?: bool,
54+
}
55+
56+
@module("use-debounce")
57+
external useThrottledCallbackOriginal: ('a, int, ~options: throttleCallbackOptions=?) => 'a =
58+
"useThrottledCallback"
59+
60+
let useThrottledCallback = (callback, timeout, ~options) => {
61+
let throttledCallback = useThrottledCallbackOriginal(callback, timeout, ~options)
62+
(throttledCallback, getMethods(throttledCallback))
63+
}

0 commit comments

Comments
 (0)