File tree 11 files changed +97
-0
lines changed
11 files changed +97
-0
lines changed Original file line number Diff line number Diff line change @@ -147,6 +147,7 @@ module.exports = {
147
147
[ "composable/misc/vmodel" , "vModel" ] ,
148
148
[ "composable/misc/injectFactory" , "injectFactory" ] ,
149
149
[ "composable/misc/lockScroll" , "lockScroll" ] ,
150
+ [ "composable/misc/refDebounced" , "refDebounced" ] ,
150
151
] ,
151
152
} ,
152
153
{
Original file line number Diff line number Diff line change @@ -82,6 +82,7 @@ Check out the [examples folder](examples) or start hacking on [codesandbox](http
82
82
- [ injectFactory] ( composable/misc/injectFactory ) - same as [ inject] ( https://vue-composition-api-rfc.netlify.app/api.html#dependency-injection ) but allows you to have a factory as default value
83
83
- [ interval] ( composable/misc/interval ) - self-remove ` setInterval ` on unmount
84
84
- [ lockScroll] ( composable/misc/lockScroll ) - ` lock-scroll ` component
85
+ - [ refDebounced] ( composable/misc/refDebounced ) - debounces the update value of a ` ref `
85
86
86
87
### Storage
87
88
Original file line number Diff line number Diff line change
1
+ # refDebounced
2
+
3
+ > debounces the update value of a ` ref `
4
+
5
+ ## Parameters
6
+
7
+ ``` js
8
+ import { refDebounced } from " vue-composable" ;
9
+
10
+ refDebounced (delay);
11
+ refDebounced (value, delay);
12
+ ```
13
+
14
+ | Parameters | Type | Required | Default | Description |
15
+ | ---------- | -------- | -------- | ----------- | -------------- |
16
+ | ` delay ` | ` number ` | ` true ` | | debounce delay |
17
+ | ` value ` | ` T ` | ` false ` | ` undefined ` | initial value |
18
+
19
+ ## State
20
+
21
+ The ` refDebounced ` function retuns a ` ref ` :
22
+
23
+ ``` js
24
+ import { refDebounced } from " vue-composable" ;
25
+
26
+ const debouncedValue = refDebounced ();
27
+ ```
28
+
29
+ | State | Type | Description |
30
+ | -------------- | -------- | --------------- |
31
+ | debouncedValue | ` Ref<T> ` | debounced ` ref ` |
Original file line number Diff line number Diff line change @@ -81,6 +81,7 @@ Check our [documentation](https://pikax.me/vue-composable/)
81
81
- [ injectFactory] ( https://pikax.me/vue-composable/composable/misc/injectFactory ) - same as [ inject] ( https://vue-composition-api-rfc.netlify.app/api.html#dependency-injection ) but allows you to have a factory as default value
82
82
- [ interval] ( https://pikax.me/vue-composable/composable/misc/interval ) - self-remove ` setInterval ` on unmount
83
83
- [ lockScroll] ( https://pikax.me/vue-composable/composable/misc/lockScroll ) - ` lock-scroll ` component
84
+ - [ refDebounced] ( https://pikax.me/vue-composable/composable/misc/refDebounced ) - debounces the update value of a ` ref `
84
85
85
86
### Storage
86
87
Original file line number Diff line number Diff line change
1
+ import { refDebounced } from "../../src" ;
2
+
3
+ describe ( "refDebounced" , ( ) => {
4
+ beforeAll ( ( ) => {
5
+ jest . useFakeTimers ( ) ;
6
+ } ) ;
7
+ afterAll ( ( ) => {
8
+ jest . useRealTimers ( ) ;
9
+ } ) ;
10
+ it ( "should work" , ( ) => {
11
+ const r = refDebounced ( "a" , 1 ) ;
12
+
13
+ expect ( r . value ) . toBe ( "a" ) ;
14
+
15
+ r . value = "b" ;
16
+
17
+ // should not been updated yet
18
+ expect ( r . value ) . toBe ( "a" ) ;
19
+
20
+ jest . runTimersToTime ( 2 ) ;
21
+ expect ( r . value ) . toBe ( "b" ) ;
22
+ } ) ;
23
+
24
+ it ( "should allow use only a delay" , ( ) => {
25
+ const r = refDebounced ( 1 ) ;
26
+ expect ( r . value ) . toBe ( undefined ) ;
27
+
28
+ r . value = 1 ;
29
+
30
+ // should not been updated yet
31
+ expect ( r . value ) . toBe ( undefined ) ;
32
+
33
+ jest . runTimersToTime ( 2 ) ;
34
+ expect ( r . value ) . toBe ( 1 ) ;
35
+ } ) ;
36
+ } ) ;
Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ export {
21
21
ComputedRef ,
22
22
toRaw ,
23
23
UnwrapRef , // Plugin,
24
+ customRef ,
24
25
} from "@vue/composition-api" ;
25
26
export { VueConstructor as App } from "vue" ;
26
27
Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ export {
25
25
readonly ,
26
26
toRaw ,
27
27
DeepReadonly ,
28
+ customRef ,
28
29
} from "@vue/runtime-core" ;
29
30
30
31
// istanbul ignore next
Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ export {
25
25
readonly ,
26
26
toRaw ,
27
27
DeepReadonly ,
28
+ customRef ,
28
29
} from "@vue/runtime-core" ;
29
30
30
31
// istanbul ignore next
Original file line number Diff line number Diff line change @@ -4,3 +4,4 @@ export * from "./lockScroll";
4
4
export * from "./vmodel" ;
5
5
export * from "./injectFactory" ;
6
6
export * from "./interval" ;
7
+ export * from "./refDebounced" ;
Original file line number Diff line number Diff line change
1
+ import { Ref , customRef } from "../api" ;
2
+ import { debounce } from "../debounce" ;
3
+ import { isNumber } from "../utils" ;
4
+
5
+ export function refDebounced < T > ( delay : number ) : Ref < T | undefined > ;
6
+ export function refDebounced < T > ( value : T , delay : number ) : Ref < T | undefined > ;
7
+ export function refDebounced < T > ( value : T | number , delay ?: number ) : Ref < T > {
8
+ let [ v , d ] =
9
+ arguments . length === 1 && isNumber ( value )
10
+ ? [ undefined , value ]
11
+ : [ value , delay ] ;
12
+ return customRef < T > ( ( track , trigger ) => ( {
13
+ get ( ) {
14
+ track ( ) ;
15
+ return v as any ;
16
+ } ,
17
+ set : debounce ( ( val ) => {
18
+ v = val ;
19
+ trigger ( ) ;
20
+ } , d ) ,
21
+ } ) ) ;
22
+ }
Original file line number Diff line number Diff line change @@ -81,6 +81,7 @@ Check our [documentation](https://pikax.me/vue-composable/)
81
81
- [ injectFactory] ( https://pikax.me/vue-composable/composable/misc/injectFactory ) - same as [ inject] ( https://vue-composition-api-rfc.netlify.app/api.html#dependency-injection ) but allows you to have a factory as default value
82
82
- [ interval] ( https://pikax.me/vue-composable/composable/misc/interval ) - self-remove ` setInterval ` on unmount
83
83
- [ lockScroll] ( https://pikax.me/vue-composable/composable/misc/lockScroll ) - ` lock-scroll ` component
84
+ - [ refDebounced] ( https://pikax.me/vue-composable/composable/misc/refDebounced ) - debounces the update value of a ` ref `
84
85
85
86
### Storage
86
87
You can’t perform that action at this time.
0 commit comments