mirror of
https://github.com/openswarm-ai/openswarm.git
synced 2026-08-23 05:02:21 +02:00
[eric] frontend: keepalive flush + pagehide drain so window close stops eating queued events
This commit is contained in:
@@ -41,13 +41,19 @@ function _flush(): void {
|
||||
const batch = _queue.splice(0);
|
||||
// Ship whole queue in one POST; /service/submit accepts a single object or array.
|
||||
const body = JSON.stringify(batch.length === 1 ? batch[0] : batch);
|
||||
// keepalive lets the request outlive a closing window, so the last batch isn't eaten on quit.
|
||||
fetch(`${API_BASE}/service/submit`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body,
|
||||
keepalive: true,
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
window.addEventListener('pagehide', () => _flush());
|
||||
}
|
||||
|
||||
export function sync(data: Record<string, unknown> = {}, opts: { immediate?: boolean } = {}): void {
|
||||
_lastTs = Date.now();
|
||||
// Stamp submission id + client ts so the cloud dedupes retries and orders by happen-time.
|
||||
|
||||
Reference in New Issue
Block a user