@adhese/sdk 0.11.2 → 0.12.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.cjs CHANGED
@@ -58,7 +58,7 @@ function createSafeFrame({
58
58
  }
59
59
  const name = "@adhese/sdk";
60
60
  const type = "module";
61
- const version = "0.11.2";
61
+ const version = "0.12.0";
62
62
  const description = "Adhese SDK";
63
63
  const license = "GPL-3.0";
64
64
  const repository = {
@@ -88,10 +88,10 @@ const scripts = {
88
88
  prepareRelease: "npm run build"
89
89
  };
90
90
  const dependencies = {
91
- "@adhese/sdk-shared": "^0.1.0",
91
+ "@adhese/sdk-shared": "^0.2.0",
92
92
  "@vue/runtime-core": "^3.4.21",
93
93
  remeda: "^1.61.0",
94
- zod: "^3.23.4"
94
+ zod: "^3.23.5"
95
95
  };
96
96
  const packageJson = {
97
97
  name,
@@ -223,17 +223,6 @@ function createSyncHook(name2, {
223
223
  };
224
224
  return [run, (callback) => add(callback, { name: name2, onAdd })];
225
225
  }
226
- function createPassiveHook(name2, {
227
- onRun,
228
- onAdd
229
- } = {}) {
230
- hookMap.set(name2, /* @__PURE__ */ new Set());
231
- function run(arg) {
232
- Promise.allSettled(Array.from(hookMap.get(name2) ?? []).map((callback) => callback(arg))).catch(console.trace);
233
- onRun == null ? void 0 : onRun(hookMap.get(name2));
234
- }
235
- return [run, (callback) => add(callback, { name: name2, onAdd })];
236
- }
237
226
  function isCallbackAsync(callback) {
238
227
  return callback.constructor.name === "AsyncFunction";
239
228
  }
@@ -269,77 +258,7 @@ const [runOnInit, onInit] = createSyncHook("onInit", {
269
258
  runOnInit();
270
259
  }
271
260
  });
272
- const defaultLogLevels = ["trace", "debug", "info", "warn", "error"];
273
- function createLogger({
274
- scope,
275
- logLevels = defaultLogLevels,
276
- minLogLevelThreshold = logLevels[2]
277
- }) {
278
- const logs = /* @__PURE__ */ new Set();
279
- let currentMinLogLevelThreshold = minLogLevelThreshold;
280
- const events = sdkShared.createEventManager();
281
- const logFunctions = Object.fromEntries(logLevels.map((level, index) => {
282
- const logFunction = (message, attributes) => {
283
- logs.add({
284
- scope,
285
- level,
286
- message,
287
- attributes,
288
- timestamp: Date.now(),
289
- id: sdkShared.uniqueId()
290
- });
291
- events.log.dispatch({
292
- scope,
293
- level,
294
- message,
295
- attributes,
296
- timestamp: Date.now(),
297
- id: sdkShared.uniqueId()
298
- });
299
- if (index >= logLevels.indexOf(currentMinLogLevelThreshold)) {
300
- if (["warn", "error", "trace"].includes(level)) {
301
- console[level](...[
302
- `%c${scope}`,
303
- "color: red; font-weight: bold;",
304
- message,
305
- attributes
306
- ].filter(Boolean));
307
- } else {
308
- console.log(...[
309
- `%c${scope} %c${level.toUpperCase()}`,
310
- "color: red; font-weight: bold;",
311
- "font-weight: bold;",
312
- message,
313
- attributes
314
- ].filter(Boolean));
315
- }
316
- }
317
- };
318
- return [level, logFunction];
319
- }));
320
- return {
321
- ...logFunctions,
322
- scope,
323
- events,
324
- setMinLogLevelThreshold(level) {
325
- currentMinLogLevelThreshold = level;
326
- },
327
- resetMinLogLevelThreshold() {
328
- currentMinLogLevelThreshold = minLogLevelThreshold;
329
- },
330
- getMinLogLevelThreshold() {
331
- return currentMinLogLevelThreshold;
332
- },
333
- getLogs() {
334
- return Array.from(logs);
335
- },
336
- resetLogs() {
337
- events.reset.dispatch();
338
- logs.clear();
339
- }
340
- };
341
- }
342
- const logger = createLogger({
261
+ const logger = sdkShared.createLogger({
343
262
  scope: "Adhese SDK"
344
263
  });
345
264
  const [runOnRequest, onRequest] = createAsyncHook("onRequest");
@@ -541,7 +460,7 @@ function getPreviewObjects() {
541
460
  return previewObjects;
542
461
  }
543
462
  function requestWithPost({
544
- context: { options: { host }, parameters },
463
+ context,
545
464
  ...options
546
465
  }) {
547
466
  const payload = {
@@ -550,9 +469,9 @@ function requestWithPost({
550
469
  slotname: runtimeCore.toValue(slot.name),
551
470
  parameters: parseParameters(slot.parameters)
552
471
  })),
553
- parameters: parameters && parseParameters(parameters)
472
+ parameters: context.parameters && parseParameters(context.parameters)
554
473
  };
555
- return fetch(`${new URL(host).href}json`, {
474
+ return fetch(`${new URL(context.options.host).href}json`, {
556
475
  method: "POST",
557
476
  body: JSON.stringify(payload),
558
477
  headers: {
@@ -621,7 +540,7 @@ async function requestAds(requestOptions) {
621
540
  const options = await runOnRequest(requestOptions);
622
541
  const { context } = options;
623
542
  try {
624
- (_a = context.events) == null ? void 0 : _a.requestAd.dispatch({
543
+ (_a = context == null ? void 0 : context.events) == null ? void 0 : _a.requestAd.dispatch({
625
544
  ...options,
626
545
  context
627
546
  });
@@ -652,13 +571,12 @@ async function requestAds(requestOptions) {
652
571
  return mergedResult;
653
572
  } catch (error) {
654
573
  logger.error(String(error));
655
- (_e = context.events) == null ? void 0 : _e.requestError.dispatch(error);
574
+ (_e = context == null ? void 0 : context.events) == null ? void 0 : _e.requestError.dispatch(error);
656
575
  throw error;
657
576
  }
658
577
  }
659
578
  const [runOnRender, onRender] = createAsyncHook("onRender");
660
579
  const [runOnSlotCreate, onSlotCreate] = createSyncHook("onSlotCreate");
661
- const [runOnViewabilityChanged, onViewabilityChanged] = createPassiveHook("onViewabilityChanged");
662
580
  function useViewabilityObserver({ context, ad, name: name2, element }) {
663
581
  let timeoutId = null;
664
582
  const {
@@ -807,11 +725,9 @@ function createSlot(slotOptions) {
807
725
  (_b = context.events) == null ? void 0 : _b.changeSlots.dispatch(Array.from(((_a = context.getAll) == null ? void 0 : _a.call(context)) ?? []));
808
726
  });
809
727
  runtimeCore.watch(isInViewport, (value) => {
810
- runOnViewabilityChanged({
811
- name: name2.value,
812
- isInViewport: value
813
- });
814
- }, { immediate: true });
728
+ var _a;
729
+ (_a = options.onViewabilityChanged) == null ? void 0 : _a.call(options, value);
730
+ });
815
731
  const [
816
732
  isViewabilityTracked,
817
733
  disposeViewabilityObserver
@@ -890,7 +806,7 @@ function createSlot(slotOptions) {
890
806
  scope.stop();
891
807
  }
892
808
  return {
893
- location: context.location,
809
+ location: context.location ?? "",
894
810
  lazyLoading: options.lazyLoading ?? false,
895
811
  slot,
896
812
  parameters,
@@ -960,6 +876,10 @@ function createSlotManager({
960
876
  slot.dispose();
961
877
  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
878
  }
879
+ const disposeSlotWatch = runtimeCore.watch(slot.name, (newName, previousName) => {
880
+ slots.set(newName, slot);
881
+ slots.delete(previousName);
882
+ });
963
883
  function onDispose2() {
964
884
  var _a2;
965
885
  slots.delete(slot.name.value);
@@ -968,12 +888,9 @@ function createSlotManager({
968
888
  slots: Array.from(slots)
969
889
  });
970
890
  (_a2 = context.events) == null ? void 0 : _a2.removeSlot.dispatch(slot);
891
+ disposeSlotWatch();
971
892
  }
972
893
  slots.set(slot.name.value, slot);
973
- runtimeCore.watch(slot.name, (newName, previousName) => {
974
- slots.set(newName, slot);
975
- slots.delete(previousName);
976
- });
977
894
  logger.debug("Slot added", {
978
895
  slot,
979
896
  slots: Array.from(slots.values())
@@ -1087,7 +1004,14 @@ function createAdhese(options) {
1087
1004
  debug: mergedOptions.debug,
1088
1005
  options: mergedOptions,
1089
1006
  logger,
1090
- isDisposed: false
1007
+ isDisposed: false,
1008
+ parameters: /* @__PURE__ */ new Map(),
1009
+ events: sdkShared.createEventManager(),
1010
+ dispose,
1011
+ findDomSlots: findDomSlots2,
1012
+ getAll,
1013
+ get,
1014
+ addSlot
1091
1015
  });
1092
1016
  for (const [index, plugin] of mergedOptions.plugins.entries()) {
1093
1017
  plugin(context, {
@@ -1098,8 +1022,7 @@ function createAdhese(options) {
1098
1022
  onRender,
1099
1023
  onRequest,
1100
1024
  onResponse,
1101
- onSlotCreate,
1102
- onViewabilityChanged
1025
+ onSlotCreate
1103
1026
  });
1104
1027
  }
1105
1028
  context.events = sdkShared.createEventManager();
@@ -1107,32 +1030,25 @@ function createAdhese(options) {
1107
1030
  renderFile: `${mergedOptions.poolHost}/sf/r.html`,
1108
1031
  context
1109
1032
  }) : void 0;
1110
- function getLocation() {
1111
- return context.location;
1112
- }
1113
- function setLocation(newLocation) {
1033
+ runtimeCore.watch(() => context.location, (newLocation) => {
1114
1034
  var _a;
1115
- context.location = newLocation;
1116
1035
  (_a = context.events) == null ? void 0 : _a.locationChange.dispatch(newLocation);
1117
- }
1036
+ });
1118
1037
  const queryDetector = createQueryDetector({
1119
1038
  onChange: onQueryChange,
1120
1039
  queries: mergedOptions.queries
1121
1040
  });
1122
1041
  context.parameters = createParameters(mergedOptions, queryDetector);
1123
1042
  runtimeCore.watch(
1124
- context.parameters,
1125
- onParametersChange,
1043
+ () => context.parameters,
1044
+ (newParameters) => {
1045
+ var _a;
1046
+ (_a = context.events) == null ? void 0 : _a.parametersChange.dispatch(newParameters);
1047
+ },
1126
1048
  {
1127
- deep: true,
1128
- immediate: true
1049
+ deep: true
1129
1050
  }
1130
1051
  );
1131
- function onParametersChange() {
1132
- var _a;
1133
- if (context.parameters)
1134
- (_a = context.events) == null ? void 0 : _a.parametersChange.dispatch(context.parameters);
1135
- }
1136
1052
  const debouncedFetchAllUnrenderedSlots = remeda.debounce(fetchAllUnrenderedSlots, {
1137
1053
  waitMs: 100,
1138
1054
  timing: "both"
@@ -1144,15 +1060,13 @@ function createAdhese(options) {
1144
1060
  (_b = context.parameters) == null ? void 0 : _b.set("br", query);
1145
1061
  await debouncedFetchAllUnrenderedSlots.call();
1146
1062
  }
1147
- function getConsent() {
1148
- return context.consent;
1149
- }
1150
- function setConsent(newConsent) {
1063
+ runtimeCore.watch(() => context.consent, (newConsent) => {
1151
1064
  var _a, _b;
1152
1065
  (_a = context.parameters) == null ? void 0 : _a.set("tl", newConsent ? "all" : "none");
1153
- context.consent = newConsent;
1154
1066
  (_b = context.events) == null ? void 0 : _b.consentChange.dispatch(newConsent);
1155
- }
1067
+ }, {
1068
+ immediate: true
1069
+ });
1156
1070
  const slotManager = createSlotManager({
1157
1071
  initialSlots: mergedOptions.initialSlots,
1158
1072
  context
@@ -1166,9 +1080,7 @@ function createAdhese(options) {
1166
1080
  }
1167
1081
  context.get = get;
1168
1082
  function addSlot(slotOptions) {
1169
- const newSlot = slotManager.add(slotOptions);
1170
- debouncedFetchAllUnrenderedSlots.call().catch(logger.error);
1171
- return newSlot;
1083
+ return slotManager.add(slotOptions);
1172
1084
  }
1173
1085
  context.addSlot = addSlot;
1174
1086
  async function findDomSlots2() {
@@ -1186,10 +1098,10 @@ function createAdhese(options) {
1186
1098
  }
1187
1099
  return domSlots;
1188
1100
  }
1189
- async function toggleDebug() {
1101
+ context.findDomSlots = findDomSlots2;
1102
+ runtimeCore.watch(() => context.debug, async (newDebug) => {
1190
1103
  var _a, _b;
1191
- context.debug = !context.debug;
1192
- if (context.debug) {
1104
+ if (newDebug) {
1193
1105
  logger.setMinLogLevelThreshold("debug");
1194
1106
  logger.debug("Debug mode enabled");
1195
1107
  (_a = context.events) == null ? void 0 : _a.debugChange.dispatch(true);
@@ -1198,8 +1110,9 @@ function createAdhese(options) {
1198
1110
  logger.setMinLogLevelThreshold("info");
1199
1111
  (_b = context.events) == null ? void 0 : _b.debugChange.dispatch(false);
1200
1112
  }
1201
- return context.debug;
1202
- }
1113
+ }, {
1114
+ immediate: true
1115
+ });
1203
1116
  async function fetchAllUnrenderedSlots() {
1204
1117
  const slots = (slotManager.getAll() ?? []).filter((slot) => !slot.lazyLoading && !slot.ad.value);
1205
1118
  if (slots.length === 0)
@@ -1240,34 +1153,20 @@ function createAdhese(options) {
1240
1153
  clearAllHooks();
1241
1154
  scope.stop();
1242
1155
  }
1156
+ context.dispose = dispose;
1243
1157
  onInit(async () => {
1244
1158
  var _a;
1245
1159
  if ((slotManager.getAll().length ?? 0) > 0)
1246
1160
  await fetchAllUnrenderedSlots().catch(logger.error);
1247
1161
  if (mergedOptions.findDomSlotsOnLoad)
1248
- await findDomSlots2();
1162
+ await (context == null ? void 0 : context.findDomSlots());
1249
1163
  if (mergedOptions.debug || window.location.search.includes("adhese_debug=true") || isPreviewMode())
1250
1164
  (_a = context.events) == null ? void 0 : _a.debugChange.dispatch(true);
1251
1165
  if (!scope.active)
1252
1166
  dispose();
1253
1167
  });
1254
1168
  runOnInit();
1255
- return {
1256
- parameters: context.parameters,
1257
- events: context.events,
1258
- getLocation,
1259
- setLocation,
1260
- getConsent,
1261
- setConsent,
1262
- addSlot,
1263
- findDomSlots: findDomSlots2,
1264
- dispose,
1265
- toggleDebug,
1266
- get: slotManager.get,
1267
- getAll: slotManager.getAll,
1268
- context,
1269
- options: mergedOptions
1270
- };
1169
+ return context;
1271
1170
  });
1272
1171
  }
1273
1172
  exports.createAdhese = createAdhese;