-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fetchWebSocket
should return a promise
#286
Comments
For this to work the websocket functions have to become stateful. A WebsocketClient class with a map of currently requested resources and corresponding callback should do the trick |
I've opted for something like this: const defaultTimeout = 5000;
/** Sends a GET message for some resource over websockets. */
export async function fetchWebSocket(
client: WebSocket,
subject: string,
): Promise<Resource> {
return new Promise((resolve, reject) => {
client.addEventListener('message', function listener(ev) {
const timeoutId = setTimeout(() => {
client.removeEventListener('message', listener);
reject(
new Error(
`Request for subject "${subject}" timed out after ${defaultTimeout}ms.`,
),
);
}, defaultTimeout);
if (ev.data.startsWith('RESOURCE ')) {
parseResourceMessage(ev).forEach(resource => {
// if it is the requested subject, return the resource
if (resource.getSubject() === subject) {
clearTimeout(timeoutId);
client.removeEventListener('message', listener);
resolve(resource);
}
});
}
});
client.send('GET ' + subject);
});
} |
I'm having some performance doupts. Say on init, the user requests about 300 resources using websockets (e.g. when opening a Table or Chat or something). For each incoming resource, the eventListeners for every other resource are called. This means that we have 300*300 evaluations. I'm not sure if this will severely impact performance, but I can certainly imagine it will. One simple way to prevent this, is by only doing the whole eventlistener thing if we know the requesting function is actually waiting for the response. If they aren't But how do we know this?
|
Currenlty,
fetchWebSocket
simply sends aGET
message. The receivedRESOURCE
is then properly processed, but the function itself does not return this resource. That means users can'tawait
the requested resource.How can we fix this?
Well, we could add a
.onmessage
handler that calls the return thing, which is awaited. Or does this replace the earlieronmessage
?EDIT: Fixed, but we could need a more performant solution (see below)
The text was updated successfully, but these errors were encountered: