@adhese/sdk 0.10.0 → 0.11.1
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.cjs +57 -98
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +12 -66
- package/dist/index.js +50 -91
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
package/dist/index.cjs
CHANGED
|
@@ -1,76 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const sdkShared = require("@adhese/sdk-shared");
|
|
3
4
|
const runtimeCore = require("@vue/runtime-core");
|
|
4
5
|
const remeda = require("remeda");
|
|
5
6
|
const zod = require("zod");
|
|
6
|
-
async function waitForDomLoad() {
|
|
7
|
-
return new Promise((resolve) => {
|
|
8
|
-
function onDomLoad() {
|
|
9
|
-
resolve();
|
|
10
|
-
window.removeEventListener("DOMContentLoaded", onDomLoad);
|
|
11
|
-
}
|
|
12
|
-
if (document.readyState === "loading")
|
|
13
|
-
document.addEventListener("DOMContentLoaded", onDomLoad);
|
|
14
|
-
else
|
|
15
|
-
resolve();
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
function createEventManager() {
|
|
19
|
-
const disposables = /* @__PURE__ */ new Set();
|
|
20
|
-
function dispose() {
|
|
21
|
-
for (const disposable of disposables)
|
|
22
|
-
disposable();
|
|
23
|
-
}
|
|
24
|
-
return new Proxy({
|
|
25
|
-
dispose
|
|
26
|
-
}, {
|
|
27
|
-
// eslint-disable-next-line ts/explicit-function-return-type
|
|
28
|
-
get(target, key, receiver) {
|
|
29
|
-
if (!(key in target) && typeof key === "string") {
|
|
30
|
-
const event = createEvent();
|
|
31
|
-
disposables.add(() => {
|
|
32
|
-
event.listeners.clear();
|
|
33
|
-
});
|
|
34
|
-
Reflect.set(target, key, event, receiver);
|
|
35
|
-
}
|
|
36
|
-
return Reflect.get(target, key, receiver);
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
function createEvent() {
|
|
41
|
-
const listeners = /* @__PURE__ */ new Set();
|
|
42
|
-
function dispatch(data) {
|
|
43
|
-
for (const listener of listeners)
|
|
44
|
-
void listener(data);
|
|
45
|
-
}
|
|
46
|
-
async function dispatchAsync(data) {
|
|
47
|
-
await Promise.allSettled(
|
|
48
|
-
Array.from(listeners).map((listener) => listener(data))
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
function addListener(listener) {
|
|
52
|
-
listeners.add(listener);
|
|
53
|
-
}
|
|
54
|
-
function removeListener(listener) {
|
|
55
|
-
listeners.delete(listener);
|
|
56
|
-
}
|
|
57
|
-
return {
|
|
58
|
-
listeners,
|
|
59
|
-
dispatch,
|
|
60
|
-
dispatchAsync,
|
|
61
|
-
addListener,
|
|
62
|
-
removeListener
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
const savedIds = /* @__PURE__ */ new Set();
|
|
66
|
-
function uniqueId() {
|
|
67
|
-
let id;
|
|
68
|
-
do
|
|
69
|
-
id = Math.random().toString(36).slice(2);
|
|
70
|
-
while (savedIds.has(id));
|
|
71
|
-
savedIds.add(id);
|
|
72
|
-
return id;
|
|
73
|
-
}
|
|
74
7
|
function createSafeFrame({
|
|
75
8
|
renderFile,
|
|
76
9
|
context
|
|
@@ -91,7 +24,7 @@ function createSafeFrame({
|
|
|
91
24
|
const html = ad.ext === "js" && ad.body ? ad.body : ad.tag;
|
|
92
25
|
if (typeof html !== "string")
|
|
93
26
|
throw new Error("Ad tag is not a string");
|
|
94
|
-
const elementId = element.id || `ad-${ad.id}-${uniqueId()}`;
|
|
27
|
+
const elementId = element.id || `ad-${ad.id}-${sdkShared.uniqueId()}`;
|
|
95
28
|
element.id = elementId;
|
|
96
29
|
const position = new safeFrame.host.Position({
|
|
97
30
|
id: elementId,
|
|
@@ -125,7 +58,7 @@ function createSafeFrame({
|
|
|
125
58
|
}
|
|
126
59
|
const name = "@adhese/sdk";
|
|
127
60
|
const type = "module";
|
|
128
|
-
const version = "0.
|
|
61
|
+
const version = "0.11.1";
|
|
129
62
|
const description = "Adhese SDK";
|
|
130
63
|
const license = "GPL-3.0";
|
|
131
64
|
const repository = {
|
|
@@ -155,9 +88,10 @@ const scripts = {
|
|
|
155
88
|
prepareRelease: "npm run build"
|
|
156
89
|
};
|
|
157
90
|
const dependencies = {
|
|
91
|
+
"@adhese/sdk-shared": "^0.1.0",
|
|
158
92
|
"@vue/runtime-core": "^3.4.21",
|
|
159
93
|
remeda: "^1.61.0",
|
|
160
|
-
zod: "^3.23.
|
|
94
|
+
zod: "^3.23.4"
|
|
161
95
|
};
|
|
162
96
|
const packageJson = {
|
|
163
97
|
name,
|
|
@@ -343,7 +277,7 @@ function createLogger({
|
|
|
343
277
|
}) {
|
|
344
278
|
const logs = /* @__PURE__ */ new Set();
|
|
345
279
|
let currentMinLogLevelThreshold = minLogLevelThreshold;
|
|
346
|
-
const events = createEventManager();
|
|
280
|
+
const events = sdkShared.createEventManager();
|
|
347
281
|
const logFunctions = Object.fromEntries(logLevels.map((level, index) => {
|
|
348
282
|
const logFunction = (message, attributes) => {
|
|
349
283
|
logs.add({
|
|
@@ -352,7 +286,7 @@ function createLogger({
|
|
|
352
286
|
message,
|
|
353
287
|
attributes,
|
|
354
288
|
timestamp: Date.now(),
|
|
355
|
-
id: uniqueId()
|
|
289
|
+
id: sdkShared.uniqueId()
|
|
356
290
|
});
|
|
357
291
|
events.log.dispatch({
|
|
358
292
|
scope,
|
|
@@ -360,7 +294,7 @@ function createLogger({
|
|
|
360
294
|
message,
|
|
361
295
|
attributes,
|
|
362
296
|
timestamp: Date.now(),
|
|
363
|
-
id: uniqueId()
|
|
297
|
+
id: sdkShared.uniqueId()
|
|
364
298
|
});
|
|
365
299
|
if (index >= logLevels.indexOf(currentMinLogLevelThreshold)) {
|
|
366
300
|
if (["warn", "error", "trace"].includes(level)) {
|
|
@@ -652,6 +586,36 @@ function filterSpecialChars(value) {
|
|
|
652
586
|
const specialRegex = /[^\p{L}\p{N}_]/gu;
|
|
653
587
|
return value.replaceAll(specialRegex, "_");
|
|
654
588
|
}
|
|
589
|
+
const batch = /* @__PURE__ */ new Map();
|
|
590
|
+
const debouncedRequestAds = remeda.debounce(async (context) => {
|
|
591
|
+
if (batch.size === 0)
|
|
592
|
+
return [];
|
|
593
|
+
const ads = await requestAds({
|
|
594
|
+
slots: Array.from(batch.values()).map(({ options }) => options.slot),
|
|
595
|
+
context
|
|
596
|
+
});
|
|
597
|
+
for (const { options, resolve, reject } of batch.values()) {
|
|
598
|
+
const ad = ads.find(({ slotName }) => runtimeCore.toValue(slotName) === runtimeCore.toValue(options.slot.name));
|
|
599
|
+
if (ad)
|
|
600
|
+
resolve(ad);
|
|
601
|
+
else
|
|
602
|
+
reject(new Error(`Ad: ${runtimeCore.toValue(options.slot.name)} not found`));
|
|
603
|
+
}
|
|
604
|
+
batch.clear();
|
|
605
|
+
return ads;
|
|
606
|
+
}, {
|
|
607
|
+
waitMs: 20,
|
|
608
|
+
timing: "trailing"
|
|
609
|
+
});
|
|
610
|
+
async function requestAd(options) {
|
|
611
|
+
const promise = new Promise(
|
|
612
|
+
(resolve, reject) => {
|
|
613
|
+
batch.set(runtimeCore.toValue(options.slot.name), { options, resolve, reject });
|
|
614
|
+
}
|
|
615
|
+
);
|
|
616
|
+
await debouncedRequestAds.call(options.context);
|
|
617
|
+
return promise;
|
|
618
|
+
}
|
|
655
619
|
async function requestAds(requestOptions) {
|
|
656
620
|
var _a, _b, _c, _d, _e;
|
|
657
621
|
const options = await runOnRequest(requestOptions);
|
|
@@ -692,16 +656,6 @@ async function requestAds(requestOptions) {
|
|
|
692
656
|
throw error;
|
|
693
657
|
}
|
|
694
658
|
}
|
|
695
|
-
async function requestAd({
|
|
696
|
-
slot,
|
|
697
|
-
...options
|
|
698
|
-
}) {
|
|
699
|
-
const [ad] = await requestAds({
|
|
700
|
-
slots: [slot],
|
|
701
|
-
...options
|
|
702
|
-
});
|
|
703
|
-
return ad;
|
|
704
|
-
}
|
|
705
659
|
const [runOnRender, onRender] = createAsyncHook("onRender");
|
|
706
660
|
const [runOnSlotCreate, onSlotCreate] = createSyncHook("onSlotCreate");
|
|
707
661
|
const [runOnViewabilityChanged, onViewabilityChanged] = createPassiveHook("onViewabilityChanged");
|
|
@@ -882,7 +836,7 @@ function createSlot(slotOptions) {
|
|
|
882
836
|
}
|
|
883
837
|
async function render(adToRender) {
|
|
884
838
|
var _a, _b;
|
|
885
|
-
await waitForDomLoad();
|
|
839
|
+
await sdkShared.waitForDomLoad();
|
|
886
840
|
await waitOnInit;
|
|
887
841
|
let renderAd = adToRender ?? ad.value ?? originalAd.value ?? await requestAd$1();
|
|
888
842
|
if (renderAd)
|
|
@@ -954,13 +908,13 @@ function createSlot(slotOptions) {
|
|
|
954
908
|
function useDomLoaded() {
|
|
955
909
|
const isDomLoaded = runtimeCore.ref(false);
|
|
956
910
|
onInit(async () => {
|
|
957
|
-
await waitForDomLoad();
|
|
911
|
+
await sdkShared.waitForDomLoad();
|
|
958
912
|
isDomLoaded.value = true;
|
|
959
913
|
});
|
|
960
914
|
return isDomLoaded;
|
|
961
915
|
}
|
|
962
916
|
async function findDomSlots(context) {
|
|
963
|
-
await waitForDomLoad();
|
|
917
|
+
await sdkShared.waitForDomLoad();
|
|
964
918
|
return Array.from(document.querySelectorAll(".adunit")).filter((element) => {
|
|
965
919
|
var _a;
|
|
966
920
|
if (!element.dataset.format)
|
|
@@ -1002,6 +956,10 @@ function createSlotManager({
|
|
|
1002
956
|
onDispose: onDispose2,
|
|
1003
957
|
context
|
|
1004
958
|
});
|
|
959
|
+
if (slots.has(slot.name.value)) {
|
|
960
|
+
slot.dispose();
|
|
961
|
+
throw new Error(`Slot with the name: ${slot.name.value} already exists. Create a new slot with a different format, slot, or the location.`);
|
|
962
|
+
}
|
|
1005
963
|
function onDispose2() {
|
|
1006
964
|
var _a2;
|
|
1007
965
|
slots.delete(slot.name.value);
|
|
@@ -1127,11 +1085,9 @@ function createAdhese(options) {
|
|
|
1127
1085
|
location: mergedOptions.location,
|
|
1128
1086
|
consent: mergedOptions.consent,
|
|
1129
1087
|
debug: mergedOptions.debug,
|
|
1130
|
-
getAll,
|
|
1131
|
-
get,
|
|
1132
1088
|
options: mergedOptions,
|
|
1133
1089
|
logger,
|
|
1134
|
-
|
|
1090
|
+
isDisposed: false
|
|
1135
1091
|
});
|
|
1136
1092
|
for (const [index, plugin] of mergedOptions.plugins.entries()) {
|
|
1137
1093
|
plugin(context, {
|
|
@@ -1146,7 +1102,7 @@ function createAdhese(options) {
|
|
|
1146
1102
|
onViewabilityChanged
|
|
1147
1103
|
});
|
|
1148
1104
|
}
|
|
1149
|
-
context.events = createEventManager();
|
|
1105
|
+
context.events = sdkShared.createEventManager();
|
|
1150
1106
|
context.safeFrame = options.safeFrame ? createSafeFrame({
|
|
1151
1107
|
renderFile: `${mergedOptions.poolHost}/sf/r.html`,
|
|
1152
1108
|
context
|
|
@@ -1204,22 +1160,25 @@ function createAdhese(options) {
|
|
|
1204
1160
|
function getAll() {
|
|
1205
1161
|
return slotManager.getAll() ?? [];
|
|
1206
1162
|
}
|
|
1163
|
+
context.getAll = getAll;
|
|
1207
1164
|
function get(name2) {
|
|
1208
1165
|
return slotManager.get(name2);
|
|
1209
1166
|
}
|
|
1167
|
+
context.get = get;
|
|
1210
1168
|
function addSlot(slotOptions) {
|
|
1211
1169
|
const newSlot = slotManager.add(slotOptions);
|
|
1212
1170
|
debouncedFetchAllUnrenderedSlots.call().catch(logger.error);
|
|
1213
1171
|
return newSlot;
|
|
1214
1172
|
}
|
|
1173
|
+
context.addSlot = addSlot;
|
|
1215
1174
|
async function findDomSlots2() {
|
|
1216
1175
|
const domSlots = (await slotManager.findDomSlots() ?? []).filter((slot) => !slot.lazyLoading);
|
|
1217
1176
|
if (domSlots.length <= 0)
|
|
1218
1177
|
return [];
|
|
1219
|
-
const ads = await
|
|
1220
|
-
|
|
1178
|
+
const ads = await Promise.all(domSlots.map((slot) => requestAd({
|
|
1179
|
+
slot,
|
|
1221
1180
|
context
|
|
1222
|
-
});
|
|
1181
|
+
})));
|
|
1223
1182
|
for (const ad of ads) {
|
|
1224
1183
|
const slot = slotManager.get(ad.slotName);
|
|
1225
1184
|
if (slot)
|
|
@@ -1245,10 +1204,10 @@ function createAdhese(options) {
|
|
|
1245
1204
|
const slots = (slotManager.getAll() ?? []).filter((slot) => !slot.lazyLoading && !slot.ad.value);
|
|
1246
1205
|
if (slots.length === 0)
|
|
1247
1206
|
return;
|
|
1248
|
-
const ads = await
|
|
1249
|
-
|
|
1207
|
+
const ads = await Promise.all(slots.map((slot) => requestAd({
|
|
1208
|
+
slot,
|
|
1250
1209
|
context
|
|
1251
|
-
});
|
|
1210
|
+
})));
|
|
1252
1211
|
for (const ad of ads) {
|
|
1253
1212
|
const slot = slotManager.get(ad.slotName);
|
|
1254
1213
|
if (slot)
|
|
@@ -1268,6 +1227,7 @@ function createAdhese(options) {
|
|
|
1268
1227
|
});
|
|
1269
1228
|
function dispose() {
|
|
1270
1229
|
var _a, _b;
|
|
1230
|
+
context.isDisposed = true;
|
|
1271
1231
|
queryDetector.dispose();
|
|
1272
1232
|
slotManager.dispose();
|
|
1273
1233
|
queryDetector.dispose();
|
|
@@ -1313,5 +1273,4 @@ function createAdhese(options) {
|
|
|
1313
1273
|
exports.createAdhese = createAdhese;
|
|
1314
1274
|
exports.logger = logger;
|
|
1315
1275
|
exports.requestAd = requestAd;
|
|
1316
|
-
exports.requestAds = requestAds;
|
|
1317
1276
|
//# sourceMappingURL=index.cjs.map
|