Skip to content

Commit 55633f5

Browse files
committed
refactor(cues): improve cue handling reliability
1 parent e81ef2e commit 55633f5

File tree

1 file changed

+39
-46
lines changed

1 file changed

+39
-46
lines changed

src/index.tsx

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -59,55 +59,35 @@ export const InteractiveVideo: React.FC<InteractiveVideoProps> = ({
5959
const uniqueIdRef = useRef<string>(generateUniqueId());
6060

6161
useEffect(() => {
62-
if (containerRef.current && !playerRef.current) {
63-
const playerConfig: PlayerConfig = {
64-
videoUrl,
65-
...restOptions,
66-
};
62+
if (!containerRef.current) return;
6763

68-
try {
69-
setTimeout(() => {
70-
if (containerRef.current) {
71-
const player = new IVLabsPlayer(uniqueIdRef.current, playerConfig);
72-
playerRef.current = player;
73-
74-
if (onAnalyticsEvent) {
75-
player.on('PLAYER_LOADED', (payload?: AnalyticsPayload) =>
76-
onAnalyticsEvent('PLAYER_LOADED', payload)
77-
);
78-
player.on('VIDEO_STARTED', (payload?: AnalyticsPayload) =>
79-
onAnalyticsEvent('VIDEO_STARTED', payload)
80-
);
81-
player.on('VIDEO_PAUSED', (payload?: AnalyticsPayload) =>
82-
onAnalyticsEvent('VIDEO_PAUSED', payload)
83-
);
84-
player.on('VIDEO_ENDED', (payload?: AnalyticsPayload) =>
85-
onAnalyticsEvent('VIDEO_ENDED', payload)
86-
);
87-
player.on('CUE_TRIGGERED', (payload?: AnalyticsPayload) =>
88-
onAnalyticsEvent('CUE_TRIGGERED', payload)
89-
);
90-
player.on('INTERACTION_COMPLETED', (payload?: AnalyticsPayload) =>
91-
onAnalyticsEvent('INTERACTION_COMPLETED', payload)
92-
);
93-
player.on('ERROR', (payload?: AnalyticsPayload) =>
94-
onAnalyticsEvent('ERROR', payload)
95-
);
96-
}
64+
const playerConfig: PlayerConfig = {
65+
videoUrl,
66+
...restOptions,
67+
};
9768

98-
if (cues) {
99-
player.loadCues(cues);
100-
}
69+
try {
70+
const player = new IVLabsPlayer(uniqueIdRef.current, playerConfig);
71+
playerRef.current = player;
10172

102-
if (translations) {
103-
const locale = restOptions.locale || 'en';
104-
player.loadTranslations(locale, translations);
105-
}
106-
}
107-
}, 0);
108-
} catch (error) {
109-
console.error('Error initializing IVLabsPlayer:', error);
73+
if (onAnalyticsEvent) {
74+
const events: AnalyticsEvent[] = [
75+
'PLAYER_LOADED',
76+
'VIDEO_STARTED',
77+
'VIDEO_PAUSED',
78+
'VIDEO_ENDED',
79+
'CUE_TRIGGERED',
80+
'INTERACTION_COMPLETED',
81+
'ERROR',
82+
];
83+
events.forEach((event) => {
84+
player.on(event, (payload?: AnalyticsPayload) =>
85+
onAnalyticsEvent(event, payload)
86+
);
87+
});
11088
}
89+
} catch (error) {
90+
console.error('Error initializing IVLabsPlayer:', error);
11191
}
11292

11393
return () => {
@@ -116,7 +96,20 @@ export const InteractiveVideo: React.FC<InteractiveVideoProps> = ({
11696
playerRef.current = null;
11797
}
11898
};
119-
}, [videoUrl, onAnalyticsEvent, cues, translations, restOptions]);
99+
}, [videoUrl, onAnalyticsEvent, restOptions]);
100+
101+
useEffect(() => {
102+
if (playerRef.current && cues) {
103+
playerRef.current.loadCues(cues);
104+
}
105+
}, [cues]);
106+
107+
useEffect(() => {
108+
if (playerRef.current && translations) {
109+
const locale = restOptions.locale || 'en';
110+
playerRef.current.loadTranslations(locale, translations);
111+
}
112+
}, [translations, restOptions.locale]);
120113

121114
return (
122115
<div

0 commit comments

Comments
 (0)