react-native-audio-browser 0.2.0-next.61 → 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.
- package/android/src/main/java/com/audiobrowser/browser/BrowserManager.kt +16 -6
- package/android/src/main/java/com/audiobrowser/player/PlaybackStateMachine.kt +9 -6
- package/android/src/main/java/com/audiobrowser/player/PlayerListener.kt +0 -5
- package/android/src/test/java/com/audiobrowser/browser/BrowserManagerInvalidationTest.kt +14 -0
- package/android/src/test/java/com/audiobrowser/player/PlaybackStateMachineTest.kt +7 -6
- package/android/src/test/java/com/audiobrowser/player/PlayerRetryTest.kt +35 -0
- package/ios/Browser/BrowserManager.swift +22 -7
- package/ios/HybridAudioBrowser+Options.swift +14 -0
- package/ios/HybridAudioBrowser.swift +4 -1
- package/ios/Player/DeferredSeek.swift +4 -1
- package/ios/Player/Player.swift +10 -7
- package/ios/Tests/DeferredSeekTests.swift +12 -4
- package/ios/Tests/PlayerPolicyTests.swift +86 -0
- package/lib/commonjs/CLAUDE.md +2 -2
- package/lib/commonjs/features/sleepTimer.js +8 -12
- package/lib/commonjs/features/sleepTimer.js.map +1 -1
- package/lib/commonjs/utils/LazyNativeEmitter.js +3 -3
- package/lib/commonjs/utils/NativeUpdatedValue.js +10 -13
- package/lib/commonjs/utils/NativeUpdatedValue.js.map +1 -1
- package/lib/commonjs/utils/useNativeUpdatedValue.js +12 -20
- package/lib/commonjs/utils/useNativeUpdatedValue.js.map +1 -1
- package/lib/commonjs/utils/useNativeValueRefreshedBy.js +10 -5
- package/lib/commonjs/utils/useNativeValueRefreshedBy.js.map +1 -1
- package/lib/module/CLAUDE.md +2 -2
- package/lib/module/features/sleepTimer.js +9 -13
- package/lib/module/features/sleepTimer.js.map +1 -1
- package/lib/module/utils/LazyNativeEmitter.js +3 -3
- package/lib/module/utils/NativeUpdatedValue.js +10 -13
- package/lib/module/utils/NativeUpdatedValue.js.map +1 -1
- package/lib/module/utils/useNativeUpdatedValue.js +13 -21
- package/lib/module/utils/useNativeUpdatedValue.js.map +1 -1
- package/lib/module/utils/useNativeValueRefreshedBy.js +11 -6
- package/lib/module/utils/useNativeValueRefreshedBy.js.map +1 -1
- package/lib/typescript/src/features/sleepTimer.d.ts.map +1 -1
- package/lib/typescript/src/utils/LazyNativeEmitter.d.ts +3 -3
- package/lib/typescript/src/utils/NativeUpdatedValue.d.ts +4 -13
- package/lib/typescript/src/utils/NativeUpdatedValue.d.ts.map +1 -1
- package/lib/typescript/src/utils/useNativeUpdatedValue.d.ts +4 -4
- package/lib/typescript/src/utils/useNativeUpdatedValue.d.ts.map +1 -1
- package/lib/typescript/src/utils/useNativeValueRefreshedBy.d.ts +3 -4
- package/lib/typescript/src/utils/useNativeValueRefreshedBy.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/CLAUDE.md +2 -2
- package/src/features/sleepTimer.test.ts +25 -26
- package/src/features/sleepTimer.ts +9 -9
- package/src/utils/LazyNativeEmitter.ts +3 -3
- package/src/utils/NativeUpdatedValue.ts +10 -13
- package/src/utils/useNativeUpdatedValue.test.ts +30 -22
- package/src/utils/useNativeUpdatedValue.ts +13 -17
- package/src/utils/useNativeValueRefreshedBy.test.ts +68 -0
- package/src/utils/useNativeValueRefreshedBy.ts +13 -6
|
@@ -12,21 +12,23 @@ const native = vi.hoisted(() => ({
|
|
|
12
12
|
|
|
13
13
|
vi.mock('../native', () => ({ nativeBrowser: native }))
|
|
14
14
|
|
|
15
|
-
const {
|
|
16
|
-
|
|
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
|
-
|
|
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
|
|
56
|
-
//
|
|
57
|
-
|
|
58
|
-
|
|
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
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
|
|
166
|
-
|
|
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
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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
|
-
*
|
|
10
|
-
*
|
|
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
|
|
9
|
-
*
|
|
10
|
-
*
|
|
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
|
-
*
|
|
33
|
-
*
|
|
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
|
-
|
|
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.
|
|
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
|
|
60
|
+
* @returns The subscription instance
|
|
64
61
|
* @internal
|
|
65
62
|
*/
|
|
66
63
|
static emitterize<T>(
|
|
@@ -26,7 +26,7 @@ function makeEmitter<T>() {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
describe('useNativeUpdatedValue', () => {
|
|
29
|
-
it('
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
72
|
-
// after the useState initializer has read, before the effect subscribes.
|
|
58
|
+
let native = 'atRender'
|
|
73
59
|
const getter = () => {
|
|
74
|
-
|
|
75
|
-
|
|
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('
|
|
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
|
|
8
|
-
*
|
|
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 -
|
|
11
|
-
* @param emitter - NativeUpdatedValue that
|
|
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
|
|
21
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
*
|
|
14
|
-
*
|
|
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
|
-
|
|
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
|
)
|