fix(junction): isolate lifecycle previews and drafts
This commit is contained in:
1 parent
89f4ce79b0
commit
a5f64a2349
4 files changed
+75
-1
No files matched your search
@@ -125,6 +125,13 @@ a swapped object would leave the requester arbitrating for a session nobody read
|
||||
request layer only sends and cancels. The discard rule is worth unit-testing and
|
||||
must not depend on network timing.
|
||||
|
||||
Selection changes are a second freshness boundary. `PreviewRequester.invalidate()`
|
||||
must cancel the active request and advance its generation; `dispatch()` checks
|
||||
that captured generation before giving a response to `EditSession`. Abort alone
|
||||
is insufficient because a transport may resolve after receiving its abort signal.
|
||||
When changing junction/segment scope, clear scoped preview geometry before the
|
||||
new manifest arrives, so an A-scope result cannot render while B is selected.
|
||||
|
||||
Undo of a **saved** gesture appends an inverse operation; persisted history is
|
||||
never rewritten. Undo of an unsaved one just moves the cursor.
|
||||
|
||||
|
||||
@@ -483,6 +483,19 @@ function App() {
|
||||
onError: (error) => setStatus(`预览失败:${(error as Error).message}`),
|
||||
});
|
||||
}, [session]);
|
||||
const previewScopeKey = junctionRoute
|
||||
? `${junctionRoute.type}:${junctionRoute.id}`
|
||||
: selected?.segmentId
|
||||
? `segment:${selected.segmentId}`
|
||||
: 'area';
|
||||
useEffect(() => {
|
||||
requester?.invalidate();
|
||||
// Preview geometry is scoped server output. Retaining it during an A -> B
|
||||
// transition would draw A while B's manifest is loading.
|
||||
setPreview(null);
|
||||
setEditDiagnostics([]);
|
||||
gesture.current = null;
|
||||
}, [previewScopeKey, requester]);
|
||||
/** A drag becomes one drafted constraint layered over the session's document. */
|
||||
const draftFor = (handle: EditHandle, value: number): PreviewDraft | null => {
|
||||
// Road-anchored edits carry a directional road id. Junction-approach
|
||||
@@ -862,6 +875,7 @@ function App() {
|
||||
};
|
||||
const compile = async (): Promise<boolean> => {
|
||||
if (!(await save())) return false;
|
||||
const savedDirectEdits = Boolean(session?.dirty);
|
||||
if (!(await saveDirectEdits(false))) return false;
|
||||
if (signalDirty && signalDraftRef.current && state) {
|
||||
try {
|
||||
@@ -888,7 +902,11 @@ function App() {
|
||||
setStatus('已保存并重新生成');
|
||||
return true;
|
||||
} catch (error) {
|
||||
setStatus((error as Error).message);
|
||||
setStatus(
|
||||
savedDirectEdits
|
||||
? `直接编辑已保存,但重新生成失败:${(error as Error).message}`
|
||||
: `重新生成失败:${(error as Error).message}`,
|
||||
);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
@@ -941,6 +959,24 @@ function App() {
|
||||
window.history.pushState({}, '', '/');
|
||||
setJunctionRoute(null);
|
||||
};
|
||||
const prepareTopologyChange = (): boolean => {
|
||||
if (!junctionSession?.dirty && !signalDirty) return true;
|
||||
const choice = window.prompt('路口有未应用草稿:输入 1 应用,2 放弃,3 留在工作区', '3');
|
||||
if (choice === '3' || !choice) return false;
|
||||
if (choice === '1') {
|
||||
if (signalDirty) {
|
||||
setStatus('信号灯草稿仍未保存,请在工作区内完成保存或放弃。');
|
||||
return false;
|
||||
}
|
||||
setJunctionSession((current) => (current ? applyJunctionDraft(current) : current));
|
||||
return true;
|
||||
}
|
||||
if (choice === '2') {
|
||||
discardJunctionDraft();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
const handleFeature = (properties: Record<string, unknown>) => {
|
||||
setMarking(properties);
|
||||
const objectKey = junctionStructure
|
||||
@@ -1253,6 +1289,7 @@ function App() {
|
||||
}
|
||||
onUpdateComplexNodes={(nodeIds) => {
|
||||
if (junctionDescriptor.ref.type !== 'cluster') return;
|
||||
if (!prepareTopologyChange()) return;
|
||||
void api
|
||||
.updateJunctionCluster(junctionDescriptor.ref.id, nodeIds)
|
||||
.then((next) => {
|
||||
|
||||
@@ -101,6 +101,20 @@ describe('cancellation', () => {
|
||||
expect(signals).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('drops a response after its owning scope was invalidated', async () => {
|
||||
const { send, pending, signals } = deferredTransport();
|
||||
const requester = new PreviewRequester({ session, send, onSettled });
|
||||
requester.flush({ constraints: [] });
|
||||
requester.invalidate();
|
||||
expect(signals[0].aborted).toBe(true);
|
||||
// Transports are allowed to resolve despite abort; this is the lifecycle
|
||||
// boundary that protects a newly selected junction.
|
||||
pending[0].resolve(preview(1));
|
||||
await vi.advanceTimersByTimeAsync(0);
|
||||
expect(onSettled).not.toHaveBeenCalled();
|
||||
expect(session.lastValidPreview()).toBeNull();
|
||||
});
|
||||
|
||||
it('never reports an abort as an error', async () => {
|
||||
const send = vi.fn<PreviewTransport>(async () => {
|
||||
throw Object.assign(new Error('cancelled'), { name: 'AbortError' });
|
||||
|
||||
@@ -38,6 +38,8 @@ function isAbort(error: unknown): boolean {
|
||||
export class PreviewRequester {
|
||||
private timer: ReturnType<typeof setTimeout> | null = null;
|
||||
private inFlight: AbortController | null = null;
|
||||
/** Changes whenever the owning map/junction scope changes. */
|
||||
private generation = 0;
|
||||
private readonly debounceMs: number;
|
||||
|
||||
constructor(private readonly options: PreviewRequesterOptions) {
|
||||
@@ -75,6 +77,18 @@ export class PreviewRequester {
|
||||
this.inFlight = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aborts the active scope and makes responses from it ineligible for apply.
|
||||
*
|
||||
* Abort is cooperative: a transport may resolve after the signal fires. The
|
||||
* generation check in `dispatch()` is therefore the authority that prevents
|
||||
* an A-scope response from replacing the UI after A -> B navigation.
|
||||
*/
|
||||
invalidate(): void {
|
||||
this.generation += 1;
|
||||
this.cancel();
|
||||
}
|
||||
|
||||
private async dispatch(draft: PreviewDraft): Promise<void> {
|
||||
// Abort the older request before starting a newer one: its answer is already
|
||||
// obsolete, and leaving it running wastes a solve the user cannot see.
|
||||
@@ -82,8 +96,10 @@ export class PreviewRequester {
|
||||
const controller = new AbortController();
|
||||
this.inFlight = controller;
|
||||
const previewSeq = this.options.session.nextPreviewSeq();
|
||||
const generation = this.generation;
|
||||
try {
|
||||
const response = await this.options.send({ previewSeq, ...draft }, controller.signal);
|
||||
if (generation !== this.generation) return;
|
||||
const outcome = this.options.session.acceptPreview(response);
|
||||
// A stale response is dropped silently; that is the arbitration working.
|
||||
if (outcome.applied) this.options.onSettled(outcome, response);
|
||||
|
||||
Reference in new issue
Block a user