Skip to content

Commit

Permalink
fix: Only emit updates for distributed entities on leader again
Browse files Browse the repository at this point in the history
The change in state/attributes update mechanisms removed this check by
accident, it has now been re-introduced.
  • Loading branch information
mKeRix committed Jan 20, 2020
1 parent 6c5efed commit 6c5fd43
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
11 changes: 6 additions & 5 deletions src/entities/attributes.proxy.ts
@@ -1,12 +1,13 @@
import _ from 'lodash';
import { EntitiesEventEmitter } from './entities.events';
import _ from "lodash";
import { EntitiesEventEmitter } from "./entities.events";

export class AttributesProxyHandler
implements ProxyHandler<{ [key: string]: string | number | boolean }> {
constructor(
private readonly entityId: string,
private readonly distributed: boolean,
private readonly emitter: EntitiesEventEmitter
private readonly emitter: EntitiesEventEmitter,
private readonly isLeader: () => boolean
) {}

set(
Expand All @@ -18,9 +19,9 @@ export class AttributesProxyHandler
const oldValue = target[p as string];
target[p as string] = value;

if (!_.isEqual(value, oldValue)) {
if (!_.isEqual(value, oldValue) && (!this.distributed || this.isLeader())) {
this.emitter.emit(
'attributesUpdate',
"attributesUpdate",
this.entityId,
target,
this.distributed
Expand Down
18 changes: 10 additions & 8 deletions src/entities/entities.service.ts
@@ -1,15 +1,17 @@
import { Injectable } from '@nestjs/common';
import { Entity } from './entity.entity';
import { EntityProxyHandler } from './entity.proxy';
import { InjectEventEmitter } from 'nest-emitter';
import { EntitiesEventEmitter } from './entities.events';
import { EntityCustomization } from './entity-customization.interface';
import { Injectable } from "@nestjs/common";
import { Entity } from "./entity.entity";
import { EntityProxyHandler } from "./entity.proxy";
import { InjectEventEmitter } from "nest-emitter";
import { EntitiesEventEmitter } from "./entities.events";
import { EntityCustomization } from "./entity-customization.interface";
import { ClusterService } from "../cluster/cluster.service";

@Injectable()
export class EntitiesService {
private readonly entities: Map<string, Entity> = new Map<string, Entity>();

constructor(
private readonly clusterService: ClusterService,
@InjectEventEmitter() private readonly emitter: EntitiesEventEmitter
) {}

Expand All @@ -31,10 +33,10 @@ export class EntitiesService {

const proxy = new Proxy<Entity>(
entity,
new EntityProxyHandler(this.emitter)
new EntityProxyHandler(this.emitter, this.clusterService.isLeader)
);
this.entities.set(entity.id, proxy);
this.emitter.emit('newEntity', proxy, customizations);
this.emitter.emit("newEntity", proxy, customizations);
return proxy;
}
}
28 changes: 20 additions & 8 deletions src/entities/entity.proxy.ts
@@ -1,15 +1,23 @@
import { Entity } from './entity.entity';
import { AttributesProxyHandler } from './attributes.proxy';
import { EntitiesEventEmitter } from './entities.events';
import { Entity } from "./entity.entity";
import { AttributesProxyHandler } from "./attributes.proxy";
import { EntitiesEventEmitter } from "./entities.events";

export class EntityProxyHandler implements ProxyHandler<Entity> {
constructor(private readonly emitter: EntitiesEventEmitter) {}
constructor(
private readonly emitter: EntitiesEventEmitter,
private readonly isLeader: () => boolean
) {}

get(target: Entity, p: string | number | symbol, receiver: any): any {
if (p === 'attributes') {
if (p === "attributes") {
return new Proxy(
target[p],
new AttributesProxyHandler(target.id, target.distributed, this.emitter)
new AttributesProxyHandler(
target.id,
target.distributed,
this.emitter,
this.isLeader
)
);
} else {
return target[p];
Expand All @@ -25,8 +33,12 @@ export class EntityProxyHandler implements ProxyHandler<Entity> {
const oldValue = target[p];
target[p] = value;

if (p === 'state' && oldValue !== value) {
this.emitter.emit('stateUpdate', target.id, value, target.distributed);
if (
p === "state" &&
oldValue !== value &&
(!target.distributed || this.isLeader())
) {
this.emitter.emit("stateUpdate", target.id, value, target.distributed);
}

return true;
Expand Down

0 comments on commit 6c5fd43

Please sign in to comment.