@f3liz/rescript-misskey-api 0.6.6 → 0.6.7

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.
@@ -1,5 +1,6 @@
1
1
  // Generated by ReScript, PLEASE EDIT WITH CARE
2
2
 
3
+ import * as WebSocket from "../bindings/WebSocket.mjs";
3
4
  import * as Stdlib_Dict from "@rescript/runtime/lib/es6/Stdlib_Dict.js";
4
5
  import * as StreamProtocol from "./StreamProtocol.mjs";
5
6
  import * as Primitive_object from "@rescript/runtime/lib/es6/Primitive_object.js";
@@ -25,11 +26,17 @@ function make(origin, credential, param) {
25
26
  idCounter: 0,
26
27
  broadcastHandlers: {},
27
28
  onConnectedCallback: undefined,
28
- onDisconnectedCallback: undefined
29
+ onDisconnectedCallback: undefined,
30
+ pendingMessages: []
29
31
  };
30
32
  ws.addEventListener("open", _event => {
31
33
  let isReconnect = stream.state === "reconnecting";
32
34
  stream.state = "connected";
35
+ let pending = stream.pendingMessages;
36
+ stream.pendingMessages = [];
37
+ pending.forEach(msg => {
38
+ stream.ws.send(msg);
39
+ });
33
40
  let cb = stream.onConnectedCallback;
34
41
  if (cb !== undefined) {
35
42
  cb();
@@ -99,7 +106,11 @@ function make(origin, credential, param) {
99
106
 
100
107
  function send(stream, msg) {
101
108
  let serialized = StreamProtocol.serializeOutgoing(msg);
102
- stream.ws.send(serialized);
109
+ if (WebSocket.isOpen(stream.ws) && stream.pendingMessages.length === 0) {
110
+ stream.ws.send(serialized);
111
+ } else {
112
+ stream.pendingMessages = stream.pendingMessages.concat([serialized]);
113
+ }
103
114
  }
104
115
 
105
116
  function ping(stream) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@f3liz/rescript-misskey-api",
3
- "version": "0.6.6",
3
+ "version": "0.6.7",
4
4
  "description": "ReScript bindings for Misskey API using OpenAPI code generation",
5
5
  "keywords": [
6
6
  "misskey",
@@ -29,6 +29,7 @@ type t = {
29
29
  broadcastHandlers: Dict.t<JSON.t => unit>,
30
30
  mutable onConnectedCallback: option<unit => unit>,
31
31
  mutable onDisconnectedCallback: option<unit => unit>,
32
+ mutable pendingMessages: array<string>,
32
33
  }
33
34
 
34
35
  // Generate unique ID for connections
@@ -54,6 +55,7 @@ let make = (~origin: string, ~credential: option<string>=?, ()): t => {
54
55
  broadcastHandlers: Dict.make(),
55
56
  onConnectedCallback: None,
56
57
  onDisconnectedCallback: None,
58
+ pendingMessages: [],
57
59
  }
58
60
 
59
61
  // Set up WebSocket event handlers
@@ -61,6 +63,11 @@ let make = (~origin: string, ~credential: option<string>=?, ()): t => {
61
63
  let isReconnect = stream.state == #reconnecting
62
64
  stream.state = #connected
63
65
 
66
+ // Flush any pending messages
67
+ let pending = stream.pendingMessages
68
+ stream.pendingMessages = []
69
+ pending->Array.forEach(msg => stream.ws->WS.send(msg))
70
+
64
71
  // Call user callback
65
72
  switch stream.onConnectedCallback {
66
73
  | Some(cb) => cb()
@@ -148,10 +155,15 @@ let make = (~origin: string, ~credential: option<string>=?, ()): t => {
148
155
  stream
149
156
  }
150
157
 
151
- // Send a message
158
+ // Send a message (queues if WebSocket not yet open)
152
159
  let send = (stream: t, msg: Protocol.outgoingMessage): unit => {
153
160
  let serialized = Protocol.serializeOutgoing(msg)
154
- stream.ws->WS.send(serialized)
161
+ if WS.isOpen(stream.ws) && Array.length(stream.pendingMessages) == 0 {
162
+ stream.ws->WS.send(serialized)
163
+ } else {
164
+ // Queue for when connection opens or if there are already pending messages
165
+ stream.pendingMessages = stream.pendingMessages->Array.concat([serialized])
166
+ }
155
167
  }
156
168
 
157
169
  // Ping the server