@adhese/sdk 0.6.4 → 0.7.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.d.ts CHANGED
@@ -212,9 +212,9 @@ declare const baseSchema: zod.ZodObject<{
212
212
  width: zod.ZodOptional<zod.ZodEffects<zod.ZodUnion<[zod.ZodString, zod.ZodLiteral<"">]>, number | undefined, string>>;
213
213
  widthLarge: zod.ZodOptional<zod.ZodEffects<zod.ZodUnion<[zod.ZodString, zod.ZodLiteral<"">, zod.ZodNumber]>, string | undefined, string | number>>;
214
214
  }, "strip", zod.ZodTypeAny, {
215
+ adType: string;
215
216
  id: string;
216
217
  origin: "JERLICIA" | "DALE";
217
- adType: string;
218
218
  slotID: string;
219
219
  slotName: string;
220
220
  adDuration?: number | undefined;
@@ -264,9 +264,9 @@ declare const baseSchema: zod.ZodObject<{
264
264
  width?: number | undefined;
265
265
  widthLarge?: string | undefined;
266
266
  }, {
267
+ adType: string;
267
268
  id: string;
268
269
  origin: "JERLICIA" | "DALE";
269
- adType: string;
270
270
  slotID: string;
271
271
  slotName: string;
272
272
  adDuration?: string | undefined;
@@ -602,6 +602,11 @@ type AdheseOptions = {
602
602
  */
603
603
  queries?: Record<string, string>;
604
604
  safeFrame?: boolean;
605
+ /**
606
+ * The plugins that are used for the Adhese instance. These plugins are called with the Adhese context and run during
607
+ * the initialization of the Adhese instance.
608
+ */
609
+ plugins?: ReadonlyArray<(context: AdheseContext) => void>;
605
610
  } & ({
606
611
  viewabilityTracking?: true;
607
612
  /**
@@ -725,4 +730,8 @@ type AdheseContext = Partial<Pick<Adhese, 'events' | 'getAll' | 'get' | 'paramet
725
730
  */
726
731
  declare function createAdhese(options: AdheseOptions): Readonly<Adhese>;
727
732
 
728
- export { type Ad, type AdRequestOptions, type Adhese, type AdheseContext, type AdheseOptions, type AdheseSlot, type AdheseSlotOptions, createAdhese };
733
+ declare const onInit: (callback: Function) => void;
734
+
735
+ declare const onDispose: (callback: Function) => void;
736
+
737
+ export { type Ad, type AdRequestOptions, type Adhese, type AdheseContext, type AdheseOptions, type AdheseSlot, type AdheseSlotOptions, createAdhese, onDispose, onInit };
package/dist/index.js CHANGED
@@ -1,10 +1,5 @@
1
- import { createDevtools } from "@adhese/sdk-devtools";
2
1
  import { effectScope, shallowReactive, watchEffect, watch, reactive, ref, computed, toValue } from "@vue/runtime-core";
3
- import uniqueId from "lodash/uniqueId";
4
- import debounce from "lodash/debounce";
5
- import random from "lodash/random";
6
- import isEqual from "lodash/isEqual";
7
- import round from "lodash/round";
2
+ import { debounce, round, isDeepEqual } from "remeda";
8
3
  import { union, coerce, literal, number, string, ZodIssueCode, NEVER, object, unknown, lazy } from "zod";
9
4
  async function waitForDomLoad() {
10
5
  return new Promise((resolve) => {
@@ -65,6 +60,15 @@ function createEvent() {
65
60
  removeListener
66
61
  };
67
62
  }
63
+ const savedIds = /* @__PURE__ */ new Set();
64
+ function uniqueId() {
65
+ let id;
66
+ do
67
+ id = Math.random().toString(36).slice(2);
68
+ while (savedIds.has(id));
69
+ savedIds.add(id);
70
+ return id;
71
+ }
68
72
  function createSafeFrame({
69
73
  renderFile,
70
74
  context
@@ -186,10 +190,10 @@ function createSlotManager({
186
190
  var _a;
187
191
  const slot = createSlot({
188
192
  ...options,
189
- onDispose,
193
+ onDispose: onDispose2,
190
194
  context
191
195
  });
192
- function onDispose() {
196
+ function onDispose2() {
193
197
  var _a2;
194
198
  slots.delete(slot.name.value);
195
199
  logger.debug("Slot removed", {
@@ -271,14 +275,16 @@ function createQueryDetector({
271
275
  const handleOnChange = debounce(() => {
272
276
  void (onChange == null ? void 0 : onChange(getQuery()));
273
277
  logger.debug(`Change device ${getQuery()}`);
274
- }, 50);
278
+ }, {
279
+ waitMs: 50
280
+ });
275
281
  if (onChange) {
276
282
  for (const query of mediaMap.values())
277
- query.addEventListener("change", handleOnChange);
283
+ query.addEventListener("change", handleOnChange.call);
278
284
  }
279
285
  function dispose() {
280
286
  for (const query of mediaMap.values())
281
- query.removeEventListener("change", handleOnChange);
287
+ query.removeEventListener("change", handleOnChange.call);
282
288
  }
283
289
  return {
284
290
  queries: mediaMap,
@@ -370,7 +376,7 @@ function createParameters(options, queryDetector) {
370
376
  tl: options.consent ? "all" : "none",
371
377
  dt: queryDetector.getQuery(),
372
378
  br: queryDetector.getQuery(),
373
- rn: random(1e4).toString()
379
+ rn: Math.round(Math.random() * 1e4).toString()
374
380
  }))
375
381
  parameters.set(key, value);
376
382
  return parameters;
@@ -389,7 +395,8 @@ function isPreviewMode() {
389
395
  }
390
396
  function createHook({
391
397
  onRun,
392
- onAdd
398
+ onAdd,
399
+ onDispose: onDispose2
393
400
  }) {
394
401
  const callbacks = /* @__PURE__ */ new Set();
395
402
  function run() {
@@ -401,7 +408,11 @@ function createHook({
401
408
  callbacks.add(callback);
402
409
  onAdd == null ? void 0 : onAdd(callbacks);
403
410
  }
404
- return [run, add];
411
+ function dispose() {
412
+ onDispose2 == null ? void 0 : onDispose2(callbacks);
413
+ callbacks.clear();
414
+ }
415
+ return [run, add, dispose];
405
416
  }
406
417
  let resolveOnInitPromise = () => {
407
418
  };
@@ -409,7 +420,7 @@ let isInit = false;
409
420
  const waitOnInit = new Promise((resolve) => {
410
421
  resolveOnInitPromise = resolve;
411
422
  });
412
- const [runOnInit, onInit] = createHook({
423
+ const [runOnInit, onInit, disposeOnInit] = createHook({
413
424
  onRun(callbacks) {
414
425
  isInit = true;
415
426
  resolveOnInitPromise();
@@ -421,6 +432,24 @@ const [runOnInit, onInit] = createHook({
421
432
  runOnInit();
422
433
  }
423
434
  });
435
+ let resolveOnDisposePromise = () => {
436
+ };
437
+ let isDisposed = false;
438
+ new Promise((resolve) => {
439
+ resolveOnDisposePromise = resolve;
440
+ });
441
+ const [runOnDispose, onDispose, disposeOnDispose] = createHook({
442
+ onRun(callbacks) {
443
+ isDisposed = true;
444
+ resolveOnDisposePromise();
445
+ logger.debug("Disposal completed");
446
+ callbacks.clear();
447
+ },
448
+ onAdd() {
449
+ if (isDisposed)
450
+ runOnDispose();
451
+ }
452
+ });
424
453
  function createAdhese(options) {
425
454
  const scope = effectScope();
426
455
  return scope.run(() => {
@@ -438,6 +467,7 @@ function createAdhese(options) {
438
467
  safeFrame: false,
439
468
  eagerRendering: false,
440
469
  viewabilityTracking: true,
470
+ plugins: [],
441
471
  ...options
442
472
  };
443
473
  setupLogging(mergedOptions);
@@ -534,19 +564,15 @@ function createAdhese(options) {
534
564
  }
535
565
  return domSlots;
536
566
  }
537
- let unmountDevtools;
538
567
  async function toggleDebug() {
539
568
  var _a, _b;
540
569
  context.debug = !context.debug;
541
- if (context.debug && !unmountDevtools) {
542
- unmountDevtools = await createDevtools(context);
570
+ if (context.debug) {
543
571
  logger.setMinLogLevelThreshold("debug");
544
572
  logger.debug("Debug mode enabled");
545
573
  (_a = context.events) == null ? void 0 : _a.debugChange.dispatch(true);
546
574
  } else {
547
575
  logger.debug("Debug mode disabled");
548
- unmountDevtools == null ? void 0 : unmountDevtools();
549
- unmountDevtools = void 0;
550
576
  logger.setMinLogLevelThreshold("info");
551
577
  (_b = context.events) == null ? void 0 : _b.debugChange.dispatch(false);
552
578
  }
@@ -579,7 +605,6 @@ function createAdhese(options) {
579
605
  });
580
606
  function dispose() {
581
607
  var _a, _b;
582
- unmountDevtools == null ? void 0 : unmountDevtools();
583
608
  queryDetector.dispose();
584
609
  slotManager.dispose();
585
610
  queryDetector.dispose();
@@ -588,18 +613,21 @@ function createAdhese(options) {
588
613
  logger.resetLogs();
589
614
  (_b = context.events) == null ? void 0 : _b.dispose();
590
615
  logger.info("Adhese instance disposed");
616
+ runOnDispose();
617
+ disposeOnInit();
618
+ disposeOnDispose();
591
619
  scope.stop();
592
620
  }
621
+ for (const plugin of mergedOptions.plugins)
622
+ plugin(context);
593
623
  onInit(async () => {
594
624
  var _a;
595
625
  if ((slotManager.getAll().length ?? 0) > 0)
596
626
  await fetchAndRenderAllSlots().catch(logger.error);
597
627
  if (mergedOptions.findDomSlotsOnLoad)
598
628
  await findDomSlots2();
599
- if (mergedOptions.debug || window.location.search.includes("adhese_debug=true") || isPreviewMode()) {
600
- unmountDevtools = await createDevtools(context);
629
+ if (mergedOptions.debug || window.location.search.includes("adhese_debug=true") || isPreviewMode())
601
630
  (_a = context.events) == null ? void 0 : _a.debugChange.dispatch(true);
602
- }
603
631
  if (!scope.active)
604
632
  dispose();
605
633
  });
@@ -780,7 +808,7 @@ function createSlot(options) {
780
808
  });
781
809
  watch([ad, isInViewport], async ([newAd, newIsInViewport], [oldAd]) => {
782
810
  var _a, _b;
783
- if (!newAd || isEqual(newAd, oldAd))
811
+ if (!newAd || oldAd && isDeepEqual(newAd, oldAd))
784
812
  return;
785
813
  if (newIsInViewport || context.options.eagerRendering)
786
814
  await render(newAd);
@@ -1118,8 +1146,16 @@ function parseParameters(parameters) {
1118
1146
  return true;
1119
1147
  logger.warn(`Invalid parameter key: ${key}. Key should be exactly 2 characters long. Key will be ignored.`);
1120
1148
  return false;
1149
+ }).map(([key, value]) => {
1150
+ if (typeof value === "string")
1151
+ return [key, filterSpecialChars(value)];
1152
+ return [key, value.map(filterSpecialChars)];
1121
1153
  }));
1122
1154
  }
1155
+ function filterSpecialChars(value) {
1156
+ const specialRegex = /[^\p{L}\p{N}_]/gu;
1157
+ return value.replaceAll(specialRegex, "_");
1158
+ }
1123
1159
  async function requestAds(options) {
1124
1160
  var _a, _b, _c, _d, _e;
1125
1161
  const { context } = options;
@@ -1170,6 +1206,8 @@ async function requestAd({
1170
1206
  return ad;
1171
1207
  }
1172
1208
  export {
1173
- createAdhese
1209
+ createAdhese,
1210
+ onDispose,
1211
+ onInit
1174
1212
  };
1175
1213
  //# sourceMappingURL=index.js.map