Skip to content

Commit

Permalink
refactor(platform-browser): move TransferState init logic into its co…
Browse files Browse the repository at this point in the history
…nstructor (#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
  • Loading branch information
AndrewKushnir committed Feb 24, 2023
1 parent d0fa598 commit 8542593
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
1 change: 1 addition & 0 deletions goldens/public-api/platform-browser/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 5 additions & 10 deletions packages/platform-browser/src/browser/transfer_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit 8542593

Please sign in to comment.