26 lines
877 B
TypeScript
26 lines
877 B
TypeScript
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
import { useEffect, useRef } from 'react';
|
|
import { projectSnapshotHost, type ProjectSnapshotParticipant } from './project_snapshot.js';
|
|
/** Callbacks follow the live frame; identity is captured only at mount. */
|
|
export function useProjectSnapshot(
|
|
key: string,
|
|
participant: ProjectSnapshotParticipant,
|
|
revision: unknown,
|
|
): void {
|
|
const host = useRef(projectSnapshotHost()).current;
|
|
const live = useRef(participant);
|
|
live.current = participant;
|
|
useEffect(
|
|
() =>
|
|
host?.register(key, {
|
|
flush: () => live.current.flush(),
|
|
isDirty: () => live.current.isDirty?.() ?? false,
|
|
isBusy: () => live.current.isBusy?.() ?? false,
|
|
}),
|
|
[host, key],
|
|
);
|
|
useEffect(() => {
|
|
if (revision !== undefined && (live.current.isDirty?.() ?? true)) host?.modified();
|
|
}, [host, revision]);
|
|
}
|