forked from Blendman974/faceclaw
Fix a race condition that sometimes caused apps to skip their first paint after launch
This commit is contained in:
1 parent
ccd28b7ac9
commit
7ebf921299
9 files changed
+74
No files matched your search
@@ -167,6 +167,12 @@ function post(message: WorkerAppReply): void {
|
|||||||
global.postMessage(message);
|
global.postMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The host queues messages until this arrives: posts to a worker whose bundle
|
||||||
|
// is still evaluating can be silently dropped (see WorkerAppHost). Top-level
|
||||||
|
// evaluation is synchronous, so the handler below is installed before any
|
||||||
|
// queued message can be delivered.
|
||||||
|
post({ type: "worker-ready" });
|
||||||
|
|
||||||
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||||
const message = event.data;
|
const message = event.data;
|
||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
|
|||||||
@@ -128,6 +128,12 @@ function post(message: WorkerAppReply): void {
|
|||||||
global.postMessage(message);
|
global.postMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The host queues messages until this arrives: posts to a worker whose bundle
|
||||||
|
// is still evaluating can be silently dropped (see WorkerAppHost). Top-level
|
||||||
|
// evaluation is synchronous, so the handler below is installed before any
|
||||||
|
// queued message can be delivered.
|
||||||
|
post({ type: "worker-ready" });
|
||||||
|
|
||||||
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||||
const message = event.data;
|
const message = event.data;
|
||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
|
|||||||
@@ -132,6 +132,12 @@ function post(message: WorkerAppReply): void {
|
|||||||
global.postMessage(message);
|
global.postMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The host queues messages until this arrives: posts to a worker whose bundle
|
||||||
|
// is still evaluating can be silently dropped (see WorkerAppHost). Top-level
|
||||||
|
// evaluation is synchronous, so the handler below is installed before any
|
||||||
|
// queued message can be delivered.
|
||||||
|
post({ type: "worker-ready" });
|
||||||
|
|
||||||
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||||
const message = event.data;
|
const message = event.data;
|
||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
|
|||||||
@@ -148,6 +148,12 @@ function post(message: WorkerAppReply): void {
|
|||||||
global.postMessage(message);
|
global.postMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The host queues messages until this arrives: posts to a worker whose bundle
|
||||||
|
// is still evaluating can be silently dropped (see WorkerAppHost). Top-level
|
||||||
|
// evaluation is synchronous, so the handler below is installed before any
|
||||||
|
// queued message can be delivered.
|
||||||
|
post({ type: "worker-ready" });
|
||||||
|
|
||||||
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||||
const message = event.data;
|
const message = event.data;
|
||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
|
|||||||
@@ -287,6 +287,12 @@ function post(message: WorkerAppReply): void {
|
|||||||
global.postMessage(message);
|
global.postMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The host queues messages until this arrives: posts to a worker whose bundle
|
||||||
|
// is still evaluating can be silently dropped (see WorkerAppHost). Top-level
|
||||||
|
// evaluation is synchronous, so the handler below is installed before any
|
||||||
|
// queued message can be delivered.
|
||||||
|
post({ type: "worker-ready" });
|
||||||
|
|
||||||
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||||
const message = event.data;
|
const message = event.data;
|
||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
|
|||||||
@@ -154,6 +154,12 @@ function post(message: WorkerAppReply): void {
|
|||||||
global.postMessage(message);
|
global.postMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The host queues messages until this arrives: posts to a worker whose bundle
|
||||||
|
// is still evaluating can be silently dropped (see WorkerAppHost). Top-level
|
||||||
|
// evaluation is synchronous, so the handler below is installed before any
|
||||||
|
// queued message can be delivered.
|
||||||
|
post({ type: "worker-ready" });
|
||||||
|
|
||||||
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||||
const message = event.data;
|
const message = event.data;
|
||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
|
|||||||
@@ -281,6 +281,12 @@ function post(message: WorkerAppReply): void {
|
|||||||
global.postMessage(message);
|
global.postMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The host queues messages until this arrives: posts to a worker whose bundle
|
||||||
|
// is still evaluating can be silently dropped (see WorkerAppHost). Top-level
|
||||||
|
// evaluation is synchronous, so the handler below is installed before any
|
||||||
|
// queued message can be delivered.
|
||||||
|
post({ type: "worker-ready" });
|
||||||
|
|
||||||
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||||
const message = event.data;
|
const message = event.data;
|
||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
|
|||||||
@@ -118,6 +118,12 @@ function post(message: WorkerAppReply): void {
|
|||||||
global.postMessage(message);
|
global.postMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The host queues messages until this arrives: posts to a worker whose bundle
|
||||||
|
// is still evaluating can be silently dropped (see WorkerAppHost). Top-level
|
||||||
|
// evaluation is synchronous, so the handler below is installed before any
|
||||||
|
// queued message can be delivered.
|
||||||
|
post({ type: "worker-ready" });
|
||||||
|
|
||||||
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||||
const message = event.data;
|
const message = event.data;
|
||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
|
|||||||
@@ -24,6 +24,14 @@ export type WorkerAppMessage =
|
|||||||
| { type: "tool-call"; callId: string; windowId: string; name: string; args: unknown };
|
| { type: "tool-call"; callId: string; windowId: string; name: string; args: unknown };
|
||||||
|
|
||||||
export type WorkerAppReply =
|
export type WorkerAppReply =
|
||||||
|
| {
|
||||||
|
/**
|
||||||
|
* The worker's bundle has evaluated and its onmessage handler is
|
||||||
|
* installed. Messages posted to a still-loading worker can be silently
|
||||||
|
* dropped, so the host queues everything until this arrives.
|
||||||
|
*/
|
||||||
|
type: "worker-ready";
|
||||||
|
}
|
||||||
| { type: "yield-focus"; windowId: string }
|
| { type: "yield-focus"; windowId: string }
|
||||||
| {
|
| {
|
||||||
/** Foreground and focus one of the app's existing windows. */
|
/** Foreground and focus one of the app's existing windows. */
|
||||||
@@ -166,12 +174,26 @@ export class WorkerAppHost {
|
|||||||
private readonly openWindows = new Set<string>();
|
private readonly openWindows = new Set<string>();
|
||||||
private readonly pendingToolCalls = new Map<string, PendingToolCall>();
|
private readonly pendingToolCalls = new Map<string, PendingToolCall>();
|
||||||
private nextCallSerial = 1;
|
private nextCallSerial = 1;
|
||||||
|
/**
|
||||||
|
* Messages posted before the worker finishes evaluating its bundle can be
|
||||||
|
* silently dropped (notably the launch-time foreground/render pair, leaving
|
||||||
|
* a black window until the first input). Queue everything until the worker
|
||||||
|
* posts worker-ready, then flush in order.
|
||||||
|
*/
|
||||||
|
private workerReady = false;
|
||||||
|
private readonly queuedMessages: WorkerAppMessage[] = [];
|
||||||
|
|
||||||
constructor(private readonly options: WorkerAppHostOptions) {
|
constructor(private readonly options: WorkerAppHostOptions) {
|
||||||
options.worker.onmessage = (event: MessageEvent) => {
|
options.worker.onmessage = (event: MessageEvent) => {
|
||||||
const message = event.data as WorkerAppReply | undefined;
|
const message = event.data as WorkerAppReply | undefined;
|
||||||
if (!message) return;
|
if (!message) return;
|
||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
|
case "worker-ready":
|
||||||
|
this.workerReady = true;
|
||||||
|
for (const queued of this.queuedMessages.splice(0)) {
|
||||||
|
this.options.worker.postMessage(queued);
|
||||||
|
}
|
||||||
|
break;
|
||||||
case "yield-focus":
|
case "yield-focus":
|
||||||
// Only the focused window's yield is meaningful.
|
// Only the focused window's yield is meaningful.
|
||||||
if (shell.foregroundWindow()?.windowId === message.windowId) {
|
if (shell.foregroundWindow()?.windowId === message.windowId) {
|
||||||
@@ -378,6 +400,10 @@ export class WorkerAppHost {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private post(message: WorkerAppMessage): void {
|
private post(message: WorkerAppMessage): void {
|
||||||
|
if (!this.workerReady) {
|
||||||
|
this.queuedMessages.push(message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
this.options.worker.postMessage(message);
|
this.options.worker.postMessage(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user