UNPKG

@towns-protocol/sdk

Version:

For more details, visit the following resources:

86 lines 3.77 kB
import { StreamStateView_AbstractContent } from './streamStateView_AbstractContent'; import { StreamStateView_ChannelMetadata } from './streamStateView_ChannelMetadata'; import { check } from '@towns-protocol/dlog'; import { logNever } from './check'; export class StreamStateView_GDMChannel extends StreamStateView_AbstractContent { gdmStreamsView; streamId; channelMetadata; get lastEventCreatedAtEpochMs() { return this.gdmStreamModel.lastEventCreatedAtEpochMs; } get gdmStreamModel() { return this.gdmStreamsView.get(this.streamId); } constructor(streamId, gdmStreamsView) { super(); this.gdmStreamsView = gdmStreamsView; this.channelMetadata = new StreamStateView_ChannelMetadata(streamId, gdmStreamsView); this.streamId = streamId; } applySnapshot(snapshot, content, cleartexts, encryptionEmitter) { if (content.channelProperties) { this.channelMetadata.applySnapshot(content.channelProperties, cleartexts, encryptionEmitter); } } prependEvent(event, cleartext, encryptionEmitter, _stateEmitter) { check(event.remoteEvent.event.payload.case === 'gdmChannelPayload'); const payload = event.remoteEvent.event.payload.value; switch (payload.content.case) { case 'inception': this.updateLastEvent(event.remoteEvent, undefined); break; case 'message': this.decryptEvent('channelMessage', event, payload.content.value, cleartext, encryptionEmitter); this.updateLastEvent(event.remoteEvent, undefined); break; case 'channelProperties': // nothing to do, conveyed in the snapshot break; case undefined: break; default: logNever(payload.content); } } appendEvent(event, cleartext, encryptionEmitter, stateEmitter) { check(event.remoteEvent.event.payload.case === 'gdmChannelPayload'); const payload = event.remoteEvent.event.payload.value; switch (payload.content.case) { case 'inception': this.updateLastEvent(event.remoteEvent, stateEmitter); break; case 'message': this.decryptEvent('channelMessage', event, payload.content.value, cleartext, encryptionEmitter); this.updateLastEvent(event.remoteEvent, stateEmitter); break; case 'channelProperties': this.channelMetadata.appendEvent(event, cleartext, encryptionEmitter); break; case undefined: break; default: logNever(payload.content); } } onDecryptedContent(eventId, content, emitter) { if (content.kind === 'channelProperties') { this.channelMetadata.onDecryptedContent(eventId, content, emitter); } } onConfirmedEvent(event, emitter, encryptionEmitter) { super.onConfirmedEvent(event, emitter, encryptionEmitter); } onAppendLocalEvent(event, stateEmitter) { this.gdmStreamsView.setLastEventCreatedAtEpochMs(this.streamId, event.createdAtEpochMs); stateEmitter?.emit('streamLatestTimestampUpdated', this.streamId); } updateLastEvent(event, stateEmitter) { const createdAtEpochMs = event.event.createdAtEpochMs; if (createdAtEpochMs > this.lastEventCreatedAtEpochMs) { this.gdmStreamsView.setLastEventCreatedAtEpochMs(this.streamId, createdAtEpochMs); stateEmitter?.emit('streamLatestTimestampUpdated', this.streamId); } } } //# sourceMappingURL=streamStateView_GDMChannel.js.map