@jacksontian/mwt 1.0.0 → 1.2.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.
@@ -4,15 +4,18 @@ export class WSClient {
4
4
  this.listeners = new Map(); // id -> { onData, onExit }
5
5
  this.pendingMessages = [];
6
6
  this.reconnectDelay = 1000;
7
+ this.reconnectAttempts = 0;
7
8
  this.onReconnectCallbacks = [];
8
9
  this.onSessionRestoreCallback = null;
9
10
  this.onRestoreCompleteCallback = null;
11
+ this.onAlreadyConnectedCallback = null;
12
+ this.onGiveUpCallback = null;
10
13
  this.sessionId = this._getOrCreateSessionId();
11
14
  this.connect();
12
15
  }
13
16
 
14
17
  _getOrCreateSessionId() {
15
- const key = 'myterminal-session-id';
18
+ const key = 'mwt-session-id';
16
19
  let id = localStorage.getItem(key);
17
20
  if (!id) {
18
21
  id = crypto.randomUUID();
@@ -27,6 +30,7 @@ export class WSClient {
27
30
 
28
31
  this.ws.onopen = () => {
29
32
  this.reconnectDelay = 1000;
33
+ this.reconnectAttempts = 0;
30
34
  // flush pending messages
31
35
  for (const msg of this.pendingMessages) {
32
36
  this.ws.send(msg);
@@ -65,7 +69,7 @@ export class WSClient {
65
69
  }
66
70
 
67
71
  const listener = this.listeners.get(msg.id);
68
- if (!listener) return;
72
+ if (!listener) {return;}
69
73
 
70
74
  if (msg.type === 'data' && listener.onData) {
71
75
  listener.onData(msg.data);
@@ -74,11 +78,20 @@ export class WSClient {
74
78
  }
75
79
  };
76
80
 
77
- this.ws.onclose = () => {
81
+ this.ws.onclose = (event) => {
82
+ if (event.code === 4409) {
83
+ if (this.onAlreadyConnectedCallback) {this.onAlreadyConnectedCallback();}
84
+ return;
85
+ }
86
+ this.reconnectAttempts += 1;
87
+ if (this.reconnectAttempts > 5) {
88
+ if (this.onGiveUpCallback) {this.onGiveUpCallback();}
89
+ return;
90
+ }
78
91
  setTimeout(() => {
79
92
  this.reconnectDelay = Math.min(this.reconnectDelay * 1.5, 10000);
80
93
  this.connect();
81
- for (const cb of this.onReconnectCallbacks) cb();
94
+ for (const cb of this.onReconnectCallbacks) {cb();}
82
95
  }, this.reconnectDelay);
83
96
  };
84
97
  }
@@ -109,12 +122,12 @@ export class WSClient {
109
122
  }
110
123
 
111
124
  onTerminalData(id, callback) {
112
- if (!this.listeners.has(id)) this.listeners.set(id, {});
125
+ if (!this.listeners.has(id)) {this.listeners.set(id, {});}
113
126
  this.listeners.get(id).onData = callback;
114
127
  }
115
128
 
116
129
  onTerminalExit(id, callback) {
117
- if (!this.listeners.has(id)) this.listeners.set(id, {});
130
+ if (!this.listeners.has(id)) {this.listeners.set(id, {});}
118
131
  this.listeners.get(id).onExit = callback;
119
132
  }
120
133
 
@@ -133,4 +146,12 @@ export class WSClient {
133
146
  onRestoreComplete(callback) {
134
147
  this.onRestoreCompleteCallback = callback;
135
148
  }
149
+
150
+ onAlreadyConnected(callback) {
151
+ this.onAlreadyConnectedCallback = callback;
152
+ }
153
+
154
+ onGiveUp(callback) {
155
+ this.onGiveUpCallback = callback;
156
+ }
136
157
  }