Skip to content

Commit f64ee15

Browse files
marclavalkara
authored andcommitted
feat(ivy): implement pipes (angular#22254)
PR Close angular#22254
1 parent 5d4fa7f commit f64ee15

File tree

9 files changed

+695
-67
lines changed

9 files changed

+695
-67
lines changed

packages/core/src/render3/definition.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,10 @@ export const defineDirective = defineComponent as<T>(directiveDefinition: Direct
181181
* @param pure Whether the pipe is pure.
182182
*/
183183
export function definePipe<T>(
184-
{type, factory, pure}: {type: Type<T>, factory: () => PipeTransform, pure?: boolean}):
185-
PipeDef<T> {
186-
throw new Error('TODO: implement!');
184+
{type, factory, pure}: {type: Type<T>, factory: () => T, pure?: boolean}): PipeDef<T> {
185+
return <PipeDef<T>>{
186+
n: factory,
187+
pure: pure !== false,
188+
onDestroy: type.prototype.ngOnDestroy || null
189+
};
187190
}

packages/core/src/render3/instructions.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {assertNodeType} from './node_assert';
2020
import {appendChild, insertChild, insertView, appendProjectedNode, removeView, canInsertNativeNode} from './node_manipulation';
2121
import {matchingSelectorIndex} from './node_selector_matcher';
2222
import {ComponentDef, ComponentTemplate, ComponentType, DirectiveDef, DirectiveType} from './interfaces/definition';
23-
import {RElement, RText, Renderer3, RendererFactory3, ProceduralRenderer3, RendererStyleFlags3, isProceduralRenderer} from './interfaces/renderer';
23+
import {RElement, RText, Renderer3, RendererFactory3, ProceduralRenderer3, ObjectOrientedRenderer3, RendererStyleFlags3, isProceduralRenderer} from './interfaces/renderer';
2424
import {isDifferent, stringify} from './util';
2525
import {executeHooks, executeContentHooks, queueLifecycleHooks, queueInitHooks, executeInitHooks} from './hooks';
2626
import {ViewRef} from './view_ref';
@@ -122,7 +122,7 @@ export function getCreationMode(): boolean {
122122
}
123123

124124
/**
125-
* An array of nodes (text, element, container, etc), their bindings, and
125+
* An array of nodes (text, element, container, etc), pipes, their bindings, and
126126
* any local variables that need to be stored between invocations.
127127
*/
128128
let data: any[];
@@ -1806,6 +1806,10 @@ export function bindingUpdated4(exp1: any, exp2: any, exp3: any, exp4: any): boo
18061806
return bindingUpdated2(exp3, exp4) || different;
18071807
}
18081808

1809+
export function getTView(): TView {
1810+
return currentView.tView;
1811+
}
1812+
18091813
export function getDirectiveInstance<T>(instanceOrArray: T | [T]): T {
18101814
// Directives with content queries store an array in data[directiveIndex]
18111815
// with the instance as the first index

packages/core/src/render3/interfaces/definition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export interface PipeDef<T> {
140140
* NOTE: this property is short (1 char) because it is used in
141141
* component templates which is sensitive to size.
142142
*/
143-
n: () => PipeTransform;
143+
n: () => T;
144144

145145
/**
146146
* Whether or not the pipe is pure.

packages/core/src/render3/interfaces/view.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {LContainer} from './container';
10-
import {ComponentTemplate, DirectiveDef} from './definition';
10+
import {ComponentTemplate, DirectiveDef, PipeDef} from './definition';
1111
import {LElementNode, LViewNode, TNode} from './node';
1212
import {LQueries} from './query';
1313
import {Renderer3} from './renderer';
@@ -324,11 +324,11 @@ export const enum LifecycleStage {
324324
* Static data that corresponds to the instance-specific data array on an LView.
325325
*
326326
* Each node's static data is stored in tData at the same index that it's stored
327-
* in the data array. Each directive's definition is stored here at the same index
328-
* as its directive instance in the data array. Any nodes that do not have static
327+
* in the data array. Each directive/pipe's definition is stored here at the same index
328+
* as its directive/pipe instance in the data array. Any nodes that do not have static
329329
* data store a null value in tData to avoid a sparse array.
330330
*/
331-
export type TData = (TNode | DirectiveDef<any>| null)[];
331+
export type TData = (TNode | DirectiveDef<any>| PipeDef<any>| null)[];
332332

333333
// Note: This hack is necessary so we don't erroneously get a circular dependency
334334
// failure based on types.

packages/core/src/render3/pipe.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,32 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {PipeTransform} from '../change_detection/pipe_transform';
10+
11+
import {getTView, load, store} from './instructions';
912
import {PipeDef} from './interfaces/definition';
13+
import {pureFunction1, pureFunction2, pureFunction3, pureFunction4, pureFunctionV} from './pure_function';
14+
1015

1116
/**
1217
* Create a pipe.
1318
*
1419
* @param index Pipe index where the pipe will be stored.
1520
* @param pipeDef Pipe definition object for registering life cycle hooks.
16-
* @param pipe A Pipe instance.
21+
* @param firstInstance (optional) The first instance of the pipe that can be reused for pure pipes.
22+
* @returns T the instance of the pipe.
1723
*/
18-
export function pipe<T>(index: number, pipeDef: PipeDef<T>, pipe: T): void {
19-
throw new Error('TODO: implement!');
24+
export function pipe<T>(index: number, pipeDef: PipeDef<T>, firstInstance?: T): T {
25+
const tView = getTView();
26+
if (tView.firstTemplatePass) {
27+
tView.data[index] = pipeDef;
28+
if (pipeDef.onDestroy != null) {
29+
(tView.destroyHooks || (tView.destroyHooks = [])).push(index, pipeDef.onDestroy);
30+
}
31+
}
32+
const pipeInstance = pipeDef.pure && firstInstance ? firstInstance : pipeDef.n();
33+
store(index, pipeInstance);
34+
return pipeInstance;
2035
}
2136

2237
/**
@@ -29,7 +44,9 @@ export function pipe<T>(index: number, pipeDef: PipeDef<T>, pipe: T): void {
2944
* @param v1 1st argument to {@link PipeTransform#transform}.
3045
*/
3146
export function pipeBind1(index: number, v1: any): any {
32-
throw new Error('TODO: implement!');
47+
const pipeInstance = load<PipeTransform>(index);
48+
return isPure(index) ? pureFunction1(pipeInstance.transform, v1, pipeInstance) :
49+
pipeInstance.transform(v1);
3350
}
3451

3552
/**
@@ -43,7 +60,9 @@ export function pipeBind1(index: number, v1: any): any {
4360
* @param v2 2nd argument to {@link PipeTransform#transform}.
4461
*/
4562
export function pipeBind2(index: number, v1: any, v2: any): any {
46-
throw new Error('TODO: implement!');
63+
const pipeInstance = load<PipeTransform>(index);
64+
return isPure(index) ? pureFunction2(pipeInstance.transform, v1, v2, pipeInstance) :
65+
pipeInstance.transform(v1, v2);
4766
}
4867

4968
/**
@@ -58,7 +77,9 @@ export function pipeBind2(index: number, v1: any, v2: any): any {
5877
* @param v3 4rd argument to {@link PipeTransform#transform}.
5978
*/
6079
export function pipeBind3(index: number, v1: any, v2: any, v3: any): any {
61-
throw new Error('TODO: implement!');
80+
const pipeInstance = load<PipeTransform>(index);
81+
return isPure(index) ? pureFunction3(pipeInstance.transform.bind(pipeInstance), v1, v2, v3) :
82+
pipeInstance.transform(v1, v2, v3);
6283
}
6384

6485
/**
@@ -74,7 +95,9 @@ export function pipeBind3(index: number, v1: any, v2: any, v3: any): any {
7495
* @param v4 4th argument to {@link PipeTransform#transform}.
7596
*/
7697
export function pipeBind4(index: number, v1: any, v2: any, v3: any, v4: any): any {
77-
throw new Error('TODO: implement!');
98+
const pipeInstance = load<PipeTransform>(index);
99+
return isPure(index) ? pureFunction4(pipeInstance.transform, v1, v2, v3, v4, pipeInstance) :
100+
pipeInstance.transform(v1, v2, v3, v4);
78101
}
79102

80103
/**
@@ -87,5 +110,11 @@ export function pipeBind4(index: number, v1: any, v2: any, v3: any, v4: any): an
87110
* @param values Array of arguments to pass to {@link PipeTransform#transform} method.
88111
*/
89112
export function pipeBindV(index: number, values: any[]): any {
90-
throw new Error('TODO: implement!');
91-
}
113+
const pipeInstance = load<PipeTransform>(index);
114+
return isPure(index) ? pureFunctionV(pipeInstance.transform, values, pipeInstance) :
115+
pipeInstance.transform.apply(pipeInstance, values);
116+
}
117+
118+
function isPure(index: number): boolean {
119+
return (<PipeDef<any>>getTView().data[index]).pure;
120+
}

packages/core/src/render3/pure_function.ts

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import {bindingUpdated, bindingUpdated2, bindingUpdated4, checkAndUpdateBinding,
1717
* @param pureFn Function that returns a value
1818
* @returns value
1919
*/
20-
export function pureFunction0<T>(pureFn: () => T): T {
21-
return getCreationMode() ? checkAndUpdateBinding(pureFn()) : consumeBinding();
20+
export function pureFunction0<T>(pureFn: () => T, thisArg?: any): T {
21+
return getCreationMode() ? checkAndUpdateBinding(thisArg ? pureFn.call(thisArg) : pureFn()) :
22+
consumeBinding();
2223
}
2324

2425
/**
@@ -29,8 +30,10 @@ export function pureFunction0<T>(pureFn: () => T): T {
2930
* @param exp Updated expression value
3031
* @returns Updated value
3132
*/
32-
export function pureFunction1(pureFn: (v: any) => any, exp: any): any {
33-
return bindingUpdated(exp) ? checkAndUpdateBinding(pureFn(exp)) : consumeBinding();
33+
export function pureFunction1(pureFn: (v: any) => any, exp: any, thisArg?: any): any {
34+
return bindingUpdated(exp) ?
35+
checkAndUpdateBinding(thisArg ? pureFn.call(thisArg, exp) : pureFn(exp)) :
36+
consumeBinding();
3437
}
3538

3639
/**
@@ -42,8 +45,11 @@ export function pureFunction1(pureFn: (v: any) => any, exp: any): any {
4245
* @param exp2
4346
* @returns Updated value
4447
*/
45-
export function pureFunction2(pureFn: (v1: any, v2: any) => any, exp1: any, exp2: any): any {
46-
return bindingUpdated2(exp1, exp2) ? checkAndUpdateBinding(pureFn(exp1, exp2)) : consumeBinding();
48+
export function pureFunction2(
49+
pureFn: (v1: any, v2: any) => any, exp1: any, exp2: any, thisArg?: any): any {
50+
return bindingUpdated2(exp1, exp2) ?
51+
checkAndUpdateBinding(thisArg ? pureFn.call(thisArg, exp1, exp2) : pureFn(exp1, exp2)) :
52+
consumeBinding();
4753
}
4854

4955
/**
@@ -57,10 +63,13 @@ export function pureFunction2(pureFn: (v1: any, v2: any) => any, exp1: any, exp2
5763
* @returns Updated value
5864
*/
5965
export function pureFunction3(
60-
pureFn: (v1: any, v2: any, v3: any) => any, exp1: any, exp2: any, exp3: any): any {
66+
pureFn: (v1: any, v2: any, v3: any) => any, exp1: any, exp2: any, exp3: any,
67+
thisArg?: any): any {
6168
const different = bindingUpdated2(exp1, exp2);
62-
return bindingUpdated(exp3) || different ? checkAndUpdateBinding(pureFn(exp1, exp2, exp3)) :
63-
consumeBinding();
69+
return bindingUpdated(exp3) || different ?
70+
checkAndUpdateBinding(
71+
thisArg ? pureFn.call(thisArg, exp1, exp2, exp3) : pureFn(exp1, exp2, exp3)) :
72+
consumeBinding();
6473
}
6574

6675
/**
@@ -75,10 +84,11 @@ export function pureFunction3(
7584
* @returns Updated value
7685
*/
7786
export function pureFunction4(
78-
pureFn: (v1: any, v2: any, v3: any, v4: any) => any, exp1: any, exp2: any, exp3: any,
79-
exp4: any): any {
87+
pureFn: (v1: any, v2: any, v3: any, v4: any) => any, exp1: any, exp2: any, exp3: any, exp4: any,
88+
thisArg?: any): any {
8089
return bindingUpdated4(exp1, exp2, exp3, exp4) ?
81-
checkAndUpdateBinding(pureFn(exp1, exp2, exp3, exp4)) :
90+
checkAndUpdateBinding(
91+
thisArg ? pureFn.call(thisArg, exp1, exp2, exp3, exp4) : pureFn(exp1, exp2, exp3, exp4)) :
8292
consumeBinding();
8393
}
8494

@@ -96,10 +106,12 @@ export function pureFunction4(
96106
*/
97107
export function pureFunction5(
98108
pureFn: (v1: any, v2: any, v3: any, v4: any, v5: any) => any, exp1: any, exp2: any, exp3: any,
99-
exp4: any, exp5: any): any {
109+
exp4: any, exp5: any, thisArg?: any): any {
100110
const different = bindingUpdated4(exp1, exp2, exp3, exp4);
101111
return bindingUpdated(exp5) || different ?
102-
checkAndUpdateBinding(pureFn(exp1, exp2, exp3, exp4, exp5)) :
112+
checkAndUpdateBinding(
113+
thisArg ? pureFn.call(thisArg, exp1, exp2, exp3, exp4, exp5) :
114+
pureFn(exp1, exp2, exp3, exp4, exp5)) :
103115
consumeBinding();
104116
}
105117

@@ -118,10 +130,12 @@ export function pureFunction5(
118130
*/
119131
export function pureFunction6(
120132
pureFn: (v1: any, v2: any, v3: any, v4: any, v5: any, v6: any) => any, exp1: any, exp2: any,
121-
exp3: any, exp4: any, exp5: any, exp6: any): any {
133+
exp3: any, exp4: any, exp5: any, exp6: any, thisArg?: any): any {
122134
const different = bindingUpdated4(exp1, exp2, exp3, exp4);
123135
return bindingUpdated2(exp5, exp6) || different ?
124-
checkAndUpdateBinding(pureFn(exp1, exp2, exp3, exp4, exp5, exp6)) :
136+
checkAndUpdateBinding(
137+
thisArg ? pureFn.call(thisArg, exp1, exp2, exp3, exp4, exp5, exp6) :
138+
pureFn(exp1, exp2, exp3, exp4, exp5, exp6)) :
125139
consumeBinding();
126140
}
127141

@@ -141,11 +155,13 @@ export function pureFunction6(
141155
*/
142156
export function pureFunction7(
143157
pureFn: (v1: any, v2: any, v3: any, v4: any, v5: any, v6: any, v7: any) => any, exp1: any,
144-
exp2: any, exp3: any, exp4: any, exp5: any, exp6: any, exp7: any): any {
158+
exp2: any, exp3: any, exp4: any, exp5: any, exp6: any, exp7: any, thisArg?: any): any {
145159
let different = bindingUpdated4(exp1, exp2, exp3, exp4);
146160
different = bindingUpdated2(exp5, exp6) || different;
147161
return bindingUpdated(exp7) || different ?
148-
checkAndUpdateBinding(pureFn(exp1, exp2, exp3, exp4, exp5, exp6, exp7)) :
162+
checkAndUpdateBinding(
163+
thisArg ? pureFn.call(thisArg, exp1, exp2, exp3, exp4, exp5, exp6, exp7) :
164+
pureFn(exp1, exp2, exp3, exp4, exp5, exp6, exp7)) :
149165
consumeBinding();
150166
}
151167

@@ -166,10 +182,13 @@ export function pureFunction7(
166182
*/
167183
export function pureFunction8(
168184
pureFn: (v1: any, v2: any, v3: any, v4: any, v5: any, v6: any, v7: any, v8: any) => any,
169-
exp1: any, exp2: any, exp3: any, exp4: any, exp5: any, exp6: any, exp7: any, exp8: any): any {
185+
exp1: any, exp2: any, exp3: any, exp4: any, exp5: any, exp6: any, exp7: any, exp8: any,
186+
thisArg?: any): any {
170187
const different = bindingUpdated4(exp1, exp2, exp3, exp4);
171188
return bindingUpdated4(exp5, exp6, exp7, exp8) || different ?
172-
checkAndUpdateBinding(pureFn(exp1, exp2, exp3, exp4, exp5, exp6, exp7, exp8)) :
189+
checkAndUpdateBinding(
190+
thisArg ? pureFn.call(thisArg, exp1, exp2, exp3, exp4, exp5, exp6, exp7, exp8) :
191+
pureFn(exp1, exp2, exp3, exp4, exp5, exp6, exp7, exp8)) :
173192
consumeBinding();
174193
}
175194

@@ -184,11 +203,11 @@ export function pureFunction8(
184203
* @param exp An array of binding values
185204
* @returns Updated value
186205
*/
187-
export function pureFunctionV(pureFn: (...v: any[]) => any, exps: any[]): any {
206+
export function pureFunctionV(pureFn: (...v: any[]) => any, exps: any[], thisArg?: any): any {
188207
let different = false;
189208

190209
for (let i = 0; i < exps.length; i++) {
191210
bindingUpdated(exps[i]) && (different = true);
192211
}
193-
return different ? checkAndUpdateBinding(pureFn.apply(null, exps)) : consumeBinding();
212+
return different ? checkAndUpdateBinding(pureFn.apply(thisArg, exps)) : consumeBinding();
194213
}

0 commit comments

Comments
 (0)