@pond-ts/react 0.4.2 → 0.4.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/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/useLatest.d.ts +19 -0
- package/dist/useLatest.d.ts.map +1 -0
- package/dist/useLatest.js +69 -0
- package/dist/useLatest.js.map +1 -0
- package/dist/useLiveQuery.d.ts +31 -0
- package/dist/useLiveQuery.d.ts.map +1 -0
- package/dist/useLiveQuery.js +36 -0
- package/dist/useLiveQuery.js.map +1 -0
- package/dist/useSnapshot.d.ts +24 -3
- package/dist/useSnapshot.d.ts.map +1 -1
- package/dist/useSnapshot.js +20 -6
- package/dist/useSnapshot.js.map +1 -1
- package/dist/useWindow.d.ts +2 -1
- package/dist/useWindow.d.ts.map +1 -1
- package/dist/useWindow.js +9 -6
- package/dist/useWindow.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export { useLiveSeries } from './useLiveSeries.js';
|
|
2
2
|
export { useTimeSeries } from './useTimeSeries.js';
|
|
3
|
-
export { useSnapshot, type UseSnapshotOptions } from './useSnapshot.js';
|
|
3
|
+
export { useSnapshot, type UseSnapshotOptions, type SnapshotSource, } from './useSnapshot.js';
|
|
4
4
|
export { useWindow } from './useWindow.js';
|
|
5
5
|
export { useDerived } from './useDerived.js';
|
|
6
|
+
export { useLiveQuery } from './useLiveQuery.js';
|
|
7
|
+
export { useLatest } from './useLatest.js';
|
|
6
8
|
export { takeSnapshot } from './takeSnapshot.js';
|
|
7
9
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EACL,WAAW,EACX,KAAK,kBAAkB,EACvB,KAAK,cAAc,GACpB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
// @pond-ts/react — React hooks for pond-ts live time series
|
|
2
2
|
export { useLiveSeries } from './useLiveSeries.js';
|
|
3
3
|
export { useTimeSeries } from './useTimeSeries.js';
|
|
4
|
-
export { useSnapshot } from './useSnapshot.js';
|
|
4
|
+
export { useSnapshot, } from './useSnapshot.js';
|
|
5
5
|
export { useWindow } from './useWindow.js';
|
|
6
6
|
export { useDerived } from './useDerived.js';
|
|
7
|
+
export { useLiveQuery } from './useLiveQuery.js';
|
|
8
|
+
export { useLatest } from './useLatest.js';
|
|
7
9
|
export { takeSnapshot } from './takeSnapshot.js';
|
|
8
10
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAE5D,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAE5D,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EACL,WAAW,GAGZ,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { EventForSchema, LiveSource, SeriesSchema } from 'pond-ts';
|
|
2
|
+
import type { SnapshotSource, UseSnapshotOptions } from './useSnapshot.js';
|
|
3
|
+
/**
|
|
4
|
+
* Subscribe to a live source and return only the latest event.
|
|
5
|
+
*
|
|
6
|
+
* Many dashboard stats only need the most recent value — current CPU, latest
|
|
7
|
+
* count, last price. Building a full `TimeSeries` snapshot for a single event
|
|
8
|
+
* is wasteful. `useLatest` returns just the last event, throttled like
|
|
9
|
+
* `useSnapshot`:
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* const latest = useLatest(live);
|
|
13
|
+
* // latest?.get('cpu') → number
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* Returns `null` when the source is empty or `null`.
|
|
17
|
+
*/
|
|
18
|
+
export declare function useLatest<S extends SeriesSchema>(source: SnapshotSource<S> | LiveSource<S> | null, options?: UseSnapshotOptions): EventForSchema<S> | null;
|
|
19
|
+
//# sourceMappingURL=useLatest.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useLatest.d.ts","sourceRoot":"","sources":["../src/useLatest.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACxE,OAAO,KAAK,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAE3E;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,YAAY,EAC9C,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,GAAG,IAAI,EAChD,OAAO,CAAC,EAAE,kBAAkB,GAC3B,cAAc,CAAC,CAAC,CAAC,GAAG,IAAI,CAyD1B"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Subscribe to a live source and return only the latest event.
|
|
4
|
+
*
|
|
5
|
+
* Many dashboard stats only need the most recent value — current CPU, latest
|
|
6
|
+
* count, last price. Building a full `TimeSeries` snapshot for a single event
|
|
7
|
+
* is wasteful. `useLatest` returns just the last event, throttled like
|
|
8
|
+
* `useSnapshot`:
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* const latest = useLatest(live);
|
|
12
|
+
* // latest?.get('cpu') → number
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* Returns `null` when the source is empty or `null`.
|
|
16
|
+
*/
|
|
17
|
+
export function useLatest(source, options) {
|
|
18
|
+
const throttleMs = options?.throttle ?? 100;
|
|
19
|
+
const [latest, setLatest] = useState(() => {
|
|
20
|
+
if (!source || source.length === 0)
|
|
21
|
+
return null;
|
|
22
|
+
return source.at(source.length - 1);
|
|
23
|
+
});
|
|
24
|
+
const sourceRef = useRef(source);
|
|
25
|
+
sourceRef.current = source;
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
if (!source) {
|
|
28
|
+
setLatest(null);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
// Re-read on source change
|
|
32
|
+
if (source.length > 0) {
|
|
33
|
+
setLatest(source.at(source.length - 1));
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
setLatest(null);
|
|
37
|
+
}
|
|
38
|
+
let timer = null;
|
|
39
|
+
let pending = false;
|
|
40
|
+
const flush = () => {
|
|
41
|
+
timer = null;
|
|
42
|
+
pending = false;
|
|
43
|
+
const s = sourceRef.current;
|
|
44
|
+
if (s && s.length > 0) {
|
|
45
|
+
setLatest(s.at(s.length - 1));
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
const unsub = source.on('event', () => {
|
|
49
|
+
if (throttleMs === 0) {
|
|
50
|
+
const s = sourceRef.current;
|
|
51
|
+
if (s && s.length > 0) {
|
|
52
|
+
setLatest(s.at(s.length - 1));
|
|
53
|
+
}
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (!pending) {
|
|
57
|
+
pending = true;
|
|
58
|
+
timer = setTimeout(flush, throttleMs);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
return () => {
|
|
62
|
+
unsub();
|
|
63
|
+
if (timer !== null)
|
|
64
|
+
clearTimeout(timer);
|
|
65
|
+
};
|
|
66
|
+
}, [source, throttleMs]);
|
|
67
|
+
return latest;
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=useLatest.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useLatest.js","sourceRoot":"","sources":["../src/useLatest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAIpD;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,SAAS,CACvB,MAAgD,EAChD,OAA4B;IAE5B,MAAM,UAAU,GAAG,OAAO,EAAE,QAAQ,IAAI,GAAG,CAAC;IAE5C,MAAM,CAAC,MAAM,EA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AG,SAAS,CAAC,OAAO,CAAC;gBAC5B,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACtB,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,CAAsB,CAAC,CAAC;gBACrD,CAAC;gBACD,OAAO;YACT,CAAC;YACD,IAAI,CAAC,OAAO,EAAE,CAAC;gBACb,OAAO,GAAG,IAAI,CAAC;gBACf,KAAK,GAAG,UAAU,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;YACxC,CAAC;QACH,CAAC,CAAC,CAAC;QAEH,OAAO,GAAG,EAAE;YACV,KAAK,EAAE,CAAC;YACR,IAAI,KAAK,KAAK,IAAI;gBAAE,YAAY,CAAC,KAAK,CAAC,CAAC;QAC1C,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC;IAEzB,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { LiveSource, SeriesSchema, TimeSeries } from 'pond-ts';
|
|
2
|
+
import { type SnapshotSource, type UseSnapshotOptions } from './useSnapshot.js';
|
|
3
|
+
/**
|
|
4
|
+
* Build a derived live view once, subscribe, and return both the view and a
|
|
5
|
+
* throttled snapshot — combining `useMemo` + `useSnapshot` in one call.
|
|
6
|
+
*
|
|
7
|
+
* Typical dashboard code creates 5–10 derived views (filters, aggregations,
|
|
8
|
+
* rolling windows). Without this hook each one needs a manual `useMemo` for
|
|
9
|
+
* stability plus a separate `useSnapshot` for subscription. `useLiveQuery`
|
|
10
|
+
* bundles that into a single call whose return shape matches `useLiveSeries`:
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* const [, rollingSnap] = useLiveQuery(
|
|
14
|
+
* () => live.rolling('1m', { cpu: 'avg' }),
|
|
15
|
+
* [live],
|
|
16
|
+
* { throttle: 200 },
|
|
17
|
+
* );
|
|
18
|
+
*
|
|
19
|
+
* const [highCpu, highCpuSnap] = useLiveQuery(
|
|
20
|
+
* () => live.filter((e) => e.get('cpu') > 0.7),
|
|
21
|
+
* [live],
|
|
22
|
+
* );
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @param build Factory that creates the live view. Called once per `deps` change.
|
|
26
|
+
* @param deps React dependency array — when any dep changes, the view is rebuilt.
|
|
27
|
+
* @param options Snapshot throttle options (default 100 ms).
|
|
28
|
+
* @returns `[view, snapshot]` — the stable view and its throttled `TimeSeries`.
|
|
29
|
+
*/
|
|
30
|
+
export declare function useLiveQuery<T extends SnapshotSource<S> | LiveSource<S>, S extends SeriesSchema = T extends SnapshotSource<infer U> ? U : T extends LiveSource<infer V> ? V : SeriesSchema>(build: () => T, deps: readonly unknown[], options?: UseSnapshotOptions): [T, TimeSeries<S> | null];
|
|
31
|
+
//# sourceMappingURL=useLiveQuery.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useLiveQuery.d.ts","sourceRoot":"","sources":["../src/useLiveQuery.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACpE,OAAO,EAEL,KAAK,cAAc,EACnB,KAAK,kBAAkB,EACxB,MAAM,kBAAkB,CAAC;AAE1B;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,YAAY,CAC1B,CAAC,SAAS,cAAc,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,EAC3C,CAAC,SAAS,YAAY,GAAG,CAAC,SAAS,cAAc,CAAC,MAAM,CAAC,CAAC,GACtD,CAAC,GACD,CAAC,SAAS,UAAU,CAAC,MAAM,CAAC,CAAC,GAC3B,CAAC,GACD,YAAY,EAElB,KAAK,EAAE,MAAM,CAAC,EACd,IAAI,EAAE,SAAS,OAAO,EAAE,EACxB,OAAO,CAAC,EAAE,kBAAkB,GAC3B,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAK3B"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { useSnapshot, } from './useSnapshot.js';
|
|
3
|
+
/**
|
|
4
|
+
* Build a derived live view once, subscribe, and return both the view and a
|
|
5
|
+
* throttled snapshot — combining `useMemo` + `useSnapshot` in one call.
|
|
6
|
+
*
|
|
7
|
+
* Typical dashboard code creates 5–10 derived views (filters, aggregations,
|
|
8
|
+
* rolling windows). Without this hook each one needs a manual `useMemo` for
|
|
9
|
+
* stability plus a separate `useSnapshot` for subscription. `useLiveQuery`
|
|
10
|
+
* bundles that into a single call whose return shape matches `useLiveSeries`:
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* const [, rollingSnap] = useLiveQuery(
|
|
14
|
+
* () => live.rolling('1m', { cpu: 'avg' }),
|
|
15
|
+
* [live],
|
|
16
|
+
* { throttle: 200 },
|
|
17
|
+
* );
|
|
18
|
+
*
|
|
19
|
+
* const [highCpu, highCpuSnap] = useLiveQuery(
|
|
20
|
+
* () => live.filter((e) => e.get('cpu') > 0.7),
|
|
21
|
+
* [live],
|
|
22
|
+
* );
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @param build Factory that creates the live view. Called once per `deps` change.
|
|
26
|
+
* @param deps React dependency array — when any dep changes, the view is rebuilt.
|
|
27
|
+
* @param options Snapshot throttle options (default 100 ms).
|
|
28
|
+
* @returns `[view, snapshot]` — the stable view and its throttled `TimeSeries`.
|
|
29
|
+
*/
|
|
30
|
+
export function useLiveQuery(build, deps, options) {
|
|
31
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
32
|
+
const view = useMemo(build, deps);
|
|
33
|
+
const snapshot = useSnapshot(view, options);
|
|
34
|
+
return [view, snapshot];
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=useLiveQuery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useLiveQuery.js","sourceRoot":"","sources":["../src/useLiveQuery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAEhC,OAAO,EACL,WAAW,GAGZ,MAAM,kBAAkB,CAAC;AAE1B;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,UAAU,YAAY,CAQ1B,KAAc,EACd,IAAwB,EACxB,OAA4B;IAE5B,uDAAuD;IACvD,MAAM,IAAI,GAAG,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IAClC,MAAM,QAAQ,GAAG,WAAW,CAAC,IAAyB,EAAE,OAAO,CAAC,CAAC;IACjE,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;AAC1B,CAAC"}
|
package/dist/useSnapshot.d.ts
CHANGED
|
@@ -5,10 +5,31 @@ export interface UseSnapshotOptions {
|
|
|
5
5
|
throttle?: number;
|
|
6
6
|
}
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
8
|
+
* Structural type covering all pond-ts live objects (`LiveSeries`, `LiveView`,
|
|
9
|
+
* `LiveAggregation`, `LiveRollingAggregation`).
|
|
10
|
+
*
|
|
11
|
+
* Using a structural type instead of the nominal `LiveSource<S>` avoids casts
|
|
12
|
+
* when passing `LiveAggregation` (whose output schema differs from its input
|
|
13
|
+
* type parameter).
|
|
14
|
+
*/
|
|
15
|
+
export type SnapshotSource<S extends SeriesSchema = SeriesSchema> = {
|
|
16
|
+
readonly name: string;
|
|
17
|
+
readonly schema: S;
|
|
18
|
+
readonly length: number;
|
|
19
|
+
at(index: number): unknown;
|
|
20
|
+
on(type: 'event', fn: (...args: any[]) => void): () => void;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Subscribe to any live source and return a throttled `TimeSeries` snapshot.
|
|
9
24
|
*
|
|
10
25
|
* The snapshot updates at most once per `throttle` interval (default 100 ms).
|
|
11
|
-
* Returns `null` when the source is empty
|
|
26
|
+
* Returns `null` when the source is empty or `null`.
|
|
27
|
+
*
|
|
28
|
+
* Accepts `LiveSource`, `LiveAggregation`, `LiveRollingAggregation`, or any
|
|
29
|
+
* object with a compatible shape — no casts needed.
|
|
30
|
+
*
|
|
31
|
+
* Accepts `null` as a source so hooks like `useWindow` can pass a
|
|
32
|
+
* not-yet-created source without violating the Rules of Hooks.
|
|
12
33
|
*/
|
|
13
|
-
export declare function useSnapshot<S extends SeriesSchema>(source: LiveSource<S
|
|
34
|
+
export declare function useSnapshot<S extends SeriesSchema>(source: SnapshotSource<S> | LiveSource<S> | null, options?: UseSnapshotOptions): TimeSeries<S> | null;
|
|
14
35
|
//# sourceMappingURL=useSnapshot.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSnapshot.d.ts","sourceRoot":"","sources":["../src/useSnapshot.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACxD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAG1C,MAAM,WAAW,kBAAkB;IACjC,2FAA2F;IAC3F,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED
|
|
1
|
+
{"version":3,"file":"useSnapshot.d.ts","sourceRoot":"","sources":["../src/useSnapshot.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACxD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAG1C,MAAM,WAAW,kBAAkB;IACjC,2FAA2F;IAC3F,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;;;;;;GAOG;AACH,MAAM,MAAM,cAAc,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,IAAI;IAClE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;IACnB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;IAC3B,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;CAC7D,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAAC,CAAC,SAAS,YAAY,EAChD,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,GAAG,IAAI,EAChD,OAAO,CAAC,EAAE,kBAAkB,GAC3B,UAAU,CAAC,CAAC,CAAC,GAAG,IAAI,CAsDtB"}
|
package/dist/useSnapshot.js
CHANGED
|
@@ -1,18 +1,28 @@
|
|
|
1
1
|
import { useEffect, useRef, useState } from 'react';
|
|
2
2
|
import { takeSnapshot } from './takeSnapshot.js';
|
|
3
3
|
/**
|
|
4
|
-
* Subscribe to any
|
|
4
|
+
* Subscribe to any live source and return a throttled `TimeSeries` snapshot.
|
|
5
5
|
*
|
|
6
6
|
* The snapshot updates at most once per `throttle` interval (default 100 ms).
|
|
7
|
-
* Returns `null` when the source is empty
|
|
7
|
+
* Returns `null` when the source is empty or `null`.
|
|
8
|
+
*
|
|
9
|
+
* Accepts `LiveSource`, `LiveAggregation`, `LiveRollingAggregation`, or any
|
|
10
|
+
* object with a compatible shape — no casts needed.
|
|
11
|
+
*
|
|
12
|
+
* Accepts `null` as a source so hooks like `useWindow` can pass a
|
|
13
|
+
* not-yet-created source without violating the Rules of Hooks.
|
|
8
14
|
*/
|
|
9
15
|
export function useSnapshot(source, options) {
|
|
10
16
|
const throttleMs = options?.throttle ?? 100;
|
|
11
|
-
const [snapshot, setSnapshot] = useState(() => takeSnapshot(source));
|
|
12
|
-
// Track the latest source
|
|
17
|
+
const [snapshot, setSnapshot] = useState(() => source ? takeSnapshot(source) : null);
|
|
18
|
+
// Track the latest source so the flush callback always reads current state.
|
|
13
19
|
const sourceRef = useRef(source);
|
|
14
20
|
sourceRef.current = source;
|
|
15
21
|
useEffect(() => {
|
|
22
|
+
if (!source) {
|
|
23
|
+
setSnapshot(null);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
16
26
|
// Re-snapshot on source change
|
|
17
27
|
setSnapshot(takeSnapshot(source));
|
|
18
28
|
let timer = null;
|
|
@@ -20,11 +30,15 @@ export function useSnapshot(source, options) {
|
|
|
20
30
|
const flush = () => {
|
|
21
31
|
timer = null;
|
|
22
32
|
pending = false;
|
|
23
|
-
|
|
33
|
+
if (sourceRef.current) {
|
|
34
|
+
setSnapshot(takeSnapshot(sourceRef.current));
|
|
35
|
+
}
|
|
24
36
|
};
|
|
25
37
|
const unsub = source.on('event', () => {
|
|
26
38
|
if (throttleMs === 0) {
|
|
27
|
-
|
|
39
|
+
if (sourceRef.current) {
|
|
40
|
+
setSnapshot(takeSnapshot(sourceRef.current));
|
|
41
|
+
}
|
|
28
42
|
return;
|
|
29
43
|
}
|
|
30
44
|
if (!pending) {
|
package/dist/useSnapshot.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSnapshot.js","sourceRoot":"","sources":["../src/useSnapshot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAGpD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"useSnapshot.js","sourceRoot":"","sources":["../src/useSnapshot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAGpD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAuBjD;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,WAAW,CACzB,MAAgD,EAChD,OAA4B;IAE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C;YACxC,CAAC;QACH,CAAC,CAAC,CAAC;QAEH,OAAO,GAAG,EAAE;YACV,KAAK,EAAE,CAAC;YACR,IAAI,KAAK,KAAK,IAAI;gBAAE,YAAY,CAAC,KAAK,CAAC,CAAC;QAC1C,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC;IAEzB,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
package/dist/useWindow.d.ts
CHANGED
|
@@ -8,7 +8,8 @@ type Windowable<S extends SeriesSchema> = LiveSource<S> & {
|
|
|
8
8
|
/**
|
|
9
9
|
* Create a windowed view of a live source and return a throttled snapshot.
|
|
10
10
|
*
|
|
11
|
-
* The view is created
|
|
11
|
+
* The view is created and disposed inside an effect, so it works correctly
|
|
12
|
+
* under React StrictMode's double-mount cycle.
|
|
12
13
|
* Returns `null` when the window is empty.
|
|
13
14
|
*/
|
|
14
15
|
export declare function useWindow<S extends SeriesSchema>(source: Windowable<S>, size: RollingWindow, options?: UseSnapshotOptions): TimeSeries<S> | null;
|
package/dist/useWindow.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useWindow.d.ts","sourceRoot":"","sources":["../src/useWindow.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACpE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7C,OAAO,EAAe,KAAK,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAExE,yFAAyF;AACzF,KAAK,UAAU,CAAC,CAAC,SAAS,YAAY,IAAI,UAAU,CAAC,CAAC,CAAC,GAAG;IACxD,MAAM,CAAC,IAAI,EAAE,aAAa,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;CAC5C,CAAC;AAEF
|
|
1
|
+
{"version":3,"file":"useWindow.d.ts","sourceRoot":"","sources":["../src/useWindow.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACpE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7C,OAAO,EAAe,KAAK,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAExE,yFAAyF;AACzF,KAAK,UAAU,CAAC,CAAC,SAAS,YAAY,IAAI,UAAU,CAAC,CAAC,CAAC,GAAG;IACxD,MAAM,CAAC,IAAI,EAAE,aAAa,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;CAC5C,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,YAAY,EAC9C,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC,EACrB,IAAI,EAAE,aAAa,EACnB,OAAO,CAAC,EAAE,kBAAkB,GAC3B,UAAU,CAAC,CAAC,CAAC,GAAG,IAAI,CAetB"}
|
package/dist/useWindow.js
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
|
-
import { useEffect,
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
2
|
import { useSnapshot } from './useSnapshot.js';
|
|
3
3
|
/**
|
|
4
4
|
* Create a windowed view of a live source and return a throttled snapshot.
|
|
5
5
|
*
|
|
6
|
-
* The view is created
|
|
6
|
+
* The view is created and disposed inside an effect, so it works correctly
|
|
7
|
+
* under React StrictMode's double-mount cycle.
|
|
7
8
|
* Returns `null` when the window is empty.
|
|
8
9
|
*/
|
|
9
10
|
export function useWindow(source, size, options) {
|
|
10
|
-
const view =
|
|
11
|
+
const [view, setView] = useState(null);
|
|
11
12
|
useEffect(() => {
|
|
13
|
+
const v = source.window(size);
|
|
14
|
+
setView(v);
|
|
12
15
|
return () => {
|
|
13
|
-
if ('dispose' in
|
|
14
|
-
|
|
16
|
+
if ('dispose' in v && typeof v.dispose === 'function') {
|
|
17
|
+
v.dispose();
|
|
15
18
|
}
|
|
16
19
|
};
|
|
17
|
-
}, [
|
|
20
|
+
}, [source, size]);
|
|
18
21
|
return useSnapshot(view, options);
|
|
19
22
|
}
|
|
20
23
|
//# sourceMappingURL=useWindow.js.map
|
package/dist/useWindow.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useWindow.js","sourceRoot":"","sources":["../src/useWindow.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,
|
|
1
|
+
{"version":3,"file":"useWindow.js","sourceRoot":"","sources":["../src/useWindow.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAG5C,OAAO,EAAE,WAAW,EAA2B,MAAM,kBAAkB,CAAC;AAOxE;;;;;;GAMG;AACH,MAAM,UAAU,SAAS,CACvB,MAAqB,EACrB,IAAmB,EACnB,OAA4B;IAE5B,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,QAAQ,CAAuB,IAAI,CAAC,CAAC;IAE7D,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAC9B,OAAO,CAAC,CAAC,CAAC,CAAC;QAEX,OAAO,GAAG,EAAE;YACV,IAAI,SAAS,IAAI,CAAC,IAAI,OAAQ,CAAS,CAAC,OAAO,KAAK,UAAU,EAAE,CAAC;gBAC9D,CAAS,CAAC,OAAO,EAAE,CAAC;YACvB,CAAC;QACH,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;IAEnB,OAAO,WAAW,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AACpC,CAAC"}
|