@atsignal/browser 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/README.md CHANGED
@@ -14,12 +14,15 @@ pnpm add @atsignal/browser
14
14
  import signal from "@atsignal/browser";
15
15
 
16
16
  await signal.init("your-api-key", {
17
+ endpoint: "https://your-collector.example.com/track",
17
18
  persistQueue: false,
18
19
  });
19
20
 
20
21
  await signal.track("PageView");
21
22
  ```
22
23
 
24
+ Set `endpoint` to your real collector URL. The SDK default, `https://api.example.com/track`, is a placeholder used for docs/tests and will not send events anywhere useful by itself.
25
+
23
26
  Optional browser plugins are published as separate packages. For example, install `@atsignal/browser-plugin-enrich-context` and import it before using `signal.plugins.enrichContext()`.
24
27
 
25
28
  More SDK details live in the monorepo root README.
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { ClientPlugin, KeyValueStorage, SignalClient, ClientConfig, Transport, QueueStorageInput } from '@atsignal/js-core';
1
+ import { ClientPlugin, KeyValueStorage, SignalClient, ClientConfig, QueueStorageInput } from '@atsignal/js-core';
2
2
  export { ClientPlugin } from '@atsignal/js-core';
3
3
 
4
4
  type BrowserPluginFactory = (...args: unknown[]) => ClientPlugin;
@@ -28,7 +28,6 @@ declare class BrowserLocalStorage implements KeyValueStorage {
28
28
  }
29
29
 
