-
-
Notifications
You must be signed in to change notification settings - Fork 320
/
Copy pathreactpy-client.ts
264 lines (230 loc) · 6.85 KB
/
reactpy-client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import { ReactPyModule } from "./reactpy-vdom";
import logger from "./logger";
/**
* A client for communicating with a ReactPy server.
*/
export interface ReactPyClient {
/**
* Register a handler for a message type.
*
* The first time this is called, the client will be considered ready.
*
* @param type The type of message to handle.
* @param handler The handler to call when a message of the given type is received.
* @returns A function to unregister the handler.
*/
onMessage(type: string, handler: (message: any) => void): () => void;
/**
* Send a message to the server.
*
* @param message The message to send. Messages must have a `type` property.
*/
sendMessage(message: any): void;
/**
* Load a module from the server.
* @param moduleName The name of the module to load.
* @returns A promise that resolves to the module.
*/
loadModule(moduleName: string): Promise<ReactPyModule>;
}
export abstract class BaseReactPyClient implements ReactPyClient {
private readonly handlers: { [key: string]: ((message: any) => void)[] } = {};
protected readonly ready: Promise<void>;
private resolveReady: (value: undefined) => void;
constructor() {
this.resolveReady = () => {};
this.ready = new Promise((resolve) => (this.resolveReady = resolve));
}
onMessage(type: string, handler: (message: any) => void): () => void {
(this.handlers[type] || (this.handlers[type] = [])).push(handler);
this.resolveReady(undefined);
return () => {
this.handlers[type] = this.handlers[type].filter((h) => h !== handler);
};
}
abstract sendMessage(message: any): void;
abstract loadModule(moduleName: string): Promise<ReactPyModule>;
/**
* Handle an incoming message.
*
* This should be called by subclasses when a message is received.
*
* @param message The message to handle. The message must have a `type` property.
*/
protected handleIncoming(message: any): void {
if (!message.type) {
logger.warn("Received message without type", message);
return;
}
const messageHandlers: ((m: any) => void)[] | undefined =
this.handlers[message.type];
if (!messageHandlers) {
logger.warn("Received message without handler", message);
return;
}
messageHandlers.forEach((h) => h(message));
}
}
export type SimpleReactPyClientProps = {
serverLocation?: LocationProps;
reconnectOptions?: ReconnectProps;
};
/**
* The location of the server.
*
* This is used to determine the location of the server's API endpoints. All endpoints
* are expected to be found at the base URL, with the following paths:
*
* - `_reactpy/stream/${route}${query}`: The websocket endpoint for the stream.
* - `_reactpy/modules`: The directory containing the dynamically loaded modules.
* - `_reactpy/assets`: The directory containing the static assets.
*/
type LocationProps = {
/**
* The base URL of the server.
*
* @default - document.location.origin
*/
url: string;
/**
* The route to the page being rendered.
*
* @default - document.location.pathname
*/
route: string;
/**
* The query string of the page being rendered.
*
* @default - document.location.search
*/
query: string;
};
type ReconnectProps = {
maxInterval?: number;
maxRetries?: number;
backoffRate?: number;
intervalJitter?: number;
};
export class SimpleReactPyClient
extends BaseReactPyClient
implements ReactPyClient
{
private readonly urls: ServerUrls;
private readonly socket: { current?: WebSocket };
constructor(props: SimpleReactPyClientProps) {
super();
this.urls = getServerUrls(
props.serverLocation || {
url: document.location.origin,
route: document.location.pathname,
query: document.location.search,
},
);
this.socket = createReconnectingWebSocket({
readyPromise: this.ready,
url: this.urls.stream,
onMessage: async ({ data }) => this.handleIncoming(JSON.parse(data)),
...props.reconnectOptions,
});
}
sendMessage(message: any): void {
this.socket.current?.send(JSON.stringify(message));
}
loadModule(moduleName: string): Promise<ReactPyModule> {
return import(`${this.urls.modules}/${moduleName}`);
}
}
type ServerUrls = {
base: URL;
stream: string;
modules: string;
assets: string;
};
function getServerUrls(props: LocationProps): ServerUrls {
const base = new URL(`${props.url || document.location.origin}/_reactpy`);
const modules = `${base}/modules`;
const assets = `${base}/assets`;
const streamProtocol = `ws${base.protocol === "https:" ? "s" : ""}`;
const streamPath = rtrim(`${base.pathname}/stream${props.route || ""}`, "/");
const stream = `${streamProtocol}://${base.host}${streamPath}${props.query}`;
return { base, modules, assets, stream };
}
function createReconnectingWebSocket(
props: {
url: string;
readyPromise: Promise<void>;
onOpen?: () => void;
onMessage: (message: MessageEvent<any>) => void;
onClose?: () => void;
} & ReconnectProps,
) {
const {
maxInterval = 60000,
maxRetries = 50,
backoffRate = 1.1,
intervalJitter = 0.1,
} = props;
const startInterval = 750;
let retries = 0;
let interval = startInterval;
const closed = false;
let everConnected = false;
const socket: { current?: WebSocket } = {};
const connect = () => {
if (closed) {
return;
}
socket.current = new WebSocket(props.url);
socket.current.onopen = () => {
everConnected = true;
logger.log("client connected");
interval = startInterval;
retries = 0;
if (props.onOpen) {
props.onOpen();
}
};
socket.current.onmessage = props.onMessage;
socket.current.onclose = () => {
if (!everConnected) {
logger.log("failed to connect");
return;
}
logger.log("client disconnected");
if (props.onClose) {
props.onClose();
}
if (retries >= maxRetries) {
return;
}
const thisInterval = addJitter(interval, intervalJitter);
logger.log(
`reconnecting in ${(thisInterval / 1000).toPrecision(4)} seconds...`,
);
setTimeout(connect, thisInterval);
interval = nextInterval(interval, backoffRate, maxInterval);
retries++;
};
};
props.readyPromise.then(() => logger.log("starting client...")).then(connect);
return socket;
}
function nextInterval(
currentInterval: number,
backoffRate: number,
maxInterval: number,
): number {
return Math.min(
currentInterval *
// increase interval by backoff rate
backoffRate,
// don't exceed max interval
maxInterval,
);
}
function addJitter(interval: number, jitter: number): number {
return interval + (Math.random() * jitter * interval * 2 - jitter * interval);
}
function rtrim(text: string, trim: string): string {
return text.replace(new RegExp(`${trim}+$`), "");
}