@folklore/ads 0.0.39 → 0.0.41

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.
Files changed (3) hide show
  1. package/dist/cjs.js +48 -24
  2. package/dist/es.js +48 -24
  3. package/package.json +2 -2
package/dist/cjs.js CHANGED
@@ -276,12 +276,16 @@ class AdSlot extends EventEmitter__default["default"] {
276
276
  /* globals refreshDisabledLineItems: [] */
277
277
  const debug = createDebug__default["default"]('folklore:ads');
278
278
  class AdsManager extends EventEmitter__default["default"] {
279
- static index = 0;
280
- static createAdId() {
281
- const newId = `div-gpt-ad-${new Date().getTime()}-${Math.round(Math.random() * (Math.random() * 1000))}-${AdsManager.index}`;
282
- AdsManager.index += 1;
283
- return newId;
284
- }
279
+ // static index = 0;
280
+
281
+ // static createAdId() {
282
+ // const newId = `div-gpt-ad-${new Date().getTime()}-${Math.round(
283
+ // Math.random() * (Math.random() * 1000),
284
+ // )}-${AdsManager.index}`;
285
+ // AdsManager.index += 1;
286
+ // return newId;
287
+ // }
288
+
285
289
  static getArticleTargeting(article) {
286
290
  if (article === null) {
287
291
  return null;
@@ -346,10 +350,19 @@ class AdsManager extends EventEmitter__default["default"] {
346
350
  cmd: []
347
351
  };
348
352
  this.slots = [];
353
+ this.index = 0;
349
354
  if (this.options.autoInit) {
350
355
  this.init();
351
356
  }
352
357
  }
358
+ createAdId() {
359
+ // const newId = `div-gpt-ad-${new Date().getTime()}-${Math.round(
360
+ // Math.random() * (Math.random() * 1000),
361
+ // )}-${this.index}`;
362
+ const newId = `div-gpt-ad-${this.index}`;
363
+ this.index += 1;
364
+ return newId;
365
+ }
353
366
  onGPTReady() {
354
367
  const {
355
368
  googletag
@@ -478,7 +491,7 @@ class AdsManager extends EventEmitter__default["default"] {
478
491
  const {
479
492
  id: providedId = null
480
493
  } = opts;
481
- const id = providedId || AdsManager.createAdId();
494
+ const id = providedId || this.createAdId();
482
495
  debug('Creating slot #%s(%s)...', id, path);
483
496
  const slot = new AdSlot(id, path, size, {
484
497
  ...opts
@@ -871,28 +884,35 @@ function useAd(path, size) {
871
884
  const [renderEvent, setRenderEvent] = React.useState(null);
872
885
 
873
886
  // Create slot
874
- const currentSlot = React.useRef(null);
875
- const slot = React.useMemo(() => {
876
- if (currentSlot.current !== null) {
877
- adsManager.destroySlot(currentSlot.current);
878
- currentSlot.current = null;
879
- }
880
- currentSlot.current = path !== null && !disabled ? adsManager.createSlot(path, size, {
887
+ // const currentSlot = useRef(null);
888
+ const [slot, setSlot] = React.useState();
889
+ React.useEffect(() => {
890
+ // if (currentSlot.current !== null) {
891
+ // adsManager.destroySlot(currentSlot.current);
892
+ // currentSlot.current = null;
893
+ // }
894
+ const newSlot = path !== null && !disabled ? adsManager.createSlot(path, size, {
881
895
  visible: isVisible,
882
896
  sizeMapping,
883
897
  targeting,
884
898
  categoryExclusions
885
899
  }) : null;
900
+ setSlot(newSlot);
886
901
  // if (currentSlot.current !== null && adsReady) {
887
902
  // adsManager.defineSlot(currentSlot.current);
888
903
  // }
889
- return currentSlot.current;
904
+ // return currentSlot.current;
905
+ return () => {
906
+ if (newSlot !== null) {
907
+ adsManager.destroySlot(newSlot);
908
+ }
909
+ };
890
910
  }, [adsManager, path, disabled, size, sizeMapping, alwaysRender, categoryExclusions]);
891
911
  React.useEffect(() => {
892
- if (currentSlot.current !== null) {
893
- currentSlot.current.setTargeting(targeting);
912
+ if (slot !== null) {
913
+ slot.setTargeting(targeting);
894
914
  }
895
- }, [targeting]);
915
+ }, [slot, targeting]);
896
916
 
897
917
  // Set visibility
898
918
  React.useEffect(() => {
@@ -960,12 +980,16 @@ function useAd(path, size) {
960
980
  }, [slot, disabled, setRenderEvent, onRender, track]);
961
981
 
962
982
  // Destroy slot
963
- React.useEffect(() => () => {
964
- if (slot !== null) {
965
- currentSlot.current = null;
966
- adsManager.destroySlot(slot);
967
- }
968
- }, []);
983
+ // useEffect(
984
+ // () => () => {
985
+ // if (slot !== null) {
986
+ // // currentSlot.current = null;
987
+ // adsManager.destroySlot(slot);
988
+ // }
989
+ // },
990
+ // [],
991
+ // );
992
+
969
993
  return {
970
994
  refObserver,
971
995
  slot,
package/dist/es.js CHANGED
@@ -258,12 +258,16 @@ class AdSlot extends EventEmitter {
258
258
  /* globals refreshDisabledLineItems: [] */
259
259
  const debug = createDebug('folklore:ads');
260
260
  class AdsManager extends EventEmitter {
261
- static index = 0;
262
- static createAdId() {
263
- const newId = `div-gpt-ad-${new Date().getTime()}-${Math.round(Math.random() * (Math.random() * 1000))}-${AdsManager.index}`;
264
- AdsManager.index += 1;
265
- return newId;
266
- }
261
+ // static index = 0;
262
+
263
+ // static createAdId() {
264
+ // const newId = `div-gpt-ad-${new Date().getTime()}-${Math.round(
265
+ // Math.random() * (Math.random() * 1000),
266
+ // )}-${AdsManager.index}`;
267
+ // AdsManager.index += 1;
268
+ // return newId;
269
+ // }
270
+
267
271
  static getArticleTargeting(article) {
268
272
  if (article === null) {
269
273
  return null;
@@ -328,10 +332,19 @@ class AdsManager extends EventEmitter {
328
332
  cmd: []
329
333
  };
330
334
  this.slots = [];
335
+ this.index = 0;
331
336
  if (this.options.autoInit) {
332
337
  this.init();
333
338
  }
334
339
  }
340
+ createAdId() {
341
+ // const newId = `div-gpt-ad-${new Date().getTime()}-${Math.round(
342
+ // Math.random() * (Math.random() * 1000),
343
+ // )}-${this.index}`;
344
+ const newId = `div-gpt-ad-${this.index}`;
345
+ this.index += 1;
346
+ return newId;
347
+ }
335
348
  onGPTReady() {
336
349
  const {
337
350
  googletag
@@ -460,7 +473,7 @@ class AdsManager extends EventEmitter {
460
473
  const {
461
474
  id: providedId = null
462
475
  } = opts;
463
- const id = providedId || AdsManager.createAdId();
476
+ const id = providedId || this.createAdId();
464
477
  debug('Creating slot #%s(%s)...', id, path);
465
478
  const slot = new AdSlot(id, path, size, {
466
479
  ...opts
@@ -853,28 +866,35 @@ function useAd(path, size) {
853
866
  const [renderEvent, setRenderEvent] = useState(null);
854
867
 
855
868
  // Create slot
856
- const currentSlot = useRef(null);
857
- const slot = useMemo(() => {
858
- if (currentSlot.current !== null) {
859
- adsManager.destroySlot(currentSlot.current);
860
- currentSlot.current = null;
861
- }
862
- currentSlot.current = path !== null && !disabled ? adsManager.createSlot(path, size, {
869
+ // const currentSlot = useRef(null);
870
+ const [slot, setSlot] = useState();
871
+ useEffect(() => {
872
+ // if (currentSlot.current !== null) {
873
+ // adsManager.destroySlot(currentSlot.current);
874
+ // currentSlot.current = null;
875
+ // }
876
+ const newSlot = path !== null && !disabled ? adsManager.createSlot(path, size, {
863
877
  visible: isVisible,
864
878
  sizeMapping,
865
879
  targeting,
866
880
  categoryExclusions
867
881
  }) : null;
882
+ setSlot(newSlot);
868
883
  // if (currentSlot.current !== null && adsReady) {
869
884
  // adsManager.defineSlot(currentSlot.current);
870
885
  // }
871
- return currentSlot.current;
886
+ // return currentSlot.current;
887
+ return () => {
888
+ if (newSlot !== null) {
889
+ adsManager.destroySlot(newSlot);
890
+ }
891
+ };
872
892
  }, [adsManager, path, disabled, size, sizeMapping, alwaysRender, categoryExclusions]);
873
893
  useEffect(() => {
874
- if (currentSlot.current !== null) {
875
- currentSlot.current.setTargeting(targeting);
894
+ if (slot !== null) {
895
+ slot.setTargeting(targeting);
876
896
  }
877
- }, [targeting]);
897
+ }, [slot, targeting]);
878
898
 
879
899
  // Set visibility
880
900
  useEffect(() => {
@@ -942,12 +962,16 @@ function useAd(path, size) {
942
962
  }, [slot, disabled, setRenderEvent, onRender, track]);
943
963
 
944
964
  // Destroy slot
945
- useEffect(() => () => {
946
- if (slot !== null) {
947
- currentSlot.current = null;
948
- adsManager.destroySlot(slot);
949
- }
950
- }, []);
965
+ // useEffect(
966
+ // () => () => {
967
+ // if (slot !== null) {
968
+ // // currentSlot.current = null;
969
+ // adsManager.destroySlot(slot);
970
+ // }
971
+ // },
972
+ // [],
973
+ // );
974
+
951
975
  return {
952
976
  refObserver,
953
977
  slot,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@folklore/ads",
3
- "version": "0.0.39",
3
+ "version": "0.0.41",
4
4
  "description": "Ads library",
5
5
  "keywords": [
6
6
  "javascript",
@@ -50,7 +50,7 @@
50
50
  "publishConfig": {
51
51
  "access": "public"
52
52
  },
53
- "gitHead": "6a3484815046ba47850ba5d71e5eee9afe4adeb4",
53
+ "gitHead": "ee7a99e9a056e466320a99c43f1982d1d5900079",
54
54
  "dependencies": {
55
55
  "@folklore/hooks": "^0.0.44",
56
56
  "@folklore/tracking": "^0.0.23",