2017-08-15 13:54:22 +02:00
|
|
|
// @flow
|
|
|
|
|
import {remove} from "../common/utils/ArrayUtils"
|
|
|
|
|
import {TypeRef} from "../common/EntityFunctions"
|
|
|
|
|
import {assertMainOrNode} from "../Env"
|
2017-12-05 18:41:38 +01:00
|
|
|
import type {OperationTypeEnum} from "../common/TutanotaConstants"
|
|
|
|
|
import type {LoginController} from "./LoginController"
|
2017-08-15 13:54:22 +02:00
|
|
|
|
|
|
|
|
assertMainOrNode()
|
|
|
|
|
|
|
|
|
|
export class EntityEventController {
|
|
|
|
|
|
|
|
|
|
_listeners: Array<EntityEventReceived>;
|
|
|
|
|
|
2017-12-05 18:41:38 +01:00
|
|
|
constructor(logins: LoginController) {
|
|
|
|
|
// the UserController must be notified first as other event receivers depend on it to be up-to-date
|
|
|
|
|
this._listeners = [(typeRef: TypeRef<any>, listId: ?string, elementId: string, operation: OperationTypeEnum) => {
|
|
|
|
|
if (logins.isUserLoggedIn()) {
|
|
|
|
|
logins.getUserController().entityEventReceived(typeRef, listId, elementId, operation)
|
|
|
|
|
}
|
|
|
|
|
}]
|
2017-08-15 13:54:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addListener(listener: EntityEventReceived) {
|
|
|
|
|
this._listeners.push(listener)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
removeListener(listener: EntityEventReceived) {
|
|
|
|
|
remove(this._listeners, listener)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notificationReceived(entityUpdate: EntityUpdate) {
|
|
|
|
|
let typeRef = new TypeRef(entityUpdate.application, entityUpdate.type)
|
|
|
|
|
this._listeners.forEach(listener => {
|
|
|
|
|
listener(typeRef, entityUpdate.instanceListId, entityUpdate.instanceId, entityUpdate.operation);
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|