@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.js CHANGED
@@ -1,74 +1,7 @@
1
+ import { uniqueId, createEventManager, waitForDomLoad } from "@adhese/sdk-shared";
1
2
  import { toValue, ref, computed, watch, effectScope, reactive, shallowReactive, watchEffect } from "@vue/runtime-core";
2
3
  import { debounce, round, isDeepEqual } from "remeda";
3
4
  import { union, coerce, literal, number, string, ZodIssueCode, NEVER, object, unknown, lazy } from "zod";
4
- async function waitForDomLoad() {
5
- return new Promise((resolve) => {
6
- function onDomLoad() {
7
- resolve();
8
- window.removeEventListener("DOMContentLoaded", onDomLoad);
9
- }
10
- if (document.readyState === "loading")
11
- document.addEventListener("DOMContentLoaded", onDomLoad);
12
- else
13
- resolve();
14
- });
15
- }
16
- function createEventManager() {
17
- const disposables = /* @__PURE__ */ new Set();
18
- function dispose() {
19
- for (const disposable of disposables)
20
- disposable();
21
- }
22
- return new Proxy({
23
- dispose
24
- }, {
25
- // eslint-disable-next-line ts/explicit-function-return-type
26
- get(target, key, receiver) {
27
- if (!(key in target) && typeof key === "string") {
28
- const event = createEvent();
29
- disposables.add(() => {
30
- event.listeners.clear();
31
- });
32
- Reflect.set(target, key, event, receiver);
33
- }
34
- return Reflect.get(target, key, receiver);
35
- }
36
- });
37
- }
38
- function createEvent() {
39
- const listeners = /* @__PURE__ */ new Set();
40
- function dispatch(data) {
41
- for (const listener of listeners)
42
- void listener(data);
43
- }
44
- async function dispatchAsync(data) {
45
- await Promise.allSettled(
46
- Array.from(listeners).map((listener) => listener(data))
47
- );
48
- }
49
- function addListener(listener) {
50
- listeners.add(listener);
51
- }
52
- function removeListener(listener) {
53
- listeners.delete(listener);
54
- }
55
- return {
56
- listeners,
57
- dispatch,
58
- dispatchAsync,
59
- addListener,
60
- removeListener
61
- };
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
- }
72
5
  function createSafeFrame({
73
6
  renderFile,
74
7
  context
@@ -123,7 +56,7 @@ function createSafeFrame({
123
56
  }
124
57
  const name = "@adhese/sdk";
125
58
  const type = "module";
126
- const version = "0.10.0";
59
+ const version = "0.11.1";
127
60
  const description = "Adhese SDK";
128
61
  const license = "GPL-3.0";
129
62
  const repository = {
@@ -153,9 +86,10 @@ const scripts = {
153
86
  prepareRelease: "npm run build"
154
87
  };
155
88
  const dependencies = {
89
+ "@adhese/sdk-shared": "^0.1.0",
156
90
  "@vue/runtime-core": "^3.4.21",
157
91
  remeda: "^1.61.0",
158
- zod: "^3.23.0"
92
+ zod: "^3.23.4"
159
93
  };
160
94
  const packageJson = {
161
95
  name,
@@ -650,6 +584,36 @@ function filterSpecialChars(value) {
650
584
  const specialRegex = /[^\p{L}\p{N}_]/gu;
651
585
  return value.replaceAll(specialRegex, "_");
652
586
  }
587
+ const batch = /* @__PURE__ */ new Map();
588
+ const debouncedRequestAds = debounce(async (context) => {
589
+ if (batch.size === 0)
590
+ return [];
591
+ const ads = await requestAds({
592
+ slots: Array.from(batch.values()).map(({ options }) => options.slot),
593
+ context
594
+ });
595
+ for (const { options, resolve, reject } of batch.values()) {
596
+ const ad = ads.find(({ slotName }) => toValue(slotName) === toValue(options.slot.name));
597
+ if (ad)
598
+ resolve(ad);
599
+ else
600
+ reject(new Error(`Ad: ${toValue(options.slot.name)} not found`));
601
+ }
602
+ batch.clear();
603
+ return ads;
604
+ }, {
605
+ waitMs: 20,
606
+ timing: "trailing"
607
+ });
608
+ async function requestAd(options) {
609
+ const promise = new Promise(
610
+ (resolve, reject) => {
611
+ batch.set(toValue(options.slot.name), { options, resolve, reject });
612
+ }
613
+ );
614
+ await debouncedRequestAds.call(options.context);
615
+ return promise;
616
+ }
653
617
  async function requestAds(requestOptions) {
654
618
  var _a, _b, _c, _d, _e;
655
619
  const options = await runOnRequest(requestOptions);
@@ -690,16 +654,6 @@ async function requestAds(requestOptions) {
690
654
  throw error;
691
655
  }
692
656
  }
693
- async function requestAd({
694
- slot,
695
- ...options
696
- }) {
697
- const [ad] = await requestAds({
698
- slots: [slot],
699
- ...options
700
- });
701
- return ad;
702
- }
703
657
  const [runOnRender, onRender] = createAsyncHook("onRender");
704
658
  const [runOnSlotCreate, onSlotCreate] = createSyncHook("onSlotCreate");
705
659
  const [runOnViewabilityChanged, onViewabilityChanged] = createPassiveHook("onViewabilityChanged");
@@ -1000,6 +954,10 @@ function createSlotManager({
1000
954
  onDispose: onDispose2,
1001
955
  context
1002
956
  });
957
+ if (slots.has(slot.name.value)) {
958
+ slot.dispose();
959
+ throw new Error(`Slot with the name: ${slot.name.value} already exists. Create a new slot with a different format, slot, or the location.`);
960
+ }
1003
961
  function onDispose2() {
1004
962
  var _a2;
1005
963
  slots.delete(slot.name.value);
@@ -1125,11 +1083,9 @@ function createAdhese(options) {
1125
1083
  location: mergedOptions.location,
1126
1084
  consent: mergedOptions.consent,
1127
1085
  debug: mergedOptions.debug,
1128
- getAll,
1129
- get,
1130
1086
  options: mergedOptions,
1131
1087
  logger,
1132
- addSlot
1088
+ isDisposed: false
1133
1089
  });
1134
1090
  for (const [index, plugin] of mergedOptions.plugins.entries()) {
1135
1091
  plugin(context, {
@@ -1202,22 +1158,25 @@ function createAdhese(options) {
1202
1158
  function getAll() {
1203
1159
  return slotManager.getAll() ?? [];
1204
1160
  }
1161
+ context.getAll = getAll;
1205
1162
  function get(name2) {
1206
1163
  return slotManager.get(name2);
1207
1164
  }
1165
+ context.get = get;
1208
1166
  function addSlot(slotOptions) {
1209
1167
  const newSlot = slotManager.add(slotOptions);
1210
1168
  debouncedFetchAllUnrenderedSlots.call().catch(logger.error);
1211
1169
  return newSlot;
1212
1170
  }
1171
+ context.addSlot = addSlot;
1213
1172
  async function findDomSlots2() {
1214
1173
  const domSlots = (await slotManager.findDomSlots() ?? []).filter((slot) => !slot.lazyLoading);
1215
1174
  if (domSlots.length <= 0)
1216
1175
  return [];
1217
- const ads = await requestAds({
1218
- slots: domSlots,
1176
+ const ads = await Promise.all(domSlots.map((slot) => requestAd({
1177
+ slot,
1219
1178
  context
1220
- });
1179
+ })));
1221
1180
  for (const ad of ads) {
1222
1181
  const slot = slotManager.get(ad.slotName);
1223
1182
  if (slot)
@@ -1243,10 +1202,10 @@ function createAdhese(options) {
1243
1202
  const slots = (slotManager.getAll() ?? []).filter((slot) => !slot.lazyLoading && !slot.ad.value);
1244
1203
  if (slots.length === 0)
1245
1204
  return;
1246
- const ads = await requestAds({
1247
- slots,
1205
+ const ads = await Promise.all(slots.map((slot) => requestAd({
1206
+ slot,
1248
1207
  context
1249
- });
1208
+ })));
1250
1209
  for (const ad of ads) {
1251
1210
  const slot = slotManager.get(ad.slotName);
1252
1211
  if (slot)
@@ -1266,6 +1225,7 @@ function createAdhese(options) {
1266
1225
  });
1267
1226
  function dispose() {
1268
1227
  var _a, _b;
1228
+ context.isDisposed = true;
1269
1229
  queryDetector.dispose();
1270
1230
  slotManager.dispose();
1271
1231
  queryDetector.dispose();
@@ -1311,7 +1271,6 @@ function createAdhese(options) {
1311
1271
  export {
1312
1272
  createAdhese,
1313
1273
  logger,
1314
- requestAd,
1315
- requestAds
1274
+ requestAd
1316
1275
  };
1317
1276
  //# sourceMappingURL=index.js.map