@adhese/sdk 0.8.1 → 0.9.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.d.ts CHANGED
@@ -532,9 +532,9 @@ type Logger<T extends string> = {
532
532
 
533
533
  declare const logger: Logger<"error" | "debug" | "trace" | "info" | "warn">;
534
534
 
535
- declare const onInit: (callback: () => void | Promise<void>) => () => void;
535
+ declare const onInit: (callback: (arg: void) => void | Promise<void>) => () => void;
536
536
 
537
- declare const onDispose: (callback: () => void | Promise<void>) => () => void;
537
+ declare const onDispose: (callback: (arg: void) => void | Promise<void>) => () => void;
538
538
 
539
539
  declare const onRender: (callback: (arg: Ad) => void | Ad | Promise<void | Ad>) => () => void;
540
540
 
@@ -542,6 +542,8 @@ declare const onRequest: (callback: (arg: AdRequestOptions) => void | AdRequestO
542
542
 
543
543
  declare const onResponse: (callback: (arg: readonly Ad[]) => void | readonly Ad[] | Promise<void | readonly Ad[]>) => () => void;
544
544
 
545
+ declare const onSlotCreate: (callback: (arg: AdheseSlotOptions) => void | Promise<void> | AdheseSlotOptions) => () => void;
546
+
545
547
  type AdhesePluginInformation = {
546
548
  index: number;
547
549
  version: string;
@@ -550,6 +552,7 @@ type AdhesePluginInformation = {
550
552
  onRender: typeof onRender;
551
553
  onRequest: typeof onRequest;
552
554
  onResponse: typeof onResponse;
555
+ onSlotCreate: typeof onSlotCreate;
553
556
  };
554
557
  type AdhesePlugin = (context: AdheseContext, plugin: AdhesePluginInformation) => void;
555
558
  type AdheseOptions = {
package/dist/index.js CHANGED
@@ -123,7 +123,7 @@ function createSafeFrame({
123
123
  }
124
124
  const name = "@adhese/sdk";
125
125
  const type = "module";
126
- const version = "0.8.1";
126
+ const version = "0.9.0";
127
127
  const description = "Adhese SDK";
128
128
  const license = "GPL-3.0";
129
129
  const repository = {
@@ -253,7 +253,7 @@ const hookMap = /* @__PURE__ */ new Map();
253
253
  function clearAllHooks() {
254
254
  hookMap.clear();
255
255
  }
256
- function createHook(name2, {
256
+ function createAsyncHook(name2, {
257
257
  onRun,
258
258
  onAdd
259
259
  } = {}) {
@@ -265,19 +265,45 @@ function createHook(name2, {
265
265
  onRun == null ? void 0 : onRun(hookMap.get(name2));
266
266
  return latestResult;
267
267
  };
268
- function add(callback) {
269
- const hookSet = hookMap.get(name2);
270
- if (hookSet)
271
- hookSet.add(callback);
272
- else
273
- hookMap.set(name2, /* @__PURE__ */ new Set([callback]));
274
- onAdd == null ? void 0 : onAdd(hookSet);
275
- return () => {
276
- var _a;
277
- (_a = hookMap.get(name2)) == null ? void 0 : _a.delete(callback);
278
- };
279
- }
280
- return [run, add];
268
+ return [run, (callback) => add(callback, { name: name2, onAdd })];
269
+ }
270
+ function createSyncHook(name2, {
271
+ onRun,
272
+ onAdd
273
+ } = {}) {
274
+ hookMap.set(name2, /* @__PURE__ */ new Set());
275
+ const run = (arg) => {
276
+ let latestResult = arg;
277
+ const promisedCallbacks = [];
278
+ for (const callback of hookMap.get(name2) ?? []) {
279
+ if (isCallbackAsync(callback))
280
+ promisedCallbacks.push(callback);
281
+ else
282
+ latestResult = callback(latestResult) ?? latestResult;
283
+ }
284
+ Promise.allSettled(promisedCallbacks.map((callback) => callback(latestResult))).catch(console.trace);
285
+ onRun == null ? void 0 : onRun(hookMap.get(name2));
286
+ return latestResult;
287
+ };
288
+ return [run, (callback) => add(callback, { name: name2, onAdd })];
289
+ }
290
+ function add(callback, {
291
+ name: name2,
292
+ onAdd
293
+ }) {
294
+ const hookSet = hookMap.get(name2);
295
+ if (hookSet)
296
+ hookSet.add(callback);
297
+ else
298
+ hookMap.set(name2, /* @__PURE__ */ new Set([callback]));
299
+ onAdd == null ? void 0 : onAdd(hookSet);
300
+ return () => {
301
+ var _a;
302
+ (_a = hookMap.get(name2)) == null ? void 0 : _a.delete(callback);
303
+ };
304
+ }
305
+ function isCallbackAsync(callback) {
306
+ return callback.constructor.name === "AsyncFunction";
281
307
  }
282
308
  let resolveOnInitPromise = () => {
283
309
  };
@@ -285,7 +311,7 @@ let isInit = false;
285
311
  const waitOnInit = new Promise((resolve) => {
286
312
  resolveOnInitPromise = resolve;
287
313
  });
288
- const [runOnInit, onInit] = createHook("onInit", {
314
+ const [runOnInit, onInit] = createSyncHook("onInit", {
289
315
  onRun(callbacks) {
290
316
  isInit = true;
291
317
  resolveOnInitPromise();
@@ -293,7 +319,7 @@ const [runOnInit, onInit] = createHook("onInit", {
293
319
  },
294
320
  onAdd() {
295
321
  if (isInit)
296
- runOnInit().catch(console.error);
322
+ runOnInit();
297
323
  }
298
324
  });
299
325
  const defaultLogLevels = ["trace", "debug", "info", "warn", "error"];
@@ -369,8 +395,8 @@ function createLogger({
369
395
  const logger = createLogger({
370
396
  scope: "Adhese SDK"
371
397
  });
372
- const [runOnRequest, onRequest] = createHook("onRequest");
373
- const [runOnResponse, onResponse] = createHook("onResponse");
398
+ const [runOnRequest, onRequest] = createAsyncHook("onRequest");
399
+ const [runOnResponse, onResponse] = createAsyncHook("onResponse");
374
400
  const numberLike = union([coerce.string().regex(/^\d+$/), literal("")]).transform((value) => value === "" ? void 0 : Number(value));
375
401
  const booleanLike = union([coerce.boolean(), literal("")]);
376
402
  const urlLike = union([coerce.string(), literal("")]).transform((value) => {
@@ -663,7 +689,8 @@ async function requestAd({
663
689
  });
664
690
  return ad;
665
691
  }
666
- const [runOnRender, onRender] = createHook("onRender");
692
+ const [runOnRender, onRender] = createAsyncHook("onRender");
693
+ const [runOnSlotCreate, onSlotCreate] = createSyncHook("onSlotCreate");
667
694
  function useViewabilityObserver({ context, ad, name: name2, element }) {
668
695
  let timeoutId = null;
669
696
  const {
@@ -754,9 +781,10 @@ const renderFunctions = {
754
781
  iframe: renderIframe,
755
782
  inline: renderInline
756
783
  };
757
- function createSlot(options) {
784
+ function createSlot(slotOptions) {
758
785
  const scope = effectScope();
759
786
  return scope.run(() => {
787
+ const options = runOnSlotCreate(slotOptions);
760
788
  const {
761
789
  containingElement,
762
790
  slot,
@@ -872,6 +900,7 @@ function createSlot(options) {
872
900
  containingElement
873
901
  });
874
902
  (_b = options.onRender) == null ? void 0 : _b.call(options, element.value);
903
+ ad.value = renderAd;
875
904
  disposeRenderIntersectionObserver();
876
905
  return element.value;
877
906
  }
@@ -954,7 +983,7 @@ function createSlotManager({
954
983
  function getAll() {
955
984
  return Array.from(slots).map(([, slot]) => slot);
956
985
  }
957
- function add(options) {
986
+ function add2(options) {
958
987
  var _a;
959
988
  const slot = createSlot({
960
989
  ...options,
@@ -1000,14 +1029,14 @@ function createSlotManager({
1000
1029
  scope.stop();
1001
1030
  }
1002
1031
  for (const options of initialSlots) {
1003
- add({
1032
+ add2({
1004
1033
  ...options,
1005
1034
  lazyLoading: false
1006
1035
  });
1007
1036
  }
1008
1037
  return {
1009
1038
  getAll,
1010
- add,
1039
+ add: add2,
1011
1040
  findDomSlots: findDomSlots$1,
1012
1041
  get,
1013
1042
  dispose
@@ -1051,14 +1080,14 @@ function isPreviewMode() {
1051
1080
  return window.location.search.includes("adhesePreviewCreativeId");
1052
1081
  }
1053
1082
  let isDisposed = false;
1054
- const [runOnDispose, onDispose] = createHook("onDispose", {
1083
+ const [runOnDispose, onDispose] = createSyncHook("onDispose", {
1055
1084
  onRun(callbacks) {
1056
1085
  isDisposed = true;
1057
1086
  callbacks == null ? void 0 : callbacks.clear();
1058
1087
  },
1059
1088
  onAdd() {
1060
1089
  if (isDisposed)
1061
- runOnDispose().catch(console.error);
1090
+ runOnDispose();
1062
1091
  }
1063
1092
  });
1064
1093
  function createAdhese(options) {
@@ -1100,7 +1129,8 @@ function createAdhese(options) {
1100
1129
  onDispose,
1101
1130
  onRender,
1102
1131
  onRequest,
1103
- onResponse
1132
+ onResponse,
1133
+ onSlotCreate
1104
1134
  });
1105
1135
  }
1106
1136
  context.events = createEventManager();
@@ -1229,7 +1259,7 @@ function createAdhese(options) {
1229
1259
  logger.resetLogs();
1230
1260
  (_b = context.events) == null ? void 0 : _b.dispose();
1231
1261
  logger.info("Adhese instance disposed");
1232
- runOnDispose().catch(logger.error);
1262
+ runOnDispose();
1233
1263
  clearAllHooks();
1234
1264
  scope.stop();
1235
1265
  }
@@ -1244,7 +1274,7 @@ function createAdhese(options) {
1244
1274
  if (!scope.active)
1245
1275
  dispose();
1246
1276
  });
1247
- runOnInit().catch(logger.error);
1277
+ runOnInit();
1248
1278
  return {
1249
1279
  parameters: context.parameters,
1250
1280
  events: context.events,