@mmstack/primitives 21.0.22 → 21.0.23

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mmstack/primitives",
3
- "version": "21.0.22",
3
+ "version": "21.0.23",
4
4
  "keywords": [
5
5
  "angular",
6
6
  "signals",
@@ -661,7 +661,18 @@ declare const map: <I, O>(fn: (v: I) => O) => Operator<I, O>;
661
661
  /** filter values, keeping the last value if it was ever available, if first value is filtered will return undefined */
662
662
  declare const filter: <T>(predicate: (v: T) => boolean) => Operator<T, T | undefined>;
663
663
  /** tap into the value */
664
- declare const tap: <T>(fn: (v: T) => void) => Operator<T, T>;
664
+ declare const tap: <T>(fn: (v: T) => void, injector?: Injector) => Operator<T, T>;
665
+ /**
666
+ * Like {@link filter}, but emits `initial` until a value passes the predicate
667
+ * for the first time. Avoids the `T | undefined` return type.
668
+ */
669
+ declare const filterWith: <T>(predicate: (v: T) => boolean, initial: T) => Operator<T, T>;
670
+ /** Emits `initial` first, then mirrors source. */
671
+ declare const startWith: <T, U>(initial: U) => Operator<T, T | U>;
672
+ /** Emits `[prev, curr]` pairs. The first emission has prev = undefined. */
673
+ declare const pairwise: <T>() => Operator<T, [T | undefined, T]>;
674
+ /** Reduce-like accumulator across emissions. */
675
+ declare const scan: <T, R>(reducer: (acc: R, curr: T) => R, seed: R) => Operator<T, R>;
665
676
 
666
677
  /**
667
678
  * Decorate any `Signal<T>` with a chainable `.pipe(...)` method.
@@ -807,6 +818,42 @@ declare function pooledSet<T extends Set<unknown>, U = T>(opt: CreateProvidedPoo
807
818
  declare function pooledMap<T extends Map<unknown, unknown>, U = T>(computation: Computation<T, U>, opt?: CreateSignalOptions<U>): Signal<U>;
808
819
  declare function pooledMap<T extends Map<unknown, unknown>, U = T>(opt: CreateProvidedPooledOptions<T, U>): Signal<U>;
809
820
 
821
+ type BatteryStatus = {
822
+ readonly level: number;
823
+ readonly charging: boolean;
824
+ readonly chargingTime: number;
825
+ readonly dischargingTime: number;
826
+ };
827
+ /**
828
+ * Creates a read-only signal that tracks the system battery status using the
829
+ * Battery Status API. Returns `null` until the underlying `getBattery()`
830
+ * promise resolves, or permanently when the API is unsupported (Firefox /
831
+ * Safari at the time of writing). SSR-safe.
832
+ */
833
+ declare function batteryStatus(debugName?: string): Signal<BatteryStatus | null>;
834
+
835
+ type ClipboardSignal = Signal<string> & {
836
+ /**
837
+ * Writes `value` to the system clipboard. Resolves once the write completes;
838
+ * rejects if the Clipboard API rejects (denied permission, insecure context).
839
+ */
840
+ readonly copy: (value: string) => Promise<void>;
841
+ /** `true` iff the Clipboard API is available in this environment. */
842
+ readonly isSupported: Signal<boolean>;
843
+ };
844
+ /**
845
+ * Creates a read-only signal mirroring the system clipboard contents.
846
+ *
847
+ * The signal value starts empty and updates whenever a `copy` event fires on
848
+ * the document (or {@link ClipboardSignal.copy} is invoked from this app).
849
+ * SSR-safe — returns `''` and `isSupported: false` on the server.
850
+ *
851
+ * Note: read access requires the Clipboard API and an active permission grant
852
+ * in browsers that gate it. Errors from `navigator.clipboard.readText` are
853
+ * swallowed silently to keep the signal value stable.
854
+ */
855
+ declare function clipboard(debugName?: string): ClipboardSignal;
856
+
810
857
  /**
811
858
  * Represents the size of an element.
812
859
  */
@@ -908,6 +955,73 @@ type ElementVisibilitySignal = Signal<IntersectionObserverEntry | undefined> & {
908
955
  */
909
956
  declare function elementVisibility(target?: ElementRef<Element> | Element | Signal<ElementRef<Element> | Element | null>, opt?: ElementVisibilityOptions): ElementVisibilitySignal;
910
957
 
958
+ type FocusWithinTarget = ElementRef<Element> | Element | Signal<ElementRef<Element> | Element | null>;
959
+ /**
960
+ * Creates a read-only signal that tracks whether the focused element is the
961
+ * target or a descendant of it. Mirrors the CSS `:focus-within` pseudo-class.
962
+ *
963
+ * Defaults `target` to the current `ElementRef` so it can be used inline in a
964
+ * component's `class` field. SSR-safe — returns a constant `false` signal on
965
+ * the server.
966
+ */
967
+ declare function focusWithin(target?: FocusWithinTarget): Signal<boolean>;
968
+
969
+ type GeolocationOptions = PositionOptions & {
970
+ /**
971
+ * If `true`, uses `navigator.geolocation.watchPosition` and updates the
972
+ * signal continuously. Otherwise a single `getCurrentPosition` call is made.
973
+ * @default false
974
+ */
975
+ watch?: boolean;
976
+ /** Optional debug name for the produced signal. */
977
+ debugName?: string;
978
+ };
979
+ type GeolocationSignal = Signal<GeolocationPosition | null> & {
980
+ readonly error: Signal<GeolocationPositionError | null>;
981
+ readonly loading: Signal<boolean>;
982
+ };
983
+ /**
984
+ * Creates a read-only signal that exposes the current geolocation position.
985
+ *
986
+ * The returned signal carries `error` and `loading` sub-signals for permission
987
+ * failures and the in-flight initial fetch respectively. SSR-safe — on the
988
+ * server the position is `null`, loading is `false`, and no API calls are made.
989
+ *
990
+ * @example
991
+ * ```ts
992
+ * const where = geolocation({ watch: true, enableHighAccuracy: true });
993
+ * effect(() => console.log(where()?.coords, where.error()));
994
+ * ```
995
+ */
996
+ declare function geolocation(opt?: GeolocationOptions): GeolocationSignal;
997
+
998
+ type IdleOptions = {
999
+ /**
1000
+ * Milliseconds of user inactivity before the signal flips to `true`.
1001
+ * @default 60_000
1002
+ */
1003
+ ms?: number;
1004
+ /**
1005
+ * Activity events that reset the idle timer.
1006
+ * @default ['mousemove','keydown','touchstart','scroll','visibilitychange']
1007
+ */
1008
+ events?: string[];
1009
+ /** Optional debug name for the produced signal. */
1010
+ debugName?: string;
1011
+ };
1012
+ type IdleSignal = Signal<boolean> & {
1013
+ /** Timestamp of the last idle/active transition. */
1014
+ readonly since: Signal<Date>;
1015
+ };
1016
+ /**
1017
+ * Creates a read-only signal that flips to `true` after a window of user
1018
+ * inactivity. Any of the configured `events` (default: pointer/keyboard/scroll
1019
+ * activity) resets the timer and flips the signal back to `false`.
1020
+ *
1021
+ * SSR-safe — always `false` with a frozen `since` date on the server.
1022
+ */
1023
+ declare function idle(opt?: IdleOptions): IdleSignal;
1024
+
911
1025
  /**
912
1026
  * Creates a read-only signal that reactively tracks whether a CSS media query
913
1027
  * string currently matches.
@@ -1101,6 +1215,20 @@ type NetworkStatusSignal = Signal<boolean> & {
1101
1215
  */
1102
1216
  declare function networkStatus(debugName?: string): NetworkStatusSignal;
1103
1217
 
1218
+ type ScreenOrientation = {
1219
+ /** Angle in degrees relative to the natural orientation. */
1220
+ readonly angle: number;
1221
+ /** One of the four `OrientationType` strings. */
1222
+ readonly type: OrientationType;
1223
+ };
1224
+ /**
1225
+ * Creates a read-only signal that tracks `screen.orientation`.
1226
+ *
1227
+ * SSR-safe — returns a constant `portrait-primary / 0°` signal on the server
1228
+ * and in environments without `screen.orientation` support.
1229
+ */
1230
+ declare function orientation(debugName?: string): Signal<ScreenOrientation>;
1231
+
1104
1232
  /**
1105
1233
  * Creates a read-only signal that tracks the page's visibility state.
1106
1234
  *
@@ -1353,6 +1481,39 @@ type SensorTypedOptions = {
1353
1481
  };
1354
1482
  returnType: Signal<boolean>;
1355
1483
  };
1484
+ geolocation: {
1485
+ opt: GeolocationOptions;
1486
+ returnType: GeolocationSignal;
1487
+ };
1488
+ clipboard: {
1489
+ opt: {
1490
+ debugName?: string;
1491
+ };
1492
+ returnType: ClipboardSignal;
1493
+ };
1494
+ orientation: {
1495
+ opt: {
1496
+ debugName?: string;
1497
+ };
1498
+ returnType: Signal<ScreenOrientation>;
1499
+ };
1500
+ batteryStatus: {
1501
+ opt: {
1502
+ debugName?: string;
1503
+ };
1504
+ returnType: Signal<BatteryStatus | null>;
1505
+ };
1506
+ idle: {
1507
+ opt: IdleOptions;
1508
+ returnType: IdleSignal;
1509
+ };
1510
+ focusWithin: {
1511
+ opt: {
1512
+ debugName?: string;
1513
+ target?: ElementRef<Element> | Element | Signal<ElementRef<Element> | Element | null>;
1514
+ };
1515
+ returnType: Signal<boolean>;
1516
+ };
1356
1517
  };
1357
1518
  /**
1358
1519
  * Creates a sensor signal that the elements visiblity within the viewport
@@ -1444,6 +1605,36 @@ declare function sensor(type: 'windowSize', options?: SensorTypedOptions['window
1444
1605
  * @example const pageScroll = sensor('scrollPosition', { throttle: 150 });
1445
1606
  */
1446
1607
  declare function sensor(type: 'scrollPosition', options?: SensorTypedOptions['scrollPosition']['opt']): ScrollPositionSignal;
1608
+ /**
1609
+ * Creates a sensor signal exposing the device's current geolocation position.
1610
+ * @see {geolocation}
1611
+ */
1612
+ declare function sensor(type: 'geolocation', options?: SensorTypedOptions['geolocation']['opt']): GeolocationSignal;
1613
+ /**
1614
+ * Creates a sensor signal mirroring the system clipboard contents.
1615
+ * @see {clipboard}
1616
+ */
1617
+ declare function sensor(type: 'clipboard', options?: SensorTypedOptions['clipboard']['opt']): ClipboardSignal;
1618
+ /**
1619
+ * Creates a sensor signal tracking the screen orientation.
1620
+ * @see {orientation}
1621
+ */
1622
+ declare function sensor(type: 'orientation', options?: SensorTypedOptions['orientation']['opt']): Signal<ScreenOrientation>;
1623
+ /**
1624
+ * Creates a sensor signal tracking the system battery status.
1625
+ * @see {batteryStatus}
1626
+ */
1627
+ declare function sensor(type: 'batteryStatus', options?: SensorTypedOptions['batteryStatus']['opt']): Signal<BatteryStatus | null>;
1628
+ /**
1629
+ * Creates a sensor signal that flips to `true` after a window of user inactivity.
1630
+ * @see {idle}
1631
+ */
1632
+ declare function sensor(type: 'idle', options?: SensorTypedOptions['idle']['opt']): IdleSignal;
1633
+ /**
1634
+ * Creates a sensor signal tracking whether focus is within a target subtree.
1635
+ * @see {focusWithin}
1636
+ */
1637
+ declare function sensor(type: 'focusWithin', options?: SensorTypedOptions['focusWithin']['opt']): Signal<boolean>;
1447
1638
  type SensorsOptions<TKey extends keyof SensorTypedOptions> = {
1448
1639
  [K in TKey]: SensorTypedOptions[K]['opt'];
1449
1640
  };
@@ -1452,6 +1643,47 @@ type Sensors<TKey extends keyof SensorTypedOptions> = {
1452
1643
  };
1453
1644
  declare function sensors<const TType extends keyof SensorTypedOptions>(track: TType[], opt?: SensorsOptions<TType>): Sensors<TType>;
1454
1645
 
1646
+ /**
1647
+ * Options for {@link signalFromEvent}. Extends the native
1648
+ * `AddEventListenerOptions` so callers can opt into `capture`, `passive`, etc.
1649
+ */
1650
+ type SignalFromEventOptions = AddEventListenerOptions & {
1651
+ /** Optional debug name for the produced signal. */
1652
+ debugName?: string;
1653
+ /** Override the DestroyRef used to remove the listener on teardown. */
1654
+ destroyRef?: DestroyRef;
1655
+ /** Override the Injector used to inject dependencies. */
1656
+ injector?: Injector;
1657
+ };
1658
+ type EventTargetLike = EventTarget | ElementRef<EventTarget>;
1659
+ type ResolvableTarget = EventTargetLike | Signal<EventTargetLike | null>;
1660
+ /**
1661
+ * Creates a read-only signal that emits the latest event dispatched on a
1662
+ * target. The target can be a static `EventTarget`, an `ElementRef`, or a
1663
+ * `Signal` that resolves to one (or `null` to detach).
1664
+ *
1665
+ * SSR-safe: on the server the signal returns the provided `initial` value and
1666
+ * no listener is registered.
1667
+ *
1668
+ * @example
1669
+ * ```ts
1670
+ * const click = signalFromEvent(document, 'click', null);
1671
+ * ```
1672
+ *
1673
+ * @example
1674
+ * ```ts
1675
+ * // With a projection — store just the coordinates.
1676
+ * const point = signalFromEvent<MouseEvent, { x: number; y: number }>(
1677
+ * document,
1678
+ * 'mousemove',
1679
+ * { x: 0, y: 0 },
1680
+ * (e) => ({ x: e.clientX, y: e.clientY }),
1681
+ * );
1682
+ * ```
1683
+ */
1684
+ declare function signalFromEvent<TEvent extends Event>(target: ResolvableTarget, eventName: string, initial: TEvent | null, opt?: SignalFromEventOptions): Signal<TEvent | null>;
1685
+ declare function signalFromEvent<TEvent extends Event, U>(target: ResolvableTarget, eventName: string, initial: U, project: (event: TEvent) => U, opt?: SignalFromEventOptions): Signal<U>;
1686
+
1455
1687
  type BaseType = string | number | boolean | symbol | undefined | null | Function | Date | RegExp;
1456
1688
  type Key = string | number;
1457
1689
  type AnyRecord = Record<Key, any>;
@@ -1646,47 +1878,21 @@ type StoredSignal<T> = WritableSignal<T> & {
1646
1878
  */
1647
1879
  declare function stored<T>(fallback: T, { key, store: providedStore, serialize, deserialize, syncTabs, equal, onKeyChange, cleanupOldKey, validate, ...rest }: CreateStoredOptions<T>): StoredSignal<T>;
1648
1880
 
1649
- type SyncSignalOptions = {
1881
+ type LegacySyncSignalOptions = {
1650
1882
  id?: string;
1651
1883
  };
1884
+ type SyncSignalOptions = {
1885
+ id: string;
1886
+ };
1652
1887
  /**
1653
- * Synchronizes a WritableSignal across browser tabs using BroadcastChannel API.
1654
- *
1655
- * Creates a shared signal that automatically syncs its value between all tabs
1656
- * of the same application. When the signal is updated in one tab, all other
1657
- * tabs will receive the new value automatically.
1658
- *
1659
- * @template T - The type of the WritableSignal
1660
- * @param sig - The WritableSignal to synchronize across tabs
1661
- * @param opt - Optional configuration object
1662
- * @param opt.id - Explicit channel ID for synchronization. If not provided,
1663
- * a deterministic ID is generated based on the call site.
1664
- * Use explicit IDs in production for reliability.
1665
- *
1666
- * @returns The same WritableSignal instance, now synchronized across tabs
1667
- *
1888
+ * @example tabSync(signal('dark), {id: 'theme})
1889
+ */
1890
+ declare function tabSync<T extends WritableSignal<any>>(sig: T, opt: SyncSignalOptions | string): T;
1891
+ /**
1892
+ * @deprecated Use `tabSync` with `SyncSignalOptions` instead and pass the options as the second argument
1668
1893
  * @throws {Error} When deterministic ID generation fails and no explicit ID is provided
1669
- *
1670
- * @example
1671
- * ```typescript
1672
- * // Basic usage - auto-generates channel ID from call site
1673
- * const theme = tabSync(signal('dark'));
1674
- *
1675
- * // With explicit ID (recommended for production)
1676
- * const userPrefs = tabSync(signal({ lang: 'en' }), { id: 'user-preferences' });
1677
- *
1678
- * // Changes in one tab will sync to all other tabs
1679
- * theme.set('light'); // All tabs will update to 'light'
1680
- * ```
1681
- *
1682
- * @remarks
1683
- * - Only works in browser environments (returns original signal on server)
1684
- * - Uses a single BroadcastChannel for all synchronized signals
1685
- * - Automatically cleans up listeners when the injection context is destroyed
1686
- * - Initial signal value after sync setup is not broadcasted to prevent loops
1687
- *
1688
1894
  */
1689
- declare function tabSync<T extends WritableSignal<any>>(sig: T, opt?: SyncSignalOptions): T;
1895
+ declare function tabSync<T extends WritableSignal<any>>(sig: T, opt?: LegacySyncSignalOptions): T;
1690
1896
 
1691
1897
  /**
1692
1898
  * Options for creating a throttled writable signal.
@@ -1707,6 +1913,18 @@ type CreateThrottledOptions<T> = CreateSignalOptions<T> & {
1707
1913
  * If it is not provided or injected, the timer will not be cleared automatically...which is usually fine :)
1708
1914
  */
1709
1915
  destroyRef?: DestroyRef;
1916
+ /**
1917
+ * If `true`, the throttled signal emits the first value immediately when a
1918
+ * burst starts, then enforces the cooldown window before the next emission.
1919
+ * @default false
1920
+ */
1921
+ leading?: boolean;
1922
+ /**
1923
+ * If `true`, the throttled signal emits the latest pending value at the end
1924
+ * of each cooldown window (only when at least one write occurred during it).
1925
+ * @default true
1926
+ */
1927
+ trailing?: boolean;
1710
1928
  };
1711
1929
  /**
1712
1930
  * A specialized `WritableSignal` whose publicly readable value updates are throttled.
@@ -1947,7 +2165,7 @@ type CreateHistoryOptions<T> = Omit<CreateSignalOptions<T[]>, 'equal'> & {
1947
2165
  * console.log('Can undo:', name.canUndo()); // false
1948
2166
  * ```
1949
2167
  */
1950
- declare function withHistory<T>(source: WritableSignal<T>, opt?: CreateHistoryOptions<T>): SignalWithHistory<T>;
2168
+ declare function withHistory<T>(sourceOrValue: WritableSignal<T> | T, opt?: CreateHistoryOptions<T>): SignalWithHistory<T>;
1951
2169
 
1952
- export { chunked, combineWith, debounce, debounced, derived, distinct, elementSize, elementVisibility, filter, indexArray, isDerivation, isMutable, isStore, keyArray, map, mapArray, mapObject, mediaQuery, mousePosition, mutable, mutableStore, nestedEffect, networkStatus, pageVisibility, pipeable, piped, pooled, pooledArray, pooledMap, pooledSet, prefersDarkMode, prefersReducedMotion, scrollPosition, select, sensor, sensors, store, stored, tabSync, tap, throttle, throttled, toFakeDerivation, toFakeSignalDerivation, toStore, toWritable, until, windowSize, withHistory };
1953
- export type { Computation, CreateChunkedOptions, CreateDebouncedOptions, CreateHistoryOptions, CreatePooledOptions, CreateProvidedPooledOptions, CreateStoredOptions, CreateThrottledOptions, DebouncedSignal, DerivedSignal, ElementSize, ElementSizeOptions, ElementSizeSignal, ElementVisibilityOptions, ElementVisibilitySignal, MousePositionOptions, MousePositionSignal, MutableSignal, MutableSignalStore, NetworkStatusSignal, PipeableSignal, ScrollPosition, ScrollPositionOptions, ScrollPositionSignal, SignalStore, SignalWithHistory, StoredSignal, ThrottledSignal, UntilOptions, WindowSize, WindowSizeOptions, WindowSizeSignal, WritableSignalStore };
2170
+ export { batteryStatus, chunked, clipboard, combineWith, debounce, debounced, derived, distinct, elementSize, elementVisibility, filter, filterWith, focusWithin, geolocation, idle, indexArray, isDerivation, isMutable, isStore, keyArray, map, mapArray, mapObject, mediaQuery, mousePosition, mutable, mutableStore, nestedEffect, networkStatus, orientation, pageVisibility, pairwise, pipeable, piped, pooled, pooledArray, pooledMap, pooledSet, prefersDarkMode, prefersReducedMotion, scan, scrollPosition, select, sensor, sensors, signalFromEvent, startWith, store, stored, tabSync, tap, throttle, throttled, toFakeDerivation, toFakeSignalDerivation, toStore, toWritable, until, windowSize, withHistory };
2171
+ export type { BatteryStatus, ClipboardSignal, Computation, CreateChunkedOptions, CreateDebouncedOptions, CreateHistoryOptions, CreatePooledOptions, CreateProvidedPooledOptions, CreateStoredOptions, CreateThrottledOptions, DebouncedSignal, DerivedSignal, ElementSize, ElementSizeOptions, ElementSizeSignal, ElementVisibilityOptions, ElementVisibilitySignal, GeolocationOptions, GeolocationSignal, IdleOptions, IdleSignal, MousePositionOptions, MousePositionSignal, MutableSignal, MutableSignalStore, NetworkStatusSignal, PipeableSignal, ScreenOrientation, ScrollPosition, ScrollPositionOptions, ScrollPositionSignal, SignalFromEventOptions, SignalStore, SignalWithHistory, StoredSignal, ThrottledSignal, UntilOptions, WindowSize, WindowSizeOptions, WindowSizeSignal, WritableSignalStore };