Skip to content

Commit

Permalink
Merge pull request #233 from kevin-kp/chore/pass-options-in-initialize
Browse files Browse the repository at this point in the history
Pass MuxOptions in initialize instead of the hook itself
  • Loading branch information
tvanlaerhoven authored Dec 19, 2024
2 parents f95bbc8 + 1f418fb commit 6d903bb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-keys-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@theoplayer/react-native-analytics-mux': patch
---

Changed the `useMux` hook to allow the `MuxOptions` to be passed with the initialize instead of directly passing it into the hook itself.
4 changes: 2 additions & 2 deletions mux/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ const muxOptions = {
};

const App = () => {
const [mux, initMux] = useMux(muxOptions);
const [mux, initMux] = useMux();

const onPlayerReady = (player: THEOplayer) => {
// Initialize connector
initMux(player);
initMux(player, muxOptions);
}

return (<THEOplayerView config={playerConfig} onPlayerReady={onPlayerReady}/>);
Expand Down
19 changes: 10 additions & 9 deletions mux/src/api/hooks/useMux.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { PlayerEventType, THEOplayer } from 'react-native-theoplayer';
import { RefObject, useEffect, useRef } from 'react';
import { MuxConnector } from '@theoplayer/react-native-analytics-mux';
import { PlayerEventType, THEOplayer } from "react-native-theoplayer";
import { RefObject, useEffect, useRef } from "react";
import { MuxConnector, MuxOptions } from "@theoplayer/react-native-analytics-mux";

export function useMux(options: any): [RefObject<MuxConnector | undefined>, (player: THEOplayer | undefined) => void] {
export function useMux()
: [RefObject<MuxConnector | undefined>, (player: THEOplayer | undefined, options: MuxOptions) => void] {
const connector = useRef<MuxConnector | undefined>();
const theoPlayer = useRef<THEOplayer | undefined>();

const initialize = (player: THEOplayer | undefined) => {
const initialize = (player: THEOplayer | undefined, options: MuxOptions) => {
// Optionally destroy existent connector
onDestroy();

Expand All @@ -15,20 +16,20 @@ export function useMux(options: any): [RefObject<MuxConnector | undefined>, (pla
connector.current = new MuxConnector(player, options);
player.addEventListener(PlayerEventType.DESTROY, onDestroy);
} else {
throw new Error('Invalid THEOplayer instance');
throw new Error("Invalid THEOplayer instance");
}
};
}

const onDestroy = () => {
if (connector.current) {
if (!theoPlayer.current) {
throw new Error('Invalid THEOplayer instance');
throw new Error("Invalid THEOplayer instance");
}
theoPlayer.current.removeEventListener(PlayerEventType.DESTROY, onDestroy);
connector.current.destroy();
connector.current = undefined;
}
};
}

useEffect(() => {
return onDestroy;
Expand Down

0 comments on commit 6d903bb

Please sign in to comment.