@f3liz/rescript-misskey-api 0.6.6 → 0.6.8

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.
@@ -2,6 +2,7 @@
2
2
 
3
3
  import * as Stdlib_Dict from "@rescript/runtime/lib/es6/Stdlib_Dict.js";
4
4
  import * as StreamProtocol from "./StreamProtocol.mjs";
5
+ import * as MisskeyWebSocket from "../bindings/MisskeyWebSocket.mjs";
5
6
  import * as Primitive_object from "@rescript/runtime/lib/es6/Primitive_object.js";
6
7
  import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
7
8
  import * as StreamConnection from "./StreamConnection.mjs";
@@ -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 (MisskeyWebSocket.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.8",
4
4
  "description": "ReScript bindings for Misskey API using OpenAPI code generation",
5
5
  "keywords": [
6
6
  "misskey",
@@ -1,4 +1,4 @@
1
- // WebSocket.res - Browser WebSocket API bindings for ReScript
1
+ // MisskeyWebSocket.res - Browser WebSocket API bindings for ReScript
2
2
 
3
3
  // WebSocket ready state constants
4
4
  module ReadyState = {
@@ -40,11 +40,11 @@ let sendTyped = (stream: stream, type_: string, payload: JSON.t): unit => {
40
40
  let obj = Dict.make()
41
41
  obj->Dict.set("type", type_->JSON.Encode.string)
42
42
  obj->Dict.set("body", payload)
43
- stream.ws->WebSocket.send(obj->JSON.Encode.object->JSON.stringify)
43
+ stream.ws->MisskeyWebSocket.send(obj->JSON.Encode.object->JSON.stringify)
44
44
  }
45
45
 
46
46
  let sendRaw = (stream: stream, payload: JSON.t): unit => {
47
- stream.ws->WebSocket.send(payload->JSON.stringify)
47
+ stream.ws->MisskeyWebSocket.send(payload->JSON.stringify)
48
48
  }
49
49
 
50
50
  // Connection management
@@ -1,7 +1,7 @@
1
1
  // StreamClient.res - Native ReScript WebSocket streaming client
2
2
  // Pure ReScript implementation replacing misskey-js Stream class
3
3
 
4
- module WS = WebSocket
4
+ module WS = MisskeyWebSocket
5
5
  module Protocol = StreamProtocol
6
6
  module Connection = StreamConnection
7
7
 
@@ -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