Skip to content

Commit 963ef83

Browse files
committed
fix(elements): Optimize handling for event proxies. Set poper initial depth. Limit array depth and parsing.
1 parent 042912b commit 963ef83

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

projects/igniteui-angular-elements/src/app/custom-strategy.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -422,38 +422,56 @@ class IgxCustomNgElementStrategy extends ComponentNgElementStrategy {
422422
}
423423

424424
protected patchOutputComponents(eventArgs: any) {
425-
return this.createProxyForComponentValue(eventArgs, 0);
425+
return this.createProxyForComponentValue(eventArgs, 1).value;
426426
}
427427

428428
/**
429429
* Nested search of event args that contain angular components and replace them with proxies.
430430
* If event args are array of angular component instances should return array of proxies of each of those instances.
431431
* If event args are object that has a single property being angular component should return same object except the angular component being a proxy of itself.
432432
*/
433-
protected createProxyForComponentValue(value: any, depth: number) {
433+
protected createProxyForComponentValue(value: any, depth: number): { value: any, hasProxies: boolean } {
434434
if (depth > this.maxEventProxyDepth) {
435-
return value;
435+
return { value, hasProxies: false };
436436
}
437437

438+
let hasProxies = false;
438439
// TO DO!: Not very reliable as it is a very internal API and could be subject to change. If something comes up, should be changed.
439440
if (value?.__ngContext__) {
440441
const componentConfig = this.config.find((info: ComponentConfig) => value.constructor === info.component);
441442
if (componentConfig?.templateProps) {
442-
return this.createElementsComponentProxy(value, componentConfig);
443+
return { value: this.createElementsComponentProxy(value, componentConfig), hasProxies: true };
443444
}
444445
} else if (Array.isArray(value)) {
445-
return value.map(item => this.createProxyForComponentValue(item, depth + 1))
446-
} else if (typeof value === "object" && Object.entries(value).length) {
446+
if (!value[0]) {
447+
return { value, hasProxies: false };
448+
} else {
449+
// For array limit their parsing to first level and check if first item has created proxy inside.
450+
const firstItem = this.createProxyForComponentValue(value[0], this.maxEventProxyDepth);
451+
if (firstItem.hasProxies) {
452+
const mappedArray = value.slice(1, value.length).map(item => this.createProxyForComponentValue(item, depth + 1));
453+
mappedArray.unshift(firstItem);
454+
return { value: mappedArray, hasProxies: true };
455+
}
456+
}
457+
} else if (typeof value === "object" && Object.entries(value).length && !(value instanceof Event)) {
447458
for (const [key, item] of Object.entries(value)) {
448-
value[key] = this.createProxyForComponentValue(item, depth + 1);
459+
if (!item) {
460+
value[key] = item;
461+
} else {
462+
const parsedItem = this.createProxyForComponentValue(item, depth + 1);
463+
value[key] = parsedItem.value;
464+
hasProxies = parsedItem.hasProxies || hasProxies;
465+
}
449466
}
450467
}
451468

452-
return value;
469+
return { value, hasProxies };
453470
}
454471

455472
/** Create proxy for a component that handles setting template props, making sure it provides correct TemplateRef and not Lit template */
456473
protected createElementsComponentProxy(component: any, config: ComponentConfig) {
474+
console.log("createProxy");
457475
const parentThis = this;
458476
return new Proxy(component, {
459477
set(target: any, prop: string, newValue: any) {

0 commit comments

Comments
 (0)