@simba-dev/react-native-media-player 1.17.0 → 1.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simba-dev/react-native-media-player",
3
- "version": "1.17.0",
3
+ "version": "1.18.0",
4
4
  "description": "React Native video & audio player powered by libmpv — Picture-in-Picture, MediaSession, foreground service, and a customizable TypeScript UI. Built for Android. Designed as a drop-in replacement for react-native-video / react-native-track-player / expo-av. See CHANGELOG.md for per-version notes.",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",
@@ -682,9 +682,33 @@ export function PlayerProvider({
682
682
  setState(nextState);
683
683
  }
684
684
  // Progress: apply the patch via the functional setState
685
- // form so we can compare against the previous value (the
686
- // ref may have been updated by another event handler in
687
- // the same batch).
685
+ // form so we can compare against the COMMITTED value and
686
+ // skip the update when no field changed.
687
+ //
688
+ // The ref MUST be written HERE, synchronously, for the same
689
+ // reason `stateRef.current` is written above: `applyPlayerEvent`
690
+ // reads the CURRENT progress to compute the next one, and React
691
+ // DEFERS the updater function to the render phase. A ref written
692
+ // only inside the updater is therefore out of step with the state
693
+ // ref by one flush, and any event arriving before that flush
694
+ // computes its nextProgress from a STALE progress — silently
695
+ // dropping fields an earlier event had already delivered.
696
+ //
697
+ // MEASURED on device (2026-10-20, lib 1.17.0): mpv delivered
698
+ // `duration` and `time-pos` inside one batch. The duration event
699
+ // wrote `state.durationMs = 7673792` correctly, but the PROGRESS
700
+ // context kept `durationMs = 0`, because the position event —
701
+ // computed from the stale progress — replaced it with 0.
702
+ //
703
+ // The visible effect was a seek bar with no played fill, NO
704
+ // buffered range at all (the consumer gates the buffered window
705
+ // on `durationMs > 0`), and "--:--" for the remaining time — on a
706
+ // video that was demonstrably playing.
707
+ //
708
+ // Writing the ref here also makes the updater PURE: it used to
709
+ // write `progressRef.current` from inside the updater, which is a
710
+ // side effect in a function React may invoke more than once.
711
+ progressRef.current = nextProgress;
688
712
  setProgress((prev) => {
689
713
  // Cheap reference + field equality check.
690
714
  if (
@@ -698,7 +722,6 @@ export function PlayerProvider({
698
722
  ) {
699
723
  return prev;
700
724
  }
701
- progressRef.current = nextProgress;
702
725
  return nextProgress;
703
726
  });
704
727
 
@@ -0,0 +1,194 @@
1
+ /**
2
+ * Regression tests: the PROGRESS context must not lose a field that the
3
+ * STATE context already holds.
4
+ *
5
+ * THE DEFECT (found on device, emulator-5554, 2026-10-20, lib 1.17.0):
6
+ *
7
+ * mpv delivers `duration` and `time-pos` inside ONE batch, ~3 ms apart
8
+ * (measured: `fileLoaded` 19:29:59.615, `time-pos` 19:29:59.616,
9
+ * `duration` 19:29:59.619). The provider handled each event with
10
+ * `applyPlayerEvent(stateRef.current, progressRef.current, ...)` and then
11
+ * wrote the two refs:
12
+ *
13
+ * - `stateRef.current` was written SYNCHRONOUSLY, in the event handler.
14
+ * - `progressRef.current` was written INSIDE the `setProgress` updater,
15
+ * which React DEFERS to the render phase.
16
+ *
17
+ * So the progress ref was one flush behind the state ref. The second event
18
+ * of the batch therefore computed its `nextProgress` from a STALE progress
19
+ * — one that did not yet contain the duration the first event had just
20
+ * delivered — and overwrote it with 0.
21
+ *
22
+ * MEASURED on device:
23
+ *
24
+ * state.durationMs = 7673792 (correct)
25
+ * progress.durationMs = 0 (WRONG)
26
+ *
27
+ * The visible damage was a seek bar with no played fill, NO buffered range
28
+ * at all (the consumer gates the buffered window on `durationMs > 0`), and
29
+ * "--:--" for the remaining time — on a video that was demonstrably
30
+ * playing.
31
+ *
32
+ * These tests pin the fix: the progress ref is written synchronously, in
33
+ * lockstep with the state ref.
34
+ *
35
+ * Test surface:
36
+ * - duration survives in the progress context when duration and position
37
+ * arrive in one batch (the exact device order)
38
+ * - duration survives when the order is reversed
39
+ * - duration survives across a longer run of interleaved events
40
+ * - the buffered ranges survive the same interleaving
41
+ * - the two contexts agree on the duration — the invariant the seek bar
42
+ * depends on
43
+ *
44
+ * The MpvPlayerModule bridge mock is installed globally in
45
+ * `jest.setup.ts`; its `getDuration` returns 0, so the hydrated baseline
46
+ * duration is 0 and every value asserted here arrives through the EVENT
47
+ * path. A test that passed on the hydrated baseline would prove nothing.
48
+ */
49
+
50
+ import React from 'react';
51
+ import { act, renderHook } from '@testing-library/react-native';
52
+ import { PlayerProvider } from '../PlayerProvider';
53
+ import { usePlayer, usePlayerProgress } from '../../types/player';
54
+
55
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
56
+ <PlayerProvider>{children}</PlayerProvider>
57
+ );
58
+
59
+ /**
60
+ * Deliver an event through the SAME emitter production uses.
61
+ *
62
+ * The bridge's `resolveEmitter()` builds a `NativeEventEmitter` over
63
+ * `NativeModules.MpvPlayerModule`, and on the native side the events are
64
+ * published through `RCTDeviceEventEmitter`. RN's own jest preset mocks
65
+ * `DeviceEventEmitter`, and `NativeEventEmitter` delegates to it, so
66
+ * emitting there drives the real subscription the provider registered.
67
+ */
68
+ function emit(eventName: string, payload: Record<string, unknown>): void {
69
+ const {
70
+ DeviceEventEmitter,
71
+ } = require('react-native') as typeof import('react-native');
72
+ DeviceEventEmitter.emit(eventName, payload);
73
+ }
74
+
75
+ /** The real movie length from the device session: 7673.792 seconds. */
76
+ const DURATION_S = 7673.792;
77
+ const DURATION_MS = 7673792;
78
+
79
+ describe('PlayerProvider progress context keeps the duration', () => {
80
+ it('keeps the duration when duration and position arrive in ONE batch', async () => {
81
+ // The exact device scenario: two events, one batch, duration FIRST.
82
+ const { result, unmount } = await renderHook(
83
+ () => ({ state: usePlayer().state, progress: usePlayerProgress() }),
84
+ { wrapper },
85
+ );
86
+
87
+ // The hydrated baseline is 0 (the bridge mock's getDuration returns 0),
88
+ // so anything asserted below arrived through the event path.
89
+ expect(result.current.state.durationMs).toBe(0);
90
+ expect(result.current.progress.durationMs).toBe(0);
91
+
92
+ await act(async () => {
93
+ emit('onDurationChanged', { duration: DURATION_S });
94
+ emit('onPositionChanged', { position: 12.5 });
95
+ });
96
+
97
+ expect(result.current.state.durationMs).toBe(DURATION_MS);
98
+ // THE assertion this file exists for. Before the fix this was 0:
99
+ // the position event was computed from a stale progress and replaced
100
+ // the duration with the stale 0.
101
+ expect(result.current.progress.durationMs).toBe(DURATION_MS);
102
+ // The position must not be the casualty of the fix.
103
+ expect(result.current.progress.positionMs).toBe(12500);
104
+
105
+ await unmount();
106
+ });
107
+
108
+ it('keeps the duration when the position arrives FIRST in the batch', async () => {
109
+ // The mirror order. A fix that only special-cased "duration first"
110
+ // would pass the test above and fail this one.
111
+ const { result, unmount } = await renderHook(
112
+ () => ({ state: usePlayer().state, progress: usePlayerProgress() }),
113
+ { wrapper },
114
+ );
115
+
116
+ await act(async () => {
117
+ emit('onPositionChanged', { position: 12.5 });
118
+ emit('onDurationChanged', { duration: DURATION_S });
119
+ });
120
+
121
+ expect(result.current.progress.durationMs).toBe(DURATION_MS);
122
+ expect(result.current.progress.positionMs).toBe(12500);
123
+
124
+ await unmount();
125
+ });
126
+
127
+ it('keeps the duration across a long run of interleaved events', async () => {
128
+ // The real event stream is not two events: mpv keeps sending
129
+ // `time-pos` for the whole session. Every one of them used to carry a
130
+ // stale duration into the progress context.
131
+ const { result, unmount } = await renderHook(
132
+ () => ({ state: usePlayer().state, progress: usePlayerProgress() }),
133
+ { wrapper },
134
+ );
135
+
136
+ await act(async () => {
137
+ emit('onDurationChanged', { duration: DURATION_S });
138
+ for (let i = 1; i <= 25; i++) {
139
+ emit('onPositionChanged', { position: i * 0.5 });
140
+ }
141
+ });
142
+
143
+ expect(result.current.progress.durationMs).toBe(DURATION_MS);
144
+ expect(result.current.progress.positionMs).toBe(12500);
145
+
146
+ await unmount();
147
+ });
148
+
149
+ it('keeps the buffered ranges through the same interleaving', async () => {
150
+ // The buffered ranges are the other half of the damage: the consumer
151
+ // gates the buffered window on `durationMs > 0`, so a lost duration
152
+ // silently removed the buffered indicator from the seek bar.
153
+ const { result, unmount } = await renderHook(
154
+ () => ({ state: usePlayer().state, progress: usePlayerProgress() }),
155
+ { wrapper },
156
+ );
157
+
158
+ await act(async () => {
159
+ emit('onDurationChanged', { duration: DURATION_S });
160
+ emit('onCacheState', { ranges: [{ start: 0, end: 130713.917 }] });
161
+ emit('onPositionChanged', { position: 12.5 });
162
+ });
163
+
164
+ expect(result.current.progress.durationMs).toBe(DURATION_MS);
165
+ expect(result.current.progress.cacheRanges).toEqual([
166
+ { start: 0, end: 130713.917 },
167
+ ]);
168
+
169
+ await unmount();
170
+ });
171
+
172
+ it('keeps the state context and the progress context in agreement', async () => {
173
+ // The invariant the seek bar depends on: it reads the duration from
174
+ // the progress context and the play state from the state context, so
175
+ // the two must never disagree about the length of the file.
176
+ const { result, unmount } = await renderHook(
177
+ () => ({ state: usePlayer().state, progress: usePlayerProgress() }),
178
+ { wrapper },
179
+ );
180
+
181
+ await act(async () => {
182
+ emit('onDurationChanged', { duration: DURATION_S });
183
+ emit('onPositionChanged', { position: 12.5 });
184
+ emit('onCacheState', { ranges: [{ start: 0, end: 130713.917 }] });
185
+ });
186
+
187
+ expect(result.current.progress.durationMs).toBe(
188
+ result.current.state.durationMs,
189
+ );
190
+ expect(result.current.progress.durationMs).toBe(DURATION_MS);
191
+
192
+ await unmount();
193
+ });
194
+ });