@adhese/sdk 0.16.1 → 0.17.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/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # @adhese/sdk
2
2
 
3
+ ## 0.17.0
4
+
5
+ ### Minor Changes
6
+
7
+ - ef68972: Add hooks to context instead of global variable which could break when adhese was disposed
8
+ - ef68972: Move hook create functions to @adhese/sdk-shared
9
+
10
+ ### Patch Changes
11
+
12
+ - Updated dependencies [2c01e34]
13
+ - Updated dependencies [ad9b38f]
14
+ - Updated dependencies [ef68972]
15
+ - @adhese/sdk-shared@0.6.0
16
+
17
+ ## 0.16.2
18
+
19
+ ### Patch Changes
20
+
21
+ - 2763181: Force @adhese/sdk to use @adhese/sdk-shared@0.5.1
22
+
3
23
  ## 0.16.1
4
24
 
5
25
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -3,7 +3,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const sdkShared = require("@adhese/sdk-shared");
4
4
  const remeda = require("remeda");
5
5
  const name = "@adhese/sdk";
6
- const version = "0.16.1";
6
+ const version = "0.17.0";
7
7
  function addTrackingPixel(url) {
8
8
  const img = document.createElement("img");
9
9
  img.src = url.toString();
@@ -15,104 +15,9 @@ function addTrackingPixel(url) {
15
15
  img.style.top = "0";
16
16
  return document.body.appendChild(img);
17
17
  }
18
- const hookMap = /* @__PURE__ */ new Map();
19
- function clearAllHooks() {
20
- hookMap.clear();
21
- }
22
- function createAsyncHook(name2, {
23
- onRun,
24
- onAdd
25
- } = {}) {
26
- hookMap.set(name2, /* @__PURE__ */ new Set());
27
- const run = async (arg) => {
28
- let latestResult = arg;
29
- for (const callback of hookMap.get(name2) ?? [])
30
- latestResult = await callback(latestResult) ?? latestResult;
31
- onRun == null ? void 0 : onRun(hookMap.get(name2));
32
- return latestResult;
33
- };
34
- function dispose() {
35
- hookMap.delete(name2);
36
- }
37
- return [run, (callback) => add(callback, { name: name2, onAdd }), dispose];
38
- }
39
- function createSyncHook(name2, {
40
- onRun,
41
- onAdd
42
- } = {}) {
43
- hookMap.set(name2, /* @__PURE__ */ new Set());
44
- const run = (arg) => {
45
- let latestResult = arg;
46
- const promisedCallbacks = [];
47
- for (const callback of hookMap.get(name2) ?? []) {
48
- if (isCallbackAsync(callback))
49
- promisedCallbacks.push(callback);
50
- else
51
- latestResult = callback(latestResult) ?? latestResult;
52
- }
53
- Promise.allSettled(promisedCallbacks.map((callback) => callback(latestResult))).catch(console.trace);
54
- onRun == null ? void 0 : onRun(hookMap.get(name2));
55
- return latestResult;
56
- };
57
- function dispose() {
58
- hookMap.delete(name2);
59
- }
60
- return [run, (callback) => add(callback, { name: name2, onAdd }), dispose];
61
- }
62
- function createPassiveHook(name2, {
63
- onRun,
64
- onAdd
65
- } = {}) {
66
- hookMap.set(name2, /* @__PURE__ */ new Set());
67
- function run(arg) {
68
- Promise.allSettled(Array.from(hookMap.get(name2) ?? []).map((callback) => callback(arg))).catch(console.trace);
69
- onRun == null ? void 0 : onRun(hookMap.get(name2));
70
- }
71
- function dispose() {
72
- hookMap.delete(name2);
73
- }
74
- return [run, (callback) => add(callback, { name: name2, onAdd }), dispose];
75
- }
76
- function isCallbackAsync(callback) {
77
- return callback.constructor.name === "AsyncFunction";
78
- }
79
- function add(callback, {
80
- name: name2,
81
- onAdd
82
- }) {
83
- const hookSet = hookMap.get(name2);
84
- if (hookSet)
85
- hookSet.add(callback);
86
- else
87
- hookMap.set(name2, /* @__PURE__ */ new Set([callback]));
88
- onAdd == null ? void 0 : onAdd(hookSet);
89
- return () => {
90
- var _a;
91
- (_a = hookMap.get(name2)) == null ? void 0 : _a.delete(callback);
92
- };
93
- }
94
- let resolveOnInitPromise = () => {
95
- };
96
- let isInit = false;
97
- const waitOnInit = new Promise((resolve) => {
98
- resolveOnInitPromise = resolve;
99
- });
100
- const [runOnInit, onInit] = createSyncHook("onInit", {
101
- onRun(callbacks) {
102
- isInit = true;
103
- resolveOnInitPromise();
104
- callbacks == null ? void 0 : callbacks.clear();
105
- },
106
- onAdd() {
107
- if (isInit)
108
- runOnInit();
109
- }
110
- });
111
18
  const logger = sdkShared.createLogger({
112
19
  scope: `${name}@${version}`
113
20
  });
114
- const [runOnRequest, onRequest] = createAsyncHook("onRequest");
115
- const [runOnResponse, onResponse] = createAsyncHook("onResponse");
116
21
  async function requestPreviews(account) {
117
22
  const previewObjects = getPreviewObjects();
118
23
  const [list, adSchema] = await Promise.all([
@@ -238,7 +143,7 @@ async function requestAd(options) {
238
143
  }
239
144
  async function requestAds(requestOptions) {
240
145
  var _a, _b, _c, _d, _e;
241
- const options = await runOnRequest(requestOptions);
146
+ const options = await requestOptions.context.hooks.runOnRequest(requestOptions);
242
147
  const { context } = options;
243
148
  try {
244
149
  (_a = context == null ? void 0 : context.events) == null ? void 0 : _a.requestAd.dispatch({
@@ -266,7 +171,7 @@ async function requestAds(requestOptions) {
266
171
  });
267
172
  if (matchedPreviews.length > 0)
268
173
  (_c = context.events) == null ? void 0 : _c.previewReceived.dispatch(matchedPreviews);
269
- const mergedResult = await runOnResponse([
174
+ const mergedResult = await context.hooks.runOnResponse([
270
175
  ...result.filter((ad) => !previews.some((preview) => preview.libId === ad.libId)),
271
176
  ...matchedPreviews
272
177
  ]);
@@ -278,19 +183,7 @@ async function requestAds(requestOptions) {
278
183
  throw error;
279
184
  }
280
185
  }
281
- const [runOnSlotCreate, onSlotCreate] = createSyncHook("onSlotCreate");
282
- let isDisposed = false;
283
- const [runOnDispose, onDispose] = createSyncHook("onDispose", {
284
- onRun(callbacks) {
285
- isDisposed = true;
286
- callbacks == null ? void 0 : callbacks.clear();
287
- },
288
- onAdd() {
289
- if (isDisposed)
290
- runOnDispose();
291
- }
292
- });
293
- function useQueryDetector(queries = {
186
+ function useQueryDetector(context, queries = {
294
187
  mobile: "(max-width: 768px)",
295
188
  tablet: "(min-width: 769px) and (max-width: 1024px)",
296
189
  desktop: "(min-width: 1025px)"
@@ -303,7 +196,7 @@ function useQueryDetector(queries = {
303
196
  }, {
304
197
  waitMs: 50
305
198
  });
306
- onInit(() => {
199
+ context.hooks.onInit(() => {
307
200
  for (const query of queryList)
308
201
  query.addEventListener("change", handleOnChange.call);
309
202
  handleOnChange.call();
@@ -312,7 +205,7 @@ function useQueryDetector(queries = {
312
205
  for (const query of queryList)
313
206
  query.removeEventListener("change", handleOnChange.call);
314
207
  }
315
- onDispose(dispose);
208
+ context.hooks.onDispose(dispose);
316
209
  return [sdkShared.computed(() => active.value), dispose];
317
210
  }
318
211
  function getQuery(entries) {
@@ -351,9 +244,9 @@ function renderInline(ad, element) {
351
244
  function generateName(location, format, slot) {
352
245
  return `${location}${slot ? `${slot}` : ""}-${format}`;
353
246
  }
354
- function useDomLoaded() {
247
+ function useDomLoaded(context) {
355
248
  const isDomLoaded = sdkShared.ref(false);
356
- onInit(async () => {
249
+ context.hooks.onInit(async () => {
357
250
  await sdkShared.waitForDomLoad();
358
251
  isDomLoaded.value = true;
359
252
  });
@@ -442,27 +335,27 @@ function useViewabilityObserver({ context, slotContext, hooks, onTracked }) {
442
335
  });
443
336
  return isTracked;
444
337
  }
445
- function useSlotHooks({ setup }, slotContext, id) {
446
- const [runOnBeforeRender, onBeforeRender, disposeOnBeforeRender] = createAsyncHook(`onBeforeRender:${id}`);
447
- const [runOnRender, onRender, disposeOnRender] = createAsyncHook(`onRender:${id}`);
448
- const [runOnBeforeRequest, onBeforeRequest, disposeOnBeforeRequest] = createAsyncHook(`onBeforeRequest:${id}`);
449
- const [runOnRequest2, onRequest2, disposeOnRequest] = createAsyncHook(`onRequest:${id}`);
450
- const [runOnDispose2, onDispose2, disposeOnDispose] = createPassiveHook(`onDispose:${id}`);
338
+ function useSlotHooks({ setup }, slotContext) {
339
+ const [runOnBeforeRender, onBeforeRender, disposeOnBeforeRender] = sdkShared.createAsyncHook();
340
+ const [runOnRender, onRender, disposeOnRender] = sdkShared.createAsyncHook();
341
+ const [runOnBeforeRequest, onBeforeRequest, disposeOnBeforeRequest] = sdkShared.createAsyncHook();
342
+ const [runOnRequest, onRequest, disposeOnRequest] = sdkShared.createAsyncHook();
343
+ const [runOnDispose, onDispose, disposeOnDispose] = sdkShared.createPassiveHook();
451
344
  setup == null ? void 0 : setup(slotContext, {
452
345
  onBeforeRender,
453
346
  onRender,
454
347
  onBeforeRequest,
455
- onDispose: onDispose2,
456
- onRequest: onRequest2
348
+ onDispose,
349
+ onRequest
457
350
  });
458
- onDispose2(() => {
351
+ onDispose(() => {
459
352
  disposeOnBeforeRender();
460
353
  disposeOnRender();
461
354
  disposeOnBeforeRequest();
462
355
  disposeOnRequest();
463
356
  disposeOnDispose();
464
357
  });
465
- return { runOnBeforeRender, runOnRender, runOnBeforeRequest, runOnRequest: runOnRequest2, runOnDispose: runOnDispose2, onDispose: onDispose2, onBeforeRequest, onRequest: onRequest2, onBeforeRender, onRender };
358
+ return { runOnBeforeRender, runOnRender, runOnBeforeRequest, runOnRequest, runOnDispose, onDispose, onBeforeRequest, onRequest, onBeforeRender, onRender };
466
359
  }
467
360
  const renderFunctions = {
468
361
  iframe: renderIframe,
@@ -477,7 +370,7 @@ function createSlot(slotOptions) {
477
370
  const scope = sdkShared.effectScope();
478
371
  return scope.run(() => {
479
372
  const slotContext = sdkShared.ref(null);
480
- const options = runOnSlotCreate({
373
+ const options = slotOptions.context.hooks.runOnSlotCreate({
481
374
  ...defaultOptions,
482
375
  ...slotOptions
483
376
  });
@@ -493,13 +386,13 @@ function createSlot(slotOptions) {
493
386
  runOnBeforeRender,
494
387
  runOnRender,
495
388
  runOnBeforeRequest,
496
- runOnRequest: runOnRequest2,
497
- runOnDispose: runOnDispose2,
389
+ runOnRequest,
390
+ runOnDispose,
498
391
  ...hooks
499
- } = useSlotHooks(options, slotContext, id);
500
- const isDisposed2 = sdkShared.ref(false);
392
+ } = useSlotHooks(options, slotContext);
393
+ const isDisposed = sdkShared.ref(false);
501
394
  const parameters = sdkShared.reactive(new Map(Object.entries(options.parameters ?? {})));
502
- const [device, disposeQueryDetector] = useQueryDetector(typeof options.format === "string" ? {
395
+ const [device, disposeQueryDetector] = useQueryDetector(context, typeof options.format === "string" ? {
503
396
  [options.format]: "(min-width: 0px)"
504
397
  } : Object.fromEntries(options.format.map((item) => [item.format, item.query])));
505
398
  const format = sdkShared.computed(() => typeof options.format === "string" ? options.format : device.value);
@@ -517,7 +410,7 @@ function createSlot(slotOptions) {
517
410
  data.value = newAd;
518
411
  originalData.value = newAd;
519
412
  });
520
- const isDomLoaded = useDomLoaded();
413
+ const isDomLoaded = useDomLoaded(context);
521
414
  const element = sdkShared.computed(
522
415
  () => {
523
416
  var _a;
@@ -544,7 +437,7 @@ function createSlot(slotOptions) {
544
437
  hooks.onDispose(() => {
545
438
  disposeQueryDetector();
546
439
  });
547
- onInit(async () => {
440
+ context.hooks.onInit(async () => {
548
441
  var _a;
549
442
  status.value = "initialized";
550
443
  if (options.lazyLoading)
@@ -580,7 +473,7 @@ function createSlot(slotOptions) {
580
473
  });
581
474
  }
582
475
  if (response)
583
- response = await runOnRequest2(response);
476
+ response = await runOnRequest(response);
584
477
  data.value = response;
585
478
  if (!originalData.value)
586
479
  originalData.value = response;
@@ -592,7 +485,6 @@ function createSlot(slotOptions) {
592
485
  async function render(adToRender) {
593
486
  status.value = "rendering";
594
487
  await sdkShared.waitForDomLoad();
595
- await waitOnInit;
596
488
  let renderAd = adToRender ?? data.value ?? originalData.value ?? await request();
597
489
  renderAd = renderAd && await runOnBeforeRender(renderAd);
598
490
  if (!renderAd) {
@@ -644,8 +536,8 @@ function createSlot(slotOptions) {
644
536
  cleanElement();
645
537
  (_a = impressionTrackingPixelElement.value) == null ? void 0 : _a.remove();
646
538
  data.value = null;
647
- runOnDispose2();
648
- isDisposed2.value = true;
539
+ runOnDispose();
540
+ isDisposed.value = true;
649
541
  scope.stop();
650
542
  }
651
543
  const state = sdkShared.reactive({
@@ -661,7 +553,7 @@ function createSlot(slotOptions) {
661
553
  isImpressionTracked,
662
554
  status,
663
555
  element,
664
- isDisposed: isDisposed2,
556
+ isDisposed,
665
557
  id,
666
558
  render,
667
559
  request,
@@ -708,7 +600,7 @@ function createSlotManager({
708
600
  function getAll() {
709
601
  return Array.from(context.slots).map(([, slot]) => slot);
710
602
  }
711
- function add2(options) {
603
+ function add(options) {
712
604
  var _a;
713
605
  const slot = createSlot({
714
606
  ...options,
@@ -755,14 +647,14 @@ function createSlotManager({
755
647
  context.slots.clear();
756
648
  }
757
649
  for (const options of initialSlots) {
758
- add2({
650
+ add({
759
651
  ...options,
760
652
  lazyLoading: false
761
653
  });
762
654
  }
763
655
  return {
764
656
  getAll,
765
- add: add2,
657
+ add,
766
658
  findDomSlots: findDomSlots$1,
767
659
  get,
768
660
  dispose
@@ -775,11 +667,11 @@ function useConsent(context) {
775
667
  if (data.tcString)
776
668
  consent.value = data.tcString;
777
669
  }
778
- onInit(() => {
670
+ context.hooks.onInit(() => {
779
671
  var _a;
780
672
  (_a = window.__tcfapi) == null ? void 0 : _a.call(window, "addEventListener", 2, onTcfConsentChange);
781
673
  });
782
- onDispose(() => {
674
+ context.hooks.onDispose(() => {
783
675
  var _a;
784
676
  (_a = window.__tcfapi) == null ? void 0 : _a.call(window, "removeEventListener", 2, onTcfConsentChange);
785
677
  });
@@ -816,7 +708,7 @@ async function fetchAllUnrenderedSlots(slots) {
816
708
  await Promise.allSettled(filteredSlots.map((slot) => slot.request()));
817
709
  }
818
710
  function useMainQueryDetector(mergedOptions, context) {
819
- const [device] = useQueryDetector(mergedOptions.queries);
711
+ const [device] = useQueryDetector(context, mergedOptions.queries);
820
712
  sdkShared.watch(device, async (newDevice) => {
821
713
  var _a, _b;
822
714
  context.device = newDevice;
@@ -840,7 +732,7 @@ function useMainDebugMode(context) {
840
732
  }, {
841
733
  immediate: true
842
734
  });
843
- onDispose(() => {
735
+ context.hooks.onDispose(() => {
844
736
  context.logger.resetLogs();
845
737
  context.logger.info("Adhese instance disposed");
846
738
  });
@@ -868,6 +760,56 @@ function useMainParameters(context, options) {
868
760
  }
869
761
  );
870
762
  }
763
+ function createGlobalHooks() {
764
+ const disposeFunctions = /* @__PURE__ */ new Set();
765
+ let isInit = false;
766
+ const [runOnInit, onInit, disposeOnInit] = sdkShared.createSyncHook({
767
+ onRun(callbacks) {
768
+ isInit = true;
769
+ callbacks == null ? void 0 : callbacks.clear();
770
+ },
771
+ onAdd() {
772
+ if (isInit)
773
+ runOnInit();
774
+ }
775
+ });
776
+ disposeFunctions.add(disposeOnInit);
777
+ let isDisposed = false;
778
+ const [runOnDispose, onDispose, disposeOnDispose] = sdkShared.createSyncHook({
779
+ onRun(callbacks) {
780
+ isDisposed = true;
781
+ callbacks == null ? void 0 : callbacks.clear();
782
+ },
783
+ onAdd() {
784
+ if (isDisposed)
785
+ runOnDispose();
786
+ }
787
+ });
788
+ disposeFunctions.add(disposeOnDispose);
789
+ const [runOnRequest, onRequest, disposeOnRequest] = sdkShared.createAsyncHook();
790
+ disposeFunctions.add(disposeOnRequest);
791
+ const [runOnResponse, onResponse, disposeOnResponse] = sdkShared.createAsyncHook();
792
+ disposeFunctions.add(disposeOnResponse);
793
+ const [runOnSlotCreate, onSlotCreate, disposeOnSlotCreate] = sdkShared.createSyncHook();
794
+ disposeFunctions.add(disposeOnSlotCreate);
795
+ function clearAll() {
796
+ for (const disposeFunction of disposeFunctions)
797
+ disposeFunction();
798
+ }
799
+ return {
800
+ runOnInit,
801
+ onInit,
802
+ runOnDispose,
803
+ onDispose,
804
+ runOnRequest,
805
+ onRequest,
806
+ runOnResponse,
807
+ onResponse,
808
+ runOnSlotCreate,
809
+ onSlotCreate,
810
+ clearAll
811
+ };
812
+ }
871
813
  function createAdhese(options) {
872
814
  const scope = sdkShared.effectScope();
873
815
  return scope.run(() => {
@@ -888,6 +830,7 @@ function createAdhese(options) {
888
830
  ...options
889
831
  };
890
832
  setupLogging(mergedOptions);
833
+ const hooks = createGlobalHooks();
891
834
  const context = sdkShared.reactive({
892
835
  location: mergedOptions.location,
893
836
  consent: mergedOptions.consent,
@@ -899,6 +842,7 @@ function createAdhese(options) {
899
842
  events: sdkShared.createEventManager(),
900
843
  slots: /* @__PURE__ */ new Map(),
901
844
  device: "unknown",
845
+ hooks,
902
846
  dispose,
903
847
  findDomSlots: findDomSlots2,
904
848
  getAll,
@@ -909,11 +853,7 @@ function createAdhese(options) {
909
853
  plugin(context, {
910
854
  index,
911
855
  version,
912
- onInit,
913
- onDispose,
914
- onRequest,
915
- onResponse,
916
- onSlotCreate
856
+ hooks
917
857
  });
918
858
  }
919
859
  sdkShared.watch(() => context.location, (newLocation) => {
@@ -954,12 +894,12 @@ function createAdhese(options) {
954
894
  slotManager.dispose();
955
895
  (_a = context.parameters) == null ? void 0 : _a.clear();
956
896
  (_b = context.events) == null ? void 0 : _b.dispose();
957
- runOnDispose();
958
- clearAllHooks();
897
+ hooks.runOnDispose();
898
+ hooks.clearAll();
959
899
  scope.stop();
960
900
  }
961
901
  context.dispose = dispose;
962
- onInit(async () => {
902
+ hooks.onInit(async () => {
963
903
  var _a;
964
904
  await sdkShared.awaitTimeout(0);
965
905
  if ((slotManager.getAll().length ?? 0) > 0)
@@ -971,7 +911,7 @@ function createAdhese(options) {
971
911
  if (!scope.active)
972
912
  dispose();
973
913
  });
974
- runOnInit();
914
+ hooks.runOnInit();
975
915
  return context;
976
916
  });
977
917
  }