mirror of
https://github.com/zoriya/react-native-omni.git
synced 2026-08-16 02:44:51 +00:00
Cleanup source handling
This commit is contained in:
+18
-9
@@ -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 = () => {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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[];
|
||||
@@ -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>;
|
||||
};
|
||||
Reference in New Issue
Block a user