@spoosh/plugin-debounce 0.1.4 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -8
- package/dist/index.d.mts +7 -13
- package/dist/index.d.ts +7 -13
- package/dist/index.js +1 -9
- package/dist/index.mjs +1 -9
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -20,13 +20,13 @@ const client = new Spoosh<ApiSchema, Error>("/api").use([debouncePlugin()]);
|
|
|
20
20
|
|
|
21
21
|
// Wait 300ms after typing stops before fetching
|
|
22
22
|
const { data } = useRead(
|
|
23
|
-
(api) => api.
|
|
23
|
+
(api) => api("search").GET({ query: { q: searchTerm } }),
|
|
24
24
|
{ debounce: 300 }
|
|
25
25
|
);
|
|
26
26
|
|
|
27
27
|
// Conditional debounce - only debounce when search query changes
|
|
28
28
|
const { data } = useRead(
|
|
29
|
-
(api) => api.
|
|
29
|
+
(api) => api("search").GET({ query: { q: searchTerm, page } }),
|
|
30
30
|
{ debounce: ({ prevQuery }) => (prevQuery?.q !== searchTerm ? 300 : 0) }
|
|
31
31
|
);
|
|
32
32
|
```
|
|
@@ -43,9 +43,7 @@ const { data } = useRead(
|
|
|
43
43
|
|
|
44
44
|
When using a function, you receive:
|
|
45
45
|
|
|
46
|
-
| Property
|
|
47
|
-
|
|
|
48
|
-
| `prevQuery`
|
|
49
|
-
| `
|
|
50
|
-
| `prevParams` | `object` | Previous path parameters |
|
|
51
|
-
| `prevFormData` | `object` | Previous form data |
|
|
46
|
+
| Property | Type | Description |
|
|
47
|
+
| ------------ | -------- | ------------------------- |
|
|
48
|
+
| `prevQuery` | `object` | Previous query parameters |
|
|
49
|
+
| `prevParams` | `object` | Previous path parameters |
|
package/dist/index.d.mts
CHANGED
|
@@ -3,19 +3,13 @@ import { SpooshPlugin } from '@spoosh/core';
|
|
|
3
3
|
type PrevQueryField<TQuery> = [TQuery] extends [never] ? object : {
|
|
4
4
|
prevQuery?: TQuery;
|
|
5
5
|
};
|
|
6
|
-
type PrevBodyField<TBody> = [TBody] extends [never] ? object : {
|
|
7
|
-
prevBody?: TBody;
|
|
8
|
-
};
|
|
9
6
|
type PrevParamsField<TParams> = [TParams] extends [never] ? object : {
|
|
10
7
|
prevParams?: TParams;
|
|
11
8
|
};
|
|
12
|
-
type
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
type
|
|
16
|
-
type DebounceFn<TQuery = never, TBody = never, TParams = never, TFormData = never> = (context: DebounceContext<TQuery, TBody, TParams, TFormData>) => number;
|
|
17
|
-
type DebounceValue<TQuery = never, TBody = never, TParams = never, TFormData = never> = number | DebounceFn<TQuery, TBody, TParams, TFormData>;
|
|
18
|
-
type RequestAwareDebounceFn = DebounceValue<never, never, never, never>;
|
|
9
|
+
type DebounceContext<TQuery = never, TParams = never> = PrevQueryField<TQuery> & PrevParamsField<TParams>;
|
|
10
|
+
type DebounceFn<TQuery = never, TParams = never> = (context: DebounceContext<TQuery, TParams>) => number;
|
|
11
|
+
type DebounceValue<TQuery = never, TParams = never> = number | DebounceFn<TQuery, TParams>;
|
|
12
|
+
type RequestAwareDebounceFn = DebounceValue<never, never>;
|
|
19
13
|
interface DebounceReadOptions {
|
|
20
14
|
/**
|
|
21
15
|
* Debounce requests by X milliseconds. Waits for inactivity before fetching.
|
|
@@ -29,7 +23,7 @@ type DebounceReadResult = object;
|
|
|
29
23
|
type DebounceWriteResult = object;
|
|
30
24
|
declare module "@spoosh/core" {
|
|
31
25
|
interface PluginResolvers<TContext> {
|
|
32
|
-
debounce: DebounceValue<TContext["input"]["query"], TContext["input"]["
|
|
26
|
+
debounce: DebounceValue<TContext["input"]["query"], TContext["input"]["params"]> | undefined;
|
|
33
27
|
}
|
|
34
28
|
}
|
|
35
29
|
|
|
@@ -52,12 +46,12 @@ declare module "@spoosh/core" {
|
|
|
52
46
|
* ]);
|
|
53
47
|
*
|
|
54
48
|
* // Debounce search by 300ms
|
|
55
|
-
* useRead((api) => api.
|
|
49
|
+
* useRead((api) => api("search").GET({ query: { q: searchTerm } }), {
|
|
56
50
|
* debounce: 300,
|
|
57
51
|
* });
|
|
58
52
|
*
|
|
59
53
|
* // Dynamic debounce based on previous query
|
|
60
|
-
* useRead((api) => api.
|
|
54
|
+
* useRead((api) => api("search").GET({ query: { q: searchTerm } }), {
|
|
61
55
|
* debounce: ({ prevQuery }) => prevQuery?.q ? 300 : 0,
|
|
62
56
|
* });
|
|
63
57
|
* ```
|
package/dist/index.d.ts
CHANGED
|
@@ -3,19 +3,13 @@ import { SpooshPlugin } from '@spoosh/core';
|
|
|
3
3
|
type PrevQueryField<TQuery> = [TQuery] extends [never] ? object : {
|
|
4
4
|
prevQuery?: TQuery;
|
|
5
5
|
};
|
|
6
|
-
type PrevBodyField<TBody> = [TBody] extends [never] ? object : {
|
|
7
|
-
prevBody?: TBody;
|
|
8
|
-
};
|
|
9
6
|
type PrevParamsField<TParams> = [TParams] extends [never] ? object : {
|
|
10
7
|
prevParams?: TParams;
|
|
11
8
|
};
|
|
12
|
-
type
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
type
|
|
16
|
-
type DebounceFn<TQuery = never, TBody = never, TParams = never, TFormData = never> = (context: DebounceContext<TQuery, TBody, TParams, TFormData>) => number;
|
|
17
|
-
type DebounceValue<TQuery = never, TBody = never, TParams = never, TFormData = never> = number | DebounceFn<TQuery, TBody, TParams, TFormData>;
|
|
18
|
-
type RequestAwareDebounceFn = DebounceValue<never, never, never, never>;
|
|
9
|
+
type DebounceContext<TQuery = never, TParams = never> = PrevQueryField<TQuery> & PrevParamsField<TParams>;
|
|
10
|
+
type DebounceFn<TQuery = never, TParams = never> = (context: DebounceContext<TQuery, TParams>) => number;
|
|
11
|
+
type DebounceValue<TQuery = never, TParams = never> = number | DebounceFn<TQuery, TParams>;
|
|
12
|
+
type RequestAwareDebounceFn = DebounceValue<never, never>;
|
|
19
13
|
interface DebounceReadOptions {
|
|
20
14
|
/**
|
|
21
15
|
* Debounce requests by X milliseconds. Waits for inactivity before fetching.
|
|
@@ -29,7 +23,7 @@ type DebounceReadResult = object;
|
|
|
29
23
|
type DebounceWriteResult = object;
|
|
30
24
|
declare module "@spoosh/core" {
|
|
31
25
|
interface PluginResolvers<TContext> {
|
|
32
|
-
debounce: DebounceValue<TContext["input"]["query"], TContext["input"]["
|
|
26
|
+
debounce: DebounceValue<TContext["input"]["query"], TContext["input"]["params"]> | undefined;
|
|
33
27
|
}
|
|
34
28
|
}
|
|
35
29
|
|
|
@@ -52,12 +46,12 @@ declare module "@spoosh/core" {
|
|
|
52
46
|
* ]);
|
|
53
47
|
*
|
|
54
48
|
* // Debounce search by 300ms
|
|
55
|
-
* useRead((api) => api.
|
|
49
|
+
* useRead((api) => api("search").GET({ query: { q: searchTerm } }), {
|
|
56
50
|
* debounce: 300,
|
|
57
51
|
* });
|
|
58
52
|
*
|
|
59
53
|
* // Dynamic debounce based on previous query
|
|
60
|
-
* useRead((api) => api.
|
|
54
|
+
* useRead((api) => api("search").GET({ query: { q: searchTerm } }), {
|
|
61
55
|
* debounce: ({ prevQuery }) => prevQuery?.q ? 300 : 0,
|
|
62
56
|
* });
|
|
63
57
|
* ```
|
package/dist/index.js
CHANGED
|
@@ -48,9 +48,7 @@ function debouncePlugin() {
|
|
|
48
48
|
const opts = requestOptions;
|
|
49
49
|
const currentRequest = {
|
|
50
50
|
query: opts?.query,
|
|
51
|
-
params: opts?.params
|
|
52
|
-
body: opts?.body,
|
|
53
|
-
formData: opts?.formData
|
|
51
|
+
params: opts?.params
|
|
54
52
|
};
|
|
55
53
|
const prevRequest = prevRequests.get(stableKey);
|
|
56
54
|
const prevContext = {};
|
|
@@ -60,12 +58,6 @@ function debouncePlugin() {
|
|
|
60
58
|
if (prevRequest?.params !== void 0) {
|
|
61
59
|
prevContext.prevParams = prevRequest.params;
|
|
62
60
|
}
|
|
63
|
-
if (prevRequest?.body !== void 0) {
|
|
64
|
-
prevContext.prevBody = prevRequest.body;
|
|
65
|
-
}
|
|
66
|
-
if (prevRequest?.formData !== void 0) {
|
|
67
|
-
prevContext.prevFormData = prevRequest.formData;
|
|
68
|
-
}
|
|
69
61
|
const debounceMs = resolveDebounceMs(debounceOption, prevContext);
|
|
70
62
|
prevRequests.set(stableKey, currentRequest);
|
|
71
63
|
if (!debounceMs || debounceMs <= 0) {
|
package/dist/index.mjs
CHANGED
|
@@ -22,9 +22,7 @@ function debouncePlugin() {
|
|
|
22
22
|
const opts = requestOptions;
|
|
23
23
|
const currentRequest = {
|
|
24
24
|
query: opts?.query,
|
|
25
|
-
params: opts?.params
|
|
26
|
-
body: opts?.body,
|
|
27
|
-
formData: opts?.formData
|
|
25
|
+
params: opts?.params
|
|
28
26
|
};
|
|
29
27
|
const prevRequest = prevRequests.get(stableKey);
|
|
30
28
|
const prevContext = {};
|
|
@@ -34,12 +32,6 @@ function debouncePlugin() {
|
|
|
34
32
|
if (prevRequest?.params !== void 0) {
|
|
35
33
|
prevContext.prevParams = prevRequest.params;
|
|
36
34
|
}
|
|
37
|
-
if (prevRequest?.body !== void 0) {
|
|
38
|
-
prevContext.prevBody = prevRequest.body;
|
|
39
|
-
}
|
|
40
|
-
if (prevRequest?.formData !== void 0) {
|
|
41
|
-
prevContext.prevFormData = prevRequest.formData;
|
|
42
|
-
}
|
|
43
35
|
const debounceMs = resolveDebounceMs(debounceOption, prevContext);
|
|
44
36
|
prevRequests.set(stableKey, currentRequest);
|
|
45
37
|
if (!debounceMs || debounceMs <= 0) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spoosh/plugin-debounce",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Request debouncing plugin for Spoosh - waits for inactivity before fetching",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -33,11 +33,11 @@
|
|
|
33
33
|
}
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@spoosh/core": ">=0.
|
|
36
|
+
"@spoosh/core": ">=0.6.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@spoosh/
|
|
40
|
-
"@spoosh/
|
|
39
|
+
"@spoosh/core": "0.6.0",
|
|
40
|
+
"@spoosh/test-utils": "0.1.5"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
43
|
"dev": "tsup --watch",
|