Skip to content

🎥 Make videos programmatically with React

`remotion`: Use runtime value store for playing and drop dead hooks

Noah Lindner2bdd21c1fc63
diff --git a/packages/core/src/TimelineContext.tsx b/packages/core/src/TimelineContext.tsx
index 78a953aeac..a04d64497f 100644
--- a/packages/core/src/TimelineContext.tsx
+++ b/packages/core/src/TimelineContext.tsx
@@ -6,7 +6,7 @@ import React, {
useRef,
useState,
} from 'react';
-import {createPlayingController} from './playing-state.js';
+import {createRuntimeValueStore} from './runtime-value-store.js';
import {
getInitialFrameState,
type PlayableMediaTag,
@@ -55,7 +55,10 @@ export const TimelineContextProvider: React.FC<{
readonly children: React.ReactNode;
readonly frameState: Record<string, number> | null;
}> = ({children, frameState}) => {
- const playingController = useMemo(() => createPlayingController(false), []);
+ const playingStore = useMemo(
+ () => createRuntimeValueStore({playing: false}),
+ [],
+ );
const [playbackRate, setPlaybackRate] = useState(1);
const audioAndVideoTags = useRef<PlayableMediaTag[]>([]);
@@ -107,10 +110,10 @@ export const TimelineContextProvider: React.FC<{
const timelineContextValue = useMemo((): TimelineContextValue => {
return {
frame,
- isPlaying: playingController.isPlaying,
+ isPlaying: () => playingStore.store.getSnapshot().playing as boolean,
audioAndVideoTags,
};
- }, [frame, playingController.isPlaying, audioAndVideoTags]);
+ }, [frame, playingStore, audioAndVideoTags]);
const playbackRateContextValue = useMemo((): PlaybackRateContextValue => {
return {
@@ -124,14 +127,15 @@ export const TimelineContextProvider: React.FC<{
setFrame,
setPlaying: (updater) => {
if (typeof updater === 'function') {
- playingController.setPlaying(updater(playingController.isPlaying()));
+ const current = playingStore.store.getSnapshot().playing as boolean;
+ playingStore.setSnapshot({playing: updater(current)});
} else {
- playingController.setPlaying(updater);
+ playingStore.setSnapshot({playing: updater});
}
},
- subscribePlaying: playingController.subscribePlaying,
+ subscribePlaying: playingStore.store.subscribe,
};
- }, [playingController]);
+ }, [playingStore]);
return (
<AbsoluteTimeContext.Provider value={timelineContextValue}>
diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.ts
index 4af25d3111..eafd73f022 100644
--- a/packages/core/src/internals.ts
+++ b/packages/core/src/internals.ts
@@ -131,7 +131,6 @@ import {
import type {NonceHistory} from './nonce.js';
import {NonceContext} from './nonce.js';
import {playbackLogging} from './playback-logging.js';
-import {createPlayingController} from './playing-state.js';
import {portalNode, setPortalNodeCurrentScale} from './portal-node.js';
import {PrefetchProvider} from './prefetch-state.js';
import {usePreload} from './prefetch.js';
@@ -311,7 +310,6 @@ export const Internals = {
makeRenderResourceManager,
RenderResourceManagerContext,
createRuntimeValueStore,
- createPlayingController,
useUnsafeVideoConfig,
useFrameForVolumeProp,
useTimelinePosition: TimelinePosition.useTimelinePosition,
diff --git a/packages/core/src/playing-state.ts b/packages/core/src/playing-state.ts
deleted file mode 100644
index a010d15020..0000000000
--- a/packages/core/src/playing-state.ts
+++ /dev/null
@@ -1,32 +0,0 @@
-export type PlayingController = {
- readonly isPlaying: () => boolean;
- readonly subscribePlaying: (listener: () => void) => () => void;
- readonly setPlaying: (playing: boolean) => void;
-};
-
-export const createPlayingController = (
- initialPlaying = false,
-): PlayingController => {
- let playing = initialPlaying;
- const listeners = new Set<() => void>();
-
- return {
- isPlaying: () => playing,
- subscribePlaying: (listener) => {
- listeners.add(listener);
- return () => {
- listeners.delete(listener);
- };
- },
- setPlaying: (newPlaying) => {
- if (playing === newPlaying) {
- return;
- }
-
- playing = newPlaying;
- for (const listener of listeners) {
- listener();
- }
- },
- };
-};
diff --git a/packages/core/src/timeline-position-state.ts b/packages/core/src/timeline-position-state.ts
index 18344e9e69..bd8c660036 100644
--- a/packages/core/src/timeline-position-state.ts
+++ b/packages/core/src/timeline-position-state.ts
@@ -1,5 +1,4 @@
-import type {RefObject} from 'react';
-import {useContext, useMemo} from 'react';
+import {useContext} from 'react';
import {
AbsoluteTimeContext,
PlaybackRateContext,
@@ -8,7 +7,6 @@ import {
type PlaybackRateContextValue,
type TimelineContextValue,
} from './TimelineContext.js';
-import {usePlaying} from './use-playing.js';
import {useRemotionEnvironment} from './use-remotion-environment.js';
import {useVideo} from './use-video.js';
@@ -119,32 +117,4 @@ export const useTimelineSetFrame = (): ((
return setFrame;
};
-type PlayingReturnType = readonly [
- boolean,
- (u: React.SetStateAction<boolean>) => void,
- RefObject<boolean>,
-];
-
-export const usePlayingState = (): PlayingReturnType => {
- const {isPlaying} = useTimelineContext();
- const playing = usePlaying();
- const {setPlaying} = useContext(SetTimelineContext);
-
- return useMemo(
- () => [
- playing,
- setPlaying,
- {
- get current() {
- return isPlaying();
- },
- set current(val) {
- setPlaying(val);
- },
- },
- ],
- [isPlaying, playing, setPlaying],
- );
-};
-
export {usePlaying} from './use-playing.js';
diff --git a/packages/player/src/Player.tsx b/packages/player/src/Player.tsx
index 6f1c2dad14..2683e4f2fc 100644
--- a/packages/player/src/Player.tsx
+++ b/packages/player/src/Player.tsx
@@ -244,8 +244,8 @@ const PlayerFn = <
}));
const rootRef = useRef<PlayerRef>(null);
const audioAndVideoTags = useRef<PlayableMediaTag[]>([]);
- const playingController = useMemo(
- () => Internals.createPlayingController(false),
+ const playingStore = useMemo(
+ () => Internals.createRuntimeValueStore({playing: false}),
[],
);
const [currentPlaybackRate, setCurrentPlaybackRate] = useState(playbackRate);
@@ -412,10 +412,10 @@ const PlayerFn = <
const timelineContextValue = useMemo((): TimelineContextValue => {
return {
frame,
- isPlaying: playingController.isPlaying,
+ isPlaying: () => playingStore.store.getSnapshot().playing as boolean,
audioAndVideoTags,
};
- }, [frame, playingController.isPlaying, audioAndVideoTags]);
+ }, [frame, playingStore, audioAndVideoTags]);
const playbackRateContextValue = useMemo((): PlaybackRateContextValue => {
return {
@@ -429,14 +429,15 @@ const PlayerFn = <
setFrame,
setPlaying: (updater) => {
if (typeof updater === 'function') {
- playingController.setPlaying(updater(playingController.isPlaying()));
+ const current = playingStore.store.getSnapshot().playing as boolean;
+ playingStore.setSnapshot({playing: updater(current)});
} else {
- playingController.setPlaying(updater);
+ playingStore.setSnapshot({playing: updater});
}
},
- subscribePlaying: playingController.subscribePlaying,
+ subscribePlaying: playingStore.store.subscribe,
};
- }, [playingController, setFrame]);
+ }, [playingStore, setFrame]);
if (typeof window !== 'undefined') {
// eslint-disable-next-line react-hooks/rules-of-hooks