@pond-ts/react 0.5.0 → 0.5.2
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 +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/useCurrent.d.ts +31 -0
- package/dist/useCurrent.d.ts.map +1 -0
- package/dist/useCurrent.js +38 -0
- package/dist/useCurrent.js.map +1 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -5,5 +5,6 @@ export { useWindow } from './useWindow.js';
|
|
|
5
5
|
export { useDerived } from './useDerived.js';
|
|
6
6
|
export { useLiveQuery } from './useLiveQuery.js';
|
|
7
7
|
export { useLatest } from './useLatest.js';
|
|
8
|
+
export { useCurrent, type UseCurrentOptions } from './useCurrent.js';
|
|
8
9
|
export { takeSnapshot } from './takeSnapshot.js';
|
|
9
10
|
//# 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,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"}
|
|
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,UAAU,EAAE,KAAK,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACrE,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -6,5 +6,6 @@ export { useWindow } from './useWindow.js';
|
|
|
6
6
|
export { useDerived } from './useDerived.js';
|
|
7
7
|
export { useLiveQuery } from './useLiveQuery.js';
|
|
8
8
|
export { useLatest } from './useLatest.js';
|
|
9
|
+
export { useCurrent } from './useCurrent.js';
|
|
9
10
|
export { takeSnapshot } from './takeSnapshot.js';
|
|
10
11
|
//# 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,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"}
|
|
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,UAAU,EAA0B,MAAM,iBAAiB,CAAC;AACrE,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { AggregateMap, DurationInput, LiveSource, ReduceResult, SeriesSchema } from 'pond-ts';
|
|
2
|
+
import { type SnapshotSource, type UseSnapshotOptions } from './useSnapshot.js';
|
|
3
|
+
export interface UseCurrentOptions extends UseSnapshotOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Trailing window to evaluate the mapping over, expressed as a
|
|
6
|
+
* `DurationInput` (e.g. `'30s'`, `'5m'`, or a number of milliseconds).
|
|
7
|
+
* When omitted, the full snapshot is used.
|
|
8
|
+
*/
|
|
9
|
+
tail?: DurationInput;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Subscribe to a live source and return the current value of a reducer
|
|
13
|
+
* mapping, updated on a throttle. Equivalent to
|
|
14
|
+
* `useSnapshot(src).tail(tail).reduce(mapping)` but with one subscription,
|
|
15
|
+
* one memo, and narrow per-entry types inherited from
|
|
16
|
+
* `TimeSeries.reduce`.
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* const current = useCurrent(live, { cpu: 'avg', host: 'unique' });
|
|
20
|
+
* // ^ { cpu: number | undefined;
|
|
21
|
+
* // host: ReadonlyArray<ScalarValue> | undefined }
|
|
22
|
+
*
|
|
23
|
+
* const recent = useCurrent(live, { cpu: 'p95' }, { tail: '30s' });
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* Returns a stable-shape object while the source has no events (every
|
|
27
|
+
* mapped field is `undefined`), so destructuring on first render is
|
|
28
|
+
* safe.
|
|
29
|
+
*/
|
|
30
|
+
export declare function useCurrent<S extends SeriesSchema, const Mapping extends AggregateMap<S>>(source: SnapshotSource<S> | LiveSource<S> | null, mapping: Mapping, options?: UseCurrentOptions): ReduceResult<S, Mapping>;
|
|
31
|
+
//# sourceMappingURL=useCurrent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useCurrent.d.ts","sourceRoot":"","sources":["../src/useCurrent.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,YAAY,EACZ,aAAa,EACb,UAAU,EACV,YAAY,EACZ,YAAY,EACb,MAAM,SAAS,CAAC;AACjB,OAAO,EAEL,KAAK,cAAc,EACnB,KAAK,kBAAkB,EACxB,MAAM,kBAAkB,CAAC;AAE1B,MAAM,WAAW,iBAAkB,SAAQ,kBAAkB;IAC3D;;;;OAIG;IACH,IAAI,CAAC,EAAE,aAAa,CAAC;CACtB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,UAAU,CACxB,CAAC,SAAS,YAAY,EACtB,KAAK,CAAC,OAAO,SAAS,YAAY,CAAC,CAAC,CAAC,EAErC,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,GAAG,IAAI,EAChD,OAAO,EAAE,OAAO,EAChB,OAAO,CAAC,EAAE,iBAAiB,GAC1B,YAAY,CAAC,CAAC,EAAE,OAAO,CAAC,CAe1B"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { useSnapshot, } from './useSnapshot.js';
|
|
3
|
+
/**
|
|
4
|
+
* Subscribe to a live source and return the current value of a reducer
|
|
5
|
+
* mapping, updated on a throttle. Equivalent to
|
|
6
|
+
* `useSnapshot(src).tail(tail).reduce(mapping)` but with one subscription,
|
|
7
|
+
* one memo, and narrow per-entry types inherited from
|
|
8
|
+
* `TimeSeries.reduce`.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* const current = useCurrent(live, { cpu: 'avg', host: 'unique' });
|
|
12
|
+
* // ^ { cpu: number | undefined;
|
|
13
|
+
* // host: ReadonlyArray<ScalarValue> | undefined }
|
|
14
|
+
*
|
|
15
|
+
* const recent = useCurrent(live, { cpu: 'p95' }, { tail: '30s' });
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* Returns a stable-shape object while the source has no events (every
|
|
19
|
+
* mapped field is `undefined`), so destructuring on first render is
|
|
20
|
+
* safe.
|
|
21
|
+
*/
|
|
22
|
+
export function useCurrent(source, mapping, options) {
|
|
23
|
+
const snap = useSnapshot(source, options);
|
|
24
|
+
const tailOpt = options?.tail;
|
|
25
|
+
return useMemo(() => {
|
|
26
|
+
if (!snap) {
|
|
27
|
+
// Stable empty-shape result so destructuring never explodes on
|
|
28
|
+
// first render.
|
|
29
|
+
const empty = {};
|
|
30
|
+
for (const key of Object.keys(mapping))
|
|
31
|
+
empty[key] = undefined;
|
|
32
|
+
return empty;
|
|
33
|
+
}
|
|
34
|
+
const scoped = tailOpt !== undefined ? snap.tail(tailOpt) : snap;
|
|
35
|
+
return scoped.reduce(mapping);
|
|
36
|
+
}, [snap, tailOpt, mapping]);
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=useCurrent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useCurrent.js","sourceRoot":"","sources":["../src/useCurrent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAQhC,OAAO,EACL,WAAW,GAGZ,MAAM,kBAAkB,CAAC;AAW1B;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,UAAU,CAIxB,MAAgD,EAChD,OAAgB,EAChB,OAA2B;IAE3B,MAAM,IAAI,GAAG,WAAW,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC1C,MAAM,OAAO,GAAG,OAAO,EAAE,IAAI,CAAC;IAE9B,OAAO,OAAO,CAAC,GAAG,EAAE;QAClB,IAAI,CAAC,IAAI,EAAE,CAAC;YACV,+DAA+D;YAC/D,gBAAgB;YAChB,MAAM,KAAK,GAA4B,EAAE,CAAC;YAC1C,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC;gBAAE,KAAK,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC;YAC/D,OAAO,KAAiC,CAAC;QAC3C,CAAC;QACD,MAAM,MAAM,GAAG,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;QACjE,OAAO,MAAM,CAAC,MAAM,CAAC,OAAO,CAA6B,CAAC;IAC5D,CAAC,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;AAC/B,CAAC"}
|