Skip to content

Commit ea45f30

Browse files
committed
Advance event.waitUntil() API
1 parent 91726c4 commit ea45f30

1 file changed

Lines changed: 37 additions & 22 deletions

File tree

src/webflo-runtime/webflo-routing/HttpEvent111.js

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -127,26 +127,45 @@ export class HttpEvent111 {
127127

128128
// ------
129129

130-
async waitUntil(promise, { surviveNavigation = false, authBound = false } = {}) {
130+
async waitUntil(promise, ...args) {
131131
let gcArray = [];
132-
const gc = () => gcArray.forEach((unsub) => unsub());
133-
134-
if (surviveNavigation) {
135-
const navHandler = (e) => e.preventDefault();
136-
this.client.addEventListener('navigate', navHandler);
137-
gcArray.push(() => this.client.removeEventListener('navigate', navHandler));
138-
} else {
139-
promise = Promise.race([promise, new Promise((res) => {
140-
this.client.addEventListener('navigate', res, { once: true });
141-
gcArray.push(() => this.client.removeEventListener('navigate', res));
142-
})]);
143-
}
132+
const gc = () => {
133+
gcArray.forEach((unsub) => unsub());
134+
gcArray.splice(0);
135+
};
144136

145-
if (authBound) {
146-
promise = Promise.race([promise, new Promise((res) => {
147-
gcArray.push(this.user.subscribe('id', (e) => {
148-
if (!e.value || e.oldValue && e.value !== e.oldValue) res(e);
149-
}));
137+
if (args.length) {
138+
promise = Promise.race([promise, new Promise((res, rej) => {
139+
const timeout = typeof args[0] === 'number' ? args.shift() : null;
140+
141+
if (typeof args[0] === 'function') {
142+
const lifecycleEventsHandler = args.shift();
143+
const callLifecycleEventsHandler = (e) => lifecycleEventsHandler(e, res, rej);
144+
145+
// On abort
146+
this.#abortController.signal.addEventListener('abort', callLifecycleEventsHandler);
147+
gcArray.push(() => this.#abortController.signal.removeEventListener('abort', callLifecycleEventsHandler));
148+
149+
// On navigate
150+
this.client.addEventListener('navigate', callLifecycleEventsHandler);
151+
gcArray.push(() => this.client.removeEventListener('navigate', callLifecycleEventsHandler));
152+
153+
// On auth switch
154+
gcArray.push(this.user.subscribe('id', callLifecycleEventsHandler, { scope: 2/* global */ }));
155+
156+
// On timeout
157+
if (timeout) {
158+
const id = setTimeout(() => {
159+
callLifecycleEventsHandler(new CustomEvent('timeout', { detail: { timeout } }));
160+
}, timeout);
161+
gcArray.push(() => clearTimeout(id));
162+
}
163+
} else if (timeout) {
164+
const id = setTimeout(() => {
165+
res(new CustomEvent('timeout', { detail: { timeout } }));
166+
}, timeout);
167+
gcArray.push(() => clearTimeout(id));
168+
}
150169
})]);
151170
}
152171

@@ -157,10 +176,6 @@ export class HttpEvent111 {
157176
return this.waitUntil(new Promise(() => { }));
158177
}
159178

160-
async waitUntilSignout() {
161-
return this.waitUntil(new Promise(() => { }), { authBound: true });
162-
}
163-
164179
async respondWith(data, ...args) {
165180
await this.#internalLiveResponse.replaceWith(data, ...args);
166181
}

0 commit comments

Comments
 (0)