mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
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:
@@ -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.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user