react-native-audio-browser 1.0.0-next.2 → 1.0.0-next.3
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/AudioBrowser.kt +19 -2
- package/android/src/main/java/com/audiobrowser/browser/BrowserManager.kt +29 -8
- package/android/src/main/java/com/audiobrowser/http/RequestConfigBuilder.kt +8 -10
- package/android/src/main/java/com/audiobrowser/model/PlayerSetupOptions.kt +2 -2
- package/android/src/main/java/com/audiobrowser/player/AutomaticBufferManager.kt +17 -16
- package/android/src/main/java/com/audiobrowser/player/InterceptingPlayer.kt +11 -0
- package/android/src/main/java/com/audiobrowser/player/Player.kt +8 -4
- package/android/src/test/java/com/audiobrowser/ContentRefreshNavigationErrorTest.kt +236 -0
- package/android/src/test/java/com/audiobrowser/browser/BrowserManagerUnparsableResponseTest.kt +82 -0
- package/android/src/test/java/com/audiobrowser/browser/BrowserUrlResolutionTest.kt +15 -0
- package/android/src/test/java/com/audiobrowser/http/RequestConfigBuilderTest.kt +26 -0
- package/android/src/test/java/com/audiobrowser/model/PlayerSetupOptionsTest.kt +19 -2
- package/android/src/test/java/com/audiobrowser/player/AutomaticBufferManagerTest.kt +17 -0
- package/android/src/test/java/com/audiobrowser/player/SessionQueueShuffleOrderTest.kt +38 -0
- package/ios/Browser/BrowserError.swift +8 -0
- package/ios/Browser/BrowserManager.swift +29 -28
- package/ios/Browser/BrowserPathHelper.swift +26 -24
- package/ios/Browser/FavoriteSet.swift +16 -8
- package/ios/Browser/Favorites.swift +66 -0
- package/ios/Browser/JsonModels.swift +27 -7
- package/ios/Browser/NavigationErrorState.swift +18 -1
- package/ios/Browser/PageResolver.swift +2 -2
- package/ios/Browser/RequestLayer.swift +11 -4
- package/ios/Browser/RequestPipeline.swift +8 -4
- package/ios/Browser/TabBarEntries.swift +7 -0
- package/ios/CarPlay/CarPlayImageLoader.swift +3 -7
- package/ios/CarPlay/CarPlaySession+Navigator.swift +3 -1
- package/ios/CarPlay/IntentHandlers.swift +1 -7
- package/ios/CarPlay/MediaIntentCriteria.swift +4 -4
- package/ios/Extension/CGSize+AspectFit.swift +11 -0
- package/ios/Extension/Dictionary+Headers.swift +8 -0
- package/ios/Extension/NavigationError+Conversion.swift +4 -3
- package/ios/Extension/ResolvedTrack+Copying.swift +15 -0
- package/ios/Http/HttpClient.swift +37 -11
- package/ios/HybridAudioBrowser+Browser.swift +9 -16
- package/ios/HybridAudioBrowser+Options.swift +1 -0
- package/ios/HybridAudioBrowser+Player.swift +24 -23
- package/ios/HybridAudioBrowser+PlayerCallbacks.swift +1 -2
- package/ios/Model/NitroTypeStubs.swift +39 -0
- package/ios/Model/TrackPlayerError.swift +21 -2
- package/ios/Player/AudioSessionController.swift +2 -2
- package/ios/Player/MediaLoader.swift +1 -1
- package/ios/Player/PlaybackStateMachine.swift +1 -2
- package/ios/Player/Player.swift +59 -20
- package/ios/Player/PlayerEngine.swift +5 -0
- package/ios/Player/QueueManager.swift +21 -4
- package/ios/Tests/BrowserErrorTests.swift +21 -0
- package/ios/Tests/BrowserPathHelperTests.swift +18 -0
- package/ios/Tests/CGSizeAspectFitTests.swift +36 -0
- package/ios/Tests/ClampedVolumeTests.swift +26 -0
- package/ios/Tests/FavoriteSetTests.swift +68 -3
- package/ios/Tests/FavoritesTests.swift +102 -0
- package/ios/Tests/HttpClientUserAgentTests.swift +45 -0
- package/ios/Tests/JsonModelsTests.swift +74 -6
- package/ios/Tests/MediaIntentCriteriaTests.swift +11 -1
- package/ios/Tests/MediaLoaderAssetOptionsTests.swift +7 -0
- package/ios/Tests/NavigationErrorStateTests.swift +73 -0
- package/ios/Tests/PlaybackStateMachineTests.swift +6 -4
- package/ios/Tests/PlayerPolicyTests.swift +38 -0
- package/ios/Tests/PlayerProgressTests.swift +109 -0
- package/ios/Tests/PlayerTests.swift +106 -0
- package/ios/Tests/QueueIndexTests.swift +43 -0
- package/ios/Tests/QueueManagerTests.swift +79 -15
- package/ios/Tests/RequestLayerTests.swift +22 -3
- package/ios/Tests/TabBarEntriesTests.swift +19 -0
- package/ios/Tests/TrackSelectorTests.swift +154 -0
- package/ios/Tests/UnparsableResponseTests.swift +91 -0
- package/ios/TrackSelector.swift +28 -8
- package/ios/Util/ArtworkImageFetcher.swift +9 -8
- package/ios/Util/SVGProcessor.swift +6 -17
- package/lib/commonjs/features/browser-config.js +6 -3
- package/lib/commonjs/features/browser-config.js.map +1 -1
- package/lib/commonjs/features/errors.js +12 -7
- package/lib/commonjs/features/errors.js.map +1 -1
- package/lib/commonjs/features/nowPlaying.js +9 -3
- package/lib/commonjs/features/nowPlaying.js.map +1 -1
- package/lib/commonjs/features/playback/trackPlaybackTime.js +2 -1
- package/lib/commonjs/features/playback/trackPlaybackTime.js.map +1 -1
- package/lib/commonjs/features/player/setup.js +2 -2
- package/lib/commonjs/features/queue/activeTrack.js +4 -2
- package/lib/commonjs/features/queue/activeTrack.js.map +1 -1
- package/lib/commonjs/features/sleepTimer.js +6 -5
- package/lib/commonjs/features/sleepTimer.js.map +1 -1
- package/lib/commonjs/utils/LazyNativeEmitter.js +2 -1
- package/lib/commonjs/utils/LazyNativeEmitter.js.map +1 -1
- package/lib/commonjs/utils/NativeUpdatedValue.js +2 -1
- package/lib/commonjs/utils/NativeUpdatedValue.js.map +1 -1
- package/lib/commonjs/utils/callListener.js +23 -0
- package/lib/commonjs/utils/callListener.js.map +1 -0
- package/lib/module/features/browser-config.js +6 -3
- package/lib/module/features/browser-config.js.map +1 -1
- package/lib/module/features/errors.js +12 -7
- package/lib/module/features/errors.js.map +1 -1
- package/lib/module/features/nowPlaying.js +9 -3
- package/lib/module/features/nowPlaying.js.map +1 -1
- package/lib/module/features/playback/trackPlaybackTime.js +2 -1
- package/lib/module/features/playback/trackPlaybackTime.js.map +1 -1
- package/lib/module/features/player/setup.js +2 -2
- package/lib/module/features/queue/activeTrack.js +4 -2
- package/lib/module/features/queue/activeTrack.js.map +1 -1
- package/lib/module/features/sleepTimer.js +6 -5
- package/lib/module/features/sleepTimer.js.map +1 -1
- package/lib/module/utils/LazyNativeEmitter.js +2 -1
- package/lib/module/utils/LazyNativeEmitter.js.map +1 -1
- package/lib/module/utils/NativeUpdatedValue.js +2 -1
- package/lib/module/utils/NativeUpdatedValue.js.map +1 -1
- package/lib/module/utils/callListener.js +19 -0
- package/lib/module/utils/callListener.js.map +1 -0
- package/lib/typescript/src/features/browser-config.d.ts.map +1 -1
- package/lib/typescript/src/features/errors.d.ts +8 -6
- package/lib/typescript/src/features/errors.d.ts.map +1 -1
- package/lib/typescript/src/features/nowPlaying.d.ts +2 -1
- package/lib/typescript/src/features/nowPlaying.d.ts.map +1 -1
- package/lib/typescript/src/features/playback/trackPlaybackTime.d.ts.map +1 -1
- package/lib/typescript/src/features/player/setup.d.ts +2 -2
- package/lib/typescript/src/features/queue/activeTrack.d.ts +2 -1
- package/lib/typescript/src/features/queue/activeTrack.d.ts.map +1 -1
- package/lib/typescript/src/features/sleepTimer.d.ts +2 -1
- package/lib/typescript/src/features/sleepTimer.d.ts.map +1 -1
- package/lib/typescript/src/types/browser.d.ts +3 -2
- package/lib/typescript/src/types/browser.d.ts.map +1 -1
- package/lib/typescript/src/utils/LazyNativeEmitter.d.ts.map +1 -1
- package/lib/typescript/src/utils/NativeUpdatedValue.d.ts.map +1 -1
- package/lib/typescript/src/utils/callListener.d.ts +7 -0
- package/lib/typescript/src/utils/callListener.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/features/browser-config.test.ts +15 -0
- package/src/features/browser-config.ts +6 -3
- package/src/features/errors.test.ts +83 -0
- package/src/features/errors.ts +12 -7
- package/src/features/hook-wiring.test.ts +11 -10
- package/src/features/nowPlaying.ts +9 -3
- package/src/features/playback/trackPlaybackTime.test.ts +24 -1
- package/src/features/playback/trackPlaybackTime.ts +2 -1
- package/src/features/player/setup.ts +2 -2
- package/src/features/queue/activeTrack.test.ts +32 -0
- package/src/features/queue/activeTrack.ts +8 -2
- package/src/features/sleepTimer.test.ts +24 -0
- package/src/features/sleepTimer.ts +6 -5
- package/src/types/browser.ts +3 -2
- package/src/utils/LazyNativeEmitter.test.ts +32 -0
- package/src/utils/LazyNativeEmitter.ts +3 -1
- package/src/utils/NativeUpdatedValue.test.ts +32 -0
- package/src/utils/NativeUpdatedValue.ts +3 -1
- package/src/utils/callListener.ts +17 -0
- package/website/guide/configuration.md +3 -3
- package/website/guide/errors.md +3 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
|
2
2
|
|
|
3
3
|
const nativeMock = {
|
|
4
4
|
onPlaybackInterval: undefined as undefined | (() => void),
|
|
@@ -19,6 +19,10 @@ beforeEach(() => {
|
|
|
19
19
|
nativeMock.setPlaybackIntervalEnabled.mockClear()
|
|
20
20
|
})
|
|
21
21
|
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
vi.unstubAllGlobals()
|
|
24
|
+
})
|
|
25
|
+
|
|
22
26
|
/** Ticks through the slot `install()` assigns, so this covers that wiring too. */
|
|
23
27
|
function tick(times = 1) {
|
|
24
28
|
for (let i = 0; i < times; i++) {
|
|
@@ -73,4 +77,23 @@ describe('trackPlaybackTime', () => {
|
|
|
73
77
|
cancelFast()
|
|
74
78
|
cancelSlow()
|
|
75
79
|
})
|
|
80
|
+
|
|
81
|
+
it('keeps counting for the subscribers after one whose callback throws', () => {
|
|
82
|
+
const rethrown: (() => void)[] = []
|
|
83
|
+
vi.stubGlobal('queueMicrotask', (task: () => void) => rethrown.push(task))
|
|
84
|
+
const error = new Error('callback failed')
|
|
85
|
+
const cancelThrowing = trackPlaybackTime(() => {
|
|
86
|
+
throw error
|
|
87
|
+
}, 1)
|
|
88
|
+
const totals: number[] = []
|
|
89
|
+
const cancelLater = trackPlaybackTime((e) => totals.push(e.total), 1)
|
|
90
|
+
|
|
91
|
+
tick(2)
|
|
92
|
+
|
|
93
|
+
expect(totals).toEqual([1, 2])
|
|
94
|
+
expect(rethrown).toHaveLength(2)
|
|
95
|
+
expect(rethrown[0]).toThrow(error)
|
|
96
|
+
cancelThrowing()
|
|
97
|
+
cancelLater()
|
|
98
|
+
})
|
|
76
99
|
})
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Track } from '../../types/browser-nodes'
|
|
2
2
|
import { nativeBrowser } from '../../native'
|
|
3
|
+
import { callListener } from '../../utils/callListener'
|
|
3
4
|
import { onActiveTrackChanged } from '../queue/activeTrack'
|
|
4
5
|
|
|
5
6
|
// MARK: - Types
|
|
@@ -39,7 +40,7 @@ function onTick() {
|
|
|
39
40
|
const sinceLast = handle.totalSeconds - handle.lastFireSeconds
|
|
40
41
|
if (sinceLast >= handle.period) {
|
|
41
42
|
handle.lastFireSeconds = handle.totalSeconds
|
|
42
|
-
handle.callback
|
|
43
|
+
callListener(handle.callback, {
|
|
43
44
|
total: handle.totalSeconds,
|
|
44
45
|
sinceLast,
|
|
45
46
|
track: currentTrack
|
|
@@ -746,8 +746,8 @@ function nonEmpty<K extends string, T extends object>(
|
|
|
746
746
|
*
|
|
747
747
|
* Calling it again is how a setup-only option changes after launch, and works differently per
|
|
748
748
|
* platform. On Android, newly provided fields are merged over the previous ones, except
|
|
749
|
-
* `remoteOutputs`, which is off when left out, and `nowPlaying`, which is
|
|
750
|
-
*
|
|
749
|
+
* `remoteOutputs`, which is off when left out, and `nowPlaying`, which is `true` when left
|
|
750
|
+
* out: an earlier formatter stays only when the call passes it again. The audio engine is rebuilt
|
|
751
751
|
* only when construction-bound fields (buffers, wake mode, …) changed. On iOS, each call
|
|
752
752
|
* replaces the player: the queue is cleared, and setup-only fields you omit (`ios` session
|
|
753
753
|
* category and `buffer`, `retry`, …) go back to their defaults. Runtime options set earlier
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
|
|
6
|
+
const native = vi.hoisted(() => ({
|
|
7
|
+
activeTrack: undefined as unknown,
|
|
8
|
+
getActiveTrack: () => native.activeTrack,
|
|
9
|
+
onPlaybackActiveTrackChanged: (_event: unknown) => {},
|
|
10
|
+
onPlaybackQueueChanged: (_tracks: unknown) => {},
|
|
11
|
+
onFavoriteChanged: (_event: unknown) => {}
|
|
12
|
+
}))
|
|
13
|
+
vi.mock('../../native', () => ({ nativeBrowser: native }))
|
|
14
|
+
|
|
15
|
+
import { useActiveTrack } from './activeTrack'
|
|
16
|
+
|
|
17
|
+
afterEach(cleanup)
|
|
18
|
+
|
|
19
|
+
describe('useActiveTrack', () => {
|
|
20
|
+
it('re-reads the active track when the queue changes under it', () => {
|
|
21
|
+
native.activeTrack = { src: 'a', title: 'A', favorited: false }
|
|
22
|
+
const { result } = renderHook(() => useActiveTrack())
|
|
23
|
+
expect(result.current?.favorited).toBe(false)
|
|
24
|
+
|
|
25
|
+
// The queued copy is replaced in place: no transition, no favorite event.
|
|
26
|
+
const replaced = { src: 'a', title: 'A', favorited: true }
|
|
27
|
+
native.activeTrack = replaced
|
|
28
|
+
act(() => native.onPlaybackQueueChanged([replaced]))
|
|
29
|
+
|
|
30
|
+
expect(result.current?.favorited).toBe(true)
|
|
31
|
+
})
|
|
32
|
+
})
|
|
@@ -3,6 +3,7 @@ import { nativeBrowser } from '../../native'
|
|
|
3
3
|
import { NativeUpdatedValue } from '../../utils/NativeUpdatedValue'
|
|
4
4
|
import { useNativeValueRefreshedBy } from '../../utils/useNativeValueRefreshedBy'
|
|
5
5
|
import { onFavoriteChanged } from '../favorites'
|
|
6
|
+
import { onQueueChanged } from './queue'
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* Event data for when the active track changes.
|
|
@@ -57,11 +58,16 @@ export const onActiveTrackChanged =
|
|
|
57
58
|
// MARK: - Hooks
|
|
58
59
|
|
|
59
60
|
// Module-level so the hook's subscription identity is stable across renders.
|
|
60
|
-
const activeTrackInvalidators = [
|
|
61
|
+
const activeTrackInvalidators = [
|
|
62
|
+
onActiveTrackChanged,
|
|
63
|
+
onFavoriteChanged,
|
|
64
|
+
onQueueChanged
|
|
65
|
+
]
|
|
61
66
|
|
|
62
67
|
/**
|
|
63
68
|
* Hook that returns the current active track, updating on transitions AND on
|
|
64
|
-
* in-place mutations of the active track (favorite toggles
|
|
69
|
+
* in-place mutations of the active track (favorite toggles, and a queued copy
|
|
70
|
+
* replaced in place, as `setFavorites` does on iOS).
|
|
65
71
|
* @returns The current active track or undefined
|
|
66
72
|
*/
|
|
67
73
|
export function useActiveTrack(): Track | undefined {
|
|
@@ -142,6 +142,30 @@ describe('useSleepTimer', () => {
|
|
|
142
142
|
}
|
|
143
143
|
})
|
|
144
144
|
|
|
145
|
+
it('reaches zero at a deadline that is not on a whole second of the clock', () => {
|
|
146
|
+
vi.useFakeTimers()
|
|
147
|
+
try {
|
|
148
|
+
vi.setSystemTime(1_000_000_000_250)
|
|
149
|
+
native.getSleepTimer.mockReturnValue({ time: 1_000_000_002_500 })
|
|
150
|
+
const { result } = renderHook(() => useSleepTimer())
|
|
151
|
+
expect(result.current).toMatchObject({ secondsLeft: 3 })
|
|
152
|
+
act(() => {
|
|
153
|
+
vi.advanceTimersByTime(1_250)
|
|
154
|
+
})
|
|
155
|
+
expect(result.current).toMatchObject({ secondsLeft: 1 })
|
|
156
|
+
act(() => {
|
|
157
|
+
vi.advanceTimersByTime(999)
|
|
158
|
+
})
|
|
159
|
+
expect(result.current).toMatchObject({ secondsLeft: 1 })
|
|
160
|
+
act(() => {
|
|
161
|
+
vi.advanceTimersByTime(1)
|
|
162
|
+
})
|
|
163
|
+
expect(result.current).toMatchObject({ secondsLeft: 0 })
|
|
164
|
+
} finally {
|
|
165
|
+
vi.useRealTimers()
|
|
166
|
+
}
|
|
167
|
+
})
|
|
168
|
+
|
|
145
169
|
it('does not tick while inactive', () => {
|
|
146
170
|
vi.useFakeTimers()
|
|
147
171
|
try {
|
|
@@ -177,14 +177,14 @@ export function useSleepTimer(params?: {
|
|
|
177
177
|
})
|
|
178
178
|
}
|
|
179
179
|
|
|
180
|
-
//
|
|
181
|
-
//
|
|
180
|
+
// Ticks land a whole number of intervals before the deadline, so with
|
|
181
|
+
// secondsLeft rounded up the 0 shows when the timer fires.
|
|
182
182
|
const initialTimeoutId = setTimeout(
|
|
183
183
|
() => {
|
|
184
184
|
update()
|
|
185
185
|
intervalId = setInterval(update, updateInterval)
|
|
186
186
|
},
|
|
187
|
-
|
|
187
|
+
(time - Date.now()) % updateInterval
|
|
188
188
|
)
|
|
189
189
|
let intervalId: ReturnType<typeof setInterval> | undefined
|
|
190
190
|
const clear = () => {
|
|
@@ -202,7 +202,8 @@ export function useSleepTimer(params?: {
|
|
|
202
202
|
|
|
203
203
|
/**
|
|
204
204
|
* State returned by `useSleepTimer` hook.
|
|
205
|
-
* - Time-based timer includes `secondsLeft` countdown
|
|
205
|
+
* - Time-based timer includes `secondsLeft` countdown, rounded up: 0 only
|
|
206
|
+
* once the deadline is reached
|
|
206
207
|
* - End-of-track timer includes `sleepWhenPlayedToEnd` flag
|
|
207
208
|
*/
|
|
208
209
|
export type SleepTimerState =
|
|
@@ -214,7 +215,7 @@ function addSecondsLeft(state: SleepTimer): SleepTimerState | undefined {
|
|
|
214
215
|
if ('time' in state) {
|
|
215
216
|
return {
|
|
216
217
|
...state,
|
|
217
|
-
secondsLeft: Math.max(0, Math.
|
|
218
|
+
secondsLeft: Math.max(0, Math.ceil((state.time - Date.now()) / 1000))
|
|
218
219
|
}
|
|
219
220
|
}
|
|
220
221
|
return state
|
package/src/types/browser.ts
CHANGED
|
@@ -1119,8 +1119,9 @@ export type FormattedNavigationError = {
|
|
|
1119
1119
|
* - `'network-error'`: "Network Error" (English)
|
|
1120
1120
|
* - `'http-error'`: the status text, system-localized on iOS (e.g. "Not
|
|
1121
1121
|
* Found", "Service Unavailable"); on Android an English name for common
|
|
1122
|
-
* codes. "Server Error" when there is no status code, for
|
|
1123
|
-
* does not name, and
|
|
1122
|
+
* codes. "Server Error" when there is no status code, for a 2xx (a
|
|
1123
|
+
* response that did not parse), for codes Android does not name, and
|
|
1124
|
+
* always on web
|
|
1124
1125
|
* - `'callback-error'`: "Error" (English)
|
|
1125
1126
|
* - `'empty-content'`: "Nothing here" (English)
|
|
1126
1127
|
* - `'timeout'`: "Couldn't load" (English)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { describe, it, expect, vi, afterEach } from 'vitest'
|
|
2
|
+
import { LazyNativeEmitter } from './LazyNativeEmitter'
|
|
3
|
+
|
|
4
|
+
afterEach(() => {
|
|
5
|
+
vi.unstubAllGlobals()
|
|
6
|
+
})
|
|
7
|
+
|
|
8
|
+
describe('LazyNativeEmitter', () => {
|
|
9
|
+
it('calls the listeners after one that throws, and rethrows its error from a microtask', () => {
|
|
10
|
+
const rethrown: (() => void)[] = []
|
|
11
|
+
vi.stubGlobal('queueMicrotask', (task: () => void) => rethrown.push(task))
|
|
12
|
+
let emit: (data: string) => void = () => {
|
|
13
|
+
throw new Error('the native slot was never set')
|
|
14
|
+
}
|
|
15
|
+
const emitter = LazyNativeEmitter.emitterize<string>((callback) => {
|
|
16
|
+
emit = callback
|
|
17
|
+
})
|
|
18
|
+
const error = new Error('listener failed')
|
|
19
|
+
const received: string[] = []
|
|
20
|
+
emitter.addListener(() => {
|
|
21
|
+
throw error
|
|
22
|
+
})
|
|
23
|
+
emitter.addListener((data) => received.push(data))
|
|
24
|
+
|
|
25
|
+
emit('first')
|
|
26
|
+
emit('second')
|
|
27
|
+
|
|
28
|
+
expect(received).toEqual(['first', 'second'])
|
|
29
|
+
expect(rethrown).toHaveLength(2)
|
|
30
|
+
expect(rethrown[0]).toThrow(error)
|
|
31
|
+
})
|
|
32
|
+
})
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { callListener } from './callListener'
|
|
2
|
+
|
|
1
3
|
type Callback<T> = (arg: T) => void
|
|
2
4
|
|
|
3
5
|
/**
|
|
@@ -32,7 +34,7 @@ export class LazyNativeEmitter<T> {
|
|
|
32
34
|
const listeners = (this.listeners = new Set())
|
|
33
35
|
this.setter((data: T) => {
|
|
34
36
|
for (const listener of listeners) {
|
|
35
|
-
listener
|
|
37
|
+
callListener(listener, data)
|
|
36
38
|
}
|
|
37
39
|
})
|
|
38
40
|
return listeners
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { describe, it, expect, vi, afterEach } from 'vitest'
|
|
2
|
+
import { NativeUpdatedValue } from './NativeUpdatedValue'
|
|
3
|
+
|
|
4
|
+
afterEach(() => {
|
|
5
|
+
vi.unstubAllGlobals()
|
|
6
|
+
})
|
|
7
|
+
|
|
8
|
+
describe('NativeUpdatedValue', () => {
|
|
9
|
+
it('calls the listeners after one that throws, and rethrows its error from a microtask', () => {
|
|
10
|
+
const rethrown: (() => void)[] = []
|
|
11
|
+
vi.stubGlobal('queueMicrotask', (task: () => void) => rethrown.push(task))
|
|
12
|
+
let emit: (data: string) => void = () => {
|
|
13
|
+
throw new Error('the native slot was never set')
|
|
14
|
+
}
|
|
15
|
+
const emitter = NativeUpdatedValue.emitterize<string>((callback) => {
|
|
16
|
+
emit = callback
|
|
17
|
+
})
|
|
18
|
+
const error = new Error('listener failed')
|
|
19
|
+
const received: string[] = []
|
|
20
|
+
emitter.addListener(() => {
|
|
21
|
+
throw error
|
|
22
|
+
})
|
|
23
|
+
emitter.addListener((data) => received.push(data))
|
|
24
|
+
|
|
25
|
+
emit('first')
|
|
26
|
+
emit('second')
|
|
27
|
+
|
|
28
|
+
expect(received).toEqual(['first', 'second'])
|
|
29
|
+
expect(rethrown).toHaveLength(2)
|
|
30
|
+
expect(rethrown[0]).toThrow(error)
|
|
31
|
+
})
|
|
32
|
+
})
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { callListener } from './callListener'
|
|
2
|
+
|
|
1
3
|
type Callback<T> = (arg: T) => void
|
|
2
4
|
|
|
3
5
|
/**
|
|
@@ -37,7 +39,7 @@ export class NativeUpdatedValue<T> {
|
|
|
37
39
|
setter((data: T) => {
|
|
38
40
|
this.eventCount++
|
|
39
41
|
for (const listener of this.listeners) {
|
|
40
|
-
listener
|
|
42
|
+
callListener(listener, data)
|
|
41
43
|
}
|
|
42
44
|
})
|
|
43
45
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// React Native's global types lack it; the runtime has it.
|
|
2
|
+
declare function queueMicrotask(callback: () => void): void
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Calls `listener` with `arg`. An error it throws is rethrown from a microtask,
|
|
6
|
+
* so it reaches the global error handler and the caller's loop goes on to the
|
|
7
|
+
* listeners after it.
|
|
8
|
+
*/
|
|
9
|
+
export function callListener<T>(listener: (arg: T) => void, arg: T): void {
|
|
10
|
+
try {
|
|
11
|
+
listener(arg)
|
|
12
|
+
} catch (error) {
|
|
13
|
+
queueMicrotask(() => {
|
|
14
|
+
throw error
|
|
15
|
+
})
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -43,9 +43,9 @@ both platforms. The setup-only fields behave differently on each:
|
|
|
43
43
|
- **Android** reconfigures in place. The fields you pass are merged over the
|
|
44
44
|
previous ones, and the audio engine is only rebuilt when a construction-bound
|
|
45
45
|
field (buffers, wake mode, …) changed. Two fields are exceptions. Left out,
|
|
46
|
-
`remoteOutputs` turns the remote outputs off again, and `nowPlaying`
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
`remoteOutputs` turns the remote outputs off again, and `nowPlaying` goes
|
|
47
|
+
back to `true`: the track's own title and artist replace an earlier
|
|
48
|
+
formatter, so pass the formatter again to keep it.
|
|
49
49
|
- **iOS** replaces the player, built from that call's options alone. The queue
|
|
50
50
|
is cleared, and a setup-only field you leave out (`ios.category`,
|
|
51
51
|
`ios.buffer`, `retry`, …) goes back to its default, so pass the full `ios` and
|
package/website/guide/errors.md
CHANGED
|
@@ -251,6 +251,8 @@ if (error?.retrying) {
|
|
|
251
251
|
_both_ edges of the `'error'` state — entering it (with the error) and leaving
|
|
252
252
|
it (with `error: undefined`; the hook returns `undefined` again). Advisory
|
|
253
253
|
errors never pass through them.
|
|
254
|
+
`getPlaybackError()` is the same snapshot: `undefined` while
|
|
255
|
+
the error is still advisory.
|
|
254
256
|
|
|
255
257
|
The same advisory error also reaches the
|
|
256
258
|
[Now Playing formatter](/guide/now-playing#the-formatter-derived-continuous), so
|
|
@@ -348,7 +350,7 @@ message to render.
|
|
|
348
350
|
|
|
349
351
|
| API | Purpose |
|
|
350
352
|
| ----------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
351
|
-
| `usePlaybackError()` / `getPlaybackError()` | The current playback error (`{ kind, code, message, statusCode
|
|
353
|
+
| `usePlaybackError()` / `getPlaybackError()` | The current terminal playback error (`{ kind, code, message, statusCode? }`) — branch on `kind`. |
|
|
352
354
|
| `onPlaybackError` | Subscribe to terminal playback errors outside React (fires on entering/leaving `'error'`; advisory retrying errors arrive via `onPlaybackChanged`). |
|
|
353
355
|
| `retry()` | Re-attempt the current item; meant for the `'error'` state (on iOS it reloads from any state). |
|
|
354
356
|
| `setupPlayer({ retry })` | Automatic retry with two [duration budgets](#two-duration-budgets): first-connect and recovery. Off by default. |
|