Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions packages/sds/src/message_channel/message_channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export class MessageChannel extends TypedEventEmitter<MessageChannelEvents> {
private possibleAcks: Map<MessageId, number>;
private incomingBuffer: Array<ContentMessage | SyncMessage>;
private readonly localHistory: ILocalHistory;
private localHistoryIndex: Set<MessageId>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed on Discord, move this to behind ILocalHistory interface, as an implement detail/optimisation.

ILocalHistory being an Array with a subset of function was an easy way to start setting a boundary.

You can move away from that and start expecting ILocalHistory to implement a findMissingDependencies function, that does what you want to do in an efficient manner.

private timeReceived: Map<MessageId, number>;
private readonly causalHistorySize: number;
private readonly possibleAcksThreshold: number;
Expand Down Expand Up @@ -102,6 +103,9 @@ export class MessageChannel extends TypedEventEmitter<MessageChannelEvents> {
this.possibleAcks = new Map();
this.incomingBuffer = [];
this.localHistory = localHistory;
this.localHistoryIndex = new Set(
localHistory.slice(0).map((msg) => msg.messageId)
);
this.causalHistorySize =
options.causalHistorySize ?? DEFAULT_CAUSAL_HISTORY_SIZE;
// TODO: this should be determined based on the bloom filter parameters and number of hashes
Expand Down Expand Up @@ -272,9 +276,7 @@ export class MessageChannel extends TypedEventEmitter<MessageChannelEvents> {
);
const missingDependencies = message.causalHistory.filter(
(messageHistoryEntry) =>
!this.localHistory.some(
({ messageId }) => messageId === messageHistoryEntry.messageId
)
!this.localHistoryIndex.has(messageHistoryEntry.messageId)
);
if (missingDependencies.length === 0) {
if (isContentMessage(message) && this.deliverMessage(message)) {
Expand Down Expand Up @@ -471,9 +473,7 @@ export class MessageChannel extends TypedEventEmitter<MessageChannelEvents> {

const missingDependencies = message.causalHistory.filter(
(messageHistoryEntry) =>
!this.localHistory.some(
({ messageId }) => messageId === messageHistoryEntry.messageId
)
!this.localHistoryIndex.has(messageHistoryEntry.messageId)
);

if (missingDependencies.length > 0) {
Expand Down Expand Up @@ -577,6 +577,7 @@ export class MessageChannel extends TypedEventEmitter<MessageChannelEvents> {
message.retrievalHint = retrievalHint;
this.filter.insert(messageId);
this.localHistory.push(message);
this.localHistoryIndex.add(messageId);
this.timeReceived.set(messageId, Date.now());
this.safeSendEvent(MessageChannelEvent.OutMessageSent, {
detail: message
Expand Down Expand Up @@ -657,6 +658,7 @@ export class MessageChannel extends TypedEventEmitter<MessageChannelEvents> {
}

this.localHistory.push(message);
this.localHistoryIndex.add(message.messageId);
return true;
}

Expand Down
Loading