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 = () => {
|
||||
|
||||
Reference in New Issue
Block a user