Skip to content

Commit 12eaa97

Browse files
fix(angular): fix race condition on initialization (#1052)
<!-- Please use this template for your pull request. --> <!-- Please use the sections that you need and delete other sections --> ## This PR <!-- add the description of the PR here --> Fixes a race condition when the directive is first rendered. Initially the initialization code in the constructor before the `@Input`s were bound to the variables. This meant that in some cases providers were called with `undefined` as flag key, which most providers accept, but for flipt-web the engine paniced due to the value being `unit` instead of a string. --------- Signed-off-by: Lukas Reining <[email protected]>
1 parent 8d0d0e6 commit 12eaa97

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

packages/angular/projects/angular-sdk/src/lib/feature-flag.directive.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
Input,
66
OnChanges,
77
OnDestroy,
8+
OnInit,
89
TemplateRef,
910
ViewContainerRef,
1011
} from '@angular/core';
@@ -33,7 +34,7 @@ class FeatureFlagDirectiveContext<T extends FlagValue> {
3334
standalone: true,
3435
selector: '[featureFlag]',
3536
})
36-
export abstract class FeatureFlagDirective<T extends FlagValue> implements OnDestroy, OnChanges {
37+
export abstract class FeatureFlagDirective<T extends FlagValue> implements OnInit, OnDestroy, OnChanges {
3738
protected _featureFlagDefault: T;
3839
protected _featureFlagDomain: string | undefined;
3940

@@ -69,14 +70,21 @@ export abstract class FeatureFlagDirective<T extends FlagValue> implements OnDes
6970
templateRef: TemplateRef<FeatureFlagDirectiveContext<T>>,
7071
) {
7172
this._thenTemplateRef = templateRef;
72-
this.initClient();
7373
}
7474

7575
set featureFlagDomain(domain: string | undefined) {
76+
/**
77+
* We have to handle the change of the domain explicitly because we need to get a new client when the domain changes.
78+
* This can not be done if we simply relay the onChanges method.
79+
*/
7680
this._featureFlagDomain = domain;
7781
this.initClient();
7882
}
7983

84+
ngOnInit(): void {
85+
this.initClient();
86+
}
87+
8088
ngOnChanges(): void {
8189
this._flagChangeHandler?.();
8290
}
@@ -200,7 +208,7 @@ export abstract class FeatureFlagDirective<T extends FlagValue> implements OnDes
200208
* Usage examples:
201209
*
202210
* ```
203-
* <div *booleanFeatureFlag="'flagKey'; default: 0; let value">{{ value }}</div>
211+
* <div *booleanFeatureFlag="'flagKey'; default: false; let value">{{ value }}</div>
204212
* ```
205213
* ```
206214
* <div *booleanFeatureFlag="flagKey; default: false; else: elseTemplate">Content to render when flag is true.</div>

0 commit comments

Comments
 (0)