Skip to content

Commit

Permalink
fix(entities): prevent dates from being proxied
Browse files Browse the repository at this point in the history
Applying proxies to date objects causes trouble and gives us no value
atm anyway.
  • Loading branch information
mKeRix committed Feb 28, 2021
1 parent 5dfc4a8 commit d861486
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/entities/entities.service.spec.ts
Expand Up @@ -56,6 +56,24 @@ describe('EntitiesService', () => {
expect(util.types.isProxy(returnedEntity)).toBeTruthy();
});

it("should proxify object properties", () => {
const entity = new Sensor('example', 'Example Sensor');
const returnedEntity = service.add(entity);

returnedEntity.attributes.object = {};

expect(util.types.isProxy(returnedEntity.attributes.object)).toBeTruthy();
});

it("should not proxify Date properties", () => {
const entity = new Sensor('example', 'Example Sensor');
const returnedEntity = service.add(entity);

returnedEntity.attributes.date = new Date();

expect(util.types.isProxy(returnedEntity.attributes.date)).toBeFalsy();
});

it('should return all registered entities', () => {
const entities = [];
entities.push(service.add(new Sensor('sensor', 'Test')));
Expand Down
4 changes: 2 additions & 2 deletions src/entities/entity.proxy.ts
Expand Up @@ -6,7 +6,7 @@ export class EntityProxyHandler implements ProxyHandler<Entity> {
}

get(target: Entity, p: PropertyKey): any {
if (typeof target[p] === 'object' && target[p] !== null) {
if (typeof target[p] === 'object' && target[p] !== null && !(target[p] instanceof Date)) {
return new Proxy(target[p], new EntityPropertyProxyHandler(`/${p.toString()}`, (diff) => this.emitEntityUpdate(target, diff)));
} else {
return target[p]
Expand Down Expand Up @@ -40,7 +40,7 @@ class EntityPropertyProxyHandler<T extends object> implements ProxyHandler<T> {
}

get(target: T, p: PropertyKey): any {
if (typeof target[p] === 'object' && target[p] !== null) {
if (typeof target[p] === 'object' && target[p] !== null && !(target[p] instanceof Date)) {
return new Proxy(target[p], new EntityPropertyProxyHandler(`${this.path}/${p.toString()}`, this.emitterFunc));
} else {
return target[p]
Expand Down

0 comments on commit d861486

Please sign in to comment.