Skip to content

Commit c5068e8

Browse files
committed
fix: Race condition when init
1 parent e4b4b46 commit c5068e8

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed

flagsmith-core.ts

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const Flagsmith = class {
7474
}
7575
}
7676

77-
getFlags = () => {
77+
_getFlags = () => {
7878
const { identity, api } = this;
7979
this.log("Get Flags")
8080
this.isLoading = true;
@@ -116,6 +116,7 @@ const Flagsmith = class {
116116
this.flags = flags;
117117
this.traits = userTraits;
118118
this.updateStorage();
119+
this.initializing = false;
119120
this._onChange(this.oldFlags, {
120121
isFromServer: true,
121122
flagsChanged,
@@ -205,6 +206,24 @@ const Flagsmith = class {
205206
}
206207
};
207208

209+
getFlags = () => {
210+
if(this.initializing) {
211+
const flags = new Promise(() => {
212+
const interval = setInterval(async (resolve) => {
213+
if (this.initialised && !this.initializing) {
214+
clearInterval(interval)
215+
return this._getFlags().then(resolve)
216+
}
217+
}, 3000)
218+
})
219+
return flags
220+
}
221+
if (!this.initialised || this.initializing) {
222+
return Promise.resolve();
223+
}
224+
return this._getFlags()
225+
}
226+
208227
analyticsFlags = () => {
209228
const { api } = this;
210229

@@ -246,6 +265,7 @@ const Flagsmith = class {
246265
getFlagInterval: NodeJS.Timer|null= null
247266
headers?: object | null= null
248267
initialised= false
268+
initializing= true
249269
oldFlags:IFlags|null= null
250270
onChange:IInitConfig['onChange']|null= null
251271
onError:IInitConfig['onError']|null = null
@@ -437,19 +457,19 @@ const Flagsmith = class {
437457
}
438458
if (shouldFetchFlags) {
439459
// We want to resolve init since we have cached flags
440-
this.getFlags();
460+
this._getFlags();
441461
}
442462
} else {
443463
if (!preventFetch) {
444-
await this.getFlags();
464+
await this._getFlags();
445465
}
446466
}
447467
} catch (e) {
448468
this.log("Exception fetching cached logs", e);
449469
}
450470
} else {
451471
if (!preventFetch) {
452-
await this.getFlags();
472+
await this._getFlags();
453473
} else {
454474
if (defaultFlags) {
455475
this._onChange(null,
@@ -473,7 +493,7 @@ const Flagsmith = class {
473493
} catch (e) {}
474494
}
475495
} else if (!preventFetch) {
476-
await this.getFlags();
496+
await this._getFlags();
477497
} else {
478498
if (defaultFlags) {
479499
this._onChange(null, { isFromServer: false, flagsChanged: getChanges({}, defaultFlags), traitsChanged: getChanges({}, traits) }, this._loadedState(null, FlagSource.DEFAULT_FLAGS));
@@ -506,7 +526,21 @@ const Flagsmith = class {
506526
}
507527

508528
getAllFlags() {
509-
return this.flags;
529+
if(this.initialised) {
530+
if(this.initializing) {
531+
const flags = new Promise((resolve) => {
532+
const interval = setInterval(() => {
533+
if (this.initialised && !this.initializing) {
534+
clearInterval(interval)
535+
return resolve(true)
536+
}
537+
}, 3000)
538+
})
539+
return flags.then(() => {return this.flags})
540+
} else {
541+
return this.flags;
542+
}
543+
}
510544
}
511545

512546
identify(userId: string, traits?: ITraits) {
@@ -523,8 +557,8 @@ const Flagsmith = class {
523557
...traits
524558
};
525559
}
526-
if (this.initialised) {
527-
return this.getFlags();
560+
if (this.initialised && !this.initializing) {
561+
return this._getFlags();
528562
}
529563
return Promise.resolve();
530564
}
@@ -561,8 +595,8 @@ const Flagsmith = class {
561595
logout() {
562596
this.identity = null;
563597
this.traits = {};
564-
if (this.initialised) {
565-
return this.getFlags();
598+
if (this.initialised && !this.initializing) {
599+
return this._getFlags();
566600
}
567601
return Promise.resolve();
568602
}
@@ -571,7 +605,7 @@ const Flagsmith = class {
571605
if (this.getFlagInterval) {
572606
clearInterval(this.getFlagInterval);
573607
}
574-
this.getFlagInterval = setInterval(this.getFlags, ticks);
608+
this.getFlagInterval = setInterval(this._getFlags, ticks);
575609
}
576610

577611
stopListening() {
@@ -651,8 +685,8 @@ const Flagsmith = class {
651685
this.log("Set traits prior to identifying", this.withTraits);
652686
return
653687
}
654-
if (this.initialised) {
655-
return this.getFlags()
688+
if (this.initialised && !this.initializing) {
689+
return this._getFlags()
656690
}
657691
};
658692

@@ -810,7 +844,7 @@ const Flagsmith = class {
810844
this.log('updated_at is new, but flags are loading', e.data, this.timestamp);
811845
} else {
812846
this.log('updated_at is new, fetching flags', e.data, this.timestamp);
813-
this.getFlags();
847+
this._getFlags();
814848
}
815849
} else {
816850
this.log('updated_at is outdated, skipping get flags', e.data, this.timestamp);

0 commit comments

Comments
 (0)