Skip to content

Commit

Permalink
perf: rework defining of get/set props for change detection 2
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Dec 11, 2021
1 parent 7a5c60c commit f609d48
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
26 changes: 25 additions & 1 deletion packages/core/src/entity/EntityHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class EntityHelper {
*/
private static defineProperties<T extends AnyEntity<T>>(meta: EntityMetadata<T>): void {
Object
.values<EntityProperty>(meta.properties)
.values(meta.properties)
.filter(prop => [ReferenceType.ONE_TO_ONE, ReferenceType.MANY_TO_ONE].includes(prop.reference) && (prop.inversedBy || prop.mappedBy) && !prop.mapToPk)
.forEach(prop => {
Object.defineProperty(meta.prototype, prop.name, {
Expand All @@ -100,6 +100,30 @@ export class EntityHelper {
});
});

Object
.values(meta.properties)
.filter(prop => !(prop.inherited || prop.primary || prop.persist === false))
.filter(prop => !([ReferenceType.ONE_TO_ONE, ReferenceType.MANY_TO_ONE].includes(prop.reference) && (prop.inversedBy || prop.mappedBy) && !prop.mapToPk))
.forEach(prop => {
Object.defineProperty(meta.prototype, prop.name, {
set(val) {
Object.defineProperty(this, prop.name, {
get() {
return this.__helper.__data[prop.name];
},
set(val) {
this.__helper.__data[prop.name] = val;
this.__helper.__touched = true;
},
enumerable: true,
configurable: true,
});
this.__helper.__data[prop.name] = val;
this.__helper.__touched = true;
},
});
});

/* istanbul ignore else */
if (!meta.prototype[inspect.custom]) {
meta.prototype[inspect.custom] = function (depth: number) {
Expand Down
5 changes: 0 additions & 5 deletions packages/core/src/unit-of-work/UnitOfWork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ export class UnitOfWork {
Object.keys(data).forEach(key => entity.__helper!.__loadedProperties.add(key));
this.queuedActions.delete(helper.__meta.className);
helper.__touched = false;

if (!(entity as Dictionary).__gettersDefined) {
helper.__data = { ...entity };
Object.defineProperties(entity, helper.__meta.definedProperties);
}
}

return entity;
Expand Down
5 changes: 2 additions & 3 deletions tests/EntityHelper.mongo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ describe('EntityHelperMongo', () => {
test('BaseEntity methods', async () => {
const god = new Author('God', 'hello@heaven.god');
expect(wrap(god, true).__populated).toBeUndefined();
// not managed entities do not track changes (before being flushed)
expect(wrap(god, true).__touched).toBe(false);
expect(god.isTouched()).toBe(false);
expect(wrap(god, true).__touched).toBe(true);
expect(god.isTouched()).toBe(true);
god.populated();
expect(wrap(god, true).__populated).toBe(true);
expect(wrap(god, true).__platform).toBe(orm.em.getDriver().getPlatform());
Expand Down
2 changes: 1 addition & 1 deletion tests/features/auto-flush.postgre.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('automatic flushing when querying for overlapping entities via em.find/
god.favouriteAuthor.age = 21;
god.age = 999;

expect(wrap(god, true).__touched).toBe(false);
expect(wrap(god, true).__touched).toBe(true);
await orm.em.persistAndFlush(god);
expect(wrap(god, true).__touched).toBe(false);

Expand Down

0 comments on commit f609d48

Please sign in to comment.