Miraiclip SDK
Editor guides

Reading state

Re-render your UI when the project changes.

project.subscribe(selector, listener) calls listener only when the selected slice changes. Subscribe to the slices a component renders, not the whole state.

main.ts
import { project } from "./project";

const status = document.querySelector("#status")!;

function render() {
  const { playheadUs, selection } = project.getState();
  status.textContent = `${(playheadUs / 1e6).toFixed(2)}s · ${selection.length} selected`;
}

render();
const stops = [
  project.subscribe((s) => s.playheadUs, render),
  project.subscribe((s) => s.selection, render),
];

document.querySelector("#undo")!.addEventListener("click", () => project.undo());

// When the view goes away:
// stops.forEach((stop) => stop());
Also uses project.ts
project.ts
import { createProject } from "@miraiclip/core";

// One project instance for the whole app. Pass it to components as a prop.
export const project = createProject({ width: 1280, height: 720, fps: 30 });
StatusBar.tsx
import { useProjectState, useSelection, useUndoRedo } from "@miraiclip/react";

// Inside <ProjectProvider>. Each hook re-renders only when its slice changes.
export function StatusBar() {
  const playheadUs = useProjectState((s) => s.playheadUs);
  const selection = useSelection();
  const { canUndo, undo } = useUndoRedo();

  return (
    <div>
      {(playheadUs / 1e6).toFixed(2)}s · {selection.length} selected
      <button disabled={!canUndo} onClick={undo}>Undo</button>
    </div>
  );
}
StatusBar.vue
<script setup lang="ts">
import { onScopeDispose, shallowRef } from "vue";
import type { Project } from "@miraiclip/core";

const { project } = defineProps<{ project: Project }>();

const playheadUs = shallowRef(project.getState().playheadUs);
const selection = shallowRef(project.getState().selection);

const stops = [
  project.subscribe((s) => s.playheadUs, (v) => (playheadUs.value = v)),
  project.subscribe((s) => s.selection, (v) => (selection.value = v)),
];
onScopeDispose(() => stops.forEach((stop) => stop()));
</script>

<template>
  <div>
    {{ (playheadUs / 1e6).toFixed(2) }}s · {{ selection.length }} selected
    <button @click="project.undo()">Undo</button>
  </div>
</template>
StatusBar.svelte
<script lang="ts">
  import type { Project } from "@miraiclip/core";

  let { project }: { project: Project } = $props();
  let playheadUs = $state(0);
  let selection = $state.raw<string[]>([]);

  $effect(() => {
    ({ playheadUs, selection } = project.getState());
    const stops = [
      project.subscribe((s) => s.playheadUs, (v) => (playheadUs = v)),
      project.subscribe((s) => s.selection, (v) => (selection = v)),
    ];
    return () => stops.forEach((stop) => stop());
  });
</script>

<div>
  {(playheadUs / 1e6).toFixed(2)}s · {selection.length} selected
  <button onclick={() => project.undo()}>Undo</button>
</div>

@miraiclip/react wraps project.subscribe in hooks: useProjectState(selector) for any slice, plus useClips, useSelection, usePlayheadUs and more. See React.

SlicePathChanges on
Playheads.playheadUsEvery played frame (60×/s during playback)
Selections.selectionproject.setSelection(ids)
Documents.docAny command, undo or redo
Clips, trackss.doc.clips, s.doc.tracks, s.doc.trackOrderClip and track commands

The playhead updates every frame

Keep components that read playheadUs small, or update the DOM directly in the listener without re-rendering.