Skip to content

Commit 8702a6c

Browse files
authored
Migrate Angular code to call inject() (#6992)
## Motivation for features / changes When tsc's `--target` is updated `es2022` or newer, the initialization order of parameter properties changes. Inline field initializers that directly or indirectly reference parameter properties can break with this change as the parameter properties may be initialized if the reference occurs too soon. This can be difficult to accurately statically detect because it's not always clear if callbacks are executed immediately or later. Migrating to Angular's `inject()` function solves the problems as parameter properties are no longer used. ## Technical description of changes Updates classes where some inline field initializer has an indirect reference to a parameter property to use Angular `inject()`. ## Screenshots of UI changes (or N/A) N/A ## Detailed steps to verify changes work correctly (as executed by you) Should be a no-op refactor. Simply verify test flows the involve all modified classes. ## Alternate designs / implementations considered (or N/A) We could also avoid this upcoming issue by moving the inline field initializers that reference uninitialized parameter properties into the constructor body. This is generally seen as an inferior change though.
1 parent 041643b commit 8702a6c

File tree

5 files changed

+31
-38
lines changed

5 files changed

+31
-38
lines changed

tensorboard/webapp/alert/effects/index.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License.
1414
==============================================================================*/
15-
import {Injectable} from '@angular/core';
15+
import {Injectable, inject} from '@angular/core';
1616
import {Actions, createEffect} from '@ngrx/effects';
1717
import {Store} from '@ngrx/store';
1818
import {tap} from 'rxjs/operators';
@@ -22,11 +22,9 @@ import {AlertActionModule} from '../alert_action_module';
2222

2323
@Injectable()
2424
export class AlertEffects {
25-
constructor(
26-
private readonly actions$: Actions,
27-
private readonly store: Store<State>,
28-
private readonly alertActionModule: AlertActionModule
29-
) {}
25+
private readonly actions$ = inject(Actions);
26+
private readonly store: Store<State> = inject(Store);
27+
private readonly alertActionModule = inject(AlertActionModule);
3028

3129
/** @export */
3230
reportRegisteredActionAlerts$ = createEffect(

tensorboard/webapp/feature_flag/effects/feature_flag_effects.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
1313
limitations under the License.
1414
==============================================================================*/
1515

16-
import {Injectable} from '@angular/core';
16+
import {inject, Injectable} from '@angular/core';
1717
import {Actions, createEffect, ofType} from '@ngrx/effects';
1818
import {Action, createAction, Store} from '@ngrx/store';
1919
import {combineLatestWith, map, tap, withLatestFrom} from 'rxjs/operators';
@@ -37,6 +37,10 @@ const effectsInitialized = createAction('[FEATURE FLAG] Effects Init');
3737

3838
@Injectable()
3939
export class FeatureFlagEffects {
40+
private readonly actions$ = inject(Actions);
41+
private readonly store: Store<State> = inject(Store);
42+
private readonly dataSource = inject(TBFeatureFlagDataSource);
43+
4044
// Ngrx assumes all Effect classes have properties that inherit from the base
4145
// JS Object. `tf_feature_flags` does not, so we wrap it.
4246
private readonly tfFeatureFlags = {
@@ -128,12 +132,6 @@ export class FeatureFlagEffects {
128132
{dispatch: false}
129133
);
130134

131-
constructor(
132-
private readonly actions$: Actions,
133-
private readonly store: Store<State>,
134-
private readonly dataSource: TBFeatureFlagDataSource
135-
) {}
136-
137135
/** @export */
138136
ngrxOnInitEffects(): Action {
139137
return effectsInitialized();

tensorboard/webapp/notification_center/_redux/notification_center_effects.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License.
1414
==============================================================================*/
15-
import {Injectable} from '@angular/core';
15+
import {Injectable, inject} from '@angular/core';
1616
import {Actions, createEffect, ofType, OnInitEffects} from '@ngrx/effects';
1717
import {Action, createAction, Store} from '@ngrx/store';
1818
import {EMPTY, Observable} from 'rxjs';
@@ -26,11 +26,9 @@ export const initAction = createAction('[NotificationCenter Effects] Init');
2626

2727
@Injectable()
2828
export class NotificationCenterEffects implements OnInitEffects {
29-
constructor(
30-
private readonly actions$: Actions,
31-
private readonly store: Store<State>,
32-
private readonly dataSource: NotificationCenterDataSource
33-
) {}
29+
private readonly actions$ = inject(Actions);
30+
private readonly store: Store<State> = inject(Store);
31+
private readonly dataSource = inject(NotificationCenterDataSource);
3432

3533
/** @export */
3634
ngrxOnInitEffects(): Action {

tensorboard/webapp/persistent_settings/_redux/persistent_settings_effects.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License.
1414
==============================================================================*/
15-
import {Injectable} from '@angular/core';
15+
import {Injectable, inject} from '@angular/core';
1616
import {Actions, createEffect, ofType} from '@ngrx/effects';
1717
import {Store} from '@ngrx/store';
1818
import {EMPTY, Observable, merge} from 'rxjs';
@@ -45,6 +45,15 @@ const DEBOUNCE_PERIOD_IN_MS = 500;
4545
*/
4646
@Injectable()
4747
export class PersistentSettingsEffects {
48+
private readonly actions$: Actions = inject(Actions);
49+
private readonly store: Store<{}> = inject(Store);
50+
private readonly configModule: PersistentSettingsConfigModule<
51+
{},
52+
PersistableSettings
53+
> = inject(PersistentSettingsConfigModule);
54+
private readonly dataSource: PersistentSettingsDataSource<PersistableSettings> =
55+
inject(PersistentSettingsDataSource);
56+
4857
/** @export */
4958
readonly initializeAndUpdateSettings$: Observable<void> = createEffect(
5059
() => {
@@ -109,16 +118,6 @@ export class PersistentSettingsEffects {
109118
},
110119
{dispatch: false}
111120
);
112-
113-
constructor(
114-
private readonly actions$: Actions,
115-
private readonly store: Store<{}>,
116-
private readonly configModule: PersistentSettingsConfigModule<
117-
{},
118-
PersistableSettings
119-
>,
120-
private readonly dataSource: PersistentSettingsDataSource<PersistableSettings>
121-
) {}
122121
}
123122

124123
export const TEST_ONLY = {DEBOUNCE_PERIOD_IN_MS};

tensorboard/webapp/runs/views/runs_table/regex_edit_dialog_container.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License.
1414
==============================================================================*/
15-
import {Component, Inject} from '@angular/core';
15+
import {Component, inject} from '@angular/core';
1616
import {MatDialogRef, MAT_DIALOG_DATA} from '@angular/material/dialog';
1717
import {Store} from '@ngrx/store';
1818
import {combineLatest, defer, merge, Observable, Subject} from 'rxjs';
@@ -69,6 +69,9 @@ const INPUT_CHANGE_DEBOUNCE_INTERVAL_MS = 500;
6969
],
7070
})
7171
export class RegexEditDialogContainer {
72+
private readonly store = inject<Store<State>>(Store);
73+
dialogRef = inject<MatDialogRef<RegexEditDialogContainer>>(MatDialogRef);
74+
7275
private readonly experimentIds: string[];
7376
private readonly runIdToEid$: Observable<Record<string, string>>;
7477
private readonly allRuns$: Observable<Run[]>;
@@ -152,14 +155,11 @@ export class RegexEditDialogContainer {
152155
);
153156
}).pipe(startWith([]));
154157

155-
constructor(
156-
private readonly store: Store<State>,
157-
public dialogRef: MatDialogRef<RegexEditDialogContainer>,
158-
@Inject(MAT_DIALOG_DATA)
159-
data: {
158+
constructor() {
159+
const data = inject<{
160160
experimentIds: string[];
161-
}
162-
) {
161+
}>(MAT_DIALOG_DATA);
162+
163163
this.expNameByExpId$ = this.store.select(getDashboardExperimentNames);
164164
this.enableColorByExperiment$ = this.store.select(
165165
getEnableColorByExperiment

0 commit comments

Comments
 (0)