bigbuffet-rw/src/stream.ts

130 lines
3.1 KiB
TypeScript
Raw Normal View History

import WebSocketClient from '@gamestdio/websocket';
2021-03-24 15:53:09 -07:00
import { getAccessToken } from 'soapbox/utils/auth';
2020-03-27 13:59:38 -07:00
import type { AppDispatch, RootState } from 'soapbox/store';
2020-03-27 13:59:38 -07:00
const randomIntUpTo = (max: number) => Math.floor(Math.random() * Math.floor(max));
2023-04-02 17:54:08 -07:00
interface ConnectStreamCallbacks {
onConnect(): void;
onDisconnect(): void;
onReceive(websocket: WebSocket, data: unknown): void;
2023-04-02 17:54:08 -07:00
}
type PollingRefreshFn = (dispatch: AppDispatch, done?: () => void) => void
const connectStream = (
path: string,
2023-04-02 17:54:08 -07:00
pollingRefresh: PollingRefreshFn | null = null,
callbacks: (dispatch: AppDispatch, getState: () => RootState) => ConnectStreamCallbacks,
) => (dispatch: AppDispatch, getState: () => RootState) => {
const streamingAPIBaseURL = getState().instance.configuration.urls.streaming;
const accessToken = getAccessToken(getState());
const { onConnect, onDisconnect, onReceive } = callbacks(dispatch, getState);
2020-03-27 13:59:38 -07:00
let polling: NodeJS.Timeout | null = null;
2020-03-27 13:59:38 -07:00
const setupPolling = () => {
if (pollingRefresh) {
pollingRefresh(dispatch, () => {
polling = setTimeout(() => setupPolling(), 20000 + randomIntUpTo(20000));
});
}
};
2020-03-27 13:59:38 -07:00
const clearPolling = () => {
if (polling) {
clearTimeout(polling);
polling = null;
}
};
2020-03-27 13:59:38 -07:00
let subscription: WebSocket;
// If the WebSocket fails to be created, don't crash the whole page,
// just proceed without a subscription.
try {
subscription = getStream(streamingAPIBaseURL!, accessToken!, path, {
connected() {
if (pollingRefresh) {
clearPolling();
}
onConnect();
},
disconnected() {
if (pollingRefresh) {
polling = setTimeout(() => setupPolling(), randomIntUpTo(40000));
}
onDisconnect();
},
received(data) {
onReceive(subscription, data);
},
reconnected() {
if (pollingRefresh) {
clearPolling();
pollingRefresh(dispatch);
}
onConnect();
},
});
} catch (e) {
console.error(e);
}
const disconnect = () => {
if (subscription) {
subscription.close();
}
2020-03-27 13:59:38 -07:00
clearPolling();
2020-03-27 13:59:38 -07:00
};
return disconnect;
};
const getStream = (
streamingAPIBaseURL: string,
accessToken: string,
stream: string,
{ connected, received, disconnected, reconnected }: {
connected: ((this: WebSocket, ev: Event) => any) | null;
received: (data: any) => void;
disconnected: ((this: WebSocket, ev: Event) => any) | null;
reconnected: ((this: WebSocket, ev: Event) => any);
},
) => {
2020-03-27 13:59:38 -07:00
const params = [ `stream=${stream}` ];
const ws = new WebSocketClient(`${streamingAPIBaseURL}/api/v1/streaming/?${params.join('&')}`, accessToken as any);
2020-03-27 13:59:38 -07:00
ws.onopen = connected;
ws.onclose = disconnected;
ws.onreconnect = reconnected;
ws.onmessage = (e) => {
if (!e.data) return;
try {
received(JSON.parse(e.data));
} catch (error) {
console.error(e);
2020-04-14 11:44:40 -07:00
console.error(`Could not parse the above streaming event.\n${error}`);
}
2020-04-14 11:44:40 -07:00
};
2020-03-27 13:59:38 -07:00
return ws;
};
export {
connectStream,
getStream as default,
};