@spoosh/plugin-throttle 0.1.2 → 0.1.3
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 +4 -5
- package/dist/index.js +4 -12
- package/dist/index.mjs +4 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -16,11 +16,10 @@ npm install @spoosh/plugin-throttle
|
|
|
16
16
|
import { Spoosh } from "@spoosh/core";
|
|
17
17
|
import { throttlePlugin } from "@spoosh/plugin-throttle";
|
|
18
18
|
|
|
19
|
-
const client = new Spoosh<ApiSchema, Error>("/api")
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
]);
|
|
19
|
+
const client = new Spoosh<ApiSchema, Error>("/api").use([
|
|
20
|
+
// ...otherPlugins,
|
|
21
|
+
throttlePlugin(),
|
|
22
|
+
]);
|
|
24
23
|
|
|
25
24
|
const { data } = useRead((api) => api.expensive.$get(), { throttle: 1000 });
|
|
26
25
|
```
|
package/dist/index.js
CHANGED
|
@@ -36,24 +36,16 @@ function throttlePlugin() {
|
|
|
36
36
|
if (!throttleMs || throttleMs <= 0) {
|
|
37
37
|
return next();
|
|
38
38
|
}
|
|
39
|
-
const {
|
|
39
|
+
const { path, method } = context;
|
|
40
|
+
const stableKey = `${path.join("/")}:${method}`;
|
|
40
41
|
const now = Date.now();
|
|
41
|
-
const lastTime = lastFetchTime.get(
|
|
42
|
+
const lastTime = lastFetchTime.get(stableKey) ?? 0;
|
|
42
43
|
const elapsed = now - lastTime;
|
|
43
44
|
if (elapsed < throttleMs) {
|
|
44
|
-
const cached = context.stateManager.getCache(queryKey);
|
|
45
|
-
if (cached?.state?.data !== void 0) {
|
|
46
|
-
return { data: cached.state.data, status: 200 };
|
|
47
|
-
}
|
|
48
45
|
return { data: void 0, status: 0 };
|
|
49
46
|
}
|
|
50
|
-
lastFetchTime.set(
|
|
47
|
+
lastFetchTime.set(stableKey, now);
|
|
51
48
|
return next();
|
|
52
|
-
},
|
|
53
|
-
lifecycle: {
|
|
54
|
-
onUnmount(context) {
|
|
55
|
-
lastFetchTime.delete(context.queryKey);
|
|
56
|
-
}
|
|
57
49
|
}
|
|
58
50
|
};
|
|
59
51
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -10,24 +10,16 @@ function throttlePlugin() {
|
|
|
10
10
|
if (!throttleMs || throttleMs <= 0) {
|
|
11
11
|
return next();
|
|
12
12
|
}
|
|
13
|
-
const {
|
|
13
|
+
const { path, method } = context;
|
|
14
|
+
const stableKey = `${path.join("/")}:${method}`;
|
|
14
15
|
const now = Date.now();
|
|
15
|
-
const lastTime = lastFetchTime.get(
|
|
16
|
+
const lastTime = lastFetchTime.get(stableKey) ?? 0;
|
|
16
17
|
const elapsed = now - lastTime;
|
|
17
18
|
if (elapsed < throttleMs) {
|
|
18
|
-
const cached = context.stateManager.getCache(queryKey);
|
|
19
|
-
if (cached?.state?.data !== void 0) {
|
|
20
|
-
return { data: cached.state.data, status: 200 };
|
|
21
|
-
}
|
|
22
19
|
return { data: void 0, status: 0 };
|
|
23
20
|
}
|
|
24
|
-
lastFetchTime.set(
|
|
21
|
+
lastFetchTime.set(stableKey, now);
|
|
25
22
|
return next();
|
|
26
|
-
},
|
|
27
|
-
lifecycle: {
|
|
28
|
-
onUnmount(context) {
|
|
29
|
-
lastFetchTime.delete(context.queryKey);
|
|
30
|
-
}
|
|
31
23
|
}
|
|
32
24
|
};
|
|
33
25
|
}
|