Skip to content

Commit 8542593

Browse files
committed
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 #49190. PR Close #49191
1 parent d0fa598 commit 8542593

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

goldens/public-api/platform-browser/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ export class Title {
229229

230230
// @public
231231
export class TransferState {
232+
constructor();
232233
get<T>(key: StateKey<T>, defaultValue: T): T;
233234
hasKey<T>(key: StateKey<T>): boolean;
234235
get isEmpty(): boolean;

packages/platform-browser/src/browser/transfer_state.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,15 @@ export function makeStateKey<T = void>(key: string): StateKey<T> {
8383
*
8484
* @publicApi
8585
*/
86-
@Injectable({
87-
providedIn: 'root',
88-
useFactory: () => {
89-
const doc = inject(DOCUMENT);
90-
const appId = inject(APP_ID);
91-
const state = new TransferState();
92-
state.store = retrieveTransferredState(doc, appId);
93-
return state;
94-
}
95-
})
86+
@Injectable({providedIn: 'root'})
9687
export class TransferState {
9788
private store: {[k: string]: unknown|undefined} = {};
9889
private onSerializeCallbacks: {[k: string]: () => unknown | undefined} = {};
9990

91+
constructor() {
92+
this.store = retrieveTransferredState(inject(DOCUMENT), inject(APP_ID));
93+
}
94+
10095
/**
10196
* Get the value corresponding to a key. Return `defaultValue` if key is not found.
10297
*/

0 commit comments

Comments
 (0)