-
Notifications
You must be signed in to change notification settings - Fork 26.2k
/
Copy pathinterfaces.ts
345 lines (292 loc) Β· 9.44 KB
/
interfaces.ts
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
* @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 type {Provider} from '../di/interface/provider';
import type {LContainer} from '../render3/interfaces/container';
import type {DependencyType} from '../render3/interfaces/definition';
import type {TNode} from '../render3/interfaces/node';
import type {LView} from '../render3/interfaces/view';
/**
* Basic set of data structures used for identifying a defer block
* and triggering defer blocks
*/
export interface DehydratedDeferBlock {
lView: LView;
tNode: TNode;
lContainer: LContainer;
}
/**
* Describes the shape of a function generated by the compiler
* to download dependencies that can be defer-loaded.
*/
export type DependencyResolverFn = () => Array<Promise<DependencyType>>;
/**
* Defines types of defer block triggers.
*/
export const enum TriggerType {
/**
* Represents regular triggers (e.g. `@defer (on idle) { ... }`).
*/
Regular,
/**
* Represents prefetch triggers (e.g. `@defer (prefetch on idle) { ... }`).
*/
Prefetch,
/**
* Represents hydrate triggers (e.g. `@defer (hydrate on idle) { ... }`).
*/
Hydrate,
}
/**
* Describes the state of defer block dependency loading.
*/
export enum DeferDependenciesLoadingState {
/** Initial state, dependency loading is not yet triggered */
NOT_STARTED,
/** Dependency loading is in progress */
IN_PROGRESS,
/** Dependency loading has completed successfully */
COMPLETE,
/** Dependency loading has failed */
FAILED,
}
/** Slot index where `minimum` parameter value is stored. */
export const MINIMUM_SLOT = 0;
/** Slot index where `after` parameter value is stored. */
export const LOADING_AFTER_SLOT = 1;
/** Configuration object for a loading block as it is stored in the component constants. */
export type DeferredLoadingBlockConfig = [minimumTime: number | null, afterTime: number | null];
/** Configuration object for a placeholder block as it is stored in the component constants. */
export type DeferredPlaceholderBlockConfig = [minimumTime: number | null];
/**
* Describes the data shared across all instances of a defer block.
*/
export interface TDeferBlockDetails {
/**
* Index in an LView and TData arrays where a template for the primary content
* can be found.
*/
primaryTmplIndex: number;
/**
* Index in an LView and TData arrays where a template for the loading block can be found.
*/
loadingTmplIndex: number | null;
/**
* Extra configuration parameters (such as `after` and `minimum`) for the loading block.
*/
loadingBlockConfig: DeferredLoadingBlockConfig | null;
/**
* Index in an LView and TData arrays where a template for the placeholder block can be found.
*/
placeholderTmplIndex: number | null;
/**
* Extra configuration parameters (such as `after` and `minimum`) for the placeholder block.
*/
placeholderBlockConfig: DeferredPlaceholderBlockConfig | null;
/**
* Index in an LView and TData arrays where a template for the error block can be found.
*/
errorTmplIndex: number | null;
/**
* Compiler-generated function that loads all dependencies for a defer block.
*/
dependencyResolverFn: DependencyResolverFn | null;
/**
* Keeps track of the current loading state of defer block dependencies.
*/
loadingState: DeferDependenciesLoadingState;
/**
* Dependency loading Promise. This Promise is helpful for cases when there
* are multiple instances of a defer block (e.g. if it was used inside of an *ngFor),
* which all await the same set of dependencies.
*/
loadingPromise: Promise<unknown> | null;
/**
* List of providers collected from all NgModules that were imported by
* standalone components used within this defer block.
*/
providers: Provider[] | null;
/**
* List of hydrate triggers for a given block
*/
hydrateTriggers: Map<DeferBlockTrigger, HydrateTriggerDetails | null> | null;
/**
* Defer block flags, which should be used for all
* instances of a given defer block (the flags that should be
* placed into the `TDeferDetails` at runtime).
*/
flags: TDeferDetailsFlags;
/**
* Tracks debugging information about the deferred block.
*/
debug: {
/** Text representations of the block's triggers. */
triggers?: Set<string>;
} | null;
}
/**
* Specifies defer block flags, which should be used for all
* instances of a given defer block (the flags that should be
* placed into the `TDeferDetails` at runtime).
*/
export const enum TDeferDetailsFlags {
Default = 0,
/**
* Whether or not the defer block has hydrate triggers.
*/
HasHydrateTriggers = 1 << 0,
}
/**
* Describes the current state of this defer block instance.
*
* @publicApi
*/
export enum DeferBlockState {
/** The placeholder block content is rendered */
Placeholder = 0,
/** The loading block content is rendered */
Loading = 1,
/** The main content block content is rendered */
Complete = 2,
/** The error block content is rendered */
Error = 3,
}
/**
* Represents defer trigger types.
*/
export const enum DeferBlockTrigger {
Idle,
Immediate,
Viewport,
Interaction,
Hover,
Timer,
When,
Never,
}
/** * Describes specified delay (in ms) in the `hydrate on timer()` trigger. */
export interface HydrateTimerTriggerDetails {
delay: number;
}
/** * Describes all possible hydration trigger details specified in a template. */
export type HydrateTriggerDetails = HydrateTimerTriggerDetails;
/**
* Describes the initial state of this defer block instance.
*
* Note: this state is internal only and *must* be represented
* with a number lower than any value in the `DeferBlockState` enum.
*/
export enum DeferBlockInternalState {
/** Initial state. Nothing is rendered yet. */
Initial = -1,
}
export const NEXT_DEFER_BLOCK_STATE = 0;
// Note: it's *important* to keep the state in this slot, because this slot
// is used by runtime logic to differentiate between LViews, LContainers and
// other types (see `isLView` and `isLContainer` functions). In case of defer
// blocks, this slot would always be a number.
export const DEFER_BLOCK_STATE = 1;
export const STATE_IS_FROZEN_UNTIL = 2;
export const LOADING_AFTER_CLEANUP_FN = 3;
export const TRIGGER_CLEANUP_FNS = 4;
export const PREFETCH_TRIGGER_CLEANUP_FNS = 5;
export const SSR_UNIQUE_ID = 6;
export const SSR_BLOCK_STATE = 7;
export const ON_COMPLETE_FNS = 8;
export const HYDRATE_TRIGGER_CLEANUP_FNS = 9;
/**
* Describes instance-specific defer block data.
*
* Note: currently there is only the `state` slot, but more slots
* would be added later to keep track of `after` and `maximum` features
* (which would require per-instance state).
*/
export interface LDeferBlockDetails extends Array<unknown> {
/**
* Currently rendered block state.
*/
[DEFER_BLOCK_STATE]: DeferBlockState | DeferBlockInternalState;
/**
* Block state that was requested when another state was rendered.
*/
[NEXT_DEFER_BLOCK_STATE]: DeferBlockState | null;
/**
* Timestamp indicating when the current state can be switched to
* the next one, in case teh current state has `minimum` parameter.
*/
[STATE_IS_FROZEN_UNTIL]: number | null;
/**
* Contains a reference to a cleanup function which cancels a timeout
* when Angular waits before rendering loading state. This is used when
* the loading block has the `after` parameter configured.
*/
[LOADING_AFTER_CLEANUP_FN]: VoidFunction | null;
/**
* List of cleanup functions for regular triggers.
*/
[TRIGGER_CLEANUP_FNS]: VoidFunction[] | null;
/**
* List of cleanup functions for prefetch triggers.
*/
[PREFETCH_TRIGGER_CLEANUP_FNS]: VoidFunction[] | null;
/**
* Unique id of this defer block assigned during SSR.
*/
[SSR_UNIQUE_ID]: string | null;
/**
* Defer block state after SSR.
*/
[SSR_BLOCK_STATE]: number | null;
/**
* A set of callbacks to be invoked once the main content is rendered.
*/
[ON_COMPLETE_FNS]: VoidFunction[] | null;
/**
* List of cleanup functions for hydrate triggers.
*/
[HYDRATE_TRIGGER_CLEANUP_FNS]: VoidFunction[] | null;
}
/**
* Internal structure used for configuration of defer block behavior.
* */
export interface DeferBlockConfig {
behavior: DeferBlockBehavior;
}
/**
* Options for configuring defer blocks behavior.
* @publicApi
*/
export enum DeferBlockBehavior {
/**
* Manual triggering mode for defer blocks. Provides control over when defer blocks render
* and which state they render.
*/
Manual,
/**
* Playthrough mode for defer blocks. This mode behaves like defer blocks would in a browser.
* This is the default behavior in test environments.
*/
Playthrough,
}
/**
* **INTERNAL**, avoid referencing it in application code.
*
* Describes a helper class that allows to intercept a call to retrieve current
* dependency loading function and replace it with a different implementation.
* This interceptor class is needed to allow testing blocks in different states
* by simulating loading response.
*/
export interface DeferBlockDependencyInterceptor {
/**
* Invoked for each defer block when dependency loading function is accessed.
*/
intercept(dependencyFn: DependencyResolverFn | null): DependencyResolverFn | null;
/**
* Allows to configure an interceptor function.
*/
setInterceptor(interceptorFn: (current: DependencyResolverFn) => DependencyResolverFn): void;
}