30
30
  interface SignalBrowserOptions extends Omit<ClientConfig, "storage"> {
31
- transport?: Transport;
32
31
  storage?: {
33
32
  identity?: KeyValueStorage;
34
33
  queue?: QueueStorageInput;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ClientPlugin, KeyValueStorage, SignalClient, ClientConfig, Transport, QueueStorageInput } from '@atsignal/js-core';
1
+ import { ClientPlugin, KeyValueStorage, SignalClient, ClientConfig, QueueStorageInput } from '@atsignal/js-core';
2
2
  export { ClientPlugin } from '@atsignal/js-core';
3
3
 
4
4
  type BrowserPluginFactory = (...args: unknown[]) => ClientPlugin;
@@ -28,7 +28,6 @@ declare class BrowserLocalStorage implements KeyValueStorage {
28
28
  }
29
29
 
30
30
  interface SignalBrowserOptions extends Omit<ClientConfig, "storage"> {
31
- transport?: Transport;
32
31
  storage?: {
33
32
  identity?: KeyValueStorage;
34
33
  queue?: QueueStorageInput;
@@ -158,21 +158,18 @@ var __signalBundle = (() => {
158
158
  }
159
159
  };
160
160
  function resolveTimerApi() {
161
- const maybeSetTimeout = globalThis.setTimeout;
162
- const maybeClearTimeout = globalThis.clearTimeout;
161
+ const timers = globalThis;
162
+ const maybeSetTimeout = timers == null ? void 0 : timers.setTimeout;
163
+ const maybeClearTimeout = timers == null ? void 0 : timers.clearTimeout;
163
164
  if (typeof maybeSetTimeout !== "function" || typeof maybeClearTimeout !== "function") {
164
165
  return null;
165
166
  }
166
167
  return {
167
- setTimeout: maybeSetTimeout,
168
- clearTimeout: maybeClearTimeout
168
+ setTimeout: maybeSetTimeout.bind(timers),
169
+ clearTimeout: maybeClearTimeout.bind(timers)
169
170
  };
170
171
  }
171
- function scheduleTask(callback, delayMs) {
172
- const timers = resolveTimerApi();
173
- if (!timers) {
174
- return null;
175
- }
172
+ function scheduleTask(callback, delayMs, timers) {
176
173
  const handle = timers.setTimeout(callback, delayMs);
177
174
  return {
178
175
  cancel: () => {
@@ -180,14 +177,10 @@ var __signalBundle = (() => {
180
177
  }
181
178
  };
182
179
  }
183
- function createTimeoutController(timeoutMs, createTimeoutValue) {
180
+ function createTimeoutController(timeoutMs, createTimeoutValue, timers) {
184
181
  if (typeof timeoutMs !== "number" || timeoutMs <= 0) {
185
182
  return null;
186
183
  }
187
- const timers = resolveTimerApi();
188
- if (!timers) {
189
- return null;
190
- }
191
184
  let handle;
192
185
  const timeout = new Promise((resolve) => {
193
186
  handle = timers.setTimeout(() => resolve(createTimeoutValue()), timeoutMs);
@@ -200,11 +193,12 @@ var __signalBundle = (() => {
200
193
  };
201
194
  }
202
195
  function resolveGlobalFetch() {
203
- const candidate = globalThis.fetch;
196
+ const globalObject2 = globalThis;
197
+ const candidate = globalObject2.fetch;
204
198
  if (typeof candidate !== "function") {
205
199
  return null;
206
200
  }
207
- return candidate;
201
+ return candidate.bind(globalObject2);
208
202
  }
209
203
  function encodeRequestBody(request) {
210
204
  const parts = [
@@ -234,20 +228,29 @@ var __signalBundle = (() => {
234
228
  constructor(options = {}) {
235
229
  var _a, _b, _c;
236
230
  const fetchImpl = (_a = options.fetch) != null ? _a : resolveGlobalFetch();
231
+ const timers = resolveTimerApi();
237
232
  if (!fetchImpl) {
238
233
  throw new Error("No fetch implementation found for FetchTransport");
239
234
  }
235
+ if (!timers) {
236
+ throw new Error("No timer implementation found for FetchTransport");
237
+ }
240
238
  this._fetch = fetchImpl;
241
239
  this._keepalive = (_b = options.keepalive) != null ? _b : false;
242
240
  this._headers = (_c = options.headers) != null ? _c : {};
241
+ this._timers = timers;
243
242
  }
244
243
  async send(request) {
245
- const timer = createTimeoutController(request.timeoutMs, () => ({
246
- ok: false,
247
- retryable: true,
248
- status: 0,
249
- error: new Error("Request timed out")
250
- }));
244
+ const timer = createTimeoutController(
245
+ request.timeoutMs,
246
+ () => ({
247
+ ok: false,
248
+ retryable: true,
249
+ status: 0,
250
+ error: new Error("Request timed out")
251
+ }),
252
+ this._timers
253
+ );
251
254
  const requestPromise = this._fetch(request.endpoint, {
252
255
  method: "POST",
253
256
  headers: {
@@ -395,7 +398,7 @@ var __signalBundle = (() => {
395
398
  if (!apiKey) {
396
399
  throw new Error("Signal client requires apiKey");
397
400
  }
398
- const persistQueue = (_a = config.persistQueue) != null ? _a : true;
401
+ const persistQueue = (_a = config.persistQueue) != null ? _a : false;
399
402
  const initialIdentityOverrides = {
400
403
  userId: resolveInitialUserId(config.userId),
401
404
  deviceId: resolveInitialDeviceId(config.deviceId)
@@ -934,7 +937,12 @@ var __signalBundle = (() => {
934
937
  }
935
938
  this._state = "initializing";
936
939
  try {
940
+ const timers = resolveTimerApi();
941
+ if (!timers) {
942
+ throw new Error("Signal client requires a timer implementation");
943
+ }
937
944
  const normalized = normalizeConfig(apiKey, this.resolveInitConfig(options));
945
+ this._timers = timers;
938
946
  this._applyNormalizedConfig(normalized);
939
947
  this._ready = this._initialize(normalized.initialIdentityOverrides);
940
948
  await this._ready;
@@ -1369,7 +1377,6 @@ var __signalBundle = (() => {
1369
1377
  const dropped = this._enqueue(entry);
1370
1378
  queuedResults.push({
1371
1379
  entry,
1372
- queuedEvents: this._queue.length,
1373
1380
  request
1374
1381
  });
1375
1382
  if (dropped) {
@@ -1381,11 +1388,10 @@ var __signalBundle = (() => {
1381
1388
  if (droppedIds.length > 0) {
1382
1389
  await this._removeQueueEntries(droppedIds);
1383
1390
  }
1384
- for (const { entry, queuedEvents, request } of queuedResults) {
1391
+ for (const { entry, request } of queuedResults) {
1385
1392
  await this._hooks.emitEventQueued({
1386
1393
  event: entry.event,
1387
- options: entry.options,
1388
- queuedEvents
1394
+ options: entry.options
1389
1395
  });
1390
1396
  if (!request.waitForImmediateFlush) {
1391
1397
  request.completion.resolve(void 0);
@@ -1603,13 +1609,13 @@ var __signalBundle = (() => {
1603
1609
  enqueueAutomaticFlush();
1604
1610
  return;
1605
1611
  }
1606
- const task = scheduleTask(() => {
1607
- enqueueAutomaticFlush();
1608
- }, delayMs);
1609
- if (!task) {
1610
- enqueueAutomaticFlush();
1611
- return;
1612
- }
1612
+ const task = scheduleTask(
1613
+ () => {
1614
+ enqueueAutomaticFlush();
1615
+ },
1616
+ delayMs,
1617
+ this._timers
1618
+ );
1613
1619
  this._flushTask = task;
1614
1620
  }
1615
1621
  async _performFlush(reason) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atsignal/browser",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Browser SDK for Signal.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -27,7 +27,7 @@
27
27
  "dist"
28
28
  ],
29
29
  "dependencies": {
30
- "@atsignal/js-core": "0.1.1"
30
+ "@atsignal/js-core": "0.2.0"
31
31
  },
32
32
  "publishConfig": {
33
33
  "access": "public",