index.ts

 1import { fail } from 'assert';
 2
 3export function noop() {}
 4
 5const isDev = process.env.NODE_ENV === 'development';
 6
 7export function websocketUrl(path: string): string {
 8    const loc = window.location;
 9
10    if (isDev) {
11        // react-scripts does not properly proxy websocket requests, so manually select the URL here.
12        return `ws://${loc.hostname}:5000${path}`;
13    }
14
15    return `${loc.protocol === 'https:' ? 'wss:' : 'ws:'}//${loc.host}${path}`;
16}
17
18export function assertNever(x: never): never {
19    throw new Error('Unexpected object: ' + x);
20}
21
22export function isDefined<T>(x: T | undefined | null): x is T {
23    return x !== undefined && x !== null;
24}
25
26export function assertIsDefined<T>(val: T): asserts val is NonNullable<T> {
27    if (val === undefined || val === null) {
28        fail(`Expected 'val' to be defined, but received ${val}`);
29    }
30}
31
32export const nameofFactory = <T>() => (name: keyof T) => name;
33
34export function reloadOutdatedPage() {
35    console.log('Frontend version appears to be outdated; reloading to allow the browser to update.');
36    window.location.reload(true);
37}