@reactpy/client 0.3.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.d.ts +29 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +60 -0
- package/dist/client.js.map +1 -0
- package/dist/components.d.ts +3 -4
- package/dist/components.d.ts.map +1 -1
- package/dist/components.js +38 -37
- package/dist/components.js.map +1 -1
- package/dist/index.d.ts +6 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -3
- package/dist/index.js.map +1 -1
- package/dist/logger.d.ts +1 -0
- package/dist/logger.d.ts.map +1 -1
- package/dist/logger.js +1 -0
- package/dist/logger.js.map +1 -1
- package/dist/mount.d.ts +2 -2
- package/dist/mount.d.ts.map +1 -1
- package/dist/mount.js +29 -4
- package/dist/mount.js.map +1 -1
- package/dist/types.d.ts +126 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/types.js.map +1 -0
- package/dist/vdom.d.ts +8 -0
- package/dist/vdom.d.ts.map +1 -0
- package/dist/vdom.js +168 -0
- package/dist/vdom.js.map +1 -0
- package/dist/websocket.d.ts +6 -0
- package/dist/websocket.d.ts.map +1 -0
- package/dist/websocket.js +57 -0
- package/dist/websocket.js.map +1 -0
- package/package.json +23 -22
- package/src/client.ts +83 -0
- package/src/components.tsx +40 -46
- package/src/index.ts +6 -3
- package/src/logger.ts +1 -0
- package/src/mount.tsx +37 -5
- package/src/types.ts +152 -0
- package/src/vdom.tsx +250 -0
- package/src/websocket.ts +75 -0
- package/tsconfig.json +3 -3
- package/tsconfig.tsbuildinfo +1 -0
- package/dist/messages.d.ts +0 -15
- package/dist/messages.d.ts.map +0 -1
- package/dist/messages.js +0 -2
- package/dist/messages.js.map +0 -1
- package/dist/reactpy-client.d.ts +0 -94
- package/dist/reactpy-client.d.ts.map +0 -1
- package/dist/reactpy-client.js +0 -128
- package/dist/reactpy-client.js.map +0 -1
- package/dist/reactpy-vdom.d.ts +0 -54
- package/dist/reactpy-vdom.d.ts.map +0 -1
- package/dist/reactpy-vdom.js +0 -141
- package/dist/reactpy-vdom.js.map +0 -1
- package/src/messages.ts +0 -17
- package/src/reactpy-client.ts +0 -264
- package/src/reactpy-vdom.tsx +0 -261
package/src/reactpy-client.ts
DELETED
|
@@ -1,264 +0,0 @@
|
|
|
1
|
-
import { ReactPyModule } from "./reactpy-vdom";
|
|
2
|
-
import logger from "./logger";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* A client for communicating with a ReactPy server.
|
|
6
|
-
*/
|
|
7
|
-
export interface ReactPyClient {
|
|
8
|
-
/**
|
|
9
|
-
* Register a handler for a message type.
|
|
10
|
-
*
|
|
11
|
-
* The first time this is called, the client will be considered ready.
|
|
12
|
-
*
|
|
13
|
-
* @param type The type of message to handle.
|
|
14
|
-
* @param handler The handler to call when a message of the given type is received.
|
|
15
|
-
* @returns A function to unregister the handler.
|
|
16
|
-
*/
|
|
17
|
-
onMessage(type: string, handler: (message: any) => void): () => void;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Send a message to the server.
|
|
21
|
-
*
|
|
22
|
-
* @param message The message to send. Messages must have a `type` property.
|
|
23
|
-
*/
|
|
24
|
-
sendMessage(message: any): void;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Load a module from the server.
|
|
28
|
-
* @param moduleName The name of the module to load.
|
|
29
|
-
* @returns A promise that resolves to the module.
|
|
30
|
-
*/
|
|
31
|
-
loadModule(moduleName: string): Promise<ReactPyModule>;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export abstract class BaseReactPyClient implements ReactPyClient {
|
|
35
|
-
private readonly handlers: { [key: string]: ((message: any) => void)[] } = {};
|
|
36
|
-
protected readonly ready: Promise<void>;
|
|
37
|
-
private resolveReady: (value: undefined) => void;
|
|
38
|
-
|
|
39
|
-
constructor() {
|
|
40
|
-
this.resolveReady = () => {};
|
|
41
|
-
this.ready = new Promise((resolve) => (this.resolveReady = resolve));
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
onMessage(type: string, handler: (message: any) => void): () => void {
|
|
45
|
-
(this.handlers[type] || (this.handlers[type] = [])).push(handler);
|
|
46
|
-
this.resolveReady(undefined);
|
|
47
|
-
return () => {
|
|
48
|
-
this.handlers[type] = this.handlers[type].filter((h) => h !== handler);
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
abstract sendMessage(message: any): void;
|
|
53
|
-
abstract loadModule(moduleName: string): Promise<ReactPyModule>;
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Handle an incoming message.
|
|
57
|
-
*
|
|
58
|
-
* This should be called by subclasses when a message is received.
|
|
59
|
-
*
|
|
60
|
-
* @param message The message to handle. The message must have a `type` property.
|
|
61
|
-
*/
|
|
62
|
-
protected handleIncoming(message: any): void {
|
|
63
|
-
if (!message.type) {
|
|
64
|
-
logger.warn("Received message without type", message);
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const messageHandlers: ((m: any) => void)[] | undefined =
|
|
69
|
-
this.handlers[message.type];
|
|
70
|
-
if (!messageHandlers) {
|
|
71
|
-
logger.warn("Received message without handler", message);
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
messageHandlers.forEach((h) => h(message));
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export type SimpleReactPyClientProps = {
|
|
80
|
-
serverLocation?: LocationProps;
|
|
81
|
-
reconnectOptions?: ReconnectProps;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* The location of the server.
|
|
86
|
-
*
|
|
87
|
-
* This is used to determine the location of the server's API endpoints. All endpoints
|
|
88
|
-
* are expected to be found at the base URL, with the following paths:
|
|
89
|
-
*
|
|
90
|
-
* - `_reactpy/stream/${route}${query}`: The websocket endpoint for the stream.
|
|
91
|
-
* - `_reactpy/modules`: The directory containing the dynamically loaded modules.
|
|
92
|
-
* - `_reactpy/assets`: The directory containing the static assets.
|
|
93
|
-
*/
|
|
94
|
-
type LocationProps = {
|
|
95
|
-
/**
|
|
96
|
-
* The base URL of the server.
|
|
97
|
-
*
|
|
98
|
-
* @default - document.location.origin
|
|
99
|
-
*/
|
|
100
|
-
url: string;
|
|
101
|
-
/**
|
|
102
|
-
* The route to the page being rendered.
|
|
103
|
-
*
|
|
104
|
-
* @default - document.location.pathname
|
|
105
|
-
*/
|
|
106
|
-
route: string;
|
|
107
|
-
/**
|
|
108
|
-
* The query string of the page being rendered.
|
|
109
|
-
*
|
|
110
|
-
* @default - document.location.search
|
|
111
|
-
*/
|
|
112
|
-
query: string;
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
type ReconnectProps = {
|
|
116
|
-
maxInterval?: number;
|
|
117
|
-
maxRetries?: number;
|
|
118
|
-
backoffRate?: number;
|
|
119
|
-
intervalJitter?: number;
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
export class SimpleReactPyClient
|
|
123
|
-
extends BaseReactPyClient
|
|
124
|
-
implements ReactPyClient
|
|
125
|
-
{
|
|
126
|
-
private readonly urls: ServerUrls;
|
|
127
|
-
private readonly socket: { current?: WebSocket };
|
|
128
|
-
|
|
129
|
-
constructor(props: SimpleReactPyClientProps) {
|
|
130
|
-
super();
|
|
131
|
-
|
|
132
|
-
this.urls = getServerUrls(
|
|
133
|
-
props.serverLocation || {
|
|
134
|
-
url: document.location.origin,
|
|
135
|
-
route: document.location.pathname,
|
|
136
|
-
query: document.location.search,
|
|
137
|
-
},
|
|
138
|
-
);
|
|
139
|
-
|
|
140
|
-
this.socket = createReconnectingWebSocket({
|
|
141
|
-
readyPromise: this.ready,
|
|
142
|
-
url: this.urls.stream,
|
|
143
|
-
onMessage: async ({ data }) => this.handleIncoming(JSON.parse(data)),
|
|
144
|
-
...props.reconnectOptions,
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
sendMessage(message: any): void {
|
|
149
|
-
this.socket.current?.send(JSON.stringify(message));
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
loadModule(moduleName: string): Promise<ReactPyModule> {
|
|
153
|
-
return import(`${this.urls.modules}/${moduleName}`);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
type ServerUrls = {
|
|
158
|
-
base: URL;
|
|
159
|
-
stream: string;
|
|
160
|
-
modules: string;
|
|
161
|
-
assets: string;
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
function getServerUrls(props: LocationProps): ServerUrls {
|
|
165
|
-
const base = new URL(`${props.url || document.location.origin}/_reactpy`);
|
|
166
|
-
const modules = `${base}/modules`;
|
|
167
|
-
const assets = `${base}/assets`;
|
|
168
|
-
|
|
169
|
-
const streamProtocol = `ws${base.protocol === "https:" ? "s" : ""}`;
|
|
170
|
-
const streamPath = rtrim(`${base.pathname}/stream${props.route || ""}`, "/");
|
|
171
|
-
const stream = `${streamProtocol}://${base.host}${streamPath}${props.query}`;
|
|
172
|
-
|
|
173
|
-
return { base, modules, assets, stream };
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function createReconnectingWebSocket(
|
|
177
|
-
props: {
|
|
178
|
-
url: string;
|
|
179
|
-
readyPromise: Promise<void>;
|
|
180
|
-
onOpen?: () => void;
|
|
181
|
-
onMessage: (message: MessageEvent<any>) => void;
|
|
182
|
-
onClose?: () => void;
|
|
183
|
-
} & ReconnectProps,
|
|
184
|
-
) {
|
|
185
|
-
const {
|
|
186
|
-
maxInterval = 60000,
|
|
187
|
-
maxRetries = 50,
|
|
188
|
-
backoffRate = 1.1,
|
|
189
|
-
intervalJitter = 0.1,
|
|
190
|
-
} = props;
|
|
191
|
-
|
|
192
|
-
const startInterval = 750;
|
|
193
|
-
let retries = 0;
|
|
194
|
-
let interval = startInterval;
|
|
195
|
-
const closed = false;
|
|
196
|
-
let everConnected = false;
|
|
197
|
-
const socket: { current?: WebSocket } = {};
|
|
198
|
-
|
|
199
|
-
const connect = () => {
|
|
200
|
-
if (closed) {
|
|
201
|
-
return;
|
|
202
|
-
}
|
|
203
|
-
socket.current = new WebSocket(props.url);
|
|
204
|
-
socket.current.onopen = () => {
|
|
205
|
-
everConnected = true;
|
|
206
|
-
logger.log("client connected");
|
|
207
|
-
interval = startInterval;
|
|
208
|
-
retries = 0;
|
|
209
|
-
if (props.onOpen) {
|
|
210
|
-
props.onOpen();
|
|
211
|
-
}
|
|
212
|
-
};
|
|
213
|
-
socket.current.onmessage = props.onMessage;
|
|
214
|
-
socket.current.onclose = () => {
|
|
215
|
-
if (!everConnected) {
|
|
216
|
-
logger.log("failed to connect");
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
logger.log("client disconnected");
|
|
221
|
-
if (props.onClose) {
|
|
222
|
-
props.onClose();
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
if (retries >= maxRetries) {
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
const thisInterval = addJitter(interval, intervalJitter);
|
|
230
|
-
logger.log(
|
|
231
|
-
`reconnecting in ${(thisInterval / 1000).toPrecision(4)} seconds...`,
|
|
232
|
-
);
|
|
233
|
-
setTimeout(connect, thisInterval);
|
|
234
|
-
interval = nextInterval(interval, backoffRate, maxInterval);
|
|
235
|
-
retries++;
|
|
236
|
-
};
|
|
237
|
-
};
|
|
238
|
-
|
|
239
|
-
props.readyPromise.then(() => logger.log("starting client...")).then(connect);
|
|
240
|
-
|
|
241
|
-
return socket;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
function nextInterval(
|
|
245
|
-
currentInterval: number,
|
|
246
|
-
backoffRate: number,
|
|
247
|
-
maxInterval: number,
|
|
248
|
-
): number {
|
|
249
|
-
return Math.min(
|
|
250
|
-
currentInterval *
|
|
251
|
-
// increase interval by backoff rate
|
|
252
|
-
backoffRate,
|
|
253
|
-
// don't exceed max interval
|
|
254
|
-
maxInterval,
|
|
255
|
-
);
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
function addJitter(interval: number, jitter: number): number {
|
|
259
|
-
return interval + (Math.random() * jitter * interval * 2 - jitter * interval);
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
function rtrim(text: string, trim: string): string {
|
|
263
|
-
return text.replace(new RegExp(`${trim}+$`), "");
|
|
264
|
-
}
|
package/src/reactpy-vdom.tsx
DELETED
|
@@ -1,261 +0,0 @@
|
|
|
1
|
-
import React, { ComponentType } from "react";
|
|
2
|
-
import { ReactPyClient } from "./reactpy-client";
|
|
3
|
-
import serializeEvent from "event-to-object";
|
|
4
|
-
|
|
5
|
-
export async function loadImportSource(
|
|
6
|
-
vdomImportSource: ReactPyVdomImportSource,
|
|
7
|
-
client: ReactPyClient,
|
|
8
|
-
): Promise<BindImportSource> {
|
|
9
|
-
let module: ReactPyModule;
|
|
10
|
-
if (vdomImportSource.sourceType === "URL") {
|
|
11
|
-
module = await import(vdomImportSource.source);
|
|
12
|
-
} else {
|
|
13
|
-
module = await client.loadModule(vdomImportSource.source);
|
|
14
|
-
}
|
|
15
|
-
if (typeof module.bind !== "function") {
|
|
16
|
-
throw new Error(
|
|
17
|
-
`${vdomImportSource.source} did not export a function 'bind'`,
|
|
18
|
-
);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return (node: HTMLElement) => {
|
|
22
|
-
const binding = module.bind(node, {
|
|
23
|
-
sendMessage: client.sendMessage,
|
|
24
|
-
onMessage: client.onMessage,
|
|
25
|
-
});
|
|
26
|
-
if (
|
|
27
|
-
!(
|
|
28
|
-
typeof binding.create === "function" &&
|
|
29
|
-
typeof binding.render === "function" &&
|
|
30
|
-
typeof binding.unmount === "function"
|
|
31
|
-
)
|
|
32
|
-
) {
|
|
33
|
-
console.error(`${vdomImportSource.source} returned an impropper binding`);
|
|
34
|
-
return null;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return {
|
|
38
|
-
render: (model) =>
|
|
39
|
-
binding.render(
|
|
40
|
-
createImportSourceElement({
|
|
41
|
-
client,
|
|
42
|
-
module,
|
|
43
|
-
binding,
|
|
44
|
-
model,
|
|
45
|
-
currentImportSource: vdomImportSource,
|
|
46
|
-
}),
|
|
47
|
-
),
|
|
48
|
-
unmount: binding.unmount,
|
|
49
|
-
};
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function createImportSourceElement(props: {
|
|
54
|
-
client: ReactPyClient;
|
|
55
|
-
module: ReactPyModule;
|
|
56
|
-
binding: ReactPyModuleBinding;
|
|
57
|
-
model: ReactPyVdom;
|
|
58
|
-
currentImportSource: ReactPyVdomImportSource;
|
|
59
|
-
}): any {
|
|
60
|
-
let type: any;
|
|
61
|
-
if (props.model.importSource) {
|
|
62
|
-
if (
|
|
63
|
-
!isImportSourceEqual(props.currentImportSource, props.model.importSource)
|
|
64
|
-
) {
|
|
65
|
-
console.error(
|
|
66
|
-
"Parent element import source " +
|
|
67
|
-
stringifyImportSource(props.currentImportSource) +
|
|
68
|
-
" does not match child's import source " +
|
|
69
|
-
stringifyImportSource(props.model.importSource),
|
|
70
|
-
);
|
|
71
|
-
return null;
|
|
72
|
-
} else if (!props.module[props.model.tagName]) {
|
|
73
|
-
console.error(
|
|
74
|
-
"Module from source " +
|
|
75
|
-
stringifyImportSource(props.currentImportSource) +
|
|
76
|
-
` does not export ${props.model.tagName}`,
|
|
77
|
-
);
|
|
78
|
-
return null;
|
|
79
|
-
} else {
|
|
80
|
-
type = props.module[props.model.tagName];
|
|
81
|
-
}
|
|
82
|
-
} else {
|
|
83
|
-
type = props.model.tagName;
|
|
84
|
-
}
|
|
85
|
-
return props.binding.create(
|
|
86
|
-
type,
|
|
87
|
-
createAttributes(props.model, props.client),
|
|
88
|
-
createChildren(props.model, (child) =>
|
|
89
|
-
createImportSourceElement({
|
|
90
|
-
...props,
|
|
91
|
-
model: child,
|
|
92
|
-
}),
|
|
93
|
-
),
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function isImportSourceEqual(
|
|
98
|
-
source1: ReactPyVdomImportSource,
|
|
99
|
-
source2: ReactPyVdomImportSource,
|
|
100
|
-
) {
|
|
101
|
-
return (
|
|
102
|
-
source1.source === source2.source &&
|
|
103
|
-
source1.sourceType === source2.sourceType
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function stringifyImportSource(importSource: ReactPyVdomImportSource) {
|
|
108
|
-
return JSON.stringify({
|
|
109
|
-
source: importSource.source,
|
|
110
|
-
sourceType: importSource.sourceType,
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export function createChildren<Child>(
|
|
115
|
-
model: ReactPyVdom,
|
|
116
|
-
createChild: (child: ReactPyVdom) => Child,
|
|
117
|
-
): (Child | string)[] {
|
|
118
|
-
if (!model.children) {
|
|
119
|
-
return [];
|
|
120
|
-
} else {
|
|
121
|
-
return model.children.map((child) => {
|
|
122
|
-
switch (typeof child) {
|
|
123
|
-
case "object":
|
|
124
|
-
return createChild(child);
|
|
125
|
-
case "string":
|
|
126
|
-
return child;
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export function createAttributes(
|
|
133
|
-
model: ReactPyVdom,
|
|
134
|
-
client: ReactPyClient,
|
|
135
|
-
): { [key: string]: any } {
|
|
136
|
-
return Object.fromEntries(
|
|
137
|
-
Object.entries({
|
|
138
|
-
// Normal HTML attributes
|
|
139
|
-
...model.attributes,
|
|
140
|
-
// Construct event handlers
|
|
141
|
-
...Object.fromEntries(
|
|
142
|
-
Object.entries(model.eventHandlers || {}).map(([name, handler]) =>
|
|
143
|
-
createEventHandler(client, name, handler),
|
|
144
|
-
),
|
|
145
|
-
),
|
|
146
|
-
// Convert snake_case to camelCase names
|
|
147
|
-
}).map(normalizeAttribute),
|
|
148
|
-
);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function createEventHandler(
|
|
152
|
-
client: ReactPyClient,
|
|
153
|
-
name: string,
|
|
154
|
-
{ target, preventDefault, stopPropagation }: ReactPyVdomEventHandler,
|
|
155
|
-
): [string, () => void] {
|
|
156
|
-
return [
|
|
157
|
-
name,
|
|
158
|
-
function (...args: any[]) {
|
|
159
|
-
const data = Array.from(args).map((value) => {
|
|
160
|
-
if (!(typeof value === "object" && value.nativeEvent)) {
|
|
161
|
-
return value;
|
|
162
|
-
}
|
|
163
|
-
const event = value as React.SyntheticEvent<any>;
|
|
164
|
-
if (preventDefault) {
|
|
165
|
-
event.preventDefault();
|
|
166
|
-
}
|
|
167
|
-
if (stopPropagation) {
|
|
168
|
-
event.stopPropagation();
|
|
169
|
-
}
|
|
170
|
-
return serializeEvent(event.nativeEvent);
|
|
171
|
-
});
|
|
172
|
-
client.sendMessage({ type: "layout-event", data, target });
|
|
173
|
-
},
|
|
174
|
-
];
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
function normalizeAttribute([key, value]: [string, any]): [string, any] {
|
|
178
|
-
let normKey = key;
|
|
179
|
-
let normValue = value;
|
|
180
|
-
|
|
181
|
-
if (key === "style" && typeof value === "object") {
|
|
182
|
-
normValue = Object.fromEntries(
|
|
183
|
-
Object.entries(value).map(([k, v]) => [snakeToCamel(k), v]),
|
|
184
|
-
);
|
|
185
|
-
} else if (
|
|
186
|
-
key.startsWith("data_") ||
|
|
187
|
-
key.startsWith("aria_") ||
|
|
188
|
-
DASHED_HTML_ATTRS.includes(key)
|
|
189
|
-
) {
|
|
190
|
-
normKey = key.split("_").join("-");
|
|
191
|
-
} else {
|
|
192
|
-
normKey = snakeToCamel(key);
|
|
193
|
-
}
|
|
194
|
-
return [normKey, normValue];
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
function snakeToCamel(str: string): string {
|
|
198
|
-
return str.replace(/([_][a-z])/g, (group) =>
|
|
199
|
-
group.toUpperCase().replace("_", ""),
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// see list of HTML attributes with dashes in them:
|
|
204
|
-
// https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes#attribute_list
|
|
205
|
-
const DASHED_HTML_ATTRS = ["accept_charset", "http_equiv"];
|
|
206
|
-
|
|
207
|
-
export type ReactPyComponent = ComponentType<{ model: ReactPyVdom }>;
|
|
208
|
-
|
|
209
|
-
export type ReactPyVdom = {
|
|
210
|
-
tagName: string;
|
|
211
|
-
key?: string;
|
|
212
|
-
attributes?: { [key: string]: string };
|
|
213
|
-
children?: (ReactPyVdom | string)[];
|
|
214
|
-
error?: string;
|
|
215
|
-
eventHandlers?: { [key: string]: ReactPyVdomEventHandler };
|
|
216
|
-
importSource?: ReactPyVdomImportSource;
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
export type ReactPyVdomEventHandler = {
|
|
220
|
-
target: string;
|
|
221
|
-
preventDefault?: boolean;
|
|
222
|
-
stopPropagation?: boolean;
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
export type ReactPyVdomImportSource = {
|
|
226
|
-
source: string;
|
|
227
|
-
sourceType?: "URL" | "NAME";
|
|
228
|
-
fallback?: string | ReactPyVdom;
|
|
229
|
-
unmountBeforeUpdate?: boolean;
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
export type ReactPyModule = {
|
|
233
|
-
bind: (
|
|
234
|
-
node: HTMLElement,
|
|
235
|
-
context: ReactPyModuleBindingContext,
|
|
236
|
-
) => ReactPyModuleBinding;
|
|
237
|
-
} & { [key: string]: any };
|
|
238
|
-
|
|
239
|
-
export type ReactPyModuleBindingContext = {
|
|
240
|
-
sendMessage: ReactPyClient["sendMessage"];
|
|
241
|
-
onMessage: ReactPyClient["onMessage"];
|
|
242
|
-
};
|
|
243
|
-
|
|
244
|
-
export type ReactPyModuleBinding = {
|
|
245
|
-
create: (
|
|
246
|
-
type: any,
|
|
247
|
-
props?: any,
|
|
248
|
-
children?: (any | string | ReactPyVdom)[],
|
|
249
|
-
) => any;
|
|
250
|
-
render: (element: any) => void;
|
|
251
|
-
unmount: () => void;
|
|
252
|
-
};
|
|
253
|
-
|
|
254
|
-
export type BindImportSource = (
|
|
255
|
-
node: HTMLElement,
|
|
256
|
-
) => ImportSourceBinding | null;
|
|
257
|
-
|
|
258
|
-
export type ImportSourceBinding = {
|
|
259
|
-
render: (model: ReactPyVdom) => void;
|
|
260
|
-
unmount: () => void;
|
|
261
|
-
};
|