forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipe.ts
More file actions
320 lines (305 loc) · 10.6 KB
/
pipe.ts
File metadata and controls
320 lines (305 loc) · 10.6 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
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
/**
* @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 {PipeTransform} from '../change_detection/pipe_transform';
import {setInjectImplementation} from '../di/inject_switch';
import {formatRuntimeError, RuntimeError, RuntimeErrorCode} from '../errors';
import {Type} from '../interface/type';
import {InjectorProfilerContext, setInjectorProfilerContext} from './debug/injector_profiler';
import {getFactoryDef} from './definition_factory';
import {NodeInjector, setIncludeViewProviders} from './di';
import {store, ɵɵdirectiveInject} from './instructions/all';
import {isHostComponentStandalone} from './instructions/element_validation';
import {PipeDef, PipeDefList} from './interfaces/definition';
import {TTextNode} from './interfaces/node';
import {CONTEXT, DECLARATION_COMPONENT_VIEW, HEADER_OFFSET, LView, TVIEW} from './interfaces/view';
import {
pureFunction1Internal,
pureFunction2Internal,
pureFunction3Internal,
pureFunction4Internal,
pureFunctionVInternal,
} from './pure_function';
import {getBindingRoot, getCurrentTNode, getLView, getTView} from './state';
import {load} from './util/view_utils';
/**
* Create a pipe.
*
* @param index Pipe index where the pipe will be stored.
* @param pipeName The name of the pipe
* @returns T the instance of the pipe.
*
* @codeGenApi
*/
export function ɵɵpipe(index: number, pipeName: string): any {
const tView = getTView();
let pipeDef: PipeDef<any>;
const adjustedIndex = index + HEADER_OFFSET;
if (tView.firstCreatePass) {
// The `getPipeDef` throws if a pipe with a given name is not found
// (so we use non-null assertion below).
pipeDef = getPipeDef(pipeName, tView.pipeRegistry)!;
tView.data[adjustedIndex] = pipeDef;
if (pipeDef.onDestroy) {
(tView.destroyHooks ??= []).push(adjustedIndex, pipeDef.onDestroy);
}
} else {
pipeDef = tView.data[adjustedIndex] as PipeDef<any>;
}
const pipeFactory = pipeDef.factory || (pipeDef.factory = getFactoryDef(pipeDef.type, true));
let previousInjectorProfilerContext: InjectorProfilerContext;
if (ngDevMode) {
previousInjectorProfilerContext = setInjectorProfilerContext({
injector: new NodeInjector(getCurrentTNode() as TTextNode, getLView()),
token: pipeDef.type,
});
}
const previousInjectImplementation = setInjectImplementation(ɵɵdirectiveInject);
try {
// DI for pipes is supposed to behave like directives when placed on a component
// host node, which means that we have to disable access to `viewProviders`.
const previousIncludeViewProviders = setIncludeViewProviders(false);
const pipeInstance = pipeFactory();
setIncludeViewProviders(previousIncludeViewProviders);
store(tView, getLView(), adjustedIndex, pipeInstance);
return pipeInstance;
} finally {
// we have to restore the injector implementation in finally, just in case the creation of the
// pipe throws an error.
setInjectImplementation(previousInjectImplementation);
ngDevMode && setInjectorProfilerContext(previousInjectorProfilerContext!);
}
}
/**
* Searches the pipe registry for a pipe with the given name. If one is found,
* returns the pipe. Otherwise, an error is thrown because the pipe cannot be resolved.
*
* @param name Name of pipe to resolve
* @param registry Full list of available pipes
* @returns Matching PipeDef
*/
function getPipeDef(name: string, registry: PipeDefList | null): PipeDef<any> | undefined {
if (registry) {
if (ngDevMode) {
const pipes = registry.filter((pipe) => pipe.name === name);
// TODO: Throw an error in the next major
if (pipes.length > 1) {
console.warn(
formatRuntimeError(
RuntimeErrorCode.MULTIPLE_MATCHING_PIPES,
getMultipleMatchingPipesMessage(name),
),
);
}
}
for (let i = registry.length - 1; i >= 0; i--) {
const pipeDef = registry[i];
if (name === pipeDef.name) {
return pipeDef;
}
}
}
if (ngDevMode) {
throw new RuntimeError(RuntimeErrorCode.PIPE_NOT_FOUND, getPipeNotFoundErrorMessage(name));
}
return;
}
/**
* Generates a helpful error message for the user when multiple pipes match the name.
*
* @param name Name of the pipe
* @returns The error message
*/
function getMultipleMatchingPipesMessage(name: string) {
const lView = getLView();
const declarationLView = lView[DECLARATION_COMPONENT_VIEW] as LView<Type<unknown>>;
const context = declarationLView[CONTEXT];
const hostIsStandalone = isHostComponentStandalone(lView);
const componentInfoMessage = context ? ` in the '${context.constructor.name}' component` : '';
const verifyMessage = `check ${
hostIsStandalone ? "'@Component.imports' of this component" : 'the imports of this module'
}`;
const errorMessage = `Multiple pipes match the name \`${name}\`${componentInfoMessage}. ${verifyMessage}`;
return errorMessage;
}
/**
* Generates a helpful error message for the user when a pipe is not found.
*
* @param name Name of the missing pipe
* @returns The error message
*/
function getPipeNotFoundErrorMessage(name: string) {
const lView = getLView();
const declarationLView = lView[DECLARATION_COMPONENT_VIEW] as LView<Type<unknown>>;
const context = declarationLView[CONTEXT];
const hostIsStandalone = isHostComponentStandalone(lView);
const componentInfoMessage = context ? ` in the '${context.constructor.name}' component` : '';
const verifyMessage = `Verify that it is ${
hostIsStandalone
? "included in the '@Component.imports' of this component"
: 'declared or imported in this module'
}`;
const errorMessage = `The pipe '${name}' could not be found${componentInfoMessage}. ${verifyMessage}`;
return errorMessage;
}
/**
* Invokes a pipe with 1 arguments.
*
* This instruction acts as a guard to {@link PipeTransform#transform} invoking
* the pipe only when an input to the pipe changes.
*
* @param index Pipe index where the pipe was stored on creation.
* @param offset the binding offset
* @param v1 1st argument to {@link PipeTransform#transform}.
*
* @codeGenApi
*/
export function ɵɵpipeBind1(index: number, offset: number, v1: any): any {
const adjustedIndex = index + HEADER_OFFSET;
const lView = getLView();
const pipeInstance = load<PipeTransform>(lView, adjustedIndex);
return isPure(lView, adjustedIndex)
? pureFunction1Internal(
lView,
getBindingRoot(),
offset,
pipeInstance.transform,
v1,
pipeInstance,
)
: pipeInstance.transform(v1);
}
/**
* Invokes a pipe with 2 arguments.
*
* This instruction acts as a guard to {@link PipeTransform#transform} invoking
* the pipe only when an input to the pipe changes.
*
* @param index Pipe index where the pipe was stored on creation.
* @param slotOffset the offset in the reserved slot space
* @param v1 1st argument to {@link PipeTransform#transform}.
* @param v2 2nd argument to {@link PipeTransform#transform}.
*
* @codeGenApi
*/
export function ɵɵpipeBind2(index: number, slotOffset: number, v1: any, v2: any): any {
const adjustedIndex = index + HEADER_OFFSET;
const lView = getLView();
const pipeInstance = load<PipeTransform>(lView, adjustedIndex);
return isPure(lView, adjustedIndex)
? pureFunction2Internal(
lView,
getBindingRoot(),
slotOffset,
pipeInstance.transform,
v1,
v2,
pipeInstance,
)
: pipeInstance.transform(v1, v2);
}
/**
* Invokes a pipe with 3 arguments.
*
* This instruction acts as a guard to {@link PipeTransform#transform} invoking
* the pipe only when an input to the pipe changes.
*
* @param index Pipe index where the pipe was stored on creation.
* @param slotOffset the offset in the reserved slot space
* @param v1 1st argument to {@link PipeTransform#transform}.
* @param v2 2nd argument to {@link PipeTransform#transform}.
* @param v3 4rd argument to {@link PipeTransform#transform}.
*
* @codeGenApi
*/
export function ɵɵpipeBind3(index: number, slotOffset: number, v1: any, v2: any, v3: any): any {
const adjustedIndex = index + HEADER_OFFSET;
const lView = getLView();
const pipeInstance = load<PipeTransform>(lView, adjustedIndex);
return isPure(lView, adjustedIndex)
? pureFunction3Internal(
lView,
getBindingRoot(),
slotOffset,
pipeInstance.transform,
v1,
v2,
v3,
pipeInstance,
)
: pipeInstance.transform(v1, v2, v3);
}
/**
* Invokes a pipe with 4 arguments.
*
* This instruction acts as a guard to {@link PipeTransform#transform} invoking
* the pipe only when an input to the pipe changes.
*
* @param index Pipe index where the pipe was stored on creation.
* @param slotOffset the offset in the reserved slot space
* @param v1 1st argument to {@link PipeTransform#transform}.
* @param v2 2nd argument to {@link PipeTransform#transform}.
* @param v3 3rd argument to {@link PipeTransform#transform}.
* @param v4 4th argument to {@link PipeTransform#transform}.
*
* @codeGenApi
*/
export function ɵɵpipeBind4(
index: number,
slotOffset: number,
v1: any,
v2: any,
v3: any,
v4: any,
): any {
const adjustedIndex = index + HEADER_OFFSET;
const lView = getLView();
const pipeInstance = load<PipeTransform>(lView, adjustedIndex);
return isPure(lView, adjustedIndex)
? pureFunction4Internal(
lView,
getBindingRoot(),
slotOffset,
pipeInstance.transform,
v1,
v2,
v3,
v4,
pipeInstance,
)
: pipeInstance.transform(v1, v2, v3, v4);
}
/**
* Invokes a pipe with variable number of arguments.
*
* This instruction acts as a guard to {@link PipeTransform#transform} invoking
* the pipe only when an input to the pipe changes.
*
* @param index Pipe index where the pipe was stored on creation.
* @param slotOffset the offset in the reserved slot space
* @param values Array of arguments to pass to {@link PipeTransform#transform} method.
*
* @codeGenApi
*/
export function ɵɵpipeBindV(index: number, slotOffset: number, values: [any, ...any[]]): any {
const adjustedIndex = index + HEADER_OFFSET;
const lView = getLView();
const pipeInstance = load<PipeTransform>(lView, adjustedIndex);
return isPure(lView, adjustedIndex)
? pureFunctionVInternal(
lView,
getBindingRoot(),
slotOffset,
pipeInstance.transform,
values,
pipeInstance,
)
: pipeInstance.transform.apply(pipeInstance, values);
}
function isPure(lView: LView, index: number): boolean {
return (<PipeDef<any>>lView[TVIEW].data[index]).pure;
}