@plasius/analytics 1.3.2 → 1.3.3

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.d.cts CHANGED
@@ -103,6 +103,8 @@ interface AnalyticsTransportRequest {
103
103
  body: string;
104
104
  headers: Record<string, string>;
105
105
  keepalive: boolean;
106
+ /** Aborted when the host discards this client after consent withdrawal. */
107
+ signal?: AbortSignal;
106
108
  }
107
109
  type AnalyticsTransport = (request: AnalyticsTransportRequest) => Promise<void>;
108
110
  interface LocalSpaceAnalyticsConfig {
@@ -152,7 +154,10 @@ interface LocalSpaceAnalyticsClient {
152
154
  flush: () => Promise<void>;
153
155
  updateConfig: (config: Partial<LocalSpaceAnalyticsConfig>) => void;
154
156
  getConfig: () => Readonly<ResolvedLocalSpaceAnalyticsConfig>;
155
- destroy: () => void;
157
+ /** Stop the client; discard also cancels transport and clears retained data. */
158
+ destroy: (options?: {
159
+ discard?: boolean;
160
+ }) => void;
156
161
  }
157
162
 
158
163
  declare function createLocalSpaceAnalyticsClient(config: LocalSpaceAnalyticsConfig): LocalSpaceAnalyticsClient;
package/dist/index.d.ts CHANGED
@@ -103,6 +103,8 @@ interface AnalyticsTransportRequest {
103
103
  body: string;
104
104
  headers: Record<string, string>;
105
105
  keepalive: boolean;
106
+ /** Aborted when the host discards this client after consent withdrawal. */
107
+ signal?: AbortSignal;
106
108
  }
107
109
  type AnalyticsTransport = (request: AnalyticsTransportRequest) => Promise<void>;
108
110
  interface LocalSpaceAnalyticsConfig {
@@ -152,7 +154,10 @@ interface LocalSpaceAnalyticsClient {
152
154
  flush: () => Promise<void>;
153
155
  updateConfig: (config: Partial<LocalSpaceAnalyticsConfig>) => void;
154
156
  getConfig: () => Readonly<ResolvedLocalSpaceAnalyticsConfig>;
155
- destroy: () => void;
157
+ /** Stop the client; discard also cancels transport and clears retained data. */
158
+ destroy: (options?: {
159
+ discard?: boolean;
160
+ }) => void;
156
161
  }
157
162
 
158
163
  declare function createLocalSpaceAnalyticsClient(config: LocalSpaceAnalyticsConfig): LocalSpaceAnalyticsClient;
package/dist/index.js CHANGED
@@ -215,7 +215,8 @@ async function defaultTransport({
215
215
  endpoint,
216
216
  body,
217
217
  headers,
218
- keepalive
218
+ keepalive,
219
+ signal
219
220
  }) {
220
221
  if (typeof fetch !== "function") {
221
222
  return;
@@ -224,7 +225,8 @@ async function defaultTransport({
224
225
  method: "POST",
225
226
  headers,
226
227
  body,
227
- keepalive
228
+ keepalive,
229
+ signal
228
230
  });
229
231
  if (!response.ok) {
230
232
  throw new Error(`Analytics transport failed with status ${response.status}.`);
@@ -778,6 +780,7 @@ function createLocalSpaceAnalyticsClient(config) {
778
780
  let isFlushing = false;
779
781
  let flushQueuedAfterCurrent = false;
780
782
  let isDestroyed = false;
783
+ const transportController = new AbortController();
781
784
  const issueAggregates = /* @__PURE__ */ new Map();
782
785
  const persistQueue = () => writeQueue(resolvedConfig.storageKey, queue);
783
786
  const trimQueue = () => {
@@ -944,12 +947,14 @@ function createLocalSpaceAnalyticsClient(config) {
944
947
  body,
945
948
  headers: resolvedConfig.headers,
946
949
  // Normal flushes do not need fetch keepalive; unload/pagehide uses sendBeacon.
947
- keepalive: false
950
+ keepalive: false,
951
+ signal: transportController.signal
948
952
  });
953
+ if (transportController.signal.aborted) return;
949
954
  queue = queue.slice(events.length);
950
955
  persistQueue();
951
956
  } catch (error) {
952
- resolvedConfig.onError?.(error);
957
+ if (!transportController.signal.aborted) resolvedConfig.onError?.(error);
953
958
  } finally {
954
959
  isFlushing = false;
955
960
  if (flushQueuedAfterCurrent) {
@@ -1122,7 +1127,14 @@ function createLocalSpaceAnalyticsClient(config) {
1122
1127
  getConfig() {
1123
1128
  return resolvedConfig;
1124
1129
  },
1125
- destroy() {
1130
+ destroy(options) {
1131
+ if (options?.discard) {
1132
+ transportController.abort();
1133
+ queue = [];
1134
+ flushQueuedAfterCurrent = false;
1135
+ issueAggregates.clear();
1136
+ writeQueue(resolvedConfig.storageKey, []);
1137
+ }
1126
1138
  if (isDestroyed) {
1127
1139
  return;
1128
1140
  }