fix(backend): allow backend switch

This commit is contained in:
2026-08-04 20:56:24 +02:00
parent 77c3a519e7
commit 26a2731679
10 changed files with 74 additions and 57 deletions
+31 -10
View File
@@ -1,9 +1,18 @@
import { createContext, type ReactNode, useContext, useEffect } from "react";
import {
createContext,
type ReactNode,
useContext,
useEffect,
useEffectEvent,
useState,
} from "react";
import { NitroModules } from "react-native-nitro-modules";
import type { OmniPlayerFactory } from "./specs/omni-player.nitro";
import type { OmniPlayer, PlayerBackend } from "./types/player";
import type {
OmniPlayer as NativeOmniPlayer,
OmniPlayerFactory,
} from "./specs/omni-player.nitro";
import type { AndroidBackend, OmniPlayer, PlayerBackend } from "./types/player";
import type { CastOptions, Source } from "./types/source";
import { useLazyRef } from "./utils/lazy-ref";
const ProviderFactory =
NitroModules.createHybridObject<OmniPlayerFactory>("OmniPlayerFactory");
@@ -23,18 +32,30 @@ export const OmniProvider = ({
children: ReactNode;
showNotification?: boolean;
}) => {
const player = useLazyRef(() =>
ProviderFactory.createPlayer(source, backend, cast),
const [player, setPlayer] = useState<NativeOmniPlayer | null>(null);
const createPlayer = useEffectEvent((aBackend: AndroidBackend) =>
ProviderFactory.createPlayer(source, { android: aBackend }, cast),
);
useEffect(() => {
player.source = source;
}, [source]);
setPlayer(createPlayer(backend.android ?? "vlc"));
}, [backend.android]);
useEffect(() => {
player.showNotification = showNotification;
}, [showNotification]);
if (!player) return;
return () => player.release();
}, [player]);
useEffect(() => {
if (player) player.source = source;
}, [player, source]);
useEffect(() => {
if (player) player.showNotification = showNotification;
}, [player, showNotification]);
if (!player) return null;
return <PlayerCtx.Provider value={player}>{children}</PlayerCtx.Provider>;
};
+2
View File
@@ -80,6 +80,8 @@ export interface OmniPlayer
extends HybridObject<{ android: "kotlin" }>,
OmniPlayerT {
readonly eventMap: OmniEventMap;
release(): void;
}
export interface OmniPlayerFactory extends HybridObject<{ android: "kotlin" }> {