Skip to content

Commit

Permalink
fix: props handler and benchmark performance
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenybai committed Jun 15, 2021
1 parent 5a74d07 commit 65afb89
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
13 changes: 6 additions & 7 deletions src/__test__/patch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,14 @@ describe('.patch', () => {

patchProps(
<HTMLElement>el,
{ title: 'bar', id: 'app', lang: 'pt', spellcheck: false },
{ title: 'foo', id: '', lang: '', hidden: true },
{ title: 'bar', id: 'app' },
{ title: 'foo', id: 'app1', lang: 'pt', hidden: true },
);

expect(el.id).toEqual('app'); // keeped
expect(el.lang).toEqual('pt'); // keeped
expect(el.title).toEqual('foo'); // updated
expect(el.hidden).toEqual(true); // setted
expect(el.spellcheck).toBeFalsy(); // removed
expect(el.id).toEqual('app1');
expect(el.lang).toEqual('pt');
expect(el.title).toEqual('foo');
expect(el.hidden).toEqual(true);
});

it('should patch children', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { OLD_VNODE_FIELD } from './constants';
import { createElement } from './createElement';
import { className, m, style, svg } from './m';
import { patch, patchChildren, patchProps } from './patch';
import { VElement, VFlags, VNode, VProps } from './structs';
import type { VElement, VFlags, VNode, VProps } from './structs';

export {
createElement,
Expand Down
20 changes: 10 additions & 10 deletions src/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export const patchProps = (el: HTMLElement, oldProps: VProps = {}, newProps: VPr
const oldPropKeys = Object.keys(oldProps);
const newPropKeys = Object.keys(newProps);

if ((newProps && !oldProps) || oldPropKeys.length > newPropKeys.length) {
if (oldPropKeys.length > newPropKeys.length) {
// Deletion has occured
[...oldPropKeys].forEach((propName) => {
for (const propName of oldPropKeys) {
const newPropValue = newProps[propName];
/* istanbul ignore if */
if (newPropValue) {
Expand All @@ -24,17 +24,17 @@ export const patchProps = (el: HTMLElement, oldProps: VProps = {}, newProps: VPr
}
delete el[propName];
el.removeAttribute(propName);
});
}
} else {
// Addition/No change/Content modification has occured
[...newPropKeys].forEach((propName) => {
for (const propName of newPropKeys) {
const oldPropValue = oldProps[propName];
if (oldPropValue && !newProps[propName]) {
el[propName] = oldPropValue;
return;
}
el[propName] = newProps[propName];
});
const newPropValue = newProps[propName];

if (oldPropValue) {
if (oldPropValue !== newPropValue) el[propName] = newPropValue;
} else el[propName] = newPropValue;
}
}
};

Expand Down

0 comments on commit 65afb89

Please sign in to comment.