-
Notifications
You must be signed in to change notification settings - Fork 29.5k
Expand file tree
/
Copy pathtransfer_state.ts
More file actions
180 lines (161 loc) · 5.04 KB
/
Copy pathtransfer_state.ts
File metadata and controls
180 lines (161 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {APP_ID} from './application/application_tokens';
import {inject} from './di/injector_compatibility';
import {ɵɵdefineInjectable} from './di/interface/defs';
import {DOCUMENT} from './document';
/**
* A type-safe key to use with `TransferState`.
*
* Example:
*
* ```ts
* const COUNTER_KEY = makeStateKey<number>('counter');
* let value = 10;
*
* transferState.set(COUNTER_KEY, value);
* ```
*
* @publicApi
*/
export type StateKey<T> = string & {
__not_a_string: never;
__value_type?: T;
};
/**
* Create a `StateKey<T>` that can be used to store value of type T with `TransferState`.
*
* Example:
*
* ```ts
* const COUNTER_KEY = makeStateKey<number>('counter');
* let value = 10;
*
* transferState.set(COUNTER_KEY, value);
* ```
*
* @publicApi
*/
export function makeStateKey<T = void>(key: string): StateKey<T> {
return key as StateKey<T>;
}
function createDictionary<T>(): Record<string, T> {
// StateKey can be any string, including names of special Object prototype properties.
return Object.create(null);
}
/**
* A key value store that is transferred from the application on the server side to the application
* on the client side.
*
* The `TransferState` is available as an injectable token.
* On the client, just inject this token using DI and use it, it will be lazily initialized.
*
* The values in the store are serialized/deserialized using JSON.stringify/JSON.parse. So only
* boolean, number, string, null and non-class objects will be serialized and deserialized in a
* non-lossy manner.
*
* @publicApi
*/
export class TransferState {
/** @nocollapse */
static ɵprov = /** @pureOrBreakMyCode */ /* @__PURE__ */ ɵɵdefineInjectable({
token: TransferState,
providedIn: 'root',
factory: () => {
const transferState = new TransferState();
if (typeof ngServerMode === 'undefined' || !ngServerMode) {
transferState.store = retrieveTransferredState(inject(DOCUMENT), inject(APP_ID));
}
return transferState;
},
});
/** @internal */
store: Record<string, unknown | undefined> = createDictionary();
private onSerializeCallbacks: {[k: string]: () => unknown | undefined} = createDictionary();
/**
* Get the value corresponding to a key. Return `defaultValue` if key is not found.
*/
get<T>(key: StateKey<T>, defaultValue: T): T {
if (!Object.hasOwn(this.store, key)) {
return defaultValue;
}
const value = this.store[key];
return value !== undefined ? (value as T) : defaultValue;
}
/**
* Set the value corresponding to a key.
*/
set<T>(key: StateKey<T>, value: T): void {
this.store[key] = value;
}
/**
* Remove a key from the store.
*/
remove<T>(key: StateKey<T>): void {
delete this.store[key];
}
/**
* Test whether a key exists in the store.
*/
hasKey<T>(key: StateKey<T>): boolean {
return Object.hasOwn(this.store, key);
}
/**
* Indicates whether the state is empty.
*/
get isEmpty(): boolean {
return Object.keys(this.store).length === 0;
}
/**
* Register a callback to provide the value for a key when `toJson` is called.
*/
onSerialize<T>(key: StateKey<T>, callback: () => T): void {
this.onSerializeCallbacks[key] = callback;
}
/**
* Serialize the current state of the store to JSON.
*/
toJson(): string {
// Call the onSerialize callbacks and put those values into the store.
for (const key in this.onSerializeCallbacks) {
if (Object.hasOwn(this.onSerializeCallbacks, key)) {
try {
this.store[key] = this.onSerializeCallbacks[key]();
} catch (e) {
console.warn('Exception in onSerialize callback: ', e);
}
}
}
// Escape script tag to avoid break out of <script> tag in serialized output.
// Encoding of `<` is the same behaviour as G3 script_builders.
// Encoding of `/` prevents crawlers from incorrectly indexing relative URLs in inline JSON.
return JSON.stringify(this.store).replace(/</g, '\\u003C').replace(/\//g, '\\u002F');
}
}
export function retrieveTransferredState(
doc: Document,
appId: string,
): Record<string, unknown | undefined> {
// Locate the script tag with the JSON data transferred from the server.
// The id of the script tag is set to the Angular appId + 'state'.
const script = doc.getElementById(appId + '-state');
if (script?.tagName === 'SCRIPT' && script.textContent) {
try {
// Avoid using any here as it triggers lint errors in google3 (any is not allowed).
// Decoding of `<` is done of the box by browsers and node.js, same behaviour as G3
// script_builders.
return Object.assign(
createDictionary<unknown | undefined>(),
JSON.parse(script.textContent) as {},
);
} catch (e) {
console.warn('Exception while restoring TransferState for app ' + appId, e);
}
}
return createDictionary();
}