MCPcopy Create free account
hub / github.com/angular/angular / TransferState

Class TransferState

packages/core/src/transfer_state.ts:71–158  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

69 * @publicApi
70 */
71export class TransferState {
72 /** @nocollapse */
73 static ɵprov = /** @pureOrBreakMyCode */ /* @__PURE__ */ ɵɵdefineInjectable({
74 token: TransferState,
75 providedIn: 'root',
76 factory: () => {
77 const transferState = new TransferState();
78 if (typeof ngServerMode === 'undefined' || !ngServerMode) {
79 transferState.store = retrieveTransferredState(inject(DOCUMENT), inject(APP_ID));
80 }
81
82 return transferState;
83 },
84 });
85
86 /** @internal */
87 store: Record<string, unknown | undefined> = createDictionary();
88
89 private onSerializeCallbacks: {[k: string]: () => unknown | undefined} = createDictionary();
90
91 /**
92 * Get the value corresponding to a key. Return `defaultValue` if key is not found.
93 */
94 get<T>(key: StateKey<T>, defaultValue: T): T {
95 if (!Object.hasOwn(this.store, key)) {
96 return defaultValue;
97 }
98
99 const value = this.store[key];
100 return value !== undefined ? (value as T) : defaultValue;
101 }
102
103 /**
104 * Set the value corresponding to a key.
105 */
106 set<T>(key: StateKey<T>, value: T): void {
107 this.store[key] = value;
108 }
109
110 /**
111 * Remove a key from the store.
112 */
113 remove<T>(key: StateKey<T>): void {
114 delete this.store[key];
115 }
116
117 /**
118 * Test whether a key exists in the store.
119 */
120 hasKey<T>(key: StateKey<T>): boolean {
121 return Object.hasOwn(this.store, key);
122 }
123
124 /**
125 * Indicates whether the state is empty.
126 */
127 get isEmpty(): boolean {
128 return Object.keys(this.store).length === 0;

Callers

nothing calls this directly

Calls 4

ɵɵdefineInjectableFunction · 0.90
injectFunction · 0.90
retrieveTransferredStateFunction · 0.85
createDictionaryFunction · 0.85

Tested by

no test coverage detected