PM: merge M1.12 notifications
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> # Conflicts: # docs/architecture.md # docs/gaps.md # docs/roadmap.md # docs/target-app-parity.md # docs/ui-parity.md
This commit is contained in:
commit
2f8fef876b
21 changed files with 1178 additions and 94 deletions
|
|
@ -31,6 +31,19 @@ export type EngineEventPayload =
|
|||
| {
|
||||
kind: "recoveryAvailable";
|
||||
entries: { path: string; savedAt: number; diskChanged: boolean }[];
|
||||
}
|
||||
| {
|
||||
kind: "notification";
|
||||
/** Unique id (`n1`, `n2`…); unchecked, it comes from the engine. */
|
||||
id: unknown;
|
||||
/** `info`, `warning` or `error`; unchecked. */
|
||||
level: unknown;
|
||||
/** Catalog key of the text; unchecked. */
|
||||
messageKey?: unknown;
|
||||
/** English fallback text; unchecked. */
|
||||
message?: unknown;
|
||||
/** Placeholder values; unchecked. */
|
||||
params?: unknown;
|
||||
};
|
||||
|
||||
/** Callback receiving engine events. */
|
||||
|
|
@ -39,11 +52,35 @@ export type EngineEventHandler = (event: EngineEventPayload) => void;
|
|||
/** Whether the shell was told that the frontend listens (`engine_ready`). */
|
||||
let announced = false;
|
||||
|
||||
/** Subscriptions started but not yet registered with the shell. */
|
||||
let pending = 0;
|
||||
|
||||
/** Subscriptions registered with the shell and not ended. */
|
||||
let live = 0;
|
||||
|
||||
/**
|
||||
* Subscribes to engine events. The first successful subscription tells the
|
||||
* shell that the frontend listens, so it replays the current project state
|
||||
* the page may have missed while loading. Outside the Tauri shell (tests,
|
||||
* a plain browser) subscribing fails quietly and no events arrive.
|
||||
* Tells the shell that the frontend listens, once, as soon as a
|
||||
* subscription is live and no started one is still being registered.
|
||||
* Subscriptions made while the page mounts all start in the same tick, so
|
||||
* every one of them receives the replay. A failed announcement is retried
|
||||
* by the next subscription.
|
||||
*/
|
||||
function announce(): void {
|
||||
if (announced || pending > 0 || live === 0) {
|
||||
return;
|
||||
}
|
||||
announced = true;
|
||||
invoke("engine_ready").catch(() => {
|
||||
announced = false;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribes to engine events. Once the subscriptions started together are
|
||||
* registered, the shell is told that the frontend listens, so it replays
|
||||
* the current project state and the notifications the page may have missed
|
||||
* while loading. Outside the Tauri shell (tests, a plain browser)
|
||||
* subscribing fails quietly and no events arrive.
|
||||
*
|
||||
* @param handler - called for every event, in order of arrival.
|
||||
* @returns a function that ends the subscription.
|
||||
|
|
@ -51,27 +88,31 @@ let announced = false;
|
|||
export function onEngineEvent(handler: EngineEventHandler): () => void {
|
||||
let unlisten: UnlistenFn | null = null;
|
||||
let cancelled = false;
|
||||
pending += 1;
|
||||
listen<EngineEventPayload>(ENGINE_EVENT, (event) => {
|
||||
handler(event.payload);
|
||||
})
|
||||
.then((fn) => {
|
||||
pending -= 1;
|
||||
if (cancelled) {
|
||||
fn();
|
||||
return;
|
||||
}
|
||||
unlisten = fn;
|
||||
if (!announced) {
|
||||
announced = true;
|
||||
invoke("engine_ready").catch(() => {
|
||||
announced = false;
|
||||
});
|
||||
} else {
|
||||
unlisten = fn;
|
||||
live += 1;
|
||||
}
|
||||
announce();
|
||||
})
|
||||
.catch(() => undefined);
|
||||
.catch(() => {
|
||||
pending -= 1;
|
||||
announce();
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
unlisten?.();
|
||||
unlisten = null;
|
||||
if (unlisten) {
|
||||
unlisten();
|
||||
unlisten = null;
|
||||
live -= 1;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue