@noita-ts/pollnet 0.3.7 → 0.4.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.
package/dist/async.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /** @noSelfInFile */
1
2
  import pollnet from ".";
2
3
  export declare namespace compat {
3
4
  /**
@@ -48,7 +49,6 @@ export declare class HttpResponse {
48
49
  headers: string;
49
50
  body: string;
50
51
  constructor(status: number, headers: string, body: string);
51
- /** @noSelf */
52
52
  static read(socket: pollnet.Socket): HttpResponse;
53
53
  /**
54
54
  * Parse the raw header block into a map.
@@ -73,3 +73,20 @@ export declare class HttpClient {
73
73
  */
74
74
  post(url: string, headers?: Record<string, string | string[]> | string, body?: string): Promise<HttpResponse>;
75
75
  }
76
+ /**
77
+ * Async WebSocket client built on top of an {@link AsyncReactor}.
78
+ * Messages are delivered via the `onmessage` callback, and errors via
79
+ * `onerror`.
80
+ * Call {@link send} to send a message, and {@link close} to close the
81
+ * connection and cleanup.
82
+ */
83
+ export declare class WebSocket {
84
+ private socket;
85
+ timeout: number;
86
+ onmessage: ((msg: string) => void) | null;
87
+ onerror: ((msg: string) => void) | null;
88
+ constructor(reactor: AsyncReactor, url: string);
89
+ send(message: string): void;
90
+ send_binary(data: string): void;
91
+ close(): void;
92
+ }
package/dist/async.lua CHANGED
@@ -20,7 +20,7 @@ do
20
20
  --- Run `body` inside a reactor coroutine and get its completion as a Promise.
21
21
  -- `body` may freely use socket.await() / other yield-based pollnet APIs.
22
22
  -- Errors inside body reject the promise.
23
- compat.asyncify = function(____, reactor, body)
23
+ compat.asyncify = function(reactor, body)
24
24
  return __TS__New(
25
25
  __TS__Promise,
26
26
  function(____, resolve, reject)
@@ -32,7 +32,7 @@ do
32
32
  local ____try, ____hasReturned = pcall(function()
33
33
  resolve(
34
34
  nil,
35
- body(nil)
35
+ body()
36
36
  )
37
37
  end)
38
38
  if not ____try then
@@ -46,7 +46,7 @@ do
46
46
  --- Parks the *current coroutine* until the promise settles, then returns its
47
47
  -- value or rethrows its rejection.
48
48
  -- Only call from inside a reactor coroutine (`reactor.run(() => { .. HERE .. })`).
49
- compat.yieldPromise = function(____, p)
49
+ compat.yieldPromise = function(p)
50
50
  local done = false
51
51
  local ok = false
52
52
  local result
@@ -87,14 +87,14 @@ function AsyncReactor.prototype.poll(self)
87
87
  return self.reactor:update()
88
88
  end
89
89
  function AsyncReactor.prototype.spawn(self, body)
90
- return ____exports.compat:asyncify(self.reactor, body)
90
+ return ____exports.compat.asyncify(self.reactor, body)
91
91
  end
92
92
  function AsyncReactor.prototype.runServer(self, server_sock, client_body)
93
93
  server_sock:on_connection(function(client_sock, addr)
94
94
  self.reactor:run(function()
95
- local r = client_body(nil, client_sock, addr)
95
+ local r = client_body(client_sock, addr)
96
96
  if __TS__InstanceOf(r, __TS__Promise) then
97
- ____exports.compat:yieldPromise(r)
97
+ ____exports.compat.yieldPromise(r)
98
98
  end
99
99
  end)
100
100
  end)
@@ -111,7 +111,7 @@ function HttpResponse.prototype.____constructor(self, status, headers, body)
111
111
  self.headers = headers
112
112
  self.body = body
113
113
  end
114
- function HttpResponse.read(socket)
114
+ function HttpResponse.read(self, socket)
115
115
  local msgs, ____error = socket:await_n(3)
116
116
  if not msgs then
117
117
  error(
@@ -159,9 +159,91 @@ function HttpClient.prototype.____constructor(self, reactor)
159
159
  self.reactor = reactor
160
160
  end
161
161
  function HttpClient.prototype.get(self, url, headers)
162
- return self.reactor:spawn(function() return ____exports.HttpResponse.read(pollnet.http_get(url, headers, false)) end)
162
+ return self.reactor:spawn(function() return ____exports.HttpResponse:read(pollnet.http_get(url, headers, false)) end)
163
163
  end
164
164
  function HttpClient.prototype.post(self, url, headers, body)
165
- return self.reactor:spawn(function() return ____exports.HttpResponse.read(pollnet.http_post(url, headers, body, false)) end)
165
+ return self.reactor:spawn(function() return ____exports.HttpResponse:read(pollnet.http_post(url, headers, body, false)) end)
166
+ end
167
+ --- Async WebSocket client built on top of an {@link AsyncReactor}.
168
+ -- Messages are delivered via the `onmessage` callback, and errors via
169
+ -- `onerror`.
170
+ -- Call {@link send} to send a message, and {@link close} to close the
171
+ -- connection and cleanup.
172
+ ____exports.WebSocket = __TS__Class()
173
+ local WebSocket = ____exports.WebSocket
174
+ WebSocket.name = "WebSocket"
175
+ function WebSocket.prototype.____constructor(self, reactor, url)
176
+ self.timeout = 3600
177
+ self.onmessage = nil
178
+ self.onerror = nil
179
+ self.socket = pollnet.open_ws(url)
180
+ reactor:spawn(function()
181
+ local timer = 0
182
+ while not not self.socket do
183
+ do
184
+ local ok = true
185
+ local msg
186
+ while true do
187
+ ok, msg = self.socket:poll()
188
+ if not ok or not msg then
189
+ break
190
+ end
191
+ timer = 0
192
+ local ____opt_0 = self.onmessage
193
+ if ____opt_0 ~= nil then
194
+ ____opt_0(msg)
195
+ end
196
+ if not self.socket then
197
+ return
198
+ end
199
+ end
200
+ local ____ok_2 = ok
201
+ if ____ok_2 then
202
+ timer = timer + 1
203
+ ____ok_2 = timer < self.timeout
204
+ end
205
+ if ____ok_2 then
206
+ coroutine.yield()
207
+ goto __continue39
208
+ end
209
+ timer = 0
210
+ self.socket:close()
211
+ local ____opt_3 = self.onerror
212
+ if ____opt_3 ~= nil then
213
+ ____opt_3(msg or "timeout")
214
+ end
215
+ do
216
+ local i = 0
217
+ while i < 60 do
218
+ coroutine.yield()
219
+ i = i + 1
220
+ end
221
+ end
222
+ if not not self.socket then
223
+ self.socket = pollnet.open_ws(url)
224
+ end
225
+ end
226
+ ::__continue39::
227
+ end
228
+ end)
229
+ end
230
+ function WebSocket.prototype.send(self, message)
231
+ local ____opt_5 = self.socket
232
+ if ____opt_5 ~= nil then
233
+ ____opt_5:send(message)
234
+ end
235
+ end
236
+ function WebSocket.prototype.send_binary(self, data)
237
+ local ____opt_7 = self.socket
238
+ if ____opt_7 ~= nil then
239
+ ____opt_7:send_binary(data)
240
+ end
241
+ end
242
+ function WebSocket.prototype.close(self)
243
+ local ____opt_9 = self.socket
244
+ if ____opt_9 ~= nil then
245
+ ____opt_9:close()
246
+ end
247
+ self.socket = nil
166
248
  end
167
249
  return ____exports
package/dist/index.d.ts CHANGED
@@ -146,11 +146,17 @@ declare namespace pollnet {
146
146
  callback?: (sock: Socket, addr: string) => void,
147
147
  ): this;
148
148
 
149
- /** Sets a connection handler */
149
+ /** Sets a connection handler for server sockets */
150
150
  on_connection(callback: (sock: Socket, addr: string) => void): this;
151
151
 
152
- /** Polls for socket events */
153
- poll(): LuaMultiReturn<[true, undefined]> | LuaMultiReturn<[false, string]>;
152
+ /**
153
+ * Polls for socket events.
154
+ * Returns [ok, message], where on success the message may or may not be
155
+ * available (non-blocking), and on failure the message is an error string.
156
+ */
157
+ poll():
158
+ | LuaMultiReturn<[true, string | undefined]>
159
+ | LuaMultiReturn<[false, string]>;
154
160
 
155
161
  /** Awaits a message (coroutine.yield) */
156
162
  await():
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noita-ts/pollnet",
3
- "version": "0.3.7",
3
+ "version": "0.4.0",
4
4
  "files": [
5
5
  "dist/**/*.{lua,dll,d.ts}",
6
6
  "LICENSE-pollnet"