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

Class TransferState

packages/core/src/transfer_state.ts:69–156  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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