@pond-ts/react 0.4.3 → 0.5.1

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 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
@@ -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,48 @@
1
+ import type { AggregateMap, AggregateReducer, DurationInput, LiveSource, NormalizedValueForKind, ScalarKind, SeriesSchema, ValueColumnsForSchema } from 'pond-ts';
2
+ import { type SnapshotSource, type UseSnapshotOptions } from './useSnapshot.js';
3
+ /**
4
+ * Narrowed per-entry output for `useCurrent`. Derives each field's value
5
+ * type from the source column kind and the reducer name so callers don't
6
+ * have to `as`-cast. Numeric reducers (`'sum'`, `'avg'`, `'count'`) emit
7
+ * `number`; `'unique'` and `` `top${number}` `` emit
8
+ * `ReadonlyArray<ScalarValue>`; other reducers (including custom
9
+ * functions) fall back to the source column kind.
10
+ */
11
+ type CurrentResult<S extends SeriesSchema, Mapping> = {
12
+ [K in keyof Mapping & string]: NormalizedValueForKind<K extends ValueColumnsForSchema<S>[number]['name'] ? Mapping[K] extends 'sum' | 'avg' | 'count' ? 'number' : Mapping[K] extends 'unique' | `top${number}` ? 'array' : CurrentColumnKind<S, K> : ScalarKind> | undefined;
13
+ };
14
+ type CurrentColumnKind<S extends SeriesSchema, K extends string> = Extract<ValueColumnsForSchema<S>[number], {
15
+ name: K;
16
+ }> extends {
17
+ kind: infer Kind extends ScalarKind;
18
+ } ? Kind : ScalarKind;
19
+ export interface UseCurrentOptions extends UseSnapshotOptions {
20
+ /**
21
+ * Trailing window to evaluate the mapping over, expressed as a
22
+ * `DurationInput` (e.g. `'30s'`, `'5m'`, or a number of milliseconds).
23
+ * When omitted, the full snapshot is used.
24
+ */
25
+ tail?: DurationInput;
26
+ }
27
+ /**
28
+ * Subscribe to a live source and return the current value of a reducer
29
+ * mapping, updated on a throttle. Equivalent to
30
+ * `useSnapshot(src).tail(tail).reduce(mapping)` but with one subscription,
31
+ * one memo, and narrow per-entry types.
32
+ *
33
+ * ```ts
34
+ * const current = useCurrent(live, { cpu: 'avg', host: 'unique' });
35
+ * // ^ { cpu: number | undefined; host: ReadonlyArray<ScalarValue> | undefined }
36
+ *
37
+ * const recent = useCurrent(live, { cpu: 'p95' }, { tail: '30s' });
38
+ * ```
39
+ *
40
+ * Returns an empty-result object (every mapped field set to the
41
+ * reducer's empty-bucket value) while the source has no events — the
42
+ * shape of the return is stable regardless of source state, so
43
+ * destructuring is always safe.
44
+ */
45
+ export declare function useCurrent<S extends SeriesSchema, const Mapping extends AggregateMap<S>>(source: SnapshotSource<S> | LiveSource<S> | null, mapping: Mapping, options?: UseCurrentOptions): CurrentResult<S, Mapping>;
46
+ export type _UseCurrentReducerHint = AggregateReducer;
47
+ export {};
48
+ //# 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,gBAAgB,EAEhB,aAAa,EACb,UAAU,EACV,sBAAsB,EACtB,UAAU,EACV,YAAY,EACZ,qBAAqB,EACtB,MAAM,SAAS,CAAC;AACjB,OAAO,EAEL,KAAK,cAAc,EACnB,KAAK,kBAAkB,EACxB,MAAM,kBAAkB,CAAC;AAE1B;;;;;;;GAOG;AACH,KAAK,aAAa,CAAC,CAAC,SAAS,YAAY,EAAE,OAAO,IAAI;KACnD,CAAC,IAAI,MAAM,OAAO,GAAG,MAAM,GACxB,sBAAsB,CACpB,CAAC,SAAS,qBAAqB,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,GAC9C,OAAO,CAAC,CAAC,CAAC,SAAS,KAAK,GAAG,KAAK,GAAG,OAAO,GACxC,QAAQ,GACR,OAAO,CAAC,CAAC,CAAC,SAAS,QAAQ,GAAG,MAAM,MAAM,EAAE,GAC1C,OAAO,GACP,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,GAC3B,UAAU,CACf,GACD,SAAS;CACd,CAAC;AAEF,KAAK,iBAAiB,CAAC,CAAC,SAAS,YAAY,EAAE,CAAC,SAAS,MAAM,IAC7D,OAAO,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE;IAAE,IAAI,EAAE,CAAC,CAAA;CAAE,CAAC,SAAS;IAC7D,IAAI,EAAE,MAAM,IAAI,SAAS,UAAU,CAAC;CACrC,GACG,IAAI,GACJ,UAAU,CAAC;AAEjB,MAAM,WAAW,iBAAkB,SAAQ,kBAAkB;IAC3D;;;;OAIG;IACH,IAAI,CAAC,EAAE,aAAa,CAAC;CACtB;AAED;;;;;;;;;;;;;;;;;GAiBG;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,aAAa,CAAC,CAAC,EAAE,OAAO,CAAC,CAuB3B;AAKD,MAAM,MAAM,sBAAsB,GAAG,gBAAgB,CAAC"}
@@ -0,0 +1,37 @@
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.
8
+ *
9
+ * ```ts
10
+ * const current = useCurrent(live, { cpu: 'avg', host: 'unique' });
11
+ * // ^ { cpu: number | undefined; host: ReadonlyArray<ScalarValue> | undefined }
12
+ *
13
+ * const recent = useCurrent(live, { cpu: 'p95' }, { tail: '30s' });
14
+ * ```
15
+ *
16
+ * Returns an empty-result object (every mapped field set to the
17
+ * reducer's empty-bucket value) while the source has no events — the
18
+ * shape of the return is stable regardless of source state, so
19
+ * destructuring is always safe.
20
+ */
21
+ export function useCurrent(source, mapping, options) {
22
+ const snap = useSnapshot(source, options);
23
+ const tailOpt = options?.tail;
24
+ return useMemo(() => {
25
+ if (!snap) {
26
+ // Stable empty-shape result so destructuring never explodes on
27
+ // first render.
28
+ const empty = {};
29
+ for (const key of Object.keys(mapping))
30
+ empty[key] = undefined;
31
+ return empty;
32
+ }
33
+ const scoped = tailOpt !== undefined ? snap.tail(tailOpt) : snap;
34
+ return scoped.reduce(mapping);
35
+ }, [snap, tailOpt, mapping]);
36
+ }
37
+ //# 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;AAYhC,OAAO,EACL,WAAW,GAGZ,MAAM,kBAAkB,CAAC;AAwC1B;;;;;;;;;;;;;;;;;GAiBG;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,GAA4C,EAAE,CAAC;YAC1D,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC;gBAAE,KAAK,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC;YAC/D,OAAO,KAAkC,CAAC;QAC5C,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,CAClB,OAMC,CACsC,CAAC;IAC5C,CAAC,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;AAC/B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pond-ts/react",
3
- "version": "0.4.3",
3
+ "version": "0.5.1",
4
4
  "description": "React hooks for pond-ts live time series",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -29,7 +29,7 @@
29
29
  "test:runtime": "vitest run"
30
30
  },
31
31
  "dependencies": {
32
- "pond-ts": "^0.4.0"
32
+ "pond-ts": "^0.5.0"
33
33
  },
34
34
  "peerDependencies": {
35
35
  "react": "^18.0.0 || ^19.0.0"