react-native-audio-browser 0.2.0-next.62 → 0.2.0-next.64

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.
Files changed (48) hide show
  1. package/android/src/main/java/com/audiobrowser/browser/BrowserManager.kt +16 -6
  2. package/android/src/main/java/com/audiobrowser/player/PlaybackStateMachine.kt +9 -6
  3. package/android/src/main/java/com/audiobrowser/player/PlayerListener.kt +0 -5
  4. package/android/src/test/java/com/audiobrowser/browser/BrowserManagerInvalidationTest.kt +14 -0
  5. package/android/src/test/java/com/audiobrowser/player/PlaybackStateMachineTest.kt +7 -6
  6. package/android/src/test/java/com/audiobrowser/player/PlayerRetryTest.kt +35 -0
  7. package/ios/Browser/BrowserManager.swift +22 -7
  8. package/ios/HybridAudioBrowser+Browser.swift +8 -4
  9. package/ios/HybridAudioBrowser+Options.swift +14 -0
  10. package/ios/HybridAudioBrowser.swift +11 -5
  11. package/lib/commonjs/CLAUDE.md +2 -2
  12. package/lib/commonjs/features/sleepTimer.js +8 -12
  13. package/lib/commonjs/features/sleepTimer.js.map +1 -1
  14. package/lib/commonjs/utils/LazyNativeEmitter.js +3 -3
  15. package/lib/commonjs/utils/NativeUpdatedValue.js +10 -13
  16. package/lib/commonjs/utils/NativeUpdatedValue.js.map +1 -1
  17. package/lib/commonjs/utils/useNativeUpdatedValue.js +12 -20
  18. package/lib/commonjs/utils/useNativeUpdatedValue.js.map +1 -1
  19. package/lib/commonjs/utils/useNativeValueRefreshedBy.js +10 -5
  20. package/lib/commonjs/utils/useNativeValueRefreshedBy.js.map +1 -1
  21. package/lib/module/CLAUDE.md +2 -2
  22. package/lib/module/features/sleepTimer.js +9 -13
  23. package/lib/module/features/sleepTimer.js.map +1 -1
  24. package/lib/module/utils/LazyNativeEmitter.js +3 -3
  25. package/lib/module/utils/NativeUpdatedValue.js +10 -13
  26. package/lib/module/utils/NativeUpdatedValue.js.map +1 -1
  27. package/lib/module/utils/useNativeUpdatedValue.js +13 -21
  28. package/lib/module/utils/useNativeUpdatedValue.js.map +1 -1
  29. package/lib/module/utils/useNativeValueRefreshedBy.js +11 -6
  30. package/lib/module/utils/useNativeValueRefreshedBy.js.map +1 -1
  31. package/lib/typescript/src/features/sleepTimer.d.ts.map +1 -1
  32. package/lib/typescript/src/utils/LazyNativeEmitter.d.ts +3 -3
  33. package/lib/typescript/src/utils/NativeUpdatedValue.d.ts +4 -13
  34. package/lib/typescript/src/utils/NativeUpdatedValue.d.ts.map +1 -1
  35. package/lib/typescript/src/utils/useNativeUpdatedValue.d.ts +4 -4
  36. package/lib/typescript/src/utils/useNativeUpdatedValue.d.ts.map +1 -1
  37. package/lib/typescript/src/utils/useNativeValueRefreshedBy.d.ts +3 -4
  38. package/lib/typescript/src/utils/useNativeValueRefreshedBy.d.ts.map +1 -1
  39. package/package.json +1 -1
  40. package/src/CLAUDE.md +2 -2
  41. package/src/features/sleepTimer.test.ts +25 -26
  42. package/src/features/sleepTimer.ts +9 -9
  43. package/src/utils/LazyNativeEmitter.ts +3 -3
  44. package/src/utils/NativeUpdatedValue.ts +10 -13
  45. package/src/utils/useNativeUpdatedValue.test.ts +30 -22
  46. package/src/utils/useNativeUpdatedValue.ts +13 -17
  47. package/src/utils/useNativeValueRefreshedBy.test.ts +68 -0
  48. package/src/utils/useNativeValueRefreshedBy.ts +13 -6
@@ -39,8 +39,8 @@ error until it was unified.
39
39
  Two emitter classes, picked by what the event _is_:
40
40
 
41
41
  - `NativeUpdatedValue` — state with a current value (playback, options,
42
- progress). Subscribes to native at module load so no update is missed, and
43
- caches `lastValue` so hooks can read synchronously.
42
+ progress). Subscribes to native at module load so no update is missed; hooks
43
+ read the matching getter, never a cached payload.
44
44
  - `LazyNativeEmitter` — discrete events with no current value (remote controls,
45
45
  gate hits). Assigns the native callback on the first `addListener`, so an
46
46
  event nobody listens to costs nothing.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
 
3
- import { useEffect, useState } from 'react';
3
+ import { useEffect, useRef, useState } from 'react';
4
4
  import { nativeBrowser } from '../native';
5
5
  import { NativeUpdatedValue } from '../utils/NativeUpdatedValue';
6
6
 
@@ -85,15 +85,12 @@ export const onSleepTimerChanged = NativeUpdatedValue.emitterize(cb => nativeBro
85
85
  * @returns true if any sleep timer is active, false otherwise
86
86
  */
87
87
  export function useSleepTimerActive() {
88
+ const eventCountAtRender = useRef(onSleepTimerChanged.eventCount);
88
89
  const [isActive, setIsActive] = useState(() => getSleepTimer() !== null);
89
90
  useEffect(() => {
90
- // Re-sync before subscribing: a change between the initial read above and this effect would
91
- // otherwise be missed for good. `undefined` means nothing has fired yet, whereas `null` is a
92
- // real value — the timer being cleared — so it has to re-sync too.
93
- const {
94
- lastValue
95
- } = onSleepTimerChanged;
96
- if (lastValue !== undefined) setIsActive(lastValue !== null);
91
+ if (onSleepTimerChanged.eventCount !== eventCountAtRender.current) {
92
+ setIsActive(getSleepTimer() !== null);
93
+ }
97
94
  return onSleepTimerChanged.addListener(timer => {
98
95
  setIsActive(timer !== null);
99
96
  });
@@ -114,17 +111,16 @@ export function useSleepTimerActive() {
114
111
  export function useSleepTimer(params) {
115
112
  const updateInterval = params?.updateInterval ?? 1000;
116
113
  const inactive = params?.inactive ?? false;
114
+ const eventCountAtRender = useRef(onSleepTimerChanged.eventCount);
117
115
  const [state, setState] = useState(() => {
118
116
  const timerState = getSleepTimer();
119
117
  return addSecondsLeft(timerState);
120
118
  });
121
119
  const time = state && 'time' in state ? state.time : undefined;
122
120
  useEffect(() => {
123
- // Re-sync before subscribing — see useSleepTimerActive.
124
- const {
125
- lastValue
126
- } = onSleepTimerChanged;
127
- if (lastValue !== undefined) setState(addSecondsLeft(lastValue));
121
+ if (onSleepTimerChanged.eventCount !== eventCountAtRender.current) {
122
+ setState(addSecondsLeft(getSleepTimer()));
123
+ }
128
124
  return onSleepTimerChanged.addListener(event => {
129
125
  setState(addSecondsLeft(event));
130
126
  });
@@ -1 +1 @@
1
- {"version":3,"names":["useEffect","useState","nativeBrowser","NativeUpdatedValue","getSleepTimer","setSleepTimer","seconds","options","fadeDuration","setSleepTimerToEndOfTrack","clearSleepTimer","onSleepTimerChanged","emitterize","cb","useSleepTimerActive","isActive","setIsActive","lastValue","undefined","addListener","timer","useSleepTimer","params","updateInterval","inactive","state","setState","timerState","addSecondsLeft","time","event","update","sleepTimer","result","secondsLeft","clear","initialTimeoutId","setTimeout","intervalId","setInterval","clearTimeout","clearInterval","Math","max","round","Date","now"],"sourceRoot":"../../../src","sources":["features/sleepTimer.ts"],"mappings":";;AAAA,SAASA,SAAS,EAAEC,QAAQ,QAAQ,OAAO;AAC3C,SAASC,aAAa,QAAQ,WAAW;AACzC,SAASC,kBAAkB,QAAQ,6BAA6B;;AAEhE;;AAEA;AACA;AACA;;AAMA;AACA;AACA;;AAMA;AACA;AACA;AACA;AACA;AACA;;AAGA;AACA;AACA;AACA;;AAGA;;AAEA;AACA;AACA;AACA,OAAO,SAASC,aAAaA,CAAA,EAAe;EAC1C,OAAOF,aAAa,CAACE,aAAa,CAAC,CAAC;AACtC;;AAEA;AACA;AACA;;AAaA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,aAAaA,CAC3BC,OAAe,EACfC,OAA2B,EACrB;EACNL,aAAa,CAACG,aAAa,CAACC,OAAO,EAAEC,OAAO,EAAEC,YAAY,CAAC;AAC7D;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,yBAAyBA,CAAA,EAAS;EAChDP,aAAa,CAACO,yBAAyB,CAAC,CAAC;AAC3C;;AAEA;AACA;AACA;AACA;AACA,OAAO,SAASC,eAAeA,CAAA,EAAY;EACzC,OAAOR,aAAa,CAACQ,eAAe,CAAC,CAAC;AACxC;;AAEA;;AAEA;AACA;AACA;AACA;AACA,OAAO,MAAMC,mBAAmB,GAAGR,kBAAkB,CAACS,UAAU,CAC7DC,EAAE,IAAMX,aAAa,CAACS,mBAAmB,GAAGE,EAC/C,CAAC;;AAED;;AAEA;AACA;AACA;AAC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,IAAI,CAACC,GAAG,CAAC,CAAC,EAAED,IAAI,CAACE,KAAK,CAAC,CAACnB,KAAK,CAACI,IAAI,GAAGgB,IAAI,CAACC,GAAG,CAAC,CAAC,IAAI,IAAI,CAAC;IACvE,CAAC;EACH;EACA,OAAOrB,KAAK;AACd","ignoreList":[]}
1
+ {"version":3,"names":["useEffect","useRef","useState","nativeBrowser","NativeUpdatedValue","getSleepTimer","setSleepTimer","seconds","options","fadeDuration","setSleepTimerToEndOfTrack","clearSleepTimer","onSleepTimerChanged","emitterize","cb","useSleepTimerActive","eventCountAtRender","eventCount","isActive","setIsActive","current","addListener","timer","useSleepTimer","params","updateInterval","inactive","state","setState","timerState","addSecondsLeft","time","undefined","event","update","sleepTimer","result","secondsLeft","clear","initialTimeoutId","setTimeout","intervalId","setInterval","clearTimeout","clearInterval","Math","max","round","Date","now"],"sourceRoot":"../../../src","sources":["features/sleepTimer.ts"],"mappings":";;AAAA,SAASA,SAAS,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,OAAO;AACnD,SAASC,aAAa,QAAQ,WAAW;AACzC,SAASC,kBAAkB,QAAQ,6BAA6B;;AAEhE;;AAEA;AACA;AACA;;AAMA;AACA;AACA;;AAMA;AACA;AACA;AACA;AACA;AACA;;AAGA;AACA;AACA;AACA;;AAGA;;AAEA;AACA;AACA;AACA,OAAO,SAASC,aAAaA,CAAA,EAAe;EAC1C,OAAOF,aAAa,CAACE,aAAa,CAAC,CAAC;AACtC;;AAEA;AACA;AACA;;AAaA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,aAAaA,CAC3BC,OAAe,EACfC,OAA2B,EACrB;EACNL,aAAa,CAACG,aAAa,CAACC,OAAO,EAAEC,OAAO,EAAEC,YAAY,CAAC;AAC7D;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,yBAAyBA,CAAA,EAAS;EAChDP,aAAa,CAACO,yBAAyB,CAAC,CAAC;AAC3C;;AAEA;AACA;AACA;A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AAEG,aAAa,CAACH,UAAU,CAAC;IAC3C,CAAC;IACDP,MAAM,CAAC,CAAC;IACR,OAAOI,KAAK;EACd,CAAC,EAAE,CAACZ,QAAQ,EAAEK,IAAI,EAAEN,cAAc,CAAC,CAAC;EAEpC,OAAOE,KAAK;AACd;;AAEA;;AAEA;AACA;AACA;AACA;AACA;;AAKA,SAASG,cAAcA,CAACH,KAAiB,EAA+B;EACtE,IAAI,CAACA,KAAK,EAAE,OAAOK,SAAS;EAC5B,IAAI,MAAM,IAAIL,KAAK,EAAE;IACnB,OAAO;MACL,GAAGA,KAAK;MACRU,WAAW,EAAEQ,IAAI,CAACC,GAAG,CAAC,CAAC,EAAED,IAAI,CAACE,KAAK,CAAC,CAACpB,KAAK,CAACI,IAAI,GAAGiB,IAAI,CAACC,GAAG,CAAC,CAAC,IAAI,IAAI,CAAC;IACvE,CAAC;EACH;EACA,OAAOtB,KAAK;AACd","ignoreList":[]}
@@ -5,9 +5,9 @@
5
5
  * `onRemoteSeek`, …) and `onGate`. Subscribe with {@link addListener}; the
6
6
  * returned function unsubscribes. The emitter is not callable itself.
7
7
  *
8
- * Native starts delivering the event only once the first listener is added,
9
- * and nothing is cached: there is no `lastValue`. The other `on*` exports use
10
- * `NativeUpdatedValue`, which listens from module load.
8
+ * Native starts delivering the event only once the first listener is added.
9
+ * The other `on*` exports use `NativeUpdatedValue`, which listens from module
10
+ * load.
11
11
  *
12
12
  * @example
13
13
  * ```typescript
@@ -5,14 +5,10 @@
5
5
  * `onQueueChanged`, `onTrackMetadata`, …). Subscribe with {@link addListener};
6
6
  * the returned function unsubscribes. The emitter is not callable itself.
7
7
  *
8
- * It listens to native from module load and keeps the latest payload in
9
- * {@link lastValue}. The remote-control exports and `onGate` use
10
- * `LazyNativeEmitter`, which listens only once a listener is added.
11
- *
12
- * `lastValue` is the last payload native sent, not a fresh reading: it is
13
- * `undefined` before the first event, and for the queue and active track it
14
- * can be stale after an in-place change (a favorite toggle). Use the getter
15
- * (`getQueue`, `getActiveTrack`, …) when you need the current value.
8
+ * It listens to native from module load. The remote-control exports and
9
+ * `onGate` use `LazyNativeEmitter`, which listens only once a listener is
10
+ * added. It keeps no payload: use the getter (`getQueue`, `getActiveTrack`, …)
11
+ * for the current value.
16
12
  *
17
13
  * @example
18
14
  * ```typescript
@@ -29,16 +25,17 @@ export class NativeUpdatedValue {
29
25
  listeners = new Set();
30
26
 
31
27
  /**
32
- * The last payload native sent, or `undefined` before the first. Not a fresh
33
- * reading: use the matching getter (`getQueue`, …) for the current value.
28
+ * Events received so far, so a hook can tell whether one landed between its
29
+ * render and its subscription.
30
+ * @internal
34
31
  */
35
- lastValue = undefined;
32
+ eventCount = 0;
36
33
 
37
34
  /** @internal */
38
35
  constructor(setter) {
39
36
  // Install native callback immediately at module load
40
37
  setter(data => {
41
- this.lastValue = data;
38
+ this.eventCount++;
42
39
  for (const listener of this.listeners) {
43
40
  listener(data);
44
41
  }
@@ -60,7 +57,7 @@ export class NativeUpdatedValue {
60
57
  * Creates a native value subscription.
61
58
  *
62
59
  * @param setter - Function that sets the native callback property
63
- * @returns The subscription instance with lastValue and addListener
60
+ * @returns The subscription instance
64
61
  * @internal
65
62
  */
66
63
  static emitterize(setter) {
@@ -1 +1 @@
1
- {"version":3,"names":["NativeUpdatedValue","listeners","Set","lastValue","undefined","constructor","setter","data","listener","addListener","callback","add","delete","emitterize"],"sourceRoot":"../../../src","sources":["utils/NativeUpdatedValue.ts"],"mappings":";;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMA,kBAAkB,CAAI;EACzBC,SAAS,GAAG,IAAIC,GAAG,CAAc,CAAC;;EAE1C;AACF;AACA;AACA;EACEC,SAAS,GAAkBC,SAAS;;EAEpC;EACAC,WAAWA,CAACC,MAA6C,EAAE;IACzD;IACAA,MAAM,CAAEC,IAAO,IAAK;MAClB,IAAI,CAACJ,SAAS,GAAGI,IAAI;MACrB,KAAK,MAAMC,QAAQ,IAAI,IAAI,CAACP,SAAS,EAAE;QACrCO,QAAQ,CAACD,IAAI,CAAC;MAChB;IACF,CAAC,CAAC;EACJ;;EAEA;AACF;AACA;AACA;AACA;AACA;EACEE,WAAWA,CAACC,QAAqB,EAAc;IAC7C,IAAI,CAACT,SAAS,CAACU,GAAG,CAACD,QAAQ,CAAC;IAC5B,OAAO,MAAM,IAAI,CAACT,SAAS,CAACW,MAAM,CAACF,QAAQ,CAAC;EAC9C;;EAEA;AACF;AACA;AACA;AACA;AACA;AACA;EACE,OAAOG,UAAUA,CACfP,MAA6C,EACtB;IACvB,OAAO,IAAIN,kBAAkB,CAACM,MAAM,CAAC;EACvC;AACF","ignoreList":[]}
1
+ {"version":3,"names":["NativeUpdatedValue","listeners","Set","eventCount","constructor","setter","data","listener","addListener","callback","add","delete","emitterize"],"sourceRoot":"../../../src","sources":["utils/NativeUpdatedValue.ts"],"mappings":";;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMA,kBAAkB,CAAI;EACzBC,SAAS,GAAG,IAAIC,GAAG,CAAc,CAAC;;EAE1C;AACF;AACA;AACA;AACA;EACEC,UAAU,GAAG,CAAC;;EAEd;EACAC,WAAWA,CAACC,MAA6C,EAAE;IACzD;IACAA,MAAM,CAAEC,IAAO,IAAK;MAClB,IAAI,CAACH,UAAU,EAAE;MACjB,KAAK,MAAMI,QAAQ,IAAI,IAAI,CAACN,SAAS,EAAE;QACrCM,QAAQ,CAACD,IAAI,CAAC;MAChB;IACF,CAAC,CAAC;EACJ;;EAEA;AACF;AACA;AACA;AACA;AACA;EACEE,WAAWA,CAACC,QAAqB,EAAc;IAC7C,IAAI,CAACR,SAAS,CAACS,GAAG,CAACD,QAAQ,CAAC;IAC5B,OAAO,MAAM,IAAI,CAACR,SAAS,CAACU,MAAM,CAACF,QAAQ,CAAC;EAC9C;;EAEA;AACF;AACA;AACA;AACA;AACA;AACA;EACE,OAAOG,UAAUA,CACfP,MAA6C,EACtB;IACvB,OAAO,IAAIL,kBAAkB,CAACK,MAAM,CAAC;EACvC;AACF","ignoreList":[]}
@@ -1,39 +1,31 @@
1
1
  "use strict";
2
2
 
3
- import { useEffect, useState } from 'react';
3
+ import { useEffect, useRef, useState } from 'react';
4
4
  /**
5
5
  * Generic hook for values that are fetched from native and updated via callbacks.
6
6
  *
7
- * Reads the initial value from emitter.lastValue to avoid race conditions between
8
- * initial getter() call and subscription setup.
7
+ * Reads the getter on mount, and again on subscribing only if an event landed
8
+ * since, so a change between render and subscription is not missed.
9
9
  *
10
- * @param getter - Fallback synchronous function to get the current value if emitter has no lastValue yet
11
- * @param emitter - NativeUpdatedValue that stores lastValue and provides subscription
10
+ * @param getter - Synchronous function returning the current value
11
+ * @param emitter - NativeUpdatedValue that provides the subscription
12
12
  * @param eventKey - Optional key to extract from the event object passed to the callback
13
13
  * @returns The current value, updated when the callback fires
14
14
  */
15
15
  export function useNativeUpdatedValue(getter, emitter, eventKey) {
16
- const [value, setValue] = useState(() => {
17
- const {
18
- lastValue
19
- } = emitter;
20
- if (lastValue != null) {
21
- return eventKey !== undefined ? lastValue[eventKey] : lastValue;
22
- }
23
- return getter();
24
- });
16
+ const eventCountAtRender = useRef(emitter.eventCount);
17
+ const [value, setValue] = useState(getter);
25
18
  useEffect(() => {
26
- const {
27
- lastValue
28
- } = emitter;
29
- if (lastValue != null) {
30
- const newValue = eventKey !== undefined ? lastValue[eventKey] : lastValue;
31
- setValue(newValue);
32
- }
19
+ if (emitter.eventCount !== eventCountAtRender.current) setValue(getter);
20
+ // A re-run, for another emitter or eventKey, always reads afresh.
21
+ eventCountAtRender.current = NaN;
33
22
  return emitter.addListener(event => {
34
23
  const newValue = eventKey !== undefined ? event[eventKey] : event;
35
24
  setValue(newValue);
36
25
  });
26
+ // The getter is read once per subscription: an inline one, new each render,
27
+ // would otherwise overwrite every event with a re-read.
28
+ // eslint-disable-next-line react-hooks/exhaustive-deps
37
29
  }, [emitter, eventKey]);
38
30
  return value;
39
31
  }
@@ -1 +1 @@
1
- {"version":3,"names":["useEffect","useState","useNativeUpdatedValue","getter","emitter","eventKey","value","setValue","lastValue","undefined","newValue","addListener","event"],"sourceRoot":"../../../src","sources":["utils/useNativeUpdatedValue.ts"],"mappings":";;AAAA,SAASA,SAAS,EAAEC,QAAQ,QAAQ,OAAO;AAG3C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,qBAAqBA,CACnCC,MAAe,EACfC,OAA8B,EAC9BC,QAAkB,EACf;EACH,MAAM,CAACC,KAAK,EAAEC,QAAQ,CAAC,GAAGN,QAAQ,CAAC,MAAM;IACvC,MAAM;MAAEO;IAAU,CAAC,GAAGJ,OAAO;IAC7B,IAAII,SAAS,IAAI,IAAI,EAAE;MACrB,OAAQH,QAAQ,KAAKI,SAAS,GAAGD,SAAS,CAACH,QAAQ,CAAC,GAAGG,SAAS;IAClE;IACA,OAAOL,MAAM,CAAC,CAAC;EACjB,CAAC,CAAC;EAEFH,SAAS,CAAC,MAAM;IACd,MAAM;MAAEQ;IAAU,CAAC,GAAGJ,OAAO;IAC7B,IAAII,SAAS,IAAI,IAAI,EAAE;MACrB,MAAME,QAAQ,GAAGL,QAAQ,KAAKI,SAAS,GAAGD,SAAS,CAACH,QAAQ,CAAC,GAAGG,SAAS;MACzED,QAAQ,CAACG,QAAa,CAAC;IACzB;IACA,OAAON,OAAO,CAACO,WAAW,CAAEC,KAAK,IAAK;MACpC,MAAMF,QAAQ,GAAGL,QAAQ,KAAKI,SAAS,GAAGG,KAAK,CAACP,QAAQ,CAAC,GAAGO,KAAK;MACjEL,QAAQ,CAACG,QAAa,CAAC;IACzB,CAAC,CAAC;EACJ,CAAC,EAAE,CAACN,OAAO,EAAEC,QAAQ,CAAC,CAAC;EAEvB,OAAOC,KAAK;AACd","ignoreList":[]}
1
+ {"version":3,"names":["useEffect","useRef","useState","useNativeUpdatedValue","getter","emitter","eventKey","eventCountAtRender","eventCount","value","setValue","current","NaN","addListener","event","newValue","undefined"],"sourceRoot":"../../../src","sources":["utils/useNativeUpdatedValue.ts"],"mappings":";;AAAA,SAASA,SAAS,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,OAAO;AAGnD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,qBAAqBA,CACnCC,MAAe,EACfC,OAA8B,EAC9BC,QAAkB,EACf;EACH,MAAMC,kBAAkB,GAAGN,MAAM,CAACI,OAAO,CAACG,UAAU,CAAC;EACrD,MAAM,CAACC,KAAK,EAAEC,QAAQ,CAAC,GAAGR,QAAQ,CAACE,MAAM,CAAC;EAE1CJ,SAAS,CAAC,MAAM;IACd,IAAIK,OAAO,CAACG,UAAU,KAAKD,kBAAkB,CAACI,OAAO,EAAED,QAAQ,CAACN,MAAM,CAAC;IACvE;IACAG,kBAAkB,CAACI,OAAO,GAAGC,GAAG;IAChC,OAAOP,OAAO,CAACQ,WAAW,CAAEC,KAAK,IAAK;MACpC,MAAMC,QAAQ,GAAGT,QAAQ,KAAKU,SAAS,GAAGF,KAAK,CAACR,QAAQ,CAAC,GAAGQ,KAAK;MACjEJ,QAAQ,CAACK,QAAa,CAAC;IACzB,CAAC,CAAC;IACF;IACA;IACA;EACF,CAAC,EAAE,CAACV,OAAO,EAAEC,QAAQ,CAAC,CAAC;EAEvB,OAAOG,KAAK;AACd","ignoreList":[]}
@@ -1,25 +1,30 @@
1
1
  "use strict";
2
2
 
3
- import { useEffect, useState } from 'react';
3
+ import { useEffect, useRef, useState } from 'react';
4
+ const eventCountOf = emitters => emitters.reduce((sum, emitter) => sum + emitter.eventCount, 0);
5
+
4
6
  /**
5
7
  * Hook for a native-fetched value that must stay fresh across SEVERAL event
6
8
  * sources — e.g. the active track, which changes on a queue transition
7
9
  * (`onActiveTrackChanged`) but is also mutated in place by a favorite toggle
8
10
  * (`onFavoriteChanged`).
9
11
  *
10
- * Unlike {@link useNativeUpdatedValue}, this never reads a cached event
11
- * payload: every trigger re-reads `getter()`, and the mount effect re-reads
12
- * once after the subscriptions attach (so an event firing between render and
13
- * effect cannot be missed). The getters are cheap synchronous Nitro calls.
12
+ * Every trigger re-reads `getter()`, and the mount effect re-reads it only if
13
+ * an event landed since the render read, so none is missed.
14
14
  *
15
15
  * @param getter - Synchronous native getter for the current value
16
16
  * @param emitters - Event sources that invalidate the value (a stable,
17
17
  * module-level array — identity changes resubscribe)
18
18
  */
19
19
  export function useNativeValueRefreshedBy(getter, emitters) {
20
+ const eventCountAtRender = useRef(eventCountOf(emitters));
20
21
  const [value, setValue] = useState(getter);
21
22
  useEffect(() => {
22
- setValue(getter());
23
+ if (eventCountOf(emitters) !== eventCountAtRender.current) {
24
+ setValue(getter());
25
+ }
26
+ // A re-run, for another getter or emitters, always reads afresh.
27
+ eventCountAtRender.current = NaN;
23
28
  const cleanups = emitters.map(emitter => emitter.addListener(() => setValue(getter())));
24
29
  return () => cleanups.forEach(cleanup => cleanup());
25
30
  // eslint-disable-next-line react-hooks/exhaustive-deps
@@ -1 +1 @@
1
- {"version":3,"names":["useEffect","useState","useNativeValueRefreshedBy","getter","emitters","value","setValue","cleanups","map","emitter","addListener","forEach","cleanup"],"sourceRoot":"../../../src","sources":["utils/useNativeValueRefreshedBy.ts"],"mappings":";;AAAA,SAASA,SAAS,EAAEC,QAAQ,QAAQ,OAAO;AAM3C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,yBAAyBA,CACvCC,MAAe,EACfC,QAAiC,EAC9B;EACH,MAAM,CAACC,KAAK,EAAEC,QAAQ,CAAC,GAAGL,QAAQ,CAACE,MAAM,CAAC;EAE1CH,SAAS,CAAC,MAAM;IACdM,QAAQ,CAACH,MAAM,CAAC,CAAC,CAAC;IAClB,MAAMI,QAAQ,GAAGH,QAAQ,CAACI,GAAG,CAAEC,OAAO,IACpCA,OAAO,CAACC,WAAW,CAAC,MAAMJ,QAAQ,CAACH,MAAM,CAAC,CAAC,CAAC,CAC9C,CAAC;IACD,OAAO,MAAMI,QAAQ,CAACI,OAAO,CAAEC,OAAO,IAAKA,OAAO,CAAC,CAAC,CAAC;IACrD;EACF,CAAC,EAAE,CAACT,MAAM,EAAE,GAAGC,QAAQ,CAAC,CAAC;EAEzB,OAAOC,KAAK;AACd","ignoreList":[]}
1
+ {"version":3,"names":["useEffect","useRef","useState","eventCountOf","emitters","reduce","sum","emitter","eventCount","useNativeValueRefreshedBy","getter","eventCountAtRender","value","setValue","current","NaN","cleanups","map","addListener","forEach","cleanup"],"sourceRoot":"../../../src","sources":["utils/useNativeValueRefreshedBy.ts"],"mappings":";;AAAA,SAASA,SAAS,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,OAAO;AAOnD,MAAMC,YAAY,GAAIC,QAAiC,IACrDA,QAAQ,CAACC,MAAM,CAAC,CAACC,GAAG,EAAEC,OAAO,KAAKD,GAAG,GAAGC,OAAO,CAACC,UAAU,EAAE,CAAC,CAAC;;AAEhE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,yBAAyBA,CACvCC,MAAe,EACfN,QAAiC,EAC9B;EACH,MAAMO,kBAAkB,GAAGV,MAAM,CAACE,YAAY,CAACC,QAAQ,CAAC,CAAC;EACzD,MAAM,CAACQ,KAAK,EAAEC,QAAQ,CAAC,GAAGX,QAAQ,CAACQ,MAAM,CAAC;EAE1CV,SAAS,CAAC,MAAM;IACd,IAAIG,YAAY,CAACC,QAAQ,CAAC,KAAKO,kBAAkB,CAACG,OAAO,EAAE;MACzDD,QAAQ,CAACH,MAAM,CAAC,CAAC,CAAC;IACpB;IACA;IACAC,kBAAkB,CAACG,OAAO,GAAGC,GAAG;IAChC,MAAMC,QAAQ,GAAGZ,QAAQ,CAACa,GAAG,CAAEV,OAAO,IACpCA,OAAO,CAACW,WAAW,CAAC,MAAML,QAAQ,CAACH,MAAM,CAAC,CAAC,CAAC,CAC9C,CAAC;IACD,OAAO,MAAMM,QAAQ,CAACG,OAAO,CAAEC,OAAO,IAAKA,OAAO,CAAC,CAAC,CAAC;IACrD;EACF,CAAC,EAAE,CAACV,MAAM,EAAE,GAAGN,QAAQ,CAAC,CAAC;EAEzB,OAAOQ,KAAK;AACd","ignoreList":[]}
@@ -1 +1 @@
1
- {"version":3,"file":"sleepTimer.d.ts","sourceRoot":"","sources":["../../../../src/features/sleepTimer.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,kBAAkB,EAAE,MAAM,6BAA6B,CAAA;AAIhE;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,iEAAiE;IACjE,IAAI,EAAE,MAAM,CAAA;CACb,CAAA;AAED;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC,2DAA2D;IAC3D,oBAAoB,EAAE,OAAO,CAAA;CAC9B,CAAA;AAED;;;;;GAKG;AACH,MAAM,MAAM,UAAU,GAAG,cAAc,GAAG,oBAAoB,GAAG,IAAI,CAAA;AAErE;;;GAGG;AACH,MAAM,MAAM,sBAAsB,GAAG,UAAU,CAAA;AAI/C;;GAEG;AACH,wBAAgB,aAAa,IAAI,UAAU,CAE1C;AAED;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B;;;;;;;OAOG;IACH,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB,CAAA;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAC3B,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,iBAAiB,GAC1B,IAAI,CAEN;AAED;;;;;;;GAOG;AACH,wBAAgB,yBAAyB,IAAI,IAAI,CAEhD;AAED;;;GAGG;AACH,wBAAgB,eAAe,IAAI,OAAO,CAEzC;AAID;;;GAGG;AACH,eAAO,MAAM,mBAAmB,gC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AACH,wBAAgB,eAAe,IAAI,OAAO,CAEzC;AAID;;;GAGG;AACH,eAAO,MAAM,mBAAmB,gCAE/B,CAAA;AAID;;;;;GAKG;AACH,wBAAgB,mBAAmB,IAAI,OAAO,CAc7C;AAED;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAAC,MAAM,CAAC,EAAE;IACrC,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB,GAAG,eAAe,GAAG,SAAS,CA2D9B;AAID;;;;GAIG;AACH,MAAM,MAAM,eAAe,GACvB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,GACrC;IAAE,oBAAoB,EAAE,OAAO,CAAA;CAAE,CAAA"}
@@ -4,9 +4,9 @@ type Callback<T> = (arg: T) => void;
4
4
  * `onRemoteSeek`, …) and `onGate`. Subscribe with {@link addListener}; the
5
5
  * returned function unsubscribes. The emitter is not callable itself.
6
6
  *
7
- * Native starts delivering the event only once the first listener is added,
8
- * and nothing is cached: there is no `lastValue`. The other `on*` exports use
9
- * `NativeUpdatedValue`, which listens from module load.
7
+ * Native starts delivering the event only once the first listener is added.
8
+ * The other `on*` exports use `NativeUpdatedValue`, which listens from module
9
+ * load.
10
10
  *
11
11
  * @example
12
12
  * ```typescript
@@ -4,14 +4,10 @@ type Callback<T> = (arg: T) => void;
4
4
  * `onQueueChanged`, `onTrackMetadata`, …). Subscribe with {@link addListener};
5
5
  * the returned function unsubscribes. The emitter is not callable itself.
6
6
  *
7
- * It listens to native from module load and keeps the latest payload in
8
- * {@link lastValue}. The remote-control exports and `onGate` use
9
- * `LazyNativeEmitter`, which listens only once a listener is added.
10
- *
11
- * `lastValue` is the last payload native sent, not a fresh reading: it is
12
- * `undefined` before the first event, and for the queue and active track it
13
- * can be stale after an in-place change (a favorite toggle). Use the getter
14
- * (`getQueue`, `getActiveTrack`, …) when you need the current value.
7
+ * It listens to native from module load. The remote-control exports and
8
+ * `onGate` use `LazyNativeEmitter`, which listens only once a listener is
9
+ * added. It keeps no payload: use the getter (`getQueue`, `getActiveTrack`, …)
10
+ * for the current value.
15
11
  *
16
12
  * @example
17
13
  * ```typescript
@@ -26,11 +22,6 @@ type Callback<T> = (arg: T) => void;
26
22
  */
27
23
  export declare class NativeUpdatedValue<T> {
28
24
  private listeners;
29
- /**
30
- * The last payload native sent, or `undefined` before the first. Not a fresh
31
- * reading: use the matching getter (`getQueue`, …) for the current value.
32
- */
33
- lastValue: T | undefined;
34
25
  /**
35
26
  * Subscribes to value changes.
36
27
  *
@@ -1 +1 @@
1
- {"version":3,"file":"NativeUpdatedValue.d.ts","sourceRoot":"","sources":["../../../../src/utils/NativeUpdatedValue.ts"],"names":[],"mappings":"AAAA,KAAK,QAAQ,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,KAAK,IAAI,CAAA;AAEnC;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,kBAAkB,CAAC,CAAC;IAC/B,OAAO,CAAC,SAAS,CAAyB;IAE1C;;;OAGG;IACH,SAAS,EAAE,CAAC,GAAG,SAAS,CAAY;IAapC;;;;;OAKG;IACH,WAAW,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,MAAM,IAAI,CAG7C;CAcF"}
1
+ {"version":3,"file":"NativeUpdatedValue.d.ts","sourceRoot":"","sources":["../../../../src/utils/NativeUpdatedValue.ts"],"names":[],"mappings":"AAAA,KAAK,QAAQ,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,KAAK,IAAI,CAAA;AAEnC;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,kBAAkB,CAAC,CAAC;IAC/B,OAAO,CAAC,SAAS,CAAyB;IAoB1C;;;;;OAKG;IACH,WAAW,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,MAAM,IAAI,CAG7C;CAcF"}
@@ -2,11 +2,11 @@ import { NativeUpdatedValue } from './NativeUpdatedValue';
2
2
  /**
3
3
  * Generic hook for values that are fetched from native and updated via callbacks.
4
4
  *
5
- * Reads the initial value from emitter.lastValue to avoid race conditions between
6
- * initial getter() call and subscription setup.
5
+ * Reads the getter on mount, and again on subscribing only if an event landed
6
+ * since, so a change between render and subscription is not missed.
7
7
  *
8
- * @param getter - Fallback synchronous function to get the current value if emitter has no lastValue yet
9
- * @param emitter - NativeUpdatedValue that stores lastValue and provides subscription
8
+ * @param getter - Synchronous function returning the current value
9
+ * @param emitter - NativeUpdatedValue that provides the subscription
10
10
  * @param eventKey - Optional key to extract from the event object passed to the callback
11
11
  * @returns The current value, updated when the callback fires
12
12
  */
@@ -1 +1 @@
1
- {"version":3,"file":"useNativeUpdatedValue.d.ts","sourceRoot":"","sources":["../../../../src/utils/useNativeUpdatedValue.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAA;AAEzD;;;;;;;;;;GAUG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAC5C,MAAM,EAAE,MAAM,CAAC,EACf,OAAO,EAAE,kBAAkB,CAAC,CAAC,CAAC,EAC9B,QAAQ,CAAC,EAAE,MAAM,CAAC,GACjB,CAAC,CAsBH"}
1
+ {"version":3,"file":"useNativeUpdatedValue.d.ts","sourceRoot":"","sources":["../../../../src/utils/useNativeUpdatedValue.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAA;AAEzD;;;;;;;;;;GAUG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAC5C,MAAM,EAAE,MAAM,CAAC,EACf,OAAO,EAAE,kBAAkB,CAAC,CAAC,CAAC,EAC9B,QAAQ,CAAC,EAAE,MAAM,CAAC,GACjB,CAAC,CAkBH"}
@@ -1,5 +1,6 @@
1
1
  type Subscribable = {
2
2
  addListener(callback: (event: unknown) => void): () => void;
3
+ eventCount: number;
3
4
  };
4
5
  /**
5
6
  * Hook for a native-fetched value that must stay fresh across SEVERAL event
@@ -7,10 +8,8 @@ type Subscribable = {
7
8
  * (`onActiveTrackChanged`) but is also mutated in place by a favorite toggle
8
9
  * (`onFavoriteChanged`).
9
10
  *
10
- * Unlike {@link useNativeUpdatedValue}, this never reads a cached event
11
- * payload: every trigger re-reads `getter()`, and the mount effect re-reads
12
- * once after the subscriptions attach (so an event firing between render and
13
- * effect cannot be missed). The getters are cheap synchronous Nitro calls.
11
+ * Every trigger re-reads `getter()`, and the mount effect re-reads it only if
12
+ * an event landed since the render read, so none is missed.
14
13
  *
15
14
  * @param getter - Synchronous native getter for the current value
16
15
  * @param emitters - Event sources that invalidate the value (a stable,
@@ -1 +1 @@
1
- {"version":3,"file":"useNativeValueRefreshedBy.d.ts","sourceRoot":"","sources":["../../../../src/utils/useNativeValueRefreshedBy.ts"],"names":[],"mappings":"AAEA,KAAK,YAAY,GAAG;IAClB,WAAW,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,GAAG,MAAM,IAAI,CAAA;CAC5D,CAAA;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,yBAAyB,CAAC,CAAC,EACzC,MAAM,EAAE,MAAM,CAAC,EACf,QAAQ,EAAE,SAAS,YAAY,EAAE,GAChC,CAAC,CAaH"}
1
+ {"version":3,"file":"useNativeValueRefreshedBy.d.ts","sourceRoot":"","sources":["../../../../src/utils/useNativeValueRefreshedBy.ts"],"names":[],"mappings":"AAEA,KAAK,YAAY,GAAG;IAClB,WAAW,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,GAAG,MAAM,IAAI,CAAA;IAC3D,UAAU,EAAE,MAAM,CAAA;CACnB,CAAA;AAKD;;;;;;;;;;;;GAYG;AACH,wBAAgB,yBAAyB,CAAC,CAAC,EACzC,MAAM,EAAE,MAAM,CAAC,EACf,QAAQ,EAAE,SAAS,YAAY,EAAE,GAChC,CAAC,CAkBH"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-audio-browser",
3
- "version": "0.2.0-next.62",
3
+ "version": "0.2.0-next.64",
4
4
  "description": "react-native-audio-browser is a react native package built with Nitro",
5
5
  "keywords": [
6
6
  "react-native",
package/src/CLAUDE.md CHANGED
@@ -39,8 +39,8 @@ error until it was unified.
39
39
  Two emitter classes, picked by what the event _is_:
40
40
 
41
41
  - `NativeUpdatedValue` — state with a current value (playback, options,
42
- progress). Subscribes to native at module load so no update is missed, and
43
- caches `lastValue` so hooks can read synchronously.
42
+ progress). Subscribes to native at module load so no update is missed; hooks
43
+ read the matching getter, never a cached payload.
44
44
  - `LazyNativeEmitter` — discrete events with no current value (remote controls,
45
45
  gate hits). Assigns the native callback on the first `addListener`, so an
46
46
  event nobody listens to costs nothing.
@@ -12,21 +12,23 @@ const native = vi.hoisted(() => ({
12
12
 
13
13
  vi.mock('../native', () => ({ nativeBrowser: native }))
14
14
 
15
- const { onSleepTimerChanged, useSleepTimer, useSleepTimerActive } =
16
- await import('./sleepTimer')
15
+ const { useSleepTimer, useSleepTimerActive } = await import('./sleepTimer')
16
+
17
+ /**
18
+ * A first read that sees a live timer while native clears it before the hook
19
+ * subscribes: the clear's event lands mid-render, before any listener exists.
20
+ */
21
+ const clearDuringRead = (): SleepTimer => {
22
+ native.onSleepTimerChanged?.(null)
23
+ return { time: Date.now() + 60_000 }
24
+ }
17
25
 
18
26
  /** Push a value the way native would. */
19
27
  const fire = (timer: SleepTimer) =>
20
28
  act(() => native.onSleepTimerChanged?.(timer))
21
29
 
22
- /**
23
- * The emitter is a module-level singleton, so `lastValue` outlives each test.
24
- * `undefined` is its pre-native state and distinct from `null`, which is a real
25
- * value meaning "no timer" — the distinction the re-sync gate turns on.
26
- */
27
30
  beforeEach(() => {
28
- onSleepTimerChanged.lastValue = undefined
29
- native.getSleepTimer.mockReturnValue(null)
31
+ native.getSleepTimer.mockReset().mockReturnValue(null)
30
32
  })
31
33
  afterEach(cleanup)
32
34
 
@@ -52,23 +54,17 @@ describe('useSleepTimerActive', () => {
52
54
  expect(result.current).toBe(true)
53
55
  })
54
56
 
55
- // The regression from f7a25040. The getter ran during render and saw a live
56
- // timer; the clear landed before the effect subscribed. Without the re-sync
57
- // the UI shows a running timer that is already gone — permanently, since
58
- // nothing reads it again until the next native event.
59
- it('re-syncs a clear that landed between the render read and the effect', () => {
60
- native.getSleepTimer.mockReturnValue({ time: Date.now() + 60_000 })
61
- onSleepTimerChanged.lastValue = null
57
+ // The getter ran during render and saw a live timer; the clear landed
58
+ // before the effect subscribed.
59
+ it('re-reads a clear that landed between the render read and the effect', () => {
60
+ native.getSleepTimer.mockImplementationOnce(clearDuringRead)
62
61
  const { result } = renderHook(() => useSleepTimerActive())
63
62
  expect(result.current).toBe(false)
64
63
  })
65
64
 
66
- // The gate is `!== undefined`, not truthiness: `undefined` means native has
67
- // never spoken, so the getter's answer must stand.
68
- it('keeps the getter value when native has not fired at all', () => {
69
- native.getSleepTimer.mockReturnValue({ time: Date.now() + 60_000 })
70
- const { result } = renderHook(() => useSleepTimerActive())
71
- expect(result.current).toBe(true)
65
+ it('calls the getter once when no event lands before subscribing', () => {
66
+ renderHook(() => useSleepTimerActive())
67
+ expect(native.getSleepTimer).toHaveBeenCalledTimes(1)
72
68
  })
73
69
 
74
70
  it('stops tracking after unmount', () => {
@@ -162,11 +158,14 @@ describe('useSleepTimer', () => {
162
158
  }
163
159
  })
164
160
 
165
- // Same race as useSleepTimerActive, and the reason both hooks carry the gate.
166
- it('re-syncs a clear that landed between the render read and the effect', () => {
167
- native.getSleepTimer.mockReturnValue({ time: Date.now() + 60_000 })
168
- onSleepTimerChanged.lastValue = null
161
+ it('re-reads a clear that landed between the render read and the effect', () => {
162
+ native.getSleepTimer.mockImplementationOnce(clearDuringRead)
169
163
  const { result } = renderHook(() => useSleepTimer())
170
164
  expect(result.current).toBeUndefined()
171
165
  })
166
+
167
+ it('calls the getter once when no event lands before subscribing', () => {
168
+ renderHook(() => useSleepTimer())
169
+ expect(native.getSleepTimer).toHaveBeenCalledTimes(1)
170
+ })
172
171
  })
@@ -1,4 +1,4 @@
1
- import { useEffect, useState } from 'react'
1
+ import { useEffect, useRef, useState } from 'react'
2
2
  import { nativeBrowser } from '../native'
3
3
  import { NativeUpdatedValue } from '../utils/NativeUpdatedValue'
4
4
 
@@ -109,14 +109,13 @@ export const onSleepTimerChanged = NativeUpdatedValue.emitterize<SleepTimer>(
109
109
  * @returns true if any sleep timer is active, false otherwise
110
110
  */
111
111
  export function useSleepTimerActive(): boolean {
112
+ const eventCountAtRender = useRef(onSleepTimerChanged.eventCount)
112
113
  const [isActive, setIsActive] = useState(() => getSleepTimer() !== null)
113
114
 
114
115
  useEffect(() => {
115
- // Re-sync before subscribing: a change between the initial read above and this effect would
116
- // otherwise be missed for good. `undefined` means nothing has fired yet, whereas `null` is a
117
- // real value — the timer being cleared — so it has to re-sync too.
118
- const { lastValue } = onSleepTimerChanged
119
- if (lastValue !== undefined) setIsActive(lastValue !== null)
116
+ if (onSleepTimerChanged.eventCount !== eventCountAtRender.current) {
117
+ setIsActive(getSleepTimer() !== null)
118
+ }
120
119
  return onSleepTimerChanged.addListener((timer) => {
121
120
  setIsActive(timer !== null)
122
121
  })
@@ -142,6 +141,7 @@ export function useSleepTimer(params?: {
142
141
  const updateInterval = params?.updateInterval ?? 1000
143
142
  const inactive = params?.inactive ?? false
144
143
 
144
+ const eventCountAtRender = useRef(onSleepTimerChanged.eventCount)
145
145
  const [state, setState] = useState<SleepTimerState | undefined>(() => {
146
146
  const timerState = getSleepTimer()
147
147
  return addSecondsLeft(timerState)
@@ -150,9 +150,9 @@ export function useSleepTimer(params?: {
150
150
  const time = state && 'time' in state ? state.time : undefined
151
151
 
152
152
  useEffect(() => {
153
- // Re-sync before subscribing — see useSleepTimerActive.
154
- const { lastValue } = onSleepTimerChanged
155
- if (lastValue !== undefined) setState(addSecondsLeft(lastValue))
153
+ if (onSleepTimerChanged.eventCount !== eventCountAtRender.current) {
154
+ setState(addSecondsLeft(getSleepTimer()))
155
+ }
156
156
  return onSleepTimerChanged.addListener((event) => {
157
157
  setState(addSecondsLeft(event))
158
158
  })
@@ -5,9 +5,9 @@ type Callback<T> = (arg: T) => void
5
5
  * `onRemoteSeek`, …) and `onGate`. Subscribe with {@link addListener}; the
6
6
  * returned function unsubscribes. The emitter is not callable itself.
7
7
  *
8
- * Native starts delivering the event only once the first listener is added,
9
- * and nothing is cached: there is no `lastValue`. The other `on*` exports use
10
- * `NativeUpdatedValue`, which listens from module load.
8
+ * Native starts delivering the event only once the first listener is added.
9
+ * The other `on*` exports use `NativeUpdatedValue`, which listens from module
10
+ * load.
11
11
  *
12
12
  * @example
13
13
  * ```typescript
@@ -5,14 +5,10 @@ type Callback<T> = (arg: T) => void
5
5
  * `onQueueChanged`, `onTrackMetadata`, …). Subscribe with {@link addListener};
6
6
  * the returned function unsubscribes. The emitter is not callable itself.
7
7
  *
8
- * It listens to native from module load and keeps the latest payload in
9
- * {@link lastValue}. The remote-control exports and `onGate` use
10
- * `LazyNativeEmitter`, which listens only once a listener is added.
11
- *
12
- * `lastValue` is the last payload native sent, not a fresh reading: it is
13
- * `undefined` before the first event, and for the queue and active track it
14
- * can be stale after an in-place change (a favorite toggle). Use the getter
15
- * (`getQueue`, `getActiveTrack`, …) when you need the current value.
8
+ * It listens to native from module load. The remote-control exports and
9
+ * `onGate` use `LazyNativeEmitter`, which listens only once a listener is
10
+ * added. It keeps no payload: use the getter (`getQueue`, `getActiveTrack`, …)
11
+ * for the current value.
16
12
  *
17
13
  * @example
18
14
  * ```typescript
@@ -29,16 +25,17 @@ export class NativeUpdatedValue<T> {
29
25
  private listeners = new Set<Callback<T>>()
30
26
 
31
27
  /**
32
- * The last payload native sent, or `undefined` before the first. Not a fresh
33
- * reading: use the matching getter (`getQueue`, …) for the current value.
28
+ * Events received so far, so a hook can tell whether one landed between its
29
+ * render and its subscription.
30
+ * @internal
34
31
  */
35
- lastValue: T | undefined = undefined
32
+ eventCount = 0
36
33
 
37
34
  /** @internal */
38
35
  constructor(setter: (callback: (data: T) => void) => void) {
39
36
  // Install native callback immediately at module load
40
37
  setter((data: T) => {
41
- this.lastValue = data
38
+ this.eventCount++
42
39
  for (const listener of this.listeners) {
43
40
  listener(data)
44
41
  }
@@ -60,7 +57,7 @@ export class NativeUpdatedValue<T> {
60
57
  * Creates a native value subscription.
61
58
  *
62
59
  * @param setter - Function that sets the native callback property
63
- * @returns The subscription instance with lastValue and addListener
60
+ * @returns The subscription instance
64
61
  * @internal
65
62
  */
66
63
  static emitterize<T>(