refactor(platform-browser): move TransferState init logic into its constructor (#49191)

This commit updates the TransferState class to move its init logic from the `useFactory` function to its constructor. The change is needed to make the init behavior consistent across different injection scenarios and tolerate the issue described in https://github.com/angular/angular/issues/49190.

PR Close #49191
This commit is contained in:
Andrew Kushnir
2023-02-23 15:29:50 -08:00
parent d0fa598fe4
commit 8542593c80
2 changed files with 6 additions and 10 deletions
@@ -229,6 +229,7 @@ export class Title {
// @public
export class TransferState {
constructor();
get<T>(key: StateKey<T>, defaultValue: T): T;
hasKey<T>(key: StateKey<T>): boolean;
get isEmpty(): boolean;
@@ -83,20 +83,15 @@ export function makeStateKey<T = void>(key: string): StateKey<T> {
*
* @publicApi
*/
@Injectable({
providedIn: 'root',
useFactory: () => {
const doc = inject(DOCUMENT);
const appId = inject(APP_ID);
const state = new TransferState();
state.store = retrieveTransferredState(doc, appId);
return state;
}
})
@Injectable({providedIn: 'root'})
export class TransferState {
private store: {[k: string]: unknown|undefined} = {};
private onSerializeCallbacks: {[k: string]: () => unknown | undefined} = {};
constructor() {
this.store = retrieveTransferredState(inject(DOCUMENT), inject(APP_ID));
}
/**
* Get the value corresponding to a key. Return `defaultValue` if key is not found.
*/