@mentra/miniapp 3.4.0-dev.1011 → 3.4.0-dev.1020

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.
@@ -20,6 +20,11 @@ const DEFAULT_URL = "ws://127.0.0.1:8765"
20
20
  export class LocalSocketTransport implements Transport {
21
21
  private url: string
22
22
  private ws: WebSocket | null = null
23
+ /**
24
+ * The socket an open() in progress is connecting. A later open() or close()
25
+ * abandons it, so a socket that connects late cannot replace the active one.
26
+ */
27
+ private connecting: WebSocket | null = null
23
28
  private messageHandler: TransportMessageHandler | null = null
24
29
  private disconnectHandler: TransportDisconnectHandler | null = null
25
30
 
@@ -32,9 +37,11 @@ export class LocalSocketTransport implements Transport {
32
37
  if (typeof WebSocket === "undefined") {
33
38
  throw new Error("LocalSocketTransport: browser WebSocket global not available")
34
39
  }
40
+ this.abandonConnecting()
35
41
 
36
42
  await new Promise<void>((resolve, reject) => {
37
43
  const ws = new WebSocket(this.url)
44
+ this.connecting = ws
38
45
  let settled = false
39
46
  const settle = (fn: () => void) => {
40
47
  if (settled) return
@@ -43,6 +50,11 @@ export class LocalSocketTransport implements Transport {
43
50
  }
44
51
 
45
52
  ws.onopen = () => {
53
+ if (this.connecting !== ws) {
54
+ settle(() => reject(new Error("LocalSocketTransport: open was superseded")))
55
+ return
56
+ }
57
+ this.connecting = null
46
58
  this.ws = ws
47
59
  settle(() => resolve())
48
60
  }
@@ -52,11 +64,16 @@ export class LocalSocketTransport implements Transport {
52
64
  )
53
65
  }
54
66
  ws.onmessage = (ev: MessageEvent) => {
67
+ if (this.ws !== ws) return
55
68
  const data = ev.data
56
69
  if (typeof data === "string") this.messageHandler?.(data)
57
70
  }
58
71
  ws.onclose = (ev) => {
59
- if (this.ws === ws) this.ws = null
72
+ if (this.connecting === ws) this.connecting = null
73
+ settle(() => reject(new Error(`LocalSocketTransport: closed before opening: ${ev.code} ${ev.reason}`)))
74
+ // Only the active socket's close is a disconnect; an abandoned one was never in use.
75
+ if (this.ws !== ws) return
76
+ this.ws = null
60
77
  this.disconnectHandler?.(`closed: ${ev.code} ${ev.reason}`)
61
78
  }
62
79
  })
@@ -78,6 +95,7 @@ export class LocalSocketTransport implements Transport {
78
95
  }
79
96
 
80
97
  close(): void {
98
+ this.abandonConnecting()
81
99
  if (!this.ws) return
82
100
  try {
83
101
  this.ws.close()
@@ -90,4 +108,15 @@ export class LocalSocketTransport implements Transport {
90
108
  isOpen(): boolean {
91
109
  return !!this.ws && this.ws.readyState === WebSocket.OPEN
92
110
  }
111
+
112
+ private abandonConnecting(): void {
113
+ const ws = this.connecting
114
+ this.connecting = null
115
+ if (!ws) return
116
+ try {
117
+ ws.close()
118
+ } catch {
119
+ // ignore
120
+ }
121
+ }
93
122
  }
@@ -12,7 +12,10 @@ export type TransportMessageHandler = (raw: string) => void
12
12
  export type TransportDisconnectHandler = (reason: string) => void
13
13
 
14
14
  export interface Transport {
15
- /** Establish whatever connection the transport needs. May be a no-op. */
15
+ /**
16
+ * Establish whatever connection the transport needs. May be a no-op. Resolves
17
+ * at once when already open: a retried handshake calls it again.
18
+ */
16
19
  open(): Promise<void>
17
20
  /** Send a serialized envelope. */
18
21
  send(raw: string): void