MCPcopy
hub / github.com/angular/angular / TransferState

Class TransferState

packages/core/src/transfer_state.ts:66–148  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 3

ɵɵdefineInjectableFunction · 0.90
injectFunction · 0.90
retrieveTransferredStateFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…