Cleanup source handling

This commit is contained in:
2026-04-10 19:20:45 +02:00
parent 7a5f495b39
commit fd40915bc9
26 changed files with 415 additions and 450 deletions
+18 -9
View File
@@ -1,10 +1,9 @@
import { createContext, type ReactNode, useContext } from "react";
import { createContext, type ReactNode, useContext, useEffect } from "react";
import { NitroModules } from "react-native-nitro-modules";
import type {
OmniPlayerFactory,
OmniPlayerProps,
} from "./specs/omni-player.nitro";
import type { OmniPlayerFactory } from "./specs/omni-player.nitro";
import type { OmniPlayer } from "./types/player";
import type { Source } from "./types/source";
import { useLazyRef } from "./utils/lazy-ref";
const ProviderFactory = NitroModules.createHybridObject<OmniPlayerFactory>(
"OmniProviderFactory",
@@ -14,10 +13,20 @@ const PlayerCtx = createContext<OmniPlayer>(null!);
export const OmniProvider = ({
children,
...props
}: OmniPlayerProps & { children: ReactNode }) => {
const player = ProviderFactory.createPlayer(props);
return <PlayerCtx.Provider value={player}>{children}</PlayerCtx.Provider>;
source,
}: {
source: Source;
children: ReactNode;
}) => {
const player = useLazyRef(() => ProviderFactory.createPlayer(source));
useEffect(() => {
player.current.source = source;
}, [source]);
return (
<PlayerCtx.Provider value={player.current}>{children}</PlayerCtx.Provider>
);
};
export const usePlayer = () => {
+2 -6
View File
@@ -1,15 +1,11 @@
import type { HybridObject } from "react-native-nitro-modules";
import type { OmniPlayer as OmniPlayerT } from "../types/player";
import type { OmniPlayerProps as OmniPlayerPropsT } from "../types/provider";
export interface OmniPlayerProps
extends HybridObject<{ android: "kotlin" }>,
OmniPlayerPropsT {}
import type { Source } from "../types/source";
export interface OmniPlayer
extends HybridObject<{ android: "kotlin" }>,
OmniPlayerT {}
export interface OmniPlayerFactory extends HybridObject<{ android: "kotlin" }> {
createPlayer(props: OmniPlayerProps): OmniPlayer;
createPlayer(props: Source): OmniPlayer;
}
+4
View File
@@ -1,4 +1,8 @@
import type { Source } from "./source";
export interface OmniPlayer {
source: Source;
play(): void;
pause(): void;
seekBy(offset: number): void;
@@ -1,4 +1,4 @@
export interface OmniPlayerProps {
export interface Source {
src: VideoSrc[];
startTime?: number;
subtitles: Subtitle[];
+13
View File
@@ -0,0 +1,13 @@
import { type RefObject, useRef } from "react";
const empty = Symbol("useLazyRef empty value");
export const useLazyRef = <T>(init: () => T): RefObject<T> => {
const resultRef = useRef<T | typeof empty>(empty);
if (resultRef.current === empty) {
resultRef.current = init();
}
return resultRef as RefObject<T>;
};