2022-12-27 15:37:40 +01:00
|
|
|
import { downcast, identity, isSameTypeRefByAttr, noOp, remove, TypeRef } from "@tutao/tutanota-utils"
|
|
|
|
|
import type { LoginController } from "./LoginController"
|
|
|
|
|
import type { OperationType } from "../common/TutanotaConstants"
|
2022-01-07 15:58:30 +01:00
|
|
|
import stream from "mithril/stream"
|
2022-08-16 13:00:12 +02:00
|
|
|
import Stream from "mithril/stream"
|
2022-12-27 15:37:40 +01:00
|
|
|
import { assertMainOrNode } from "../common/Env"
|
|
|
|
|
import { EntityUpdate, WebsocketCounterData } from "../entities/sys/TypeRefs"
|
|
|
|
|
import { SomeEntity } from "../common/EntityTypes.js"
|
|
|
|
|
import { isSameId } from "../common/utils/EntityUtils.js"
|
2022-01-07 15:58:30 +01:00
|
|
|
|
2017-08-15 13:54:22 +02:00
|
|
|
assertMainOrNode()
|
2018-10-19 18:13:58 +02:00
|
|
|
export type EntityUpdateData = {
|
2022-01-07 15:58:30 +01:00
|
|
|
application: string
|
|
|
|
|
type: string
|
|
|
|
|
instanceListId: string
|
|
|
|
|
instanceId: string
|
|
|
|
|
operation: OperationType
|
2018-10-19 18:13:58 +02:00
|
|
|
}
|
2021-12-23 14:03:23 +01:00
|
|
|
export type EntityEventsListener = (updates: ReadonlyArray<EntityUpdateData>, eventOwnerGroupId: Id) => Promise<any>
|
2018-10-24 16:39:21 +02:00
|
|
|
export const isUpdateForTypeRef = <T>(typeRef: TypeRef<T>, update: EntityUpdateData): boolean => isSameTypeRefByAttr(typeRef, update.application, update.type)
|
2022-11-08 17:06:42 +01:00
|
|
|
export function isUpdateFor<T extends SomeEntity>(entity: T, update: EntityUpdateData): boolean {
|
|
|
|
|
const typeRef = entity._type as TypeRef<T>
|
2022-12-27 15:37:40 +01:00
|
|
|
return (
|
|
|
|
|
isUpdateForTypeRef(typeRef, update) &&
|
|
|
|
|
(update.instanceListId === "" ? isSameId(update.instanceId, entity._id) : isSameId([update.instanceListId, update.instanceId], entity._id))
|
|
|
|
|
)
|
2022-11-08 17:06:42 +01:00
|
|
|
}
|
2022-01-07 15:58:30 +01:00
|
|
|
|
2023-01-02 17:39:05 +01:00
|
|
|
export type ExposedEventController = Pick<EventController, "onEntityUpdateReceived" | "onCountersUpdateReceived">
|
2022-12-28 15:28:28 +01:00
|
|
|
|
2019-01-21 10:48:07 +01:00
|
|
|
export class EventController {
|
2022-08-16 13:00:12 +02:00
|
|
|
private countersStream: Stream<WebsocketCounterData> = stream()
|
|
|
|
|
private entityListeners: Array<EntityEventsListener> = []
|
2017-08-15 13:54:22 +02:00
|
|
|
|
2022-12-27 15:37:40 +01:00
|
|
|
constructor(private readonly logins: LoginController) {}
|
2017-08-15 13:54:22 +02:00
|
|
|
|
2022-01-07 15:58:30 +01:00
|
|
|
addEntityListener(listener: EntityEventsListener) {
|
2022-08-16 13:00:12 +02:00
|
|
|
this.entityListeners.push(listener)
|
2022-01-07 15:58:30 +01:00
|
|
|
}
|
2019-01-21 10:48:07 +01:00
|
|
|
|
2022-01-07 15:58:30 +01:00
|
|
|
removeEntityListener(listener: EntityEventsListener) {
|
2022-08-16 13:00:12 +02:00
|
|
|
remove(this.entityListeners, listener)
|
2022-01-07 15:58:30 +01:00
|
|
|
}
|
2017-08-15 13:54:22 +02:00
|
|
|
|
2022-08-16 13:00:12 +02:00
|
|
|
getCountersStream(): Stream<WebsocketCounterData> {
|
2022-01-07 15:58:30 +01:00
|
|
|
// Create copy so it's never ended
|
2022-08-16 13:00:12 +02:00
|
|
|
return this.countersStream.map(identity)
|
2022-01-07 15:58:30 +01:00
|
|
|
}
|
2017-08-15 13:54:22 +02:00
|
|
|
|
2022-12-28 15:28:28 +01:00
|
|
|
async onEntityUpdateReceived(entityUpdates: ReadonlyArray<EntityUpdate>, eventOwnerGroupId: Id): Promise<void> {
|
2022-01-07 15:58:30 +01:00
|
|
|
let loginsUpdates = Promise.resolve()
|
2018-10-19 18:13:58 +02:00
|
|
|
|
2022-08-16 13:00:12 +02:00
|
|
|
if (this.logins.isUserLoggedIn()) {
|
2022-01-07 15:58:30 +01:00
|
|
|
// the UserController must be notified first as other event receivers depend on it to be up-to-date
|
2022-08-16 13:00:12 +02:00
|
|
|
loginsUpdates = this.logins.getUserController().entityEventsReceived(entityUpdates as ReadonlyArray<EntityUpdateData>, eventOwnerGroupId)
|
2022-01-07 15:58:30 +01:00
|
|
|
}
|
2019-01-21 10:48:07 +01:00
|
|
|
|
2022-01-07 15:58:30 +01:00
|
|
|
return loginsUpdates
|
|
|
|
|
.then(async () => {
|
|
|
|
|
// sequentially to prevent parallel loading of instances
|
2022-08-16 13:00:12 +02:00
|
|
|
for (const listener of this.entityListeners) {
|
2022-01-07 15:58:30 +01:00
|
|
|
let entityUpdatesData: Array<EntityUpdateData> = downcast(entityUpdates)
|
|
|
|
|
await listener(entityUpdatesData, eventOwnerGroupId)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.then(noOp)
|
|
|
|
|
}
|
2021-12-23 14:03:23 +01:00
|
|
|
|
2023-01-02 17:39:05 +01:00
|
|
|
async onCountersUpdateReceived(update: WebsocketCounterData): Promise<void> {
|
2022-08-16 13:00:12 +02:00
|
|
|
this.countersStream(update)
|
2022-01-07 15:58:30 +01:00
|
|
|
}
|
2022-12-27 15:37:40 +01:00
|
|
|
}
|