@poolse/sdk 2.26.0 → 3.1.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/index.cjs CHANGED
@@ -106,6 +106,7 @@ var AuthError = class extends ApiError {
106
106
  };
107
107
 
108
108
  // src/realtime/realtime.ts
109
+ var TOKEN_REFRESH_ON_ERROR_MS = 3e4;
109
110
  var PoolseRealtime = class {
110
111
  config;
111
112
  tokenCache;
@@ -115,6 +116,8 @@ var PoolseRealtime = class {
115
116
  conversations = /* @__PURE__ */ new Map();
116
117
  userChannel = null;
117
118
  status = "idle";
119
+ paused = false;
120
+ lastTokenRefreshMs = 0;
118
121
  statusListeners = /* @__PURE__ */ new Set();
119
122
  constructor(config, tokenCache, opts = {}) {
120
123
  this.config = config;
@@ -170,9 +173,15 @@ var PoolseRealtime = class {
170
173
  });
171
174
  socket.onError((err) => {
172
175
  this.setStatus("reconnecting");
176
+ if (Date.now() - this.lastTokenRefreshMs > TOKEN_REFRESH_ON_ERROR_MS) {
177
+ this.lastTokenRefreshMs = Date.now();
178
+ void this.tokenCache.getToken({ forceRefresh: true }).catch(() => {
179
+ });
180
+ }
173
181
  this.config.onSocketError?.(new PoolseError(`socket error: ${String(err)}`));
174
182
  });
175
183
  this.socket = socket;
184
+ this.lastTokenRefreshMs = Date.now();
176
185
  void this.tokenCache.getToken().catch((err) => {
177
186
  this.config.onSocketError?.(
178
187
  new PoolseError(`token fetch failed before socket open: ${String(err)}`)
@@ -181,8 +190,35 @@ var PoolseRealtime = class {
181
190
  socket.connect();
182
191
  });
183
192
  }
193
+ /**
194
+ * Close the socket but keep every channel handle and its listeners
195
+ * (for an app going to the background). `resume()` reopens it and
196
+ * Phoenix rejoins the channels. Events sent in between are not
197
+ * replayed; the React hooks reload from REST on reconnect.
198
+ */
199
+ pause() {
200
+ if (!this.socket || this.paused) return;
201
+ this.paused = true;
202
+ this.setStatus("closed");
203
+ this.socket.disconnect();
204
+ }
205
+ /** Reopen a socket closed by `pause()`, with a fresh token. */
206
+ resume() {
207
+ const socket = this.socket;
208
+ if (!socket || !this.paused) return;
209
+ this.paused = false;
210
+ this.setStatus("connecting");
211
+ void this.tokenCache.getToken().catch((err) => {
212
+ this.config.onSocketError?.(
213
+ new PoolseError(`token fetch failed before socket open: ${String(err)}`)
214
+ );
215
+ }).finally(() => {
216
+ if (this.socket === socket && !this.paused) socket.connect();
217
+ });
218
+ }
184
219
  /** Close the socket and tear down every joined channel. */
185
220
  disconnect() {
221
+ this.paused = false;
186
222
  this.setStatus("closed");
187
223
  this.conversations.forEach((c) => c._destroy());
188
224
  this.conversations.clear();
@@ -206,7 +242,10 @@ var PoolseRealtime = class {
206
242
  throw new PoolseError("socket not initialised \u2014 call connect() first");
207
243
  }
208
244
  const channel = this.socket.channel(`conversation:${conversationId}`, {});
209
- const handle = new ConversationChannel(conversationId, channel);
245
+ const handle = new ConversationChannel(conversationId, channel, () => {
246
+ if (this.conversations.get(conversationId) === handle)
247
+ this.conversations.delete(conversationId);
248
+ });
210
249
  this.conversations.set(conversationId, handle);
211
250
  handle._join();
212
251
  return handle;
@@ -255,9 +294,15 @@ var ConversationChannel = class {
255
294
  // state here and replay it on subscribe.
256
295
  presenceState = {};
257
296
  presenceStateSeen = false;
258
- constructor(conversationId, channel) {
297
+ closedListeners = /* @__PURE__ */ new Set();
298
+ constructor(conversationId, channel, onGone) {
259
299
  this.conversationId = conversationId;
260
300
  this.channel = channel;
301
+ channel.onClose(() => {
302
+ onGone?.();
303
+ this.closedListeners.forEach((l) => l());
304
+ this.closedListeners.clear();
305
+ });
261
306
  channel.on("presence_state", (payload) => {
262
307
  this.presenceState = payload ?? {};
263
308
  this.presenceStateSeen = true;
@@ -332,6 +377,18 @@ var ConversationChannel = class {
332
377
  onPresenceDiff(fn) {
333
378
  return this.subscribe("presence_diff", fn);
334
379
  }
380
+ /**
381
+ * The channel closed for good: the server removed you from the
382
+ * conversation, or `leave()` / `disconnect()` was called. No more
383
+ * events arrive on this handle; `realtime.conversation(id)` returns a
384
+ * fresh one.
385
+ */
386
+ onClosed(fn) {
387
+ this.closedListeners.add(fn);
388
+ return () => {
389
+ this.closedListeners.delete(fn);
390
+ };
391
+ }
335
392
  /** Current presence snapshot for sync access — usually used in tests. */
336
393
  getPresenceState() {
337
394
  return this.presenceState;
@@ -374,6 +431,8 @@ var UserChannel = class {
374
431
  mentionBound = false;
375
432
  conversationCreatedBound = false;
376
433
  conversationUpdatedBound = false;
434
+ // One Phoenix binding per event, fanned out — as in ConversationChannel.
435
+ listeners = /* @__PURE__ */ new Map();
377
436
  constructor(userId, channel) {
378
437
  this.userId = userId;
379
438
  this.channel = channel;
@@ -442,17 +501,47 @@ var UserChannel = class {
442
501
  this.conversationUpdatedListeners.delete(fn);
443
502
  };
444
503
  }
504
+ /**
505
+ * Your ring for a call ended — answered on another device, declined,
506
+ * cancelled by the caller or timed out. Stop ringing on this device.
507
+ */
508
+ onRingEnded(fn) {
509
+ return this.subscribe("call:ring_ended", fn);
510
+ }
511
+ /**
512
+ * A call you were part of (rung or joined) is over. Arrives on the
513
+ * user channel, which the app stays joined to even after leaving the
514
+ * conversation's channel mid-call.
515
+ */
516
+ onCallFinished(fn) {
517
+ return this.subscribe("call:finished", fn);
518
+ }
445
519
  /** @internal */
446
520
  _join() {
447
521
  this.channel.join();
448
522
  }
449
523
  /** @internal */
450
524
  _destroy() {
525
+ this.listeners.clear();
451
526
  this.mentionListeners.clear();
452
527
  this.conversationCreatedListeners.clear();
453
528
  this.conversationUpdatedListeners.clear();
454
529
  this.channel.leave();
455
530
  }
531
+ subscribe(event, fn) {
532
+ let set = this.listeners.get(event);
533
+ if (!set) {
534
+ set = /* @__PURE__ */ new Set();
535
+ this.listeners.set(event, set);
536
+ this.channel.on(event, (payload) => {
537
+ this.listeners.get(event)?.forEach((l) => l(payload));
538
+ });
539
+ }
540
+ set.add(fn);
541
+ return () => {
542
+ set.delete(fn);
543
+ };
544
+ }
456
545
  };
457
546
  function deriveWsUrl(apiUrl) {
458
547
  return apiUrl.replace(/^http/, "ws");
@@ -504,7 +593,7 @@ var AttachmentsResource = class {
504
593
  * });
505
594
  * await chat.conversations.one(convId).messages.send({
506
595
  * body: 'Look at this!',
507
- * custom_data: { attachment_id: att.id },
596
+ * attachment_ids: [att.id],
508
597
  * });
509
598
  * ```
510
599
  *
@@ -1026,10 +1115,10 @@ var CallTokensResource = class {
1026
1115
  /**
1027
1116
  * POST /v1/conversations/:conversationId/call-token
1028
1117
  *
1029
- * Resolves `null` when the deployment has no SFU configured (503).
1030
- * That is deliberately not an error — the caller falls back to the
1031
- * peer-to-peer mesh, which is how every call worked before the SFU
1032
- * existed. A 403 still throws: that one really is a refusal.
1118
+ * Resolves `null` when the deployment has no SFU configured (503):
1119
+ * calls are unavailable there, which the caller shows as such rather
1120
+ * than as a failed call. A 403 still throws: that one really is a
1121
+ * refusal.
1033
1122
  */
1034
1123
  async create(conversationId, signal) {
1035
1124
  try {
@@ -1131,7 +1220,7 @@ var CallRoom = class {
1131
1220
  room = null;
1132
1221
  status = "idle";
1133
1222
  selfId = null;
1134
- // Reference counted like VoiceRoom: a call screen and a roster can
1223
+ // Reference counted: a call screen and a roster can
1135
1224
  // both hold the same call, and the second holder must not open a
1136
1225
  // second SFU connection or tear the first one down on leaving.
1137
1226
  holders = 0;
@@ -1220,8 +1309,8 @@ var CallRoom = class {
1220
1309
  * Connect to the SFU.
1221
1310
  *
1222
1311
  * Resolves `true` once media is flowing, `false` when the deployment
1223
- * has no SFU — the caller should fall back to the mesh rather than
1224
- * treat that as a failed call.
1312
+ * has no SFU — calls are unavailable there; show that rather than a
1313
+ * failed call.
1225
1314
  */
1226
1315
  async join() {
1227
1316
  if (this.room) {
@@ -1873,7 +1962,7 @@ var Poolse = class {
1873
1962
  *
1874
1963
  * import * as livekit from 'livekit-client';
1875
1964
  * const call = poolse.call(conversationId, { livekit, video: true });
1876
- * const usingSfu = await call.join(); // false = no SFU, use the mesh
1965
+ * const joined = await call.join(); // false = calls aren't available on this deployment
1877
1966
  */
1878
1967
  call(conversationId, opts) {
1879
1968
  return new CallRoom({
@@ -1893,7 +1982,7 @@ var Poolse = class {
1893
1982
  };
1894
1983
 
1895
1984
  // src/version.ts
1896
- var version = "2.26.0";
1985
+ var version = "3.1.0";
1897
1986
 
1898
1987
  // src/call-log.ts
1899
1988
  function parseCallLogBody(body) {