Skip to content

Commit

Permalink
fix: teardown happening during initial setup (#134)
Browse files Browse the repository at this point in the history
## Description
This fixes an issue that can occur if the AdvertisingProvider unmounts
during the initial GPT setup.

I've added a new state value (isInitialSetupComplete), which gets set
when the setup completes and prevents `omponentWillUnmount` from calling
the teardown function. This ensures we don't create a race condition
between the setup and teardown, which can break the display ads.

Co-authored-by: Stephen Gill <stephen.gill@adevinta.com>
  • Loading branch information
2 people authored and thedaviddias committed Feb 6, 2024
1 parent ff666ef commit 05f2bb8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-advertising",
"version": "4.2.8",
"version": "4.2.9",
"description": "Library for display ads in React applications",
"main": "lib/index.js",
"unpkg": "dist/react-advertising.min.js",
Expand Down
7 changes: 6 additions & 1 deletion src/components/AdvertisingProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default class AdvertisingProvider extends Component {
this.state = {
activate: this.advertising.activate.bind(this.advertising),
config: this.props.config,
isInitialSetupComplete: false,
};
}

Expand Down Expand Up @@ -66,7 +67,11 @@ export default class AdvertisingProvider extends Component {
}

async componentWillUnmount() {
if (this.props.config) {
/**
* Prevent the teardown call while initial setup still in progress
* otherwise it can create a race condition that breaks the setup process
*/
if (this.props.config && this.state.isInitialSetupComplete) {
await this.teardown();
}
}
Expand Down

0 comments on commit 05f2bb8

Please sign in to comment.