Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: watchStaticFolder() API

Jonny Burger69bc20e2ee01
diff --git a/packages/studio/src/api/watch-public-folder.ts b/packages/studio/src/api/watch-public-folder.ts
new file mode 100644
index 0000000000..5863d75b74
--- /dev/null
+++ b/packages/studio/src/api/watch-public-folder.ts
@@ -0,0 +1,38 @@
+import {getRemotionEnvironment} from 'remotion';
+import {getStaticFiles, type StaticFile} from './get-static-files';
+
+type WatcherCallback = (newFiles: StaticFile[]) => void;
+
+export const WATCH_REMOTION_STATIC_FILES = 'remotion_staticFilesChanged';
+
+export type WatchRemotionStaticFilesPayload = {
+ files: StaticFile[];
+};
+
+/**
+ * @description Watch for changes in the public folder.
+ * @param {Function} callback - A callback function to be called when the file changes.
+ * @returns {{cancel: () => void}} A function that can be used to cancel the event listener.
+ * @see [Documentation](https://remotion.dev/docs/studio/watch-public-folder)
+ */
+export const watchPublicFolder = (
+ callback: WatcherCallback,
+): {cancel: () => void} => {
+ if (!getRemotionEnvironment().isStudio) {
+ // eslint-disable-next-line no-console
+ console.warn('The API is only available while using the Remotion Studio.');
+ return {cancel: () => undefined};
+ }
+
+ const emitUpdate = () => {
+ callback(getStaticFiles());
+ };
+
+ window.addEventListener(WATCH_REMOTION_STATIC_FILES, emitUpdate);
+
+ const cancel = () => {
+ return window.removeEventListener(WATCH_REMOTION_STATIC_FILES, emitUpdate);
+ };
+
+ return {cancel};
+};
diff --git a/packages/studio/src/api/watch-static-file.ts b/packages/studio/src/api/watch-static-file.ts
index cdae18ba1a..d62faff44f 100644
--- a/packages/studio/src/api/watch-static-file.ts
+++ b/packages/studio/src/api/watch-static-file.ts
@@ -1,10 +1,9 @@
import {getRemotionEnvironment} from 'remotion';
import type {StaticFile} from './get-static-files';
+import {watchPublicFolder} from './watch-public-folder';
type WatcherCallback = (newData: StaticFile | null) => void;
-export const WATCH_REMOTION_STATIC_FILES = 'remotion_staticFilesChanged';
-
export type WatchRemotionStaticFilesPayload = {
files: StaticFile[];
};
@@ -14,13 +13,12 @@ export type WatchRemotionStaticFilesPayload = {
* @param {string} fileName - The name of the static file to watch for changes.
* @param {WatcherCallback} callback - A callback function to be called when the file changes.
* @returns {{cancel: () => void}} A function that can be used to cancel the event listener.
- * @see [Documentation](https://www.remotion.dev/docs/watchstaticfile)
+ * @see [Documentation](https://remotion.dev/docs/studio/watch-static-file)
*/
export const watchStaticFile = (
fileName: string,
callback: WatcherCallback,
): {cancel: () => void} => {
- // Check if function is called in Remotion Studio
if (!getRemotionEnvironment().isStudio) {
// eslint-disable-next-line no-console
console.warn('The API is only available while using the Remotion Studio.');
@@ -38,12 +36,7 @@ export const watchStaticFile = (
(file: StaticFile) => file.name === withoutLeadingSlash,
);
- // Check if the specified static file has updated or deleted
- const checkFile = (event: Event) => {
- const staticFiles: StaticFile[] = (
- (event as CustomEvent).detail as WatchRemotionStaticFilesPayload
- ).files;
-
+ const {cancel} = watchPublicFolder((staticFiles) => {
// Check for user specified file
const newFileData = staticFiles.find(
(file: StaticFile) => file.name === withoutLeadingSlash,
@@ -66,13 +59,7 @@ export const watchStaticFile = (
callback(newFileData); // File is added or modified
prevFileData = newFileData;
}
- };
-
- window.addEventListener(WATCH_REMOTION_STATIC_FILES, checkFile);
-
- const cancel = () => {
- return window.removeEventListener(WATCH_REMOTION_STATIC_FILES, checkFile);
- };
+ });
return {cancel};
};
diff --git a/packages/studio/src/helpers/client-id.tsx b/packages/studio/src/helpers/client-id.tsx
index 94f934ac46..71481ebc60 100644
--- a/packages/studio/src/helpers/client-id.tsx
+++ b/packages/studio/src/helpers/client-id.tsx
@@ -100,13 +100,14 @@ export const PreviewServerConnection: React.FC<{
files: newEvent.files,
};
+ window.remotion_staticFiles = newEvent.files;
+ window.remotion_publicFolderExists = newEvent.folderExists;
+
window.dispatchEvent(
new CustomEvent(Internals.WATCH_REMOTION_STATIC_FILES, {
detail: payload,
}),
);
- window.remotion_staticFiles = newEvent.files;
- window.remotion_publicFolderExists = newEvent.folderExists;
}
listeners.current.forEach((l) => {