@adhese/sdk 0.16.2 → 0.18.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 +26 -0
- package/dist/index.cjs +105 -174
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +34 -47
- package/dist/index.js +104 -173
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# @adhese/sdk
|
|
2
2
|
|
|
3
|
+
## 0.18.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- d52b465: Allow modifying slot data from within a slot's setup function
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [a4b013d]
|
|
12
|
+
- Updated dependencies [696b4ea]
|
|
13
|
+
- @adhese/sdk-shared@0.7.0
|
|
14
|
+
|
|
15
|
+
## 0.17.0
|
|
16
|
+
|
|
17
|
+
### Minor Changes
|
|
18
|
+
|
|
19
|
+
- ef68972: Add hooks to context instead of global variable which could break when adhese was disposed
|
|
20
|
+
- ef68972: Move hook create functions to @adhese/sdk-shared
|
|
21
|
+
|
|
22
|
+
### Patch Changes
|
|
23
|
+
|
|
24
|
+
- Updated dependencies [2c01e34]
|
|
25
|
+
- Updated dependencies [ad9b38f]
|
|
26
|
+
- Updated dependencies [ef68972]
|
|
27
|
+
- @adhese/sdk-shared@0.6.0
|
|
28
|
+
|
|
3
29
|
## 0.16.2
|
|
4
30
|
|
|
5
31
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -3,116 +3,10 @@ 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.
|
|
7
|
-
function addTrackingPixel(url) {
|
|
8
|
-
const img = document.createElement("img");
|
|
9
|
-
img.src = url.toString();
|
|
10
|
-
img.style.height = "1px";
|
|
11
|
-
img.style.width = "1px";
|
|
12
|
-
img.style.margin = "-1px";
|
|
13
|
-
img.style.border = "0";
|
|
14
|
-
img.style.position = "absolute";
|
|
15
|
-
img.style.top = "0";
|
|
16
|
-
return document.body.appendChild(img);
|
|
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
|
-
});
|
|
6
|
+
const version = "0.18.0";
|
|
111
7
|
const logger = sdkShared.createLogger({
|
|
112
8
|
scope: `${name}@${version}`
|
|
113
9
|
});
|
|
114
|
-
const [runOnRequest, onRequest] = createAsyncHook("onRequest");
|
|
115
|
-
const [runOnResponse, onResponse] = createAsyncHook("onResponse");
|
|
116
10
|
async function requestPreviews(account) {
|
|
117
11
|
const previewObjects = getPreviewObjects();
|
|
118
12
|
const [list, adSchema] = await Promise.all([
|
|
@@ -238,7 +132,7 @@ async function requestAd(options) {
|
|
|
238
132
|
}
|
|
239
133
|
async function requestAds(requestOptions) {
|
|
240
134
|
var _a, _b, _c, _d, _e;
|
|
241
|
-
const options = await runOnRequest(requestOptions);
|
|
135
|
+
const options = await requestOptions.context.hooks.runOnRequest(requestOptions);
|
|
242
136
|
const { context } = options;
|
|
243
137
|
try {
|
|
244
138
|
(_a = context == null ? void 0 : context.events) == null ? void 0 : _a.requestAd.dispatch({
|
|
@@ -266,7 +160,7 @@ async function requestAds(requestOptions) {
|
|
|
266
160
|
});
|
|
267
161
|
if (matchedPreviews.length > 0)
|
|
268
162
|
(_c = context.events) == null ? void 0 : _c.previewReceived.dispatch(matchedPreviews);
|
|
269
|
-
const mergedResult = await runOnResponse([
|
|
163
|
+
const mergedResult = await context.hooks.runOnResponse([
|
|
270
164
|
...result.filter((ad) => !previews.some((preview) => preview.libId === ad.libId)),
|
|
271
165
|
...matchedPreviews
|
|
272
166
|
]);
|
|
@@ -278,19 +172,7 @@ async function requestAds(requestOptions) {
|
|
|
278
172
|
throw error;
|
|
279
173
|
}
|
|
280
174
|
}
|
|
281
|
-
|
|
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 = {
|
|
175
|
+
function useQueryDetector(context, queries = {
|
|
294
176
|
mobile: "(max-width: 768px)",
|
|
295
177
|
tablet: "(min-width: 769px) and (max-width: 1024px)",
|
|
296
178
|
desktop: "(min-width: 1025px)"
|
|
@@ -303,7 +185,7 @@ function useQueryDetector(queries = {
|
|
|
303
185
|
}, {
|
|
304
186
|
waitMs: 50
|
|
305
187
|
});
|
|
306
|
-
onInit(() => {
|
|
188
|
+
context.hooks.onInit(() => {
|
|
307
189
|
for (const query of queryList)
|
|
308
190
|
query.addEventListener("change", handleOnChange.call);
|
|
309
191
|
handleOnChange.call();
|
|
@@ -312,7 +194,7 @@ function useQueryDetector(queries = {
|
|
|
312
194
|
for (const query of queryList)
|
|
313
195
|
query.removeEventListener("change", handleOnChange.call);
|
|
314
196
|
}
|
|
315
|
-
onDispose(dispose);
|
|
197
|
+
context.hooks.onDispose(dispose);
|
|
316
198
|
return [sdkShared.computed(() => active.value), dispose];
|
|
317
199
|
}
|
|
318
200
|
function getQuery(entries) {
|
|
@@ -351,9 +233,9 @@ function renderInline(ad, element) {
|
|
|
351
233
|
function generateName(location, format, slot) {
|
|
352
234
|
return `${location}${slot ? `${slot}` : ""}-${format}`;
|
|
353
235
|
}
|
|
354
|
-
function useDomLoaded() {
|
|
236
|
+
function useDomLoaded(context) {
|
|
355
237
|
const isDomLoaded = sdkShared.ref(false);
|
|
356
|
-
onInit(async () => {
|
|
238
|
+
context.hooks.onInit(async () => {
|
|
357
239
|
await sdkShared.waitForDomLoad();
|
|
358
240
|
isDomLoaded.value = true;
|
|
359
241
|
});
|
|
@@ -398,7 +280,7 @@ function useViewabilityObserver({ context, slotContext, hooks, onTracked }) {
|
|
|
398
280
|
...context.options.viewabilityTrackingOptions
|
|
399
281
|
};
|
|
400
282
|
const trackingPixel = sdkShared.ref(null);
|
|
401
|
-
const isTracked = sdkShared.
|
|
283
|
+
const isTracked = sdkShared.ref(false);
|
|
402
284
|
const viewabilityObserver = new IntersectionObserver(([entry]) => {
|
|
403
285
|
if (context.options.viewabilityTracking && !trackingPixel.value) {
|
|
404
286
|
const ratio = remeda.round(entry.intersectionRatio, 1);
|
|
@@ -406,6 +288,7 @@ function useViewabilityObserver({ context, slotContext, hooks, onTracked }) {
|
|
|
406
288
|
timeoutId = setTimeout(() => {
|
|
407
289
|
timeoutId = null;
|
|
408
290
|
onTracked == null ? void 0 : onTracked(trackingPixel);
|
|
291
|
+
isTracked.value = true;
|
|
409
292
|
}, duration);
|
|
410
293
|
} else if (ratio < threshold && timeoutId) {
|
|
411
294
|
clearTimeout(timeoutId);
|
|
@@ -442,27 +325,27 @@ function useViewabilityObserver({ context, slotContext, hooks, onTracked }) {
|
|
|
442
325
|
});
|
|
443
326
|
return isTracked;
|
|
444
327
|
}
|
|
445
|
-
function useSlotHooks({ setup }, slotContext
|
|
446
|
-
const [runOnBeforeRender, onBeforeRender, disposeOnBeforeRender] = createAsyncHook(
|
|
447
|
-
const [runOnRender, onRender, disposeOnRender] = createAsyncHook(
|
|
448
|
-
const [runOnBeforeRequest, onBeforeRequest, disposeOnBeforeRequest] = createAsyncHook(
|
|
449
|
-
const [
|
|
450
|
-
const [
|
|
328
|
+
function useSlotHooks({ setup }, slotContext) {
|
|
329
|
+
const [runOnBeforeRender, onBeforeRender, disposeOnBeforeRender] = sdkShared.createAsyncHook();
|
|
330
|
+
const [runOnRender, onRender, disposeOnRender] = sdkShared.createAsyncHook();
|
|
331
|
+
const [runOnBeforeRequest, onBeforeRequest, disposeOnBeforeRequest] = sdkShared.createAsyncHook();
|
|
332
|
+
const [runOnRequest, onRequest, disposeOnRequest] = sdkShared.createAsyncHook();
|
|
333
|
+
const [runOnDispose, onDispose, disposeOnDispose] = sdkShared.createPassiveHook();
|
|
451
334
|
setup == null ? void 0 : setup(slotContext, {
|
|
452
335
|
onBeforeRender,
|
|
453
336
|
onRender,
|
|
454
337
|
onBeforeRequest,
|
|
455
|
-
onDispose
|
|
456
|
-
onRequest
|
|
338
|
+
onDispose,
|
|
339
|
+
onRequest
|
|
457
340
|
});
|
|
458
|
-
|
|
341
|
+
onDispose(() => {
|
|
459
342
|
disposeOnBeforeRender();
|
|
460
343
|
disposeOnRender();
|
|
461
344
|
disposeOnBeforeRequest();
|
|
462
345
|
disposeOnRequest();
|
|
463
346
|
disposeOnDispose();
|
|
464
347
|
});
|
|
465
|
-
return { runOnBeforeRender, runOnRender, runOnBeforeRequest, runOnRequest
|
|
348
|
+
return { runOnBeforeRender, runOnRender, runOnBeforeRequest, runOnRequest, runOnDispose, onDispose, onBeforeRequest, onRequest, onBeforeRender, onRender };
|
|
466
349
|
}
|
|
467
350
|
const renderFunctions = {
|
|
468
351
|
iframe: renderIframe,
|
|
@@ -477,7 +360,7 @@ function createSlot(slotOptions) {
|
|
|
477
360
|
const scope = sdkShared.effectScope();
|
|
478
361
|
return scope.run(() => {
|
|
479
362
|
const slotContext = sdkShared.ref(null);
|
|
480
|
-
const options = runOnSlotCreate({
|
|
363
|
+
const options = slotOptions.context.hooks.runOnSlotCreate({
|
|
481
364
|
...defaultOptions,
|
|
482
365
|
...slotOptions
|
|
483
366
|
});
|
|
@@ -493,13 +376,13 @@ function createSlot(slotOptions) {
|
|
|
493
376
|
runOnBeforeRender,
|
|
494
377
|
runOnRender,
|
|
495
378
|
runOnBeforeRequest,
|
|
496
|
-
runOnRequest
|
|
497
|
-
runOnDispose
|
|
379
|
+
runOnRequest,
|
|
380
|
+
runOnDispose,
|
|
498
381
|
...hooks
|
|
499
|
-
} = useSlotHooks(options, slotContext
|
|
500
|
-
const
|
|
382
|
+
} = useSlotHooks(options, slotContext);
|
|
383
|
+
const isDisposed = sdkShared.ref(false);
|
|
501
384
|
const parameters = sdkShared.reactive(new Map(Object.entries(options.parameters ?? {})));
|
|
502
|
-
const [device, disposeQueryDetector] = useQueryDetector(typeof options.format === "string" ? {
|
|
385
|
+
const [device, disposeQueryDetector] = useQueryDetector(context, typeof options.format === "string" ? {
|
|
503
386
|
[options.format]: "(min-width: 0px)"
|
|
504
387
|
} : Object.fromEntries(options.format.map((item) => [item.format, item.query])));
|
|
505
388
|
const format = sdkShared.computed(() => typeof options.format === "string" ? options.format : device.value);
|
|
@@ -517,7 +400,7 @@ function createSlot(slotOptions) {
|
|
|
517
400
|
data.value = newAd;
|
|
518
401
|
originalData.value = newAd;
|
|
519
402
|
});
|
|
520
|
-
const isDomLoaded = useDomLoaded();
|
|
403
|
+
const isDomLoaded = useDomLoaded(context);
|
|
521
404
|
const element = sdkShared.computed(
|
|
522
405
|
() => {
|
|
523
406
|
var _a;
|
|
@@ -544,7 +427,7 @@ function createSlot(slotOptions) {
|
|
|
544
427
|
hooks.onDispose(() => {
|
|
545
428
|
disposeQueryDetector();
|
|
546
429
|
});
|
|
547
|
-
onInit(async () => {
|
|
430
|
+
context.hooks.onInit(async () => {
|
|
548
431
|
var _a;
|
|
549
432
|
status.value = "initialized";
|
|
550
433
|
if (options.lazyLoading)
|
|
@@ -558,13 +441,17 @@ function createSlot(slotOptions) {
|
|
|
558
441
|
onTracked(trackingPixel) {
|
|
559
442
|
var _a, _b, _c, _d, _e;
|
|
560
443
|
if ((_b = (_a = slotContext.value) == null ? void 0 : _a.data) == null ? void 0 : _b.viewableImpressionCounter) {
|
|
561
|
-
trackingPixel.value = addTrackingPixel((_d = (_c = slotContext.value) == null ? void 0 : _c.data) == null ? void 0 : _d.viewableImpressionCounter);
|
|
444
|
+
trackingPixel.value = sdkShared.addTrackingPixel((_d = (_c = slotContext.value) == null ? void 0 : _c.data) == null ? void 0 : _d.viewableImpressionCounter);
|
|
562
445
|
context.logger.debug(`Viewability tracking pixel fired for ${(_e = slotContext.value) == null ? void 0 : _e.name}`);
|
|
563
446
|
}
|
|
564
447
|
}
|
|
565
448
|
});
|
|
566
449
|
const impressionTrackingPixelElement = sdkShared.ref(null);
|
|
567
|
-
const isImpressionTracked = sdkShared.
|
|
450
|
+
const isImpressionTracked = sdkShared.ref(false);
|
|
451
|
+
hooks.onDispose(() => {
|
|
452
|
+
if (impressionTrackingPixelElement.value)
|
|
453
|
+
impressionTrackingPixelElement.value.remove();
|
|
454
|
+
});
|
|
568
455
|
async function request() {
|
|
569
456
|
if (options.lazyLoading && !isInViewport.value)
|
|
570
457
|
return null;
|
|
@@ -580,7 +467,7 @@ function createSlot(slotOptions) {
|
|
|
580
467
|
});
|
|
581
468
|
}
|
|
582
469
|
if (response)
|
|
583
|
-
response = await
|
|
470
|
+
response = await runOnRequest(response);
|
|
584
471
|
data.value = response;
|
|
585
472
|
if (!originalData.value)
|
|
586
473
|
originalData.value = response;
|
|
@@ -592,7 +479,6 @@ function createSlot(slotOptions) {
|
|
|
592
479
|
async function render(adToRender) {
|
|
593
480
|
status.value = "rendering";
|
|
594
481
|
await sdkShared.waitForDomLoad();
|
|
595
|
-
await waitOnInit;
|
|
596
482
|
let renderAd = adToRender ?? data.value ?? originalData.value ?? await request();
|
|
597
483
|
renderAd = renderAd && await runOnBeforeRender(renderAd);
|
|
598
484
|
if (!renderAd) {
|
|
@@ -615,10 +501,9 @@ function createSlot(slotOptions) {
|
|
|
615
501
|
throw new Error(error);
|
|
616
502
|
}
|
|
617
503
|
renderFunctions[renderMode](renderAd, element.value);
|
|
618
|
-
if (renderAd.impressionCounter && !impressionTrackingPixelElement.value)
|
|
619
|
-
impressionTrackingPixelElement.value = addTrackingPixel(renderAd.impressionCounter);
|
|
620
|
-
|
|
621
|
-
}
|
|
504
|
+
if (renderAd.impressionCounter && !impressionTrackingPixelElement.value)
|
|
505
|
+
impressionTrackingPixelElement.value = sdkShared.addTrackingPixel(renderAd.impressionCounter);
|
|
506
|
+
isImpressionTracked.value = true;
|
|
622
507
|
logger.debug("Slot rendered", {
|
|
623
508
|
renderedElement: element,
|
|
624
509
|
location: context.location,
|
|
@@ -640,12 +525,10 @@ function createSlot(slotOptions) {
|
|
|
640
525
|
originalData.value = null;
|
|
641
526
|
}
|
|
642
527
|
function dispose() {
|
|
643
|
-
var _a;
|
|
644
528
|
cleanElement();
|
|
645
|
-
(_a = impressionTrackingPixelElement.value) == null ? void 0 : _a.remove();
|
|
646
529
|
data.value = null;
|
|
647
|
-
|
|
648
|
-
|
|
530
|
+
runOnDispose();
|
|
531
|
+
isDisposed.value = true;
|
|
649
532
|
scope.stop();
|
|
650
533
|
}
|
|
651
534
|
const state = sdkShared.reactive({
|
|
@@ -661,7 +544,7 @@ function createSlot(slotOptions) {
|
|
|
661
544
|
isImpressionTracked,
|
|
662
545
|
status,
|
|
663
546
|
element,
|
|
664
|
-
isDisposed
|
|
547
|
+
isDisposed,
|
|
665
548
|
id,
|
|
666
549
|
render,
|
|
667
550
|
request,
|
|
@@ -708,7 +591,7 @@ function createSlotManager({
|
|
|
708
591
|
function getAll() {
|
|
709
592
|
return Array.from(context.slots).map(([, slot]) => slot);
|
|
710
593
|
}
|
|
711
|
-
function
|
|
594
|
+
function add(options) {
|
|
712
595
|
var _a;
|
|
713
596
|
const slot = createSlot({
|
|
714
597
|
...options,
|
|
@@ -755,14 +638,14 @@ function createSlotManager({
|
|
|
755
638
|
context.slots.clear();
|
|
756
639
|
}
|
|
757
640
|
for (const options of initialSlots) {
|
|
758
|
-
|
|
641
|
+
add({
|
|
759
642
|
...options,
|
|
760
643
|
lazyLoading: false
|
|
761
644
|
});
|
|
762
645
|
}
|
|
763
646
|
return {
|
|
764
647
|
getAll,
|
|
765
|
-
add
|
|
648
|
+
add,
|
|
766
649
|
findDomSlots: findDomSlots$1,
|
|
767
650
|
get,
|
|
768
651
|
dispose
|
|
@@ -775,11 +658,11 @@ function useConsent(context) {
|
|
|
775
658
|
if (data.tcString)
|
|
776
659
|
consent.value = data.tcString;
|
|
777
660
|
}
|
|
778
|
-
onInit(() => {
|
|
661
|
+
context.hooks.onInit(() => {
|
|
779
662
|
var _a;
|
|
780
663
|
(_a = window.__tcfapi) == null ? void 0 : _a.call(window, "addEventListener", 2, onTcfConsentChange);
|
|
781
664
|
});
|
|
782
|
-
onDispose(() => {
|
|
665
|
+
context.hooks.onDispose(() => {
|
|
783
666
|
var _a;
|
|
784
667
|
(_a = window.__tcfapi) == null ? void 0 : _a.call(window, "removeEventListener", 2, onTcfConsentChange);
|
|
785
668
|
});
|
|
@@ -816,7 +699,7 @@ async function fetchAllUnrenderedSlots(slots) {
|
|
|
816
699
|
await Promise.allSettled(filteredSlots.map((slot) => slot.request()));
|
|
817
700
|
}
|
|
818
701
|
function useMainQueryDetector(mergedOptions, context) {
|
|
819
|
-
const [device] = useQueryDetector(mergedOptions.queries);
|
|
702
|
+
const [device] = useQueryDetector(context, mergedOptions.queries);
|
|
820
703
|
sdkShared.watch(device, async (newDevice) => {
|
|
821
704
|
var _a, _b;
|
|
822
705
|
context.device = newDevice;
|
|
@@ -840,7 +723,7 @@ function useMainDebugMode(context) {
|
|
|
840
723
|
}, {
|
|
841
724
|
immediate: true
|
|
842
725
|
});
|
|
843
|
-
onDispose(() => {
|
|
726
|
+
context.hooks.onDispose(() => {
|
|
844
727
|
context.logger.resetLogs();
|
|
845
728
|
context.logger.info("Adhese instance disposed");
|
|
846
729
|
});
|
|
@@ -868,6 +751,56 @@ function useMainParameters(context, options) {
|
|
|
868
751
|
}
|
|
869
752
|
);
|
|
870
753
|
}
|
|
754
|
+
function createGlobalHooks() {
|
|
755
|
+
const disposeFunctions = /* @__PURE__ */ new Set();
|
|
756
|
+
let isInit = false;
|
|
757
|
+
const [runOnInit, onInit, disposeOnInit] = sdkShared.createSyncHook({
|
|
758
|
+
onRun(callbacks) {
|
|
759
|
+
isInit = true;
|
|
760
|
+
callbacks == null ? void 0 : callbacks.clear();
|
|
761
|
+
},
|
|
762
|
+
onAdd() {
|
|
763
|
+
if (isInit)
|
|
764
|
+
runOnInit();
|
|
765
|
+
}
|
|
766
|
+
});
|
|
767
|
+
disposeFunctions.add(disposeOnInit);
|
|
768
|
+
let isDisposed = false;
|
|
769
|
+
const [runOnDispose, onDispose, disposeOnDispose] = sdkShared.createSyncHook({
|
|
770
|
+
onRun(callbacks) {
|
|
771
|
+
isDisposed = true;
|
|
772
|
+
callbacks == null ? void 0 : callbacks.clear();
|
|
773
|
+
},
|
|
774
|
+
onAdd() {
|
|
775
|
+
if (isDisposed)
|
|
776
|
+
runOnDispose();
|
|
777
|
+
}
|
|
778
|
+
});
|
|
779
|
+
disposeFunctions.add(disposeOnDispose);
|
|
780
|
+
const [runOnRequest, onRequest, disposeOnRequest] = sdkShared.createAsyncHook();
|
|
781
|
+
disposeFunctions.add(disposeOnRequest);
|
|
782
|
+
const [runOnResponse, onResponse, disposeOnResponse] = sdkShared.createAsyncHook();
|
|
783
|
+
disposeFunctions.add(disposeOnResponse);
|
|
784
|
+
const [runOnSlotCreate, onSlotCreate, disposeOnSlotCreate] = sdkShared.createSyncHook();
|
|
785
|
+
disposeFunctions.add(disposeOnSlotCreate);
|
|
786
|
+
function clearAll() {
|
|
787
|
+
for (const disposeFunction of disposeFunctions)
|
|
788
|
+
disposeFunction();
|
|
789
|
+
}
|
|
790
|
+
return {
|
|
791
|
+
runOnInit,
|
|
792
|
+
onInit,
|
|
793
|
+
runOnDispose,
|
|
794
|
+
onDispose,
|
|
795
|
+
runOnRequest,
|
|
796
|
+
onRequest,
|
|
797
|
+
runOnResponse,
|
|
798
|
+
onResponse,
|
|
799
|
+
runOnSlotCreate,
|
|
800
|
+
onSlotCreate,
|
|
801
|
+
clearAll
|
|
802
|
+
};
|
|
803
|
+
}
|
|
871
804
|
function createAdhese(options) {
|
|
872
805
|
const scope = sdkShared.effectScope();
|
|
873
806
|
return scope.run(() => {
|
|
@@ -888,6 +821,7 @@ function createAdhese(options) {
|
|
|
888
821
|
...options
|
|
889
822
|
};
|
|
890
823
|
setupLogging(mergedOptions);
|
|
824
|
+
const hooks = createGlobalHooks();
|
|
891
825
|
const context = sdkShared.reactive({
|
|
892
826
|
location: mergedOptions.location,
|
|
893
827
|
consent: mergedOptions.consent,
|
|
@@ -899,6 +833,7 @@ function createAdhese(options) {
|
|
|
899
833
|
events: sdkShared.createEventManager(),
|
|
900
834
|
slots: /* @__PURE__ */ new Map(),
|
|
901
835
|
device: "unknown",
|
|
836
|
+
hooks,
|
|
902
837
|
dispose,
|
|
903
838
|
findDomSlots: findDomSlots2,
|
|
904
839
|
getAll,
|
|
@@ -909,11 +844,7 @@ function createAdhese(options) {
|
|
|
909
844
|
plugin(context, {
|
|
910
845
|
index,
|
|
911
846
|
version,
|
|
912
|
-
|
|
913
|
-
onDispose,
|
|
914
|
-
onRequest,
|
|
915
|
-
onResponse,
|
|
916
|
-
onSlotCreate
|
|
847
|
+
hooks
|
|
917
848
|
});
|
|
918
849
|
}
|
|
919
850
|
sdkShared.watch(() => context.location, (newLocation) => {
|
|
@@ -954,12 +885,12 @@ function createAdhese(options) {
|
|
|
954
885
|
slotManager.dispose();
|
|
955
886
|
(_a = context.parameters) == null ? void 0 : _a.clear();
|
|
956
887
|
(_b = context.events) == null ? void 0 : _b.dispose();
|
|
957
|
-
runOnDispose();
|
|
958
|
-
|
|
888
|
+
hooks.runOnDispose();
|
|
889
|
+
hooks.clearAll();
|
|
959
890
|
scope.stop();
|
|
960
891
|
}
|
|
961
892
|
context.dispose = dispose;
|
|
962
|
-
onInit(async () => {
|
|
893
|
+
hooks.onInit(async () => {
|
|
963
894
|
var _a;
|
|
964
895
|
await sdkShared.awaitTimeout(0);
|
|
965
896
|
if ((slotManager.getAll().length ?? 0) > 0)
|
|
@@ -971,7 +902,7 @@ function createAdhese(options) {
|
|
|
971
902
|
if (!scope.active)
|
|
972
903
|
dispose();
|
|
973
904
|
});
|
|
974
|
-
runOnInit();
|
|
905
|
+
hooks.runOnInit();
|
|
975
906
|
return context;
|
|
976
907
|
});
|
|
977
908
|
}
|