Skip to content

Commit e48cf84

Browse files
committed
perf(angular): emit fast proxies
1 parent 274c09e commit e48cf84

1 file changed

Lines changed: 22 additions & 42 deletions

File tree

src/compiler/distribution/dist-angular.ts

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ async function angularDirectiveProxyOutput(config: d.Config, compilerCtx: d.Comp
4343
angularImports.push('Directive');
4444
} else {
4545
angularImports.push('Component');
46-
angularImports.push('ViewEncapsulation');
47-
angularImports.push('ChangeDetectionStrategy');
4846
angularImports.push('ChangeDetectorRef');
4947
}
5048
}
@@ -82,10 +80,12 @@ import { ${angularImports.sort().join(', ')} } from '@angular/core';
8280

8381
function inputsAuxFunction() {
8482
return `
85-
export function proxyInputs(instance: any, el: any, props: string[]) {
86-
props.forEach(propName => {
87-
Object.defineProperty(instance, propName, {
88-
get: () => el[propName], set: (val: any) => el[propName] = val
83+
export function proxyInputs(Cmp: any, inputs: string[]) {
84+
const Prototype = Cmp.prototype;
85+
inputs.forEach(item => {
86+
Object.defineProperty(Prototype, item, {
87+
get() { return this.el[item]; },
88+
set(val: any) { this.el[item] = val; },
8989
});
9090
});
9191
}`;
@@ -102,16 +102,13 @@ export function proxyOutputs(instance: any, el: any, events: string[]) {
102102

103103
function methodsAuxFunction() {
104104
return `
105-
export function proxyMethods(instance: any, el: any, methods: string[]) {
105+
export function proxyMethods(Cmp: any, methods: string[]) {
106+
const Prototype = Cmp.prototype;
106107
methods.forEach(methodName => {
107-
Object.defineProperty(instance, methodName, {
108-
get: function() {
109-
return function() {
110-
const args = arguments;
111-
return el.componentOnReady().then((el: any) => el[methodName].apply(el, args));
112-
};
113-
}
114-
});
108+
Prototype[methodName] = function() {
109+
const args = arguments;
110+
return this.el.componentOnReady().then((el: any) => el[methodName].apply(el, args));
111+
};
115112
});
116113
}
117114
`;
@@ -154,7 +151,6 @@ function generateProxy(cmpMeta: d.ComponentMeta, useDirectives: boolean) {
154151
const hasInputs = inputs.length > 0;
155152
const hasOutputs = outputs.length > 0;
156153
const hasMethods = methods.length > 0;
157-
const hasContructor = hasInputs || hasOutputs || hasMethods;
158154

159155
// Generate Angular @Directive
160156
const decorator = useDirectives ? 'Directive' : 'Component';
@@ -163,8 +159,7 @@ function generateProxy(cmpMeta: d.ComponentMeta, useDirectives: boolean) {
163159
];
164160
if (!useDirectives) {
165161
directiveOpts.push(
166-
`changeDetection: ChangeDetectionStrategy.OnPush`,
167-
`encapsulation: ViewEncapsulation.None`,
162+
`changeDetection: 0`,
168163
`template: '<ng-content></ng-content>'`
169164
);
170165
}
@@ -183,38 +178,23 @@ export class ${tagNameAsPascal} {`];
183178
lines.push(` ${output}!: EventEmitter<CustomEvent>;`);
184179
});
185180

186-
// Generate component constructor
187-
if (hasContructor) {
188-
if (useDirectives) {
189-
lines.push(`
190-
constructor(r: ElementRef) {
191-
const el = r.nativeElement;`);
192-
} else {
193-
lines.push(`
194-
constructor(c: ChangeDetectorRef, r: ElementRef) {
181+
lines.push(' el: HTMLElement');
182+
lines.push(` constructor(c: ChangeDetectorRef, r: ElementRef) {
195183
c.detach();
196-
const el = r.nativeElement;`);
197-
}
184+
this.el = r.nativeElement;`);
185+
if (hasOutputs) {
186+
lines.push(` proxyOutputs(this, this.el, ['${outputs.join(`', '`)}']);`);
198187
}
188+
lines.push(` }`);
189+
lines.push(`}`);
199190

200191
if (hasMethods) {
201-
lines.push(` proxyMethods(this, el, ['${methods.join(`', '`)}']);`);
192+
lines.push(`proxyMethods(${tagNameAsPascal}, ['${methods.join(`', '`)}']);`);
202193
}
203-
204194
if (hasInputs) {
205-
lines.push(` proxyInputs(this, el, ['${inputs.join(`', '`)}']);`);
195+
lines.push(`proxyInputs(${tagNameAsPascal}, ['${inputs.join(`', '`)}']);`);
206196
}
207197

208-
if (hasOutputs) {
209-
lines.push(` proxyOutputs(this, el, ['${outputs.join(`', '`)}']);`);
210-
}
211-
212-
if (hasContructor) {
213-
lines.push(` }`);
214-
}
215-
216-
lines.push(`}`);
217-
218198
return {
219199
text: lines.join('\n'),
220200
hasInputs,

0 commit comments

Comments
 (0)