@therealtinhtute/baroiplayer 1.0.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.
Files changed (244) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +384 -0
  3. package/dist/context/media.js +2 -0
  4. package/dist/context/media.js.map +1 -0
  5. package/dist/context/media.mjs +15 -0
  6. package/dist/context/media.mjs.map +1 -0
  7. package/dist/context/store.js +2 -0
  8. package/dist/context/store.js.map +1 -0
  9. package/dist/context/store.mjs +5 -0
  10. package/dist/context/store.mjs.map +1 -0
  11. package/dist/context-BMteAJDN.js +1140 -0
  12. package/dist/context-BMteAJDN.js.map +1 -0
  13. package/dist/context-DCJ_scmO.cjs +23 -0
  14. package/dist/context-DCJ_scmO.cjs.map +1 -0
  15. package/dist/core-events-B5W_j8yU.js +480 -0
  16. package/dist/core-events-B5W_j8yU.js.map +1 -0
  17. package/dist/core-events-CN0-Vhhq.cjs +2 -0
  18. package/dist/core-events-CN0-Vhhq.cjs.map +1 -0
  19. package/dist/core-plugins-Bq9KLany.js +696 -0
  20. package/dist/core-plugins-Bq9KLany.js.map +1 -0
  21. package/dist/core-plugins-CPDSHMiQ.cjs +2 -0
  22. package/dist/core-plugins-CPDSHMiQ.cjs.map +1 -0
  23. package/dist/core.js +37 -0
  24. package/dist/core.js.map +1 -0
  25. package/dist/core.mjs +1217 -0
  26. package/dist/core.mjs.map +1 -0
  27. package/dist/enhanced-components-DHv0MFvG.js +1073 -0
  28. package/dist/enhanced-components-DHv0MFvG.js.map +1 -0
  29. package/dist/enhanced-components-DTIObgB7.cjs +2 -0
  30. package/dist/enhanced-components-DTIObgB7.cjs.map +1 -0
  31. package/dist/hooks-C_r8wexn.cjs +4 -0
  32. package/dist/hooks-C_r8wexn.cjs.map +1 -0
  33. package/dist/hooks-Da2wVXlF.js +1630 -0
  34. package/dist/hooks-Da2wVXlF.js.map +1 -0
  35. package/dist/index.css +1 -0
  36. package/dist/index.js +112 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/index.mjs +6126 -0
  39. package/dist/index.mjs.map +1 -0
  40. package/dist/minimal-components-DSq5sQpy.js +2002 -0
  41. package/dist/minimal-components-DSq5sQpy.js.map +1 -0
  42. package/dist/minimal-components-DflWiihN.cjs +2 -0
  43. package/dist/minimal-components-DflWiihN.cjs.map +1 -0
  44. package/dist/minimal-components.css +1 -0
  45. package/dist/mona-loading.gif +0 -0
  46. package/dist/performance-monitor-CUW6f5ll.cjs +2 -0
  47. package/dist/performance-monitor-CUW6f5ll.cjs.map +1 -0
  48. package/dist/performance-monitor-rMW1RgV3.js +302 -0
  49. package/dist/performance-monitor-rMW1RgV3.js.map +1 -0
  50. package/dist/utils-BnscpYYd.js +2798 -0
  51. package/dist/utils-BnscpYYd.js.map +1 -0
  52. package/dist/utils-D_czS_0K.cjs +9 -0
  53. package/dist/utils-D_czS_0K.cjs.map +1 -0
  54. package/dist/utils.js +2 -0
  55. package/dist/utils.js.map +1 -0
  56. package/dist/utils.mjs +7 -0
  57. package/dist/utils.mjs.map +1 -0
  58. package/package.json +114 -0
  59. package/src/adapters/videojs-adapter.tsx +470 -0
  60. package/src/assets/mona-loading.gif +0 -0
  61. package/src/components/AnimateLoading.tsx +115 -0
  62. package/src/components/LoadingState.tsx +248 -0
  63. package/src/components/accessibility/keyboard-help-overlay.tsx +213 -0
  64. package/src/components/audio-player.tsx +85 -0
  65. package/src/components/basic/index.ts +37 -0
  66. package/src/components/enhanced/advanced-features.tsx +316 -0
  67. package/src/components/enhanced/debug-panel.tsx +422 -0
  68. package/src/components/enhanced/enhanced-controls.tsx +600 -0
  69. package/src/components/enhanced/enhanced-media-player.tsx +509 -0
  70. package/src/components/enhanced/error-handling.tsx +345 -0
  71. package/src/components/enhanced/index.ts +39 -0
  72. package/src/components/enhanced/media-player-enhanced.tsx +349 -0
  73. package/src/components/examples/basic-video-player.tsx +55 -0
  74. package/src/components/examples/custom-controls-example.tsx +63 -0
  75. package/src/components/examples/enhanced-controls-example.tsx +114 -0
  76. package/src/components/examples/enhanced-media-player-example.tsx +293 -0
  77. package/src/components/examples/loading-states-demo.tsx +142 -0
  78. package/src/components/examples/react-19-demo.tsx +381 -0
  79. package/src/components/examples/volume-orientation-demo.tsx +114 -0
  80. package/src/components/icon-demo.tsx +209 -0
  81. package/src/components/icons/index.tsx +166 -0
  82. package/src/components/icons/svg-icons/ArrowLeftIcon.tsx +18 -0
  83. package/src/components/icons/svg-icons/ArrowRightIcon.tsx +18 -0
  84. package/src/components/icons/svg-icons/AudioIcon.tsx +22 -0
  85. package/src/components/icons/svg-icons/BackwardIcon.tsx +12 -0
  86. package/src/components/icons/svg-icons/CameraIcon.tsx +19 -0
  87. package/src/components/icons/svg-icons/CheckIcon.tsx +19 -0
  88. package/src/components/icons/svg-icons/EnablePiPIcon.tsx +17 -0
  89. package/src/components/icons/svg-icons/ForwardIcon.tsx +12 -0
  90. package/src/components/icons/svg-icons/FullscreenEnterIcon.tsx +23 -0
  91. package/src/components/icons/svg-icons/FullscreenExitIcon.tsx +23 -0
  92. package/src/components/icons/svg-icons/LoadingIcon.tsx +32 -0
  93. package/src/components/icons/svg-icons/PauseIcon.tsx +23 -0
  94. package/src/components/icons/svg-icons/PlayIcon.tsx +21 -0
  95. package/src/components/icons/svg-icons/PlaybackSpeedIcon.tsx +23 -0
  96. package/src/components/icons/svg-icons/QualityIcon.tsx +21 -0
  97. package/src/components/icons/svg-icons/SettingsIcon.tsx +12 -0
  98. package/src/components/icons/svg-icons/SliderIcon.tsx +21 -0
  99. package/src/components/icons/svg-icons/SubtitleIcon.tsx +21 -0
  100. package/src/components/icons/svg-icons/SubtitleOffIcon.tsx +25 -0
  101. package/src/components/icons/svg-icons/UnPiPIcon.tsx +17 -0
  102. package/src/components/icons/svg-icons/VolumeMutedIcon.tsx +16 -0
  103. package/src/components/icons/svg-icons/VolumeOneIcon.tsx +22 -0
  104. package/src/components/icons/svg-icons/VolumeThreeIcon.tsx +16 -0
  105. package/src/components/icons/svg-icons/VolumeTwoIcon.tsx +16 -0
  106. package/src/components/legacy.tsx +93 -0
  107. package/src/components/media-metadata-display.tsx +100 -0
  108. package/src/components/media-player-audio.tsx +39 -0
  109. package/src/components/media-player-controls.tsx +226 -0
  110. package/src/components/media-player-error.tsx +163 -0
  111. package/src/components/media-player-fullscreen.tsx +109 -0
  112. package/src/components/media-player-hls.tsx +111 -0
  113. package/src/components/media-player-loading.tsx +76 -0
  114. package/src/components/media-player-play.tsx +75 -0
  115. package/src/components/media-player-root.tsx +340 -0
  116. package/src/components/media-player-seek.tsx +168 -0
  117. package/src/components/media-player-time.tsx +99 -0
  118. package/src/components/media-player-tooltip.tsx +58 -0
  119. package/src/components/media-player-video.tsx +39 -0
  120. package/src/components/media-player-volume.tsx +155 -0
  121. package/src/components/media-player.tsx +257 -0
  122. package/src/components/media-suspense.tsx +362 -0
  123. package/src/components/player-settings-form.tsx +163 -0
  124. package/src/components/react-player-wrapper.tsx +651 -0
  125. package/src/components/simple-video-player.tsx +60 -0
  126. package/src/components/ui/enhanced-seek-bar.tsx +235 -0
  127. package/src/components/ui/fullscreen-button.tsx +60 -0
  128. package/src/components/ui/media-resource-preloader.tsx +395 -0
  129. package/src/components/ui/play-button.tsx +107 -0
  130. package/src/components/ui/seek-bar.tsx +161 -0
  131. package/src/components/ui/time-display.tsx +40 -0
  132. package/src/components/ui/volume-control.tsx +143 -0
  133. package/src/components/unified/base-media-player.tsx +246 -0
  134. package/src/components/unified/media-player.tsx +75 -0
  135. package/src/components/variants/minimal/AccessibleMinimalPlayer.tsx +561 -0
  136. package/src/components/variants/minimal/EnhancedMinimalMediaPlayer.tsx +552 -0
  137. package/src/components/variants/minimal/MinimalControls.tsx +142 -0
  138. package/src/components/variants/minimal/MinimalLoading.tsx +32 -0
  139. package/src/components/variants/minimal/MinimalMediaPlayer.tsx +315 -0
  140. package/src/components/variants/minimal/MinimalOverlay.tsx +64 -0
  141. package/src/components/variants/minimal/MinimalPlayButton.tsx +65 -0
  142. package/src/components/variants/minimal/MinimalSeekBar.tsx +150 -0
  143. package/src/components/variants/minimal/MinimalVolumeControl.tsx +80 -0
  144. package/src/components/variants/minimal/SimpleMinimalPlayer.tsx +210 -0
  145. package/src/components/variants/minimal/components/CenterPlayButton.tsx +22 -0
  146. package/src/components/variants/minimal/components/ControlBar.tsx +146 -0
  147. package/src/components/variants/minimal/components/EnhancedSettingsMenu.tsx +361 -0
  148. package/src/components/variants/minimal/components/PlaybackControls.tsx +51 -0
  149. package/src/components/variants/minimal/components/RightControls.tsx +173 -0
  150. package/src/components/variants/minimal/components/SeekBar.tsx +131 -0
  151. package/src/components/variants/minimal/components/SettingsDropdown.tsx +70 -0
  152. package/src/components/variants/minimal/components/SubtitleDisplay.tsx +21 -0
  153. package/src/components/variants/minimal/components/TimeDisplay.tsx +18 -0
  154. package/src/components/variants/minimal/components/VideoElement.tsx +40 -0
  155. package/src/components/variants/minimal/components/VolumeControl.tsx +217 -0
  156. package/src/components/variants/minimal/index.ts +23 -0
  157. package/src/components/video-player.tsx +100 -0
  158. package/src/context/error-handling.ts +394 -0
  159. package/src/context/media-context.tsx +208 -0
  160. package/src/context/media-provider.tsx +265 -0
  161. package/src/context/media-store.ts +379 -0
  162. package/src/context/persistence.ts +453 -0
  163. package/src/core/events/event-system.ts +442 -0
  164. package/src/core/events/hooks.ts +302 -0
  165. package/src/core/events/index.ts +22 -0
  166. package/src/core/events/middleware.ts +269 -0
  167. package/src/core/index.ts +22 -0
  168. package/src/core/optimization/bundle-optimizer.tsx +308 -0
  169. package/src/core/performance/cicd.ts +96 -0
  170. package/src/core/performance/index.ts +38 -0
  171. package/src/core/performance/media-loading-optimizer.ts +538 -0
  172. package/src/core/performance/performance-budgets.ts +432 -0
  173. package/src/core/performance/performance-monitor.tsx +476 -0
  174. package/src/core/performance/performance-testing.ts +459 -0
  175. package/src/core/plugins/base-plugin.ts +224 -0
  176. package/src/core/plugins/enhanced-plugin-system.ts +411 -0
  177. package/src/core/plugins/examples/analytics-plugin.ts +242 -0
  178. package/src/core/plugins/index.ts +39 -0
  179. package/src/core/plugins/registry.ts +327 -0
  180. package/src/core/plugins/types.ts +159 -0
  181. package/src/core/plugins/utils.ts +316 -0
  182. package/src/core/progressive-loading.tsx +322 -0
  183. package/src/core/providers/enhanced-hls-provider.ts +339 -0
  184. package/src/core/providers/service-provider.tsx +168 -0
  185. package/src/core/utils.tsx +38 -0
  186. package/src/features/media-session/media-session-manager.ts +429 -0
  187. package/src/features/picture-in-picture/pip-manager.ts +429 -0
  188. package/src/features/playlist/playlist-manager.ts +606 -0
  189. package/src/features/subtitles/subtitle-manager.ts +441 -0
  190. package/src/features/subtitles/subtitle-parser.ts +280 -0
  191. package/src/features/thumbnails/thumbnail-generator.ts +519 -0
  192. package/src/hooks/accessibility/index.ts +27 -0
  193. package/src/hooks/accessibility/use-accessibility-preferences.ts +188 -0
  194. package/src/hooks/accessibility/use-focus-management.ts +176 -0
  195. package/src/hooks/accessibility/use-keyboard-shortcuts.ts +144 -0
  196. package/src/hooks/accessibility/use-screen-reader.ts +182 -0
  197. package/src/hooks/core/index.ts +47 -0
  198. package/src/hooks/legacy.ts +27 -0
  199. package/src/hooks/use-enhanced-media-player.ts +468 -0
  200. package/src/hooks/use-intelligent-preloading.ts +358 -0
  201. package/src/hooks/use-keyboard-controls.ts +115 -0
  202. package/src/hooks/use-media-actions.ts +175 -0
  203. package/src/hooks/use-media-metadata.ts +89 -0
  204. package/src/hooks/use-media-optimistic.ts +270 -0
  205. package/src/hooks/use-media-player.ts +312 -0
  206. package/src/hooks/use-media-state.ts +190 -0
  207. package/src/hooks/use-memory-optimization.ts +358 -0
  208. package/src/hooks/use-optimistic-controls.ts +92 -0
  209. package/src/hooks/use-optimized-media.ts +263 -0
  210. package/src/hooks/use-touch-gestures.ts +417 -0
  211. package/src/hooks/useEnhancedSubtitles.ts +258 -0
  212. package/src/hooks/useKeyboardShortcuts.ts +74 -0
  213. package/src/hooks/useMediaControls.ts +52 -0
  214. package/src/hooks/useScreenshot.ts +57 -0
  215. package/src/hooks/useSubtitles.ts +91 -0
  216. package/src/hooks/useVideoPlayer.ts +232 -0
  217. package/src/icons/components/FullscreenIcon.tsx +37 -0
  218. package/src/icons/components/SettingsIcon.tsx +38 -0
  219. package/src/icons/components/animated/LoadingIcon.tsx +43 -0
  220. package/src/icons/components/animated/PlayPauseIcon.tsx +50 -0
  221. package/src/icons/components/media/PauseIcon.tsx +39 -0
  222. package/src/icons/components/media/PlayIcon.tsx +38 -0
  223. package/src/icons/components/volume/VolumeIcon.tsx +39 -0
  224. package/src/icons/components/volume/VolumeOffIcon.tsx +39 -0
  225. package/src/icons/index.ts +56 -0
  226. package/src/icons/provider.tsx +64 -0
  227. package/src/icons/types.ts +40 -0
  228. package/src/index-enhanced.ts +35 -0
  229. package/src/index-legacy.ts +326 -0
  230. package/src/index.css +102 -0
  231. package/src/index.ts +216 -0
  232. package/src/lib/deprecation.ts +51 -0
  233. package/src/lib/format-time.ts +37 -0
  234. package/src/lib/index.ts +3 -0
  235. package/src/lib/utils.ts +6 -0
  236. package/src/styles/globals.css +462 -0
  237. package/src/styles/minimal-player.css +589 -0
  238. package/src/styles/pixel-art.css +46 -0
  239. package/src/styles/videojs.css +100 -0
  240. package/src/test/setup.ts +79 -0
  241. package/src/types/events.ts +47 -0
  242. package/src/types/formats.ts +95 -0
  243. package/src/types/index.ts +190 -0
  244. package/src/types/videojs-plugins.d.ts +14 -0
@@ -0,0 +1,265 @@
1
+ "use client"
2
+
3
+ import React from "react"
4
+ import {
5
+ type MediaAction,
6
+ MediaActionTypes,
7
+ MediaContext,
8
+ type MediaContextValue,
9
+ type MediaState,
10
+ type MediaStore,
11
+ MediaStoreContext,
12
+ initialMediaState
13
+ } from "./media-context"
14
+ import { createEnhancedMediaStore, type EnhancedMediaStore } from "./media-store"
15
+ import { useAutoMediaPersistence, type PersistenceConfig } from "./persistence"
16
+ import { MediaErrorHandler, type RetryConfig } from "./error-handling"
17
+
18
+ // Legacy store for backward compatibility
19
+ function createLegacyMediaStore(initialState: MediaState = initialMediaState): MediaStore {
20
+ let state = { ...initialState }
21
+ const listeners = new Set<() => void>()
22
+
23
+ const subscribe = (callback: () => void) => {
24
+ listeners.add(callback)
25
+ return () => listeners.delete(callback)
26
+ }
27
+
28
+ const getState = () => state
29
+
30
+ const setState = (updates: Partial<MediaState>) => {
31
+ const newState = { ...state, ...updates }
32
+ if (JSON.stringify(newState) !== JSON.stringify(state)) {
33
+ state = newState
34
+ listeners.forEach((callback) => callback())
35
+ }
36
+ }
37
+
38
+ const dispatch = (action: MediaAction) => {
39
+ switch (action.type) {
40
+ case MediaActionTypes.PLAY:
41
+ setState({ isPlaying: true, error: null })
42
+ break
43
+
44
+ case MediaActionTypes.PAUSE:
45
+ setState({ isPlaying: false })
46
+ break
47
+
48
+ case MediaActionTypes.SEEK:
49
+ setState({ currentTime: action.detail })
50
+ break
51
+
52
+ case MediaActionTypes.SET_VOLUME:
53
+ setState({ volume: Math.max(0, Math.min(1, action.detail)) })
54
+ break
55
+
56
+ case MediaActionTypes.MUTE:
57
+ setState({ isMuted: true })
58
+ break
59
+
60
+ case MediaActionTypes.UNMUTE:
61
+ setState({ isMuted: false })
62
+ break
63
+
64
+ case MediaActionTypes.TOGGLE_MUTE:
65
+ setState({ isMuted: !state.isMuted })
66
+ break
67
+
68
+ case MediaActionTypes.SET_DURATION:
69
+ setState({ duration: action.detail })
70
+ break
71
+
72
+ case MediaActionTypes.SET_CURRENT_TIME:
73
+ setState({ currentTime: action.detail })
74
+ break
75
+
76
+ case MediaActionTypes.SET_BUFFERED:
77
+ setState({ buffered: action.detail })
78
+ break
79
+
80
+ case MediaActionTypes.SET_LOADING:
81
+ setState({ isLoading: action.detail })
82
+ break
83
+
84
+ case MediaActionTypes.SET_ERROR:
85
+ setState({ error: action.detail, isLoading: false })
86
+ break
87
+
88
+ case MediaActionTypes.ENTER_FULLSCREEN:
89
+ setState({ isFullscreen: true })
90
+ break
91
+
92
+ case MediaActionTypes.EXIT_FULLSCREEN:
93
+ setState({ isFullscreen: false })
94
+ break
95
+
96
+ case MediaActionTypes.ENTER_PIP:
97
+ setState({ isPictureInPicture: true })
98
+ break
99
+
100
+ case MediaActionTypes.EXIT_PIP:
101
+ setState({ isPictureInPicture: false })
102
+ break
103
+
104
+ case MediaActionTypes.SET_PLAYBACK_RATE:
105
+ setState({ playbackRate: action.detail })
106
+ break
107
+
108
+ case MediaActionTypes.SET_SUBTITLE_TRACK:
109
+ setState({ selectedSubtitleTrack: action.detail })
110
+ break
111
+
112
+ case MediaActionTypes.SET_RENDITION:
113
+ setState({ selectedRendition: action.detail })
114
+ break
115
+
116
+ case MediaActionTypes.TOGGLE_SUBTITLES:
117
+ setState({ subtitlesShowing: !state.subtitlesShowing })
118
+ break
119
+
120
+ case MediaActionTypes.PREVIEW_TIME:
121
+ setState({ previewTime: action.detail })
122
+ break
123
+
124
+ case MediaActionTypes.CLEAR_PREVIEW:
125
+ setState({ previewTime: null, previewImage: null, previewCoords: null })
126
+ break
127
+
128
+ default:
129
+ console.warn(`Unknown media action type: ${action.type}`)
130
+ }
131
+ }
132
+
133
+ return { subscribe, getState, setState, dispatch }
134
+ }
135
+
136
+ // Enhanced media provider props with React 19 features
137
+ export interface MediaProviderProps {
138
+ children: React.ReactNode
139
+ mediaId?: string
140
+ labelId?: string
141
+ descriptionId?: string
142
+ dir?: "ltr" | "rtl"
143
+ portalContainer?: HTMLElement
144
+ tooltipDelayDuration?: number
145
+ tooltipSideOffset?: number
146
+ disabled?: boolean
147
+ isVideo?: boolean
148
+ withoutTooltip?: boolean
149
+ initialState?: Partial<MediaState>
150
+
151
+ // React 19 enhancements
152
+ useEnhancedStore?: boolean
153
+ persistenceConfig?: Partial<PersistenceConfig>
154
+ retryConfig?: Partial<RetryConfig>
155
+ enableOptimisticUpdates?: boolean
156
+ enableErrorRecovery?: boolean
157
+ onStateRehydrated?: (state: Partial<MediaState>) => void
158
+ }
159
+
160
+ // Enhanced media provider component with React 19 features
161
+ export function MediaProvider({
162
+ children,
163
+ mediaId = "media-player",
164
+ labelId = "media-player-label",
165
+ descriptionId = "media-player-description",
166
+ dir = "ltr",
167
+ portalContainer,
168
+ tooltipDelayDuration = 700,
169
+ tooltipSideOffset = 4,
170
+ disabled = false,
171
+ isVideo = true,
172
+ withoutTooltip = false,
173
+ initialState = {},
174
+ // React 19 enhancements
175
+ useEnhancedStore = true,
176
+ persistenceConfig,
177
+ retryConfig,
178
+ enableOptimisticUpdates = true,
179
+ enableErrorRecovery = true,
180
+ onStateRehydrated
181
+ }: MediaProviderProps) {
182
+ const rootRef = React.useRef<HTMLDivElement>(null)
183
+ const mediaRef = React.useRef<HTMLVideoElement | HTMLAudioElement>(null)
184
+
185
+ // Create enhanced or legacy store based on configuration
186
+ const store = React.useMemo(() => {
187
+ const combinedInitialState = { ...initialMediaState, ...initialState }
188
+
189
+ if (useEnhancedStore) {
190
+ return createEnhancedMediaStore(combinedInitialState, persistenceConfig) as MediaStore & EnhancedMediaStore
191
+ } else {
192
+ return createLegacyMediaStore(combinedInitialState)
193
+ }
194
+ }, [useEnhancedStore, persistenceConfig])
195
+
196
+ // Error handler for React 19 features
197
+ const errorHandler = React.useMemo(() => {
198
+ if (enableErrorRecovery && useEnhancedStore) {
199
+ return new MediaErrorHandler(retryConfig)
200
+ }
201
+ return null
202
+ }, [enableErrorRecovery, useEnhancedStore, retryConfig])
203
+
204
+ // Auto-persistence for React 19 enhanced store
205
+ const persistenceResult = useEnhancedStore ? useAutoMediaPersistence(
206
+ store.getState(),
207
+ mediaId,
208
+ persistenceConfig
209
+ ) : null
210
+
211
+ // Handle state rehydration callback
212
+ React.useEffect(() => {
213
+ if (persistenceResult?.isRehydrated && onStateRehydrated) {
214
+ const currentState = store.getState()
215
+ onStateRehydrated(currentState)
216
+ }
217
+ }, [persistenceResult?.isRehydrated, onStateRehydrated, store])
218
+
219
+ // Enhanced context value with React 19 features
220
+ const contextValue = React.useMemo<MediaContextValue & {
221
+ errorHandler?: MediaErrorHandler | null
222
+ isEnhanced?: boolean
223
+ isRehydrated?: boolean
224
+ }>(
225
+ () => ({
226
+ mediaId,
227
+ labelId,
228
+ descriptionId,
229
+ dir,
230
+ rootRef,
231
+ mediaRef,
232
+ portalContainer,
233
+ tooltipDelayDuration,
234
+ tooltipSideOffset,
235
+ disabled,
236
+ isVideo,
237
+ withoutTooltip,
238
+ // React 19 enhancements
239
+ errorHandler,
240
+ isEnhanced: useEnhancedStore,
241
+ isRehydrated: persistenceResult?.isRehydrated
242
+ }),
243
+ [
244
+ mediaId,
245
+ labelId,
246
+ descriptionId,
247
+ dir,
248
+ portalContainer,
249
+ tooltipDelayDuration,
250
+ tooltipSideOffset,
251
+ disabled,
252
+ isVideo,
253
+ withoutTooltip,
254
+ errorHandler,
255
+ useEnhancedStore,
256
+ persistenceResult?.isRehydrated
257
+ ]
258
+ )
259
+
260
+ return (
261
+ <MediaContext.Provider value={contextValue}>
262
+ <MediaStoreContext.Provider value={store}>{children}</MediaStoreContext.Provider>
263
+ </MediaContext.Provider>
264
+ )
265
+ }
@@ -0,0 +1,379 @@
1
+ "use client"
2
+
3
+ import React from "react"
4
+ import type { MediaState, MediaAction, MediaActionType } from "./media-context"
5
+ import { MediaActionTypes, initialMediaState } from "./media-context"
6
+
7
+ // Enhanced action types for React 19 features
8
+ export interface OptimisticMediaAction extends MediaAction {
9
+ optimistic?: boolean
10
+ retryCount?: number
11
+ timestamp?: number
12
+ }
13
+
14
+ // Action state for useActionState
15
+ export interface MediaActionState {
16
+ pending: boolean
17
+ error: string | null
18
+ lastAction: OptimisticMediaAction | null
19
+ retryCount: number
20
+ }
21
+
22
+ // State persistence configuration
23
+ export interface PersistenceConfig {
24
+ enabled: boolean
25
+ key: string
26
+ persist: Array<keyof MediaState>
27
+ debounceMs: number
28
+ }
29
+
30
+ // Enhanced media store interface
31
+ export interface EnhancedMediaStore {
32
+ // Original store interface
33
+ subscribe: (callback: () => void) => () => void
34
+ getState: () => MediaState
35
+ setState: (updates: Partial<MediaState>) => void
36
+ dispatch: (action: MediaAction) => void
37
+
38
+ // React 19 enhancements
39
+ getOptimisticState: () => MediaState
40
+ dispatchOptimistic: (action: OptimisticMediaAction) => Promise<void>
41
+ rollbackOptimistic: (actionId: string) => void
42
+ getActionState: () => MediaActionState
43
+ setPersistenceConfig: (config: Partial<PersistenceConfig>) => void
44
+ rehydrateFromStorage: () => Promise<void>
45
+ clearPersistedState: () => void
46
+ }
47
+
48
+ // Default persistence configuration
49
+ const DEFAULT_PERSISTENCE_CONFIG: PersistenceConfig = {
50
+ enabled: true,
51
+ key: "baroi-player-state",
52
+ persist: ["volume", "isMuted", "playbackRate", "subtitlesShowing"],
53
+ debounceMs: 500
54
+ }
55
+
56
+ // Create enhanced media store with React 19 features
57
+ export function createEnhancedMediaStore(
58
+ initialState: MediaState = initialMediaState,
59
+ persistenceConfig: Partial<PersistenceConfig> = {}
60
+ ): EnhancedMediaStore {
61
+ let state = { ...initialState }
62
+ let optimisticState = { ...initialState }
63
+ let actionState: MediaActionState = {
64
+ pending: false,
65
+ error: null,
66
+ lastAction: null,
67
+ retryCount: 0
68
+ }
69
+
70
+ const config = { ...DEFAULT_PERSISTENCE_CONFIG, ...persistenceConfig }
71
+ const listeners = new Set<() => void>()
72
+ const optimisticQueue = new Map<string, OptimisticMediaAction>()
73
+ let persistenceTimeout: NodeJS.Timeout | null = null
74
+
75
+ const subscribe = (callback: () => void) => {
76
+ listeners.add(callback)
77
+ return () => listeners.delete(callback)
78
+ }
79
+
80
+ const notifyListeners = () => {
81
+ listeners.forEach((callback) => callback())
82
+ }
83
+
84
+ const getState = () => state
85
+
86
+ const getOptimisticState = () => optimisticState
87
+
88
+ const getActionState = () => actionState
89
+
90
+ // Persistence helpers
91
+ const persistState = async () => {
92
+ if (!config.enabled || typeof window === 'undefined') return
93
+
94
+ const persistedData = config.persist.reduce((acc, key) => {
95
+ (acc as any)[key] = state[key]
96
+ return acc
97
+ }, {} as Partial<MediaState>)
98
+
99
+ try {
100
+ localStorage.setItem(config.key, JSON.stringify({
101
+ ...persistedData,
102
+ timestamp: Date.now()
103
+ }))
104
+ } catch (error) {
105
+ console.warn('Failed to persist media state:', error)
106
+ }
107
+ }
108
+
109
+ const debouncedPersist = () => {
110
+ if (persistenceTimeout) {
111
+ clearTimeout(persistenceTimeout)
112
+ }
113
+ persistenceTimeout = setTimeout(persistState, config.debounceMs)
114
+ }
115
+
116
+ const rehydrateFromStorage = async () => {
117
+ if (!config.enabled || typeof window === 'undefined') return
118
+
119
+ try {
120
+ const stored = localStorage.getItem(config.key)
121
+ if (!stored) return
122
+
123
+ const parsedData = JSON.parse(stored) as any
124
+ const { timestamp, ...persistedState } = parsedData
125
+
126
+ // Only rehydrate if data is less than 7 days old
127
+ const maxAge = 7 * 24 * 60 * 60 * 1000 // 7 days
128
+ if (timestamp && Date.now() - timestamp > maxAge) {
129
+ clearPersistedState()
130
+ return
131
+ }
132
+
133
+ // Merge persisted state with current state
134
+ const newState = { ...state, ...persistedState }
135
+ if (JSON.stringify(newState) !== JSON.stringify(state)) {
136
+ state = newState
137
+ optimisticState = { ...newState }
138
+ notifyListeners()
139
+ }
140
+ } catch (error) {
141
+ console.warn('Failed to rehydrate media state:', error)
142
+ }
143
+ }
144
+
145
+ const clearPersistedState = () => {
146
+ if (typeof window === 'undefined') return
147
+ try {
148
+ localStorage.removeItem(config.key)
149
+ } catch (error) {
150
+ console.warn('Failed to clear persisted state:', error)
151
+ }
152
+ }
153
+
154
+ const setPersistenceConfig = (newConfig: Partial<PersistenceConfig>) => {
155
+ Object.assign(config, newConfig)
156
+ }
157
+
158
+ const setState = (updates: Partial<MediaState>) => {
159
+ const newState = { ...state, ...updates }
160
+ if (JSON.stringify(newState) !== JSON.stringify(state)) {
161
+ state = newState
162
+ optimisticState = { ...newState }
163
+
164
+ // Persist relevant changes
165
+ debouncedPersist()
166
+ notifyListeners()
167
+ }
168
+ }
169
+
170
+ const applyOptimisticUpdate = (action: OptimisticMediaAction) => {
171
+ const actionId = `${action.type}-${Date.now()}-${Math.random()}`
172
+
173
+ // Store original action for rollback
174
+ optimisticQueue.set(actionId, { ...action, timestamp: Date.now() })
175
+
176
+ // Apply optimistic update to optimistic state
177
+ const optimisticUpdate = processMediaAction(optimisticState, action)
178
+ optimisticState = { ...optimisticState, ...optimisticUpdate }
179
+
180
+ notifyListeners()
181
+ return actionId
182
+ }
183
+
184
+ const rollbackOptimistic = (actionId: string) => {
185
+ if (!optimisticQueue.has(actionId)) return
186
+
187
+ // Remove from queue and recalculate optimistic state
188
+ optimisticQueue.delete(actionId)
189
+
190
+ // Rebuild optimistic state from actual state + remaining optimistic actions
191
+ optimisticState = { ...state }
192
+ for (const [, action] of optimisticQueue) {
193
+ const update = processMediaAction(optimisticState, action)
194
+ optimisticState = { ...optimisticState, ...update }
195
+ }
196
+
197
+ notifyListeners()
198
+ }
199
+
200
+ // Process media action and return state updates
201
+ const processMediaAction = (currentState: MediaState, action: MediaAction): Partial<MediaState> => {
202
+ switch (action.type) {
203
+ case MediaActionTypes.PLAY:
204
+ return { isPlaying: true, error: null }
205
+
206
+ case MediaActionTypes.PAUSE:
207
+ return { isPlaying: false }
208
+
209
+ case MediaActionTypes.SEEK:
210
+ return { currentTime: action.detail }
211
+
212
+ case MediaActionTypes.SET_VOLUME:
213
+ return { volume: Math.max(0, Math.min(1, action.detail)) }
214
+
215
+ case MediaActionTypes.MUTE:
216
+ return { isMuted: true }
217
+
218
+ case MediaActionTypes.UNMUTE:
219
+ return { isMuted: false }
220
+
221
+ case MediaActionTypes.TOGGLE_MUTE:
222
+ return { isMuted: !currentState.isMuted }
223
+
224
+ case MediaActionTypes.SET_DURATION:
225
+ return { duration: action.detail }
226
+
227
+ case MediaActionTypes.SET_CURRENT_TIME:
228
+ return { currentTime: action.detail }
229
+
230
+ case MediaActionTypes.SET_BUFFERED:
231
+ return { buffered: action.detail }
232
+
233
+ case MediaActionTypes.SET_LOADING:
234
+ return { isLoading: action.detail }
235
+
236
+ case MediaActionTypes.SET_ERROR:
237
+ return { error: action.detail, isLoading: false }
238
+
239
+ case MediaActionTypes.ENTER_FULLSCREEN:
240
+ return { isFullscreen: true }
241
+
242
+ case MediaActionTypes.EXIT_FULLSCREEN:
243
+ return { isFullscreen: false }
244
+
245
+ case MediaActionTypes.ENTER_PIP:
246
+ return { isPictureInPicture: true }
247
+
248
+ case MediaActionTypes.EXIT_PIP:
249
+ return { isPictureInPicture: false }
250
+
251
+ case MediaActionTypes.SET_PLAYBACK_RATE:
252
+ return { playbackRate: action.detail }
253
+
254
+ case MediaActionTypes.SET_SUBTITLE_TRACK:
255
+ return { selectedSubtitleTrack: action.detail }
256
+
257
+ case MediaActionTypes.SET_RENDITION:
258
+ return { selectedRendition: action.detail }
259
+
260
+ case MediaActionTypes.TOGGLE_SUBTITLES:
261
+ return { subtitlesShowing: !currentState.subtitlesShowing }
262
+
263
+ case MediaActionTypes.PREVIEW_TIME:
264
+ return { previewTime: action.detail }
265
+
266
+ case MediaActionTypes.CLEAR_PREVIEW:
267
+ return { previewTime: null, previewImage: null, previewCoords: null }
268
+
269
+ default:
270
+ console.warn(`Unknown media action type: ${action.type}`)
271
+ return {}
272
+ }
273
+ }
274
+
275
+ const dispatch = (action: MediaAction) => {
276
+ const updates = processMediaAction(state, action)
277
+ setState(updates)
278
+ }
279
+
280
+ const dispatchOptimistic = async (action: OptimisticMediaAction): Promise<void> => {
281
+ // Set action state to pending
282
+ actionState = {
283
+ pending: true,
284
+ error: null,
285
+ lastAction: action,
286
+ retryCount: action.retryCount || 0
287
+ }
288
+
289
+ // Apply optimistic update immediately
290
+ const actionId = applyOptimisticUpdate(action)
291
+
292
+ try {
293
+ // Simulate async operation (e.g., network request, DOM manipulation)
294
+ await new Promise(resolve => setTimeout(resolve, 0))
295
+
296
+ // Apply the actual update to the real state
297
+ const updates = processMediaAction(state, action)
298
+ const newState = { ...state, ...updates }
299
+
300
+ if (JSON.stringify(newState) !== JSON.stringify(state)) {
301
+ state = newState
302
+ debouncedPersist()
303
+ }
304
+
305
+ // Clean up optimistic action
306
+ optimisticQueue.delete(actionId)
307
+
308
+ // Update action state
309
+ actionState = {
310
+ pending: false,
311
+ error: null,
312
+ lastAction: action,
313
+ retryCount: 0
314
+ }
315
+
316
+ // Sync optimistic state with actual state
317
+ optimisticState = { ...state }
318
+ for (const [, queuedAction] of optimisticQueue) {
319
+ const update = processMediaAction(optimisticState, queuedAction)
320
+ optimisticState = { ...optimisticState, ...update }
321
+ }
322
+
323
+ } catch (error) {
324
+ // Rollback optimistic update on error
325
+ rollbackOptimistic(actionId)
326
+
327
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'
328
+ actionState = {
329
+ pending: false,
330
+ error: errorMessage,
331
+ lastAction: action,
332
+ retryCount: (action.retryCount || 0) + 1
333
+ }
334
+
335
+ // Auto-retry logic for certain actions
336
+ if (actionState.retryCount < 3 && shouldRetry(action.type)) {
337
+ setTimeout(() => {
338
+ dispatchOptimistic({
339
+ ...action,
340
+ retryCount: actionState.retryCount
341
+ })
342
+ }, Math.pow(2, actionState.retryCount) * 1000) // Exponential backoff
343
+ }
344
+ }
345
+
346
+ notifyListeners()
347
+ }
348
+
349
+ // Determine if an action should be retried
350
+ const shouldRetry = (actionType: MediaActionType): boolean => {
351
+ const retryableActions: MediaActionType[] = [
352
+ MediaActionTypes.PLAY,
353
+ MediaActionTypes.PAUSE,
354
+ MediaActionTypes.SEEK,
355
+ MediaActionTypes.SET_VOLUME,
356
+ MediaActionTypes.MUTE,
357
+ MediaActionTypes.UNMUTE,
358
+ MediaActionTypes.TOGGLE_MUTE
359
+ ]
360
+ return retryableActions.includes(actionType)
361
+ }
362
+
363
+ // Initialize with rehydrated state
364
+ rehydrateFromStorage()
365
+
366
+ return {
367
+ subscribe,
368
+ getState,
369
+ setState,
370
+ dispatch,
371
+ getOptimisticState,
372
+ dispatchOptimistic,
373
+ rollbackOptimistic,
374
+ getActionState,
375
+ setPersistenceConfig,
376
+ rehydrateFromStorage,
377
+ clearPersistedState
378
+ }
379
+ }