Skip to content

The open-source full-stack studio for Opus 5.5 motion video. Reverse engineering for any video. Code video with HTML: recreate, edit and render. BYOA + BYOK · Motion.md · Images, Video, Music & Voice.

575 B
Raw Blame
export interface OptimisticUpdateOptions<TSnapshot> {
  /** Apply the change to local state immediately. Return a snapshot for rollback. */
  apply: () => TSnapshot;
  /** Persist the change to the server. */
  persist: () => Promise<unknown>;
  /** Revert local state using the snapshot if persist fails. */
  rollback: (snapshot: TSnapshot) => void;
}

export async function executeOptimistic<T>(options: OptimisticUpdateOptions<T>): Promise<void> {
  const snapshot = options.apply();
  try {
    await options.persist();
  } catch {
    options.rollback(snapshot);
  }
}