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

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 (47) 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+Options.swift +14 -0
  9. package/ios/HybridAudioBrowser.swift +4 -1
  10. package/lib/commonjs/CLAUDE.md +2 -2
  11. package/lib/commonjs/features/sleepTimer.js +8 -12
  12. package/lib/commonjs/features/sleepTimer.js.map +1 -1
  13. package/lib/commonjs/utils/LazyNativeEmitter.js +3 -3
  14. package/lib/commonjs/utils/NativeUpdatedValue.js +10 -13
  15. package/lib/commonjs/utils/NativeUpdatedValue.js.map +1 -1
  16. package/lib/commonjs/utils/useNativeUpdatedValue.js +12 -20
  17. package/lib/commonjs/utils/useNativeUpdatedValue.js.map +1 -1
  18. package/lib/commonjs/utils/useNativeValueRefreshedBy.js +10 -5
  19. package/lib/commonjs/utils/useNativeValueRefreshedBy.js.map +1 -1
  20. package/lib/module/CLAUDE.md +2 -2
  21. package/lib/module/features/sleepTimer.js +9 -13
  22. package/lib/module/features/sleepTimer.js.map +1 -1
  23. package/lib/module/utils/LazyNativeEmitter.js +3 -3
  24. package/lib/module/utils/NativeUpdatedValue.js +10 -13
  25. package/lib/module/utils/NativeUpdatedValue.js.map +1 -1
  26. package/lib/module/utils/useNativeUpdatedValue.js +13 -21
  27. package/lib/module/utils/useNativeUpdatedValue.js.map +1 -1
  28. package/lib/module/utils/useNativeValueRefreshedBy.js +11 -6
  29. package/lib/module/utils/useNativeValueRefreshedBy.js.map +1 -1
  30. package/lib/typescript/src/features/sleepTimer.d.ts.map +1 -1
  31. package/lib/typescript/src/utils/LazyNativeEmitter.d.ts +3 -3
  32. package/lib/typescript/src/utils/NativeUpdatedValue.d.ts +4 -13
  33. package/lib/typescript/src/utils/NativeUpdatedValue.d.ts.map +1 -1
  34. package/lib/typescript/src/utils/useNativeUpdatedValue.d.ts +4 -4
  35. package/lib/typescript/src/utils/useNativeUpdatedValue.d.ts.map +1 -1
  36. package/lib/typescript/src/utils/useNativeValueRefreshedBy.d.ts +3 -4
  37. package/lib/typescript/src/utils/useNativeValueRefreshedBy.d.ts.map +1 -1
  38. package/package.json +1 -1
  39. package/src/CLAUDE.md +2 -2
  40. package/src/features/sleepTimer.test.ts +25 -26
  41. package/src/features/sleepTimer.ts +9 -9
  42. package/src/utils/LazyNativeEmitter.ts +3 -3
  43. package/src/utils/NativeUpdatedValue.ts +10 -13
  44. package/src/utils/useNativeUpdatedValue.test.ts +30 -22
  45. package/src/utils/useNativeUpdatedValue.ts +13 -17
  46. package/src/utils/useNativeValueRefreshedBy.test.ts +68 -0
  47. package/src/utils/useNativeValueRefreshedBy.ts +13 -6
@@ -26,7 +26,7 @@ function makeEmitter<T>() {
26
26
  }
27
27
 
28
28
  describe('useNativeUpdatedValue', () => {
29
- it('falls back to the getter when nothing has fired yet', () => {
29
+ it('reads the getter', () => {
30
30
  const { emitter } = makeEmitter<string>()
31
31
  const { result } = renderHook(() =>
32
32
  useNativeUpdatedValue(() => 'a', emitter)
@@ -34,15 +34,6 @@ describe('useNativeUpdatedValue', () => {
34
34
  expect(result.current).toBe('a')
35
35
  })
36
36
 
37
- it('prefers lastValue over the getter', () => {
38
- const { emitter, fire } = makeEmitter<string>()
39
- fire('fromNative')
40
- const getter = vi.fn(() => 'fromGetter')
41
- const { result } = renderHook(() => useNativeUpdatedValue(getter, emitter))
42
- expect(result.current).toBe('fromNative')
43
- expect(getter).not.toHaveBeenCalled()
44
- })
45
-
46
37
  it('updates when the emitter fires', () => {
47
38
  const { emitter, fire } = makeEmitter<string>()
48
39
  const { result } = renderHook(() =>
@@ -52,30 +43,47 @@ describe('useNativeUpdatedValue', () => {
52
43
  expect(result.current).toBe('b')
53
44
  })
54
45
 
55
- it('extracts eventKey from both lastValue and later events', () => {
46
+ it('extracts eventKey from events', () => {
56
47
  const { emitter, fire } = makeEmitter<{ volume: number }>()
57
- fire({ volume: 0.5 })
58
48
  const { result } = renderHook(() =>
59
- useNativeUpdatedValue(() => 0, emitter, 'volume')
49
+ useNativeUpdatedValue(() => 0.5, emitter, 'volume')
60
50
  )
61
51
  expect(result.current).toBe(0.5)
62
52
  fire({ volume: 0.9 })
63
53
  expect(result.current).toBe(0.9)
64
54
  })
65
55
 
66
- // The reason the effect re-reads `lastValue` instead of going straight to
67
- // `addListener`. Nothing reads the value again until the next native event, so
68
- // a value landing in this window would otherwise be missed permanently.
69
- it('re-syncs a value that lands between the render read and the effect', () => {
56
+ it('re-reads a change that lands between the render read and the effect', () => {
70
57
  const { emitter, fireDuringRender } = makeEmitter<string>()
71
- // Firing from inside the getter puts the emit exactly where the race is:
72
- // after the useState initializer has read, before the effect subscribes.
58
+ let native = 'atRender'
73
59
  const getter = () => {
74
- fireDuringRender('landedMidRender')
75
- return 'staleAtRender'
60
+ const value = native
61
+ if (value === 'atRender') {
62
+ native = 'changed'
63
+ fireDuringRender('changed')
64
+ }
65
+ return value
76
66
  }
77
67
  const { result } = renderHook(() => useNativeUpdatedValue(getter, emitter))
78
- expect(result.current).toBe('landedMidRender')
68
+ expect(result.current).toBe('changed')
69
+ })
70
+
71
+ it('calls the getter once when no event lands before subscribing', () => {
72
+ const { emitter, fire } = makeEmitter<string>()
73
+ fire('earlier')
74
+ const getter = vi.fn(() => 'a')
75
+ renderHook(() => useNativeUpdatedValue(getter, emitter))
76
+ expect(getter).toHaveBeenCalledTimes(1)
77
+ })
78
+
79
+ it('keeps an event over an inline getter on re-render', () => {
80
+ const { emitter, fire } = makeEmitter<string>()
81
+ const { result, rerender } = renderHook(() =>
82
+ useNativeUpdatedValue(() => 'a', emitter)
83
+ )
84
+ fire('b')
85
+ rerender()
86
+ expect(result.current).toBe('b')
79
87
  })
80
88
 
81
89
  it('stops updating after unmount', () => {
@@ -1,14 +1,14 @@
1
- import { useEffect, useState } from 'react'
1
+ import { useEffect, useRef, useState } from 'react'
2
2
  import { NativeUpdatedValue } from './NativeUpdatedValue'
3
3
 
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
  */
@@ -17,24 +17,20 @@ export function useNativeUpdatedValue<T, E = T>(
17
17
  emitter: NativeUpdatedValue<E>,
18
18
  eventKey?: keyof E
19
19
  ): T {
20
- const [value, setValue] = useState(() => {
21
- const { lastValue } = emitter
22
- if (lastValue != null) {
23
- return (eventKey !== undefined ? lastValue[eventKey] : lastValue) as T
24
- }
25
- return getter()
26
- })
20
+ const eventCountAtRender = useRef(emitter.eventCount)
21
+ const [value, setValue] = useState(getter)
27
22
 
28
23
  useEffect(() => {
29
- const { lastValue } = emitter
30
- if (lastValue != null) {
31
- const newValue = eventKey !== undefined ? lastValue[eventKey] : lastValue
32
- setValue(newValue as T)
33
- }
24
+ if (emitter.eventCount !== eventCountAtRender.current) setValue(getter)
25
+ // A re-run, for another emitter or eventKey, always reads afresh.
26
+ eventCountAtRender.current = NaN
34
27
  return emitter.addListener((event) => {
35
28
  const newValue = eventKey !== undefined ? event[eventKey] : event
36
29
  setValue(newValue as T)
37
30
  })
31
+ // The getter is read once per subscription: an inline one, new each render,
32
+ // would otherwise overwrite every event with a re-read.
33
+ // eslint-disable-next-line react-hooks/exhaustive-deps
38
34
  }, [emitter, eventKey])
39
35
 
40
36
  return value
@@ -0,0 +1,68 @@
1
+ /** @vitest-environment happy-dom */
2
+
3
+ import { renderHook, act, cleanup } from '@testing-library/react'
4
+ import { describe, it, expect, afterEach, vi } from 'vitest'
5
+ import { NativeUpdatedValue } from './NativeUpdatedValue'
6
+ import { useNativeValueRefreshedBy } from './useNativeValueRefreshedBy'
7
+
8
+ afterEach(cleanup)
9
+
10
+ function makeEmitter() {
11
+ let emit: (value: unknown) => void = () => {}
12
+ const emitter = NativeUpdatedValue.emitterize<unknown>((cb) => (emit = cb))
13
+ return { emitter, emit: (value: unknown) => emit(value) }
14
+ }
15
+
16
+ describe('useNativeValueRefreshedBy', () => {
17
+ it('re-reads the getter on an event from any emitter', () => {
18
+ const a = makeEmitter()
19
+ const b = makeEmitter()
20
+ let native = 1
21
+ const emitters = [a.emitter, b.emitter]
22
+ const { result } = renderHook(() =>
23
+ useNativeValueRefreshedBy(() => native, emitters)
24
+ )
25
+ native = 2
26
+ act(() => b.emit(undefined))
27
+ expect(result.current).toBe(2)
28
+ })
29
+
30
+ it('re-reads a change that lands between the render read and the effect', () => {
31
+ const a = makeEmitter()
32
+ const b = makeEmitter()
33
+ let native = 'atRender'
34
+ const getter = () => {
35
+ const value = native
36
+ if (value === 'atRender') {
37
+ native = 'changed'
38
+ b.emit(undefined)
39
+ }
40
+ return value
41
+ }
42
+ const emitters = [a.emitter, b.emitter]
43
+ const { result } = renderHook(() =>
44
+ useNativeValueRefreshedBy(getter, emitters)
45
+ )
46
+ expect(result.current).toBe('changed')
47
+ })
48
+
49
+ it('calls the getter once when no event lands before subscribing', () => {
50
+ const a = makeEmitter()
51
+ a.emit(undefined)
52
+ const getter = vi.fn(() => 'a')
53
+ const emitters = [a.emitter]
54
+ renderHook(() => useNativeValueRefreshedBy(getter, emitters))
55
+ expect(getter).toHaveBeenCalledTimes(1)
56
+ })
57
+
58
+ it('reads a new getter on re-render, with no event since mount', () => {
59
+ const a = makeEmitter()
60
+ const emitters = [a.emitter]
61
+ const { result, rerender } = renderHook(
62
+ ({ getter }) => useNativeValueRefreshedBy(getter, emitters),
63
+ { initialProps: { getter: () => 'first' } }
64
+ )
65
+ rerender({ getter: () => 'second' })
66
+ expect(result.current).toBe('second')
67
+ })
68
+ })
@@ -1,19 +1,21 @@
1
- import { useEffect, useState } from 'react'
1
+ import { useEffect, useRef, useState } from 'react'
2
2
 
3
3
  type Subscribable = {
4
4
  addListener(callback: (event: unknown) => void): () => void
5
+ eventCount: number
5
6
  }
6
7
 
8
+ const eventCountOf = (emitters: readonly Subscribable[]) =>
9
+ emitters.reduce((sum, emitter) => sum + emitter.eventCount, 0)
10
+
7
11
  /**
8
12
  * Hook for a native-fetched value that must stay fresh across SEVERAL event
9
13
  * sources — e.g. the active track, which changes on a queue transition
10
14
  * (`onActiveTrackChanged`) but is also mutated in place by a favorite toggle
11
15
  * (`onFavoriteChanged`).
12
16
  *
13
- * Unlike {@link useNativeUpdatedValue}, this never reads a cached event
14
- * payload: every trigger re-reads `getter()`, and the mount effect re-reads
15
- * once after the subscriptions attach (so an event firing between render and
16
- * effect cannot be missed). The getters are cheap synchronous Nitro calls.
17
+ * Every trigger re-reads `getter()`, and the mount effect re-reads it only if
18
+ * an event landed since the render read, so none is missed.
17
19
  *
18
20
  * @param getter - Synchronous native getter for the current value
19
21
  * @param emitters - Event sources that invalidate the value (a stable,
@@ -23,10 +25,15 @@ export function useNativeValueRefreshedBy<T>(
23
25
  getter: () => T,
24
26
  emitters: readonly Subscribable[]
25
27
  ): T {
28
+ const eventCountAtRender = useRef(eventCountOf(emitters))
26
29
  const [value, setValue] = useState(getter)
27
30
 
28
31
  useEffect(() => {
29
- setValue(getter())
32
+ if (eventCountOf(emitters) !== eventCountAtRender.current) {
33
+ setValue(getter())
34
+ }
35
+ // A re-run, for another getter or emitters, always reads afresh.
36
+ eventCountAtRender.current = NaN
30
37
  const cleanups = emitters.map((emitter) =>
31
38
  emitter.addListener(() => setValue(getter()))
32
39
  )