@pallasoft/ps-core-components 2.21.0 → 2.22.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/dist/esm/hooks/use-ps-change-queue.js +31 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/interfaces/ps-change-queue-input.js +1 -0
- package/dist/esm/interfaces/ps-change-queue-result.js +1 -0
- package/dist/hooks/use-ps-change-queue.d.ts +3 -0
- package/dist/hooks/use-ps-change-queue.js +35 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/interfaces/ps-change-queue-input.d.ts +5 -0
- package/dist/interfaces/ps-change-queue-input.js +2 -0
- package/dist/interfaces/ps-change-queue-result.d.ts +3 -0
- package/dist/interfaces/ps-change-queue-result.js +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef } from 'react';
|
|
2
|
+
export const usePsChangeQueue = ({ onDrained, onFailed, send, }) => {
|
|
3
|
+
const pending = useRef([]);
|
|
4
|
+
const isRunning = useRef(false);
|
|
5
|
+
const latest = useRef({ onDrained, onFailed, send });
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
latest.current = { onDrained, onFailed, send };
|
|
8
|
+
}, [onDrained, onFailed, send]);
|
|
9
|
+
const run = useCallback(async () => {
|
|
10
|
+
if (isRunning.current)
|
|
11
|
+
return;
|
|
12
|
+
isRunning.current = true;
|
|
13
|
+
await Promise.resolve();
|
|
14
|
+
while (pending.current.length > 0) {
|
|
15
|
+
const batch = pending.current;
|
|
16
|
+
pending.current = [];
|
|
17
|
+
await latest.current.send(batch).catch(() => {
|
|
18
|
+
const dropped = pending.current;
|
|
19
|
+
pending.current = [];
|
|
20
|
+
latest.current.onFailed(batch, dropped);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
isRunning.current = false;
|
|
24
|
+
latest.current.onDrained();
|
|
25
|
+
}, []);
|
|
26
|
+
const enqueue = useCallback((change) => {
|
|
27
|
+
pending.current = [...pending.current, change];
|
|
28
|
+
void run();
|
|
29
|
+
}, [run]);
|
|
30
|
+
return { enqueue };
|
|
31
|
+
};
|
package/dist/esm/index.js
CHANGED
|
@@ -56,6 +56,9 @@ export * from './models/ps-element-size';
|
|
|
56
56
|
export * from './models/ps-voice-orb';
|
|
57
57
|
export * from './models/ps-navigation-progress';
|
|
58
58
|
export * from './hooks/use-ps-debounced-value';
|
|
59
|
+
export * from './hooks/use-ps-change-queue';
|
|
60
|
+
export * from './interfaces/ps-change-queue-input';
|
|
61
|
+
export * from './interfaces/ps-change-queue-result';
|
|
59
62
|
export * from './hooks/use-ps-optimistic-rows';
|
|
60
63
|
export * from './interfaces/ps-optimistic-rows-input';
|
|
61
64
|
export * from './interfaces/ps-optimistic-rows-result';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { PsChangeQueueInput } from '../interfaces/ps-change-queue-input';
|
|
2
|
+
import type { PsChangeQueueResult } from '../interfaces/ps-change-queue-result';
|
|
3
|
+
export declare const usePsChangeQueue: <Change>({ onDrained, onFailed, send, }: PsChangeQueueInput<Change>) => PsChangeQueueResult<Change>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.usePsChangeQueue = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const usePsChangeQueue = ({ onDrained, onFailed, send, }) => {
|
|
6
|
+
const pending = (0, react_1.useRef)([]);
|
|
7
|
+
const isRunning = (0, react_1.useRef)(false);
|
|
8
|
+
const latest = (0, react_1.useRef)({ onDrained, onFailed, send });
|
|
9
|
+
(0, react_1.useEffect)(() => {
|
|
10
|
+
latest.current = { onDrained, onFailed, send };
|
|
11
|
+
}, [onDrained, onFailed, send]);
|
|
12
|
+
const run = (0, react_1.useCallback)(async () => {
|
|
13
|
+
if (isRunning.current)
|
|
14
|
+
return;
|
|
15
|
+
isRunning.current = true;
|
|
16
|
+
await Promise.resolve();
|
|
17
|
+
while (pending.current.length > 0) {
|
|
18
|
+
const batch = pending.current;
|
|
19
|
+
pending.current = [];
|
|
20
|
+
await latest.current.send(batch).catch(() => {
|
|
21
|
+
const dropped = pending.current;
|
|
22
|
+
pending.current = [];
|
|
23
|
+
latest.current.onFailed(batch, dropped);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
isRunning.current = false;
|
|
27
|
+
latest.current.onDrained();
|
|
28
|
+
}, []);
|
|
29
|
+
const enqueue = (0, react_1.useCallback)((change) => {
|
|
30
|
+
pending.current = [...pending.current, change];
|
|
31
|
+
void run();
|
|
32
|
+
}, [run]);
|
|
33
|
+
return { enqueue };
|
|
34
|
+
};
|
|
35
|
+
exports.usePsChangeQueue = usePsChangeQueue;
|
package/dist/index.d.ts
CHANGED
|
@@ -56,6 +56,9 @@ export * from './models/ps-element-size';
|
|
|
56
56
|
export * from './models/ps-voice-orb';
|
|
57
57
|
export * from './models/ps-navigation-progress';
|
|
58
58
|
export * from './hooks/use-ps-debounced-value';
|
|
59
|
+
export * from './hooks/use-ps-change-queue';
|
|
60
|
+
export * from './interfaces/ps-change-queue-input';
|
|
61
|
+
export * from './interfaces/ps-change-queue-result';
|
|
59
62
|
export * from './hooks/use-ps-optimistic-rows';
|
|
60
63
|
export * from './interfaces/ps-optimistic-rows-input';
|
|
61
64
|
export * from './interfaces/ps-optimistic-rows-result';
|
package/dist/index.js
CHANGED
|
@@ -72,6 +72,9 @@ __exportStar(require("./models/ps-element-size"), exports);
|
|
|
72
72
|
__exportStar(require("./models/ps-voice-orb"), exports);
|
|
73
73
|
__exportStar(require("./models/ps-navigation-progress"), exports);
|
|
74
74
|
__exportStar(require("./hooks/use-ps-debounced-value"), exports);
|
|
75
|
+
__exportStar(require("./hooks/use-ps-change-queue"), exports);
|
|
76
|
+
__exportStar(require("./interfaces/ps-change-queue-input"), exports);
|
|
77
|
+
__exportStar(require("./interfaces/ps-change-queue-result"), exports);
|
|
75
78
|
__exportStar(require("./hooks/use-ps-optimistic-rows"), exports);
|
|
76
79
|
__exportStar(require("./interfaces/ps-optimistic-rows-input"), exports);
|
|
77
80
|
__exportStar(require("./interfaces/ps-optimistic-rows-result"), exports);
|