From 7de86b7e733104eec7324651d8efc8ff4299e810 Mon Sep 17 00:00:00 2001 From: Patrick McDougle Date: Thu, 24 Sep 2020 06:05:12 -0700 Subject: [PATCH] Fix a race condition when refresh is called closely to defineSlots (#206) --- js/manager.js | 12 +++++++----- lib/manager.js | 30 +++++++++++++++++++----------- package-lock.json | 2 +- package.json | 2 +- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/js/manager.js b/js/manager.js index d5837d7..8be1dfe 100644 --- a/js/manager.js +++ b/js/manager.js @@ -330,11 +330,13 @@ const DFPManager = Object.assign(new EventEmitter().setMaxListeners(0), { if (loadPromise === null) { this.load(); } else { - this.gptRefreshAds( - Object.keys( - this.getRefreshableSlots(...slots), - ), - ); + loadPromise.then(() => { + this.gptRefreshAds( + Object.keys( + this.getRefreshableSlots(...slots), + ), + ); + }); } }, diff --git a/lib/manager.js b/lib/manager.js index 0e7c223..f4d25ab 100644 --- a/lib/manager.js +++ b/lib/manager.js @@ -369,17 +369,25 @@ var DFPManager = Object.assign(new _events.EventEmitter().setMaxListeners(0), { }, slots); }, refresh: function refresh() { + var _this6 = this; + + for (var _len4 = arguments.length, slots = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) { + slots[_key4] = arguments[_key4]; + } + if (loadPromise === null) { this.load(); } else { - this.gptRefreshAds(Object.keys(this.getRefreshableSlots.apply(this, arguments))); + loadPromise.then(function () { + _this6.gptRefreshAds(Object.keys(_this6.getRefreshableSlots.apply(_this6, slots))); + }); } }, gptRefreshAds: function gptRefreshAds(slots) { - var _this6 = this; + var _this7 = this; return this.getGoogletag().then(function (googletag) { - _this6.configureOptions(googletag); + _this7.configureOptions(googletag); googletag.cmd.push(function () { var pubadsService = googletag.pubads(); @@ -390,17 +398,17 @@ var DFPManager = Object.assign(new _events.EventEmitter().setMaxListeners(0), { }); }, reload: function reload() { - var _this7 = this; + var _this8 = this; return this.destroyGPTSlots.apply(this, arguments).then(function () { - return _this7.load(); + return _this8.load(); }); }, destroyGPTSlots: function destroyGPTSlots() { - var _this8 = this; + var _this9 = this; - for (var _len4 = arguments.length, slotsToDestroy = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) { - slotsToDestroy[_key4] = arguments[_key4]; + for (var _len5 = arguments.length, slotsToDestroy = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) { + slotsToDestroy[_key5] = arguments[_key5]; } if (slotsToDestroy.length === 0) { @@ -417,7 +425,7 @@ var DFPManager = Object.assign(new _events.EventEmitter().setMaxListeners(0), { slots.push(slot); } - _this8.getGoogletag().then(function (googletag) { + _this9.getGoogletag().then(function (googletag) { googletag.cmd.push(function () { if (managerAlreadyInitialized === true) { if (slotsToDestroy.length > 0) { @@ -440,7 +448,7 @@ var DFPManager = Object.assign(new _events.EventEmitter().setMaxListeners(0), { }); }, registerSlot: function registerSlot(_ref) { - var _this9 = this; + var _this10 = this; var slotId = _ref.slotId, dfpNetworkId = _ref.dfpNetworkId, @@ -479,7 +487,7 @@ var DFPManager = Object.assign(new _events.EventEmitter().setMaxListeners(0), { gptSlot = slot.gptSlot; if (loading === false && !gptSlot) { - _this9.load(slotId); + _this10.load(slotId); } } }); diff --git a/package-lock.json b/package-lock.json index 4f14454..fedabd5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "react-dfp", - "version": "0.18.0", + "version": "0.19.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 80be223..4672eba 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-dfp", - "version": "0.18.0", + "version": "0.19.0", "homepage": "https://github.com/jaanauati/react-dfp/", "author": { "name": "Jonatan Alexis Anauati",