@atsignal/js-core 0.1.1 → 0.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.
package/dist/index.cjs CHANGED
@@ -189,21 +189,18 @@ var SnapshotQueueStorage = class {
189
189
 
190
190
  // src/timers.ts
191
191
  function resolveTimerApi() {
192
- const maybeSetTimeout = globalThis.setTimeout;
193
- const maybeClearTimeout = globalThis.clearTimeout;
192
+ const timers = globalThis;
193
+ const maybeSetTimeout = timers == null ? void 0 : timers.setTimeout;
194
+ const maybeClearTimeout = timers == null ? void 0 : timers.clearTimeout;
194
195
  if (typeof maybeSetTimeout !== "function" || typeof maybeClearTimeout !== "function") {
195
196
  return null;
196
197
  }
197
198
  return {
198
- setTimeout: maybeSetTimeout,
199
- clearTimeout: maybeClearTimeout
199
+ setTimeout: maybeSetTimeout.bind(timers),
200
+ clearTimeout: maybeClearTimeout.bind(timers)
200
201
  };
201
202
  }
202
- function scheduleTask(callback, delayMs) {
203
- const timers = resolveTimerApi();
204
- if (!timers) {
205
- return null;
206
- }
203
+ function scheduleTask(callback, delayMs, timers) {
207
204
  const handle = timers.setTimeout(callback, delayMs);
208
205
  return {
209
206
  cancel: () => {
@@ -211,14 +208,10 @@ function scheduleTask(callback, delayMs) {
211
208
  }
212
209
  };
213
210
  }
214
- function createTimeoutController(timeoutMs, createTimeoutValue) {
211
+ function createTimeoutController(timeoutMs, createTimeoutValue, timers) {
215
212
  if (typeof timeoutMs !== "number" || timeoutMs <= 0) {
216
213
  return null;
217
214
  }
218
- const timers = resolveTimerApi();
219
- if (!timers) {
220
- return null;
221
- }
222
215
  let handle;
223
216
  const timeout = new Promise((resolve) => {
224
217
  handle = timers.setTimeout(() => resolve(createTimeoutValue()), timeoutMs);
@@ -233,11 +226,12 @@ function createTimeoutController(timeoutMs, createTimeoutValue) {
233
226
 
234
227
  // src/transport.ts
235
228
  function resolveGlobalFetch() {
236
- const candidate = globalThis.fetch;
229
+ const globalObject = globalThis;
230
+ const candidate = globalObject.fetch;
237
231
  if (typeof candidate !== "function") {
238
232
  return null;
239
233
  }
240
- return candidate;
234
+ return candidate.bind(globalObject);
241
235
  }
242
236
  function encodeRequestBody(request) {
243
237
  const parts = [
@@ -267,20 +261,29 @@ var FetchTransport = class {
267
261
  constructor(options = {}) {
268
262
  var _a, _b, _c;
269
263
  const fetchImpl = (_a = options.fetch) != null ? _a : resolveGlobalFetch();
264
+ const timers = resolveTimerApi();
270
265
  if (!fetchImpl) {
271
266
  throw new Error("No fetch implementation found for FetchTransport");
272
267
  }
268
+ if (!timers) {
269
+ throw new Error("No timer implementation found for FetchTransport");
270
+ }
273
271
  this._fetch = fetchImpl;
274
272
  this._keepalive = (_b = options.keepalive) != null ? _b : false;
275
273
  this._headers = (_c = options.headers) != null ? _c : {};
274
+ this._timers = timers;
276
275
  }
277
276
  async send(request) {
278
- const timer = createTimeoutController(request.timeoutMs, () => ({
279
- ok: false,
280
- retryable: true,
281
- status: 0,
282
- error: new Error("Request timed out")
283
- }));
277
+ const timer = createTimeoutController(
278
+ request.timeoutMs,
279
+ () => ({
280
+ ok: false,
281
+ retryable: true,
282
+ status: 0,
283
+ error: new Error("Request timed out")
284
+ }),
285
+ this._timers
286
+ );
284
287
  const requestPromise = this._fetch(request.endpoint, {
285
288
  method: "POST",
286
289
  headers: {
@@ -430,7 +433,7 @@ function normalizeConfig(apiKey, config) {
430
433
  if (!apiKey) {
431
434
  throw new Error("Signal client requires apiKey");
432
435
  }
433
- const persistQueue = (_a = config.persistQueue) != null ? _a : true;
436
+ const persistQueue = (_a = config.persistQueue) != null ? _a : false;
434
437
  const initialIdentityOverrides = {
435
438
  userId: resolveInitialUserId(config.userId),
436
439
  deviceId: resolveInitialDeviceId(config.deviceId)
@@ -983,7 +986,12 @@ var SignalClient = class {
983
986
  }
984
987
  this._state = "initializing";
985
988
  try {
989
+ const timers = resolveTimerApi();
990
+ if (!timers) {
991
+ throw new Error("Signal client requires a timer implementation");
992
+ }
986
993
  const normalized = normalizeConfig(apiKey, this.resolveInitConfig(options));
994
+ this._timers = timers;
987
995
  this._applyNormalizedConfig(normalized);
988
996
  this._ready = this._initialize(normalized.initialIdentityOverrides);
989
997
  await this._ready;
@@ -1418,7 +1426,6 @@ var SignalClient = class {
1418
1426
  const dropped = this._enqueue(entry);
1419
1427
  queuedResults.push({
1420
1428
  entry,
1421
- queuedEvents: this._queue.length,
1422
1429
  request
1423
1430
  });
1424
1431
  if (dropped) {
@@ -1430,11 +1437,10 @@ var SignalClient = class {
1430
1437
  if (droppedIds.length > 0) {
1431
1438
  await this._removeQueueEntries(droppedIds);
1432
1439
  }
1433
- for (const { entry, queuedEvents, request } of queuedResults) {
1440
+ for (const { entry, request } of queuedResults) {
1434
1441
  await this._hooks.emitEventQueued({
1435
1442
  event: entry.event,
1436
- options: entry.options,
1437
- queuedEvents
1443
+ options: entry.options
1438
1444
  });
1439
1445
  if (!request.waitForImmediateFlush) {
1440
1446
  request.completion.resolve(void 0);
@@ -1652,13 +1658,13 @@ var SignalClient = class {
1652
1658
  enqueueAutomaticFlush();
1653
1659
  return;
1654
1660
  }
1655
- const task = scheduleTask(() => {
1656
- enqueueAutomaticFlush();
1657
- }, delayMs);
1658
- if (!task) {
1659
- enqueueAutomaticFlush();
1660
- return;
1661
- }
1661
+ const task = scheduleTask(
1662
+ () => {
1663
+ enqueueAutomaticFlush();
1664
+ },
1665
+ delayMs,
1666
+ this._timers
1667
+ );
1662
1668
  this._flushTask = task;
1663
1669
  }
1664
1670
  async _performFlush(reason) {
package/dist/index.d.cts CHANGED
@@ -139,7 +139,6 @@ interface BeforeEnqueueHookContext {
139
139
  interface EventQueuedHookContext {
140
140
  event: Readonly<TrackedEvent>;
141
141
  options: Readonly<ResolvedTrackOptions>;
142
- queuedEvents: number;
143
142
  }
144
143
  interface BeforeFlushHookContext {
145
144
  reason: FlushReason;
@@ -234,8 +233,8 @@ interface ClientConfig {
234
233
  retry?: RetryConfig;
235
234
  /**
236
235
  * Queue durability toggle.
237
- * `true` (default): persist queue using configured `storage.queue`.
238
- * `false`: keep queue in memory only.
236
+ * `true`: persist queue using configured `storage.queue`.
237
+ * `false` (default): keep queue in memory only.
239
238
  */
240
239
  persistQueue?: boolean;
241
240
  partId?: string;
@@ -254,6 +253,7 @@ declare class SignalClient<TInitOptions extends object = ClientConfig> {
254
253
  private _config;
255
254
  private _storage;
256
255
  private _transport;
256
+ private _timers;
257
257
  private _hooks;
258
258
  private _insertIdGenerator;
259
259
  private _pluginRuntime;
package/dist/index.d.ts CHANGED
@@ -139,7 +139,6 @@ interface BeforeEnqueueHookContext {
139
139
  interface EventQueuedHookContext {
140
140
  event: Readonly<TrackedEvent>;
141
141
  options: Readonly<ResolvedTrackOptions>;
142
- queuedEvents: number;
143
142
  }
144
143
  interface BeforeFlushHookContext {
145
144
  reason: FlushReason;
@@ -234,8 +233,8 @@ interface ClientConfig {
234
233
  retry?: RetryConfig;
235
234
  /**
236
235
  * Queue durability toggle.
237
- * `true` (default): persist queue using configured `storage.queue`.
238
- * `false`: keep queue in memory only.
236
+ * `true`: persist queue using configured `storage.queue`.
237
+ * `false` (default): keep queue in memory only.
239
238
  */
240
239
  persistQueue?: boolean;
241
240
  partId?: string;
@@ -254,6 +253,7 @@ declare class SignalClient<TInitOptions extends object = ClientConfig> {
254
253
  private _config;
255
254
  private _storage;
256
255
  private _transport;
256
+ private _timers;
257
257
  private _hooks;
258
258
  private _insertIdGenerator;
259
259
  private _pluginRuntime;
package/dist/index.js CHANGED
@@ -160,21 +160,18 @@ var SnapshotQueueStorage = class {
160
160
 
161
161
  // src/timers.ts
162
162
  function resolveTimerApi() {
163
- const maybeSetTimeout = globalThis.setTimeout;
164
- const maybeClearTimeout = globalThis.clearTimeout;
163
+ const timers = globalThis;
164
+ const maybeSetTimeout = timers == null ? void 0 : timers.setTimeout;
165
+ const maybeClearTimeout = timers == null ? void 0 : timers.clearTimeout;
165
166
  if (typeof maybeSetTimeout !== "function" || typeof maybeClearTimeout !== "function") {
166
167
  return null;
167
168
  }
168
169
  return {
169
- setTimeout: maybeSetTimeout,
170
- clearTimeout: maybeClearTimeout
170
+ setTimeout: maybeSetTimeout.bind(timers),
171
+ clearTimeout: maybeClearTimeout.bind(timers)
171
172
  };
172
173
  }
173
- function scheduleTask(callback, delayMs) {
174
- const timers = resolveTimerApi();
175
- if (!timers) {
176
- return null;
177
- }
174
+ function scheduleTask(callback, delayMs, timers) {
178
175
  const handle = timers.setTimeout(callback, delayMs);
179
176
  return {
180
177
  cancel: () => {
@@ -182,14 +179,10 @@ function scheduleTask(callback, delayMs) {
182
179
  }
183
180
  };
184
181
  }
185
- function createTimeoutController(timeoutMs, createTimeoutValue) {
182
+ function createTimeoutController(timeoutMs, createTimeoutValue, timers) {
186
183
  if (typeof timeoutMs !== "number" || timeoutMs <= 0) {
187
184
  return null;
188
185
  }
189
- const timers = resolveTimerApi();
190
- if (!timers) {
191
- return null;
192
- }
193
186
  let handle;
194
187
  const timeout = new Promise((resolve) => {
195
188
  handle = timers.setTimeout(() => resolve(createTimeoutValue()), timeoutMs);
@@ -204,11 +197,12 @@ function createTimeoutController(timeoutMs, createTimeoutValue) {
204
197
 
205
198
  // src/transport.ts
206
199
  function resolveGlobalFetch() {
207
- const candidate = globalThis.fetch;
200
+ const globalObject = globalThis;
201
+ const candidate = globalObject.fetch;
208
202
  if (typeof candidate !== "function") {
209
203
  return null;
210
204
  }
211
- return candidate;
205
+ return candidate.bind(globalObject);
212
206
  }
213
207
  function encodeRequestBody(request) {
214
208
  const parts = [
@@ -238,20 +232,29 @@ var FetchTransport = class {
238
232
  constructor(options = {}) {
239
233
  var _a, _b, _c;
240
234
  const fetchImpl = (_a = options.fetch) != null ? _a : resolveGlobalFetch();
235
+ const timers = resolveTimerApi();
241
236
  if (!fetchImpl) {
242
237
  throw new Error("No fetch implementation found for FetchTransport");
243
238
  }
239
+ if (!timers) {
240
+ throw new Error("No timer implementation found for FetchTransport");
241
+ }
244
242
  this._fetch = fetchImpl;
245
243
  this._keepalive = (_b = options.keepalive) != null ? _b : false;
246
244
  this._headers = (_c = options.headers) != null ? _c : {};
245
+ this._timers = timers;
247
246
  }
248
247
  async send(request) {
249
- const timer = createTimeoutController(request.timeoutMs, () => ({
250
- ok: false,
251
- retryable: true,
252
- status: 0,
253
- error: new Error("Request timed out")
254
- }));
248
+ const timer = createTimeoutController(
249
+ request.timeoutMs,
250
+ () => ({
251
+ ok: false,
252
+ retryable: true,
253
+ status: 0,
254
+ error: new Error("Request timed out")
255
+ }),
256
+ this._timers
257
+ );
255
258
  const requestPromise = this._fetch(request.endpoint, {
256
259
  method: "POST",
257
260
  headers: {
@@ -401,7 +404,7 @@ function normalizeConfig(apiKey, config) {
401
404
  if (!apiKey) {
402
405
  throw new Error("Signal client requires apiKey");
403
406
  }
404
- const persistQueue = (_a = config.persistQueue) != null ? _a : true;
407
+ const persistQueue = (_a = config.persistQueue) != null ? _a : false;
405
408
  const initialIdentityOverrides = {
406
409
  userId: resolveInitialUserId(config.userId),
407
410
  deviceId: resolveInitialDeviceId(config.deviceId)
@@ -954,7 +957,12 @@ var SignalClient = class {
954
957
  }
955
958
  this._state = "initializing";
956
959
  try {
960
+ const timers = resolveTimerApi();
961
+ if (!timers) {
962
+ throw new Error("Signal client requires a timer implementation");
963
+ }
957
964
  const normalized = normalizeConfig(apiKey, this.resolveInitConfig(options));
965
+ this._timers = timers;
958
966
  this._applyNormalizedConfig(normalized);
959
967
  this._ready = this._initialize(normalized.initialIdentityOverrides);
960
968
  await this._ready;
@@ -1389,7 +1397,6 @@ var SignalClient = class {
1389
1397
  const dropped = this._enqueue(entry);
1390
1398
  queuedResults.push({
1391
1399
  entry,
1392
- queuedEvents: this._queue.length,
1393
1400
  request
1394
1401
  });
1395
1402
  if (dropped) {
@@ -1401,11 +1408,10 @@ var SignalClient = class {
1401
1408
  if (droppedIds.length > 0) {
1402
1409
  await this._removeQueueEntries(droppedIds);
1403
1410
  }
1404
- for (const { entry, queuedEvents, request } of queuedResults) {
1411
+ for (const { entry, request } of queuedResults) {
1405
1412
  await this._hooks.emitEventQueued({
1406
1413
  event: entry.event,
1407
- options: entry.options,
1408
- queuedEvents
1414
+ options: entry.options
1409
1415
  });
1410
1416
  if (!request.waitForImmediateFlush) {
1411
1417
  request.completion.resolve(void 0);
@@ -1623,13 +1629,13 @@ var SignalClient = class {
1623
1629
  enqueueAutomaticFlush();
1624
1630
  return;
1625
1631
  }
1626
- const task = scheduleTask(() => {
1627
- enqueueAutomaticFlush();
1628
- }, delayMs);
1629
- if (!task) {
1630
- enqueueAutomaticFlush();
1631
- return;
1632
- }
1632
+ const task = scheduleTask(
1633
+ () => {
1634
+ enqueueAutomaticFlush();
1635
+ },
1636
+ delayMs,
1637
+ this._timers
1638
+ );
1633
1639
  this._flushTask = task;
1634
1640
  }
1635
1641
  async _performFlush(reason) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atsignal/js-core",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Shared core module for Signal JavaScript SDKs.",
5
5
  "license": "MIT",
6
6
  "type": "module",