-
Notifications
You must be signed in to change notification settings - Fork 967
Expand file tree
/
Copy pathreactotron-core-client.ts
More file actions
598 lines (505 loc) · 15.8 KB
/
reactotron-core-client.ts
File metadata and controls
598 lines (505 loc) · 15.8 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
import WebSocket from "ws"
import type { Command, CommandTypeKey } from "reactotron-core-contract"
import validate from "./validate"
import logger from "./plugins/logger"
import image from "./plugins/image"
import benchmark from "./plugins/benchmark"
import stateResponses from "./plugins/state-responses"
import apiResponse from "./plugins/api-response"
import clear from "./plugins/clear"
import repl from "./plugins/repl"
import serialize from "./serialize"
import { start } from "./stopwatch"
import { ClientOptions } from "./client-options"
export type { ClientOptions }
export { assertHasLoggerPlugin } from "./plugins/logger"
export type { LoggerPlugin } from "./plugins/logger"
export { assertHasStateResponsePlugin, hasStateResponsePlugin } from "./plugins/state-responses"
export type { StateResponsePlugin } from "./plugins/state-responses"
export enum ArgType {
String = "string",
}
export interface CustomCommandArg {
name: string
type: ArgType
placeholder?: string
hidden?: boolean
}
// #region Plugin Types
export interface LifeCycleMethods {
onCommand?: (command: Command) => void
onConnect?: () => void
onDisconnect?: () => void
}
type AnyFunction = (...args: any[]) => any
export interface Plugin<Client> extends LifeCycleMethods {
features?: {
[key: string]: AnyFunction
}
onPlugin?: (client: Client) => void
}
export type PluginCreator<Client> = (client: Client) => Plugin<Client>
interface DisplayConfig {
name: string
value?: object | string | number | boolean | null | undefined
preview?: string
image?: string | { uri: string }
important?: boolean
}
interface ArgTypeMap {
[ArgType.String]: string
}
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void
? I
: never
export type CustomCommandArgs<Args extends CustomCommandArg[]> = UnionToIntersection<
Args extends Array<infer U>
? U extends CustomCommandArg
? { [K in U as U["name"]]: ArgTypeMap[U["type"]] }
: never
: never
>
export interface CustomCommand<Args extends CustomCommandArg[] = CustomCommandArg[]> {
id?: number
command: string
handler: (args?: CustomCommandArgs<Args>) => void
title?: string
description?: string
args?: Args
}
type ExtractFeatures<T> = T extends { features: infer U } ? U : never
type PluginFeatures<Client, P extends PluginCreator<Client>> = ExtractFeatures<ReturnType<P>>
export type InferFeaturesFromPlugins<
Client,
Plugins extends PluginCreator<Client>[],
> = UnionToIntersection<PluginFeatures<Client, Plugins[number]>>
type InferFeaturesFromPlugin<Client, P extends PluginCreator<Client>> = UnionToIntersection<
PluginFeatures<Client, P>
>
export interface ReactotronCore {
options: ClientOptions<this>
plugins: Plugin<this>[]
startTimer: () => () => number
close: () => void
send: <Type extends CommandTypeKey, Payload extends Command<Type>["payload"]>(
type: Type,
payload?: Payload,
important?: boolean
) => void
display: (config: DisplayConfig) => void
onCustomCommand: <Args extends CustomCommandArg[] = CustomCommand["args"]>(
config: CustomCommand<Args>
) => () => void | ((config: string, optHandler?: () => void) => () => void)
/**
* Set the configuration options.
*/
configure: (
options: ClientOptions<this>
) => ClientOptions<this>["plugins"] extends PluginCreator<this>[]
? this & InferFeaturesFromPlugins<this, ClientOptions<this>["plugins"]>
: this
use: <P extends PluginCreator<this>>(pluginCreator: P) => this & InferFeaturesFromPlugin<this, P>
connect: () => this
}
export type InferFeatures<
Client = ReactotronCore,
PC extends PluginCreator<Client> = PluginCreator<Client>,
> = PC extends (client: Client) => { features: infer U } ? U : never
export const corePlugins = [
image(),
logger(),
benchmark(),
stateResponses(),
apiResponse(),
clear(),
repl(),
] satisfies PluginCreator<ReactotronCore>[]
export type InferPluginsFromCreators<Client, PC extends PluginCreator<Client>[]> =
PC extends Array<infer P extends PluginCreator<Client>> ? ReturnType<P>[] : never
// #endregion
type CorePluginFeatures = InferFeaturesFromPlugins<ReactotronCore, typeof corePlugins>
export interface Reactotron extends ReactotronCore, CorePluginFeatures {}
// these are not for you.
const reservedFeatures = [
"configure",
"connect",
"connected",
"options",
"plugins",
"send",
"socket",
"startTimer",
"use",
] as const
type ReservedKeys = (typeof reservedFeatures)[number]
const isReservedFeature = (value: string): value is ReservedKeys =>
reservedFeatures.some((res) => res === value)
function emptyPromise() {
return Promise.resolve("")
}
export class ReactotronImpl implements ReactotronCore {
// the configuration options
options: ClientOptions<ReactotronCore>
/**
* Are we connected to a server?
*/
connected = false
/**
* The socket we're using.
*/
socket: WebSocket = null
/**
* Available plugins.
*/
plugins: Plugin<this>[] = []
/**
* Messages that need to be sent.
*/
sendQueue: string[] = []
/**
* Are we ready to start communicating?
*/
isReady = false
/**
* The last time we sent a message.
*/
lastMessageDate = new Date()
/**
* The registered custom commands
*/
customCommands: CustomCommand[] = []
/**
* The current ID for custom commands
*/
customCommandLatestId = 1
/**
* Starts a timer and returns a function you can call to stop it and return the elapsed time.
*/
startTimer = () => start()
/**
* Set the configuration options.
*/
configure(
options: ClientOptions<this>
): ClientOptions<this>["plugins"] extends PluginCreator<this>[]
? this & InferFeaturesFromPlugins<this, ClientOptions<this>["plugins"]>
: this {
// options get merged & validated before getting set
const newOptions = Object.assign(
{
createSocket: null,
host: "localhost",
port: 9090,
name: "reactotron-core-client",
secure: false,
plugins: corePlugins,
safeRecursion: true,
onCommand: () => null,
onConnect: () => null,
onDisconnect: () => null,
} satisfies ClientOptions<ReactotronCore>,
this.options,
options
)
validate(newOptions)
this.options = newOptions
// if we have plugins, let's add them here
if (Array.isArray(this.options.plugins)) {
this.options.plugins.forEach((p) => this.use(p))
}
return this as this & InferFeaturesFromPlugins<this, ClientOptions<this>["plugins"]>
}
close() {
this.connected = false
this.socket && this.socket.close && this.socket.close()
}
/**
* Connect to the Reactotron server.
*/
connect() {
this.connected = true
const {
createSocket,
secure,
host,
environment,
port,
name,
client = {},
getClientId,
} = this.options
const { onCommand, onConnect, onDisconnect } = this.options
// establish a connection to the server
const protocol = secure ? "wss" : "ws"
const socket = createSocket(`${protocol}://${host}:${port}`)
// fires when we talk to the server
const onOpen = () => {
// fire our optional onConnect handler
onConnect && onConnect()
// trigger our plugins onConnect
this.plugins.forEach((p) => p.onConnect && p.onConnect())
const getClientIdPromise = getClientId || emptyPromise
getClientIdPromise().then((clientId) => {
this.isReady = true
// introduce ourselves
this.send("client.intro", {
environment,
...client,
name,
clientId,
reactotronCoreClientVersion: "REACTOTRON_CORE_CLIENT_VERSION",
})
// flush the send queue
while (this.sendQueue.length > 0) {
const h = this.sendQueue[0]
this.sendQueue = this.sendQueue.slice(1)
this.socket.send(h)
}
})
}
// fires when we disconnect
const onClose = () => {
this.isReady = false
// trigger our disconnect handler
onDisconnect && onDisconnect()
// as well as the plugin's onDisconnect
this.plugins.forEach((p) => p.onDisconnect && p.onDisconnect())
}
const decodeCommandData = (data: unknown) => {
if (typeof data === "string") {
return JSON.parse(data)
}
if (Buffer.isBuffer(data)) {
return JSON.parse(data.toString())
}
return data
}
// fires when we receive a command, just forward it off
const onMessage = (data: any) => {
const command = decodeCommandData(data)
// trigger our own command handler
onCommand && onCommand(command)
// trigger our plugins onCommand
this.plugins.forEach((p) => p.onCommand && p.onCommand(command))
// trigger our registered custom commands
if (command.type === "custom") {
this.customCommands
.filter((cc) => {
if (typeof command.payload === "string") {
return cc.command === command.payload
}
return cc.command === command.payload.command
})
.forEach((cc) =>
cc.handler(typeof command.payload === "object" ? command.payload.args : undefined)
)
} else if (command.type === "setClientId") {
this.options.setClientId && this.options.setClientId(command.payload)
}
}
// this is ws style from require('ws') on node js
if ("on" in socket && socket.on) {
const nodeWebSocket = socket as WebSocket
nodeWebSocket.on("open", onOpen)
nodeWebSocket.on("close", onClose)
nodeWebSocket.on("message", onMessage)
// assign the socket to the instance
this.socket = socket
} else {
// this is a browser
const browserWebSocket = socket as WebSocket
socket.onopen = onOpen
socket.onclose = onClose
socket.onmessage = (evt) => onMessage(evt.data)
// assign the socket to the instance
this.socket = browserWebSocket
}
return this
}
/**
* Sends a command to the server
*/
send = <Type extends CommandTypeKey, Payload extends Command<Type>["payload"]>(
type: Type,
payload?: Payload,
important?: boolean
) => {
// set the timing info
const date = new Date()
let deltaTime = date.getTime() - this.lastMessageDate.getTime()
// glitches in the matrix
if (deltaTime < 0) {
deltaTime = 0
}
this.lastMessageDate = date
const fullMessage = {
type,
payload,
important: !!important,
date: date.toISOString(),
deltaTime,
}
const serializedMessage = serialize(fullMessage, this.options.proxyHack)
if (this.isReady) {
// send this command
try {
this.socket.send(serializedMessage)
} catch {
this.isReady = false
console.log("An error occurred communicating with reactotron. Please reload your app")
}
} else {
// queue it up until we can connect
this.sendQueue.push(serializedMessage)
}
}
/**
* Sends a custom command to the server to displays nicely.
*/
display(config: DisplayConfig) {
const { name, value, preview, image: img, important = false } = config
const payload = {
name,
value: value || null,
preview: preview || null,
image: img || null,
}
this.send("display", payload, important)
}
/**
* Client libraries can hijack this to report errors.
*/
reportError(this: any, error: Error) {
this.error(error)
}
/**
* Adds a plugin to the system
*/
use(pluginCreator: PluginCreator<this>): this & PluginFeatures<this, typeof pluginCreator> {
// we're supposed to be given a function
if (typeof pluginCreator !== "function") {
throw new Error("plugins must be a function")
}
// execute it immediately passing the send function
const plugin = pluginCreator.bind(this)(this) as ReturnType<typeof pluginCreator>
// ensure we get an Object-like creature back
if (typeof plugin !== "object") {
throw new Error("plugins must return an object")
}
// do we have features to mixin?
if (plugin.features) {
// validate
if (typeof plugin.features !== "object") {
throw new Error("features must be an object")
}
// here's how we're going to inject these in
const inject = (key: string) => {
// grab the function
const featureFunction = plugin.features[key]
// only functions may pass
if (typeof featureFunction !== "function") {
throw new Error(`feature ${key} is not a function`)
}
// ditch reserved names
if (isReservedFeature(key)) {
throw new Error(`feature ${key} is a reserved name`)
}
// ok, let's glue it up... and lose all respect from elite JS champions.
this[key] = featureFunction
}
// let's inject
Object.keys(plugin.features).forEach((key) => inject(key))
}
// add it to the list
this.plugins.push(plugin)
// call the plugins onPlugin
plugin.onPlugin && typeof plugin.onPlugin === "function" && plugin.onPlugin.bind(this)(this)
// chain-friendly
return this as this & PluginFeatures<this, typeof pluginCreator>
}
onCustomCommand(config: CustomCommand | string, optHandler?: () => void): () => void {
let command: string
let handler: () => void
let title: string
let description: string
let args: CustomCommandArg[]
if (typeof config === "string") {
command = config
handler = optHandler
} else {
command = config.command
handler = config.handler
title = config.title
description = config.description
args = config.args
}
// Validations
// Make sure there is a command
if (!command) {
throw new Error("A command is required")
}
// Make sure there is a handler
if (!handler) {
throw new Error(`A handler is required for command "${command}"`)
}
// Make sure the command doesn't already exist
const existingCommands = this.customCommands.filter((cc) => cc.command === command)
if (existingCommands.length > 0) {
existingCommands.forEach((command) => {
this.customCommands = this.customCommands.filter((cc) => cc.id !== command.id)
this.send("customCommand.unregister", {
id: command.id,
command: command.command,
})
})
}
if (args) {
const argNames = []
args.forEach((arg) => {
if (!arg.name) {
throw new Error(`A arg on the command "${command}" is missing a name`)
}
if (argNames.indexOf(arg.name) > -1) {
throw new Error(
`A arg with the name "${arg.name}" already exists in the command "${command}"`
)
}
argNames.push(arg.name)
})
}
// Create this command handlers object
const customHandler: CustomCommand = {
id: this.customCommandLatestId,
command,
handler,
title,
description,
args,
}
// Increment our id counter
this.customCommandLatestId += 1
// Add it to our array
this.customCommands.push(customHandler)
this.send("customCommand.register", {
id: customHandler.id,
command: customHandler.command,
title: customHandler.title,
description: customHandler.description,
args: customHandler.args,
})
return () => {
this.customCommands = this.customCommands.filter((cc) => cc.id !== customHandler.id)
this.send("customCommand.unregister", {
id: customHandler.id,
command: customHandler.command,
})
}
}
}
// convenience factory function
export function createClient<Client extends ReactotronCore = ReactotronCore>(
options?: ClientOptions<Client>
) {
const client = new ReactotronImpl()
return client.configure(options as unknown) as unknown as Client
}