Skip to content

Commit 3288d28

Browse files
committed
fix: switch to conditionally constructed from conditionally executed
1 parent 8cf03d3 commit 3288d28

File tree

2 files changed

+31
-52
lines changed

2 files changed

+31
-52
lines changed

packages/sds/src/message_channel/message_channel.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ export interface MessageChannelOptions {
5353
*/
5454
possibleAcksThreshold?: number;
5555
/**
56-
* SDS-R repair configuration. If not provided, repair is enabled with default settings.
56+
* Whether to enable SDS-R repair protocol.
57+
* @default true
58+
*/
59+
enableRepair?: boolean;
60+
/**
61+
* SDS-R repair configuration. Only used if enableRepair is true.
5762
*/
5863
repairConfig?: RepairConfig;
5964
}
@@ -76,7 +81,7 @@ export class MessageChannel extends TypedEventEmitter<MessageChannelEvents> {
7681
private readonly causalHistorySize: number;
7782
private readonly possibleAcksThreshold: number;
7883
private readonly timeoutForLostMessagesMs?: number;
79-
private readonly repairManager: RepairManager;
84+
private readonly repairManager?: RepairManager;
8085

8186
private tasks: Task[] = [];
8287
private handlers: Handlers = {
@@ -120,13 +125,17 @@ export class MessageChannel extends TypedEventEmitter<MessageChannelEvents> {
120125
options.possibleAcksThreshold ?? DEFAULT_POSSIBLE_ACKS_THRESHOLD;
121126
this.timeReceived = new Map();
122127
this.timeoutForLostMessagesMs = options.timeoutForLostMessagesMs;
123-
this.repairManager = new RepairManager(
124-
senderId,
125-
options.repairConfig,
126-
(event: string, detail: unknown) => {
127-
this.safeSendEvent(event as MessageChannelEvent, { detail });
128-
}
129-
);
128+
129+
// Only construct RepairManager if repair is enabled (default: true)
130+
if (options.enableRepair ?? true) {
131+
this.repairManager = new RepairManager(
132+
senderId,
133+
options.repairConfig,
134+
(event: string, detail: unknown) => {
135+
this.safeSendEvent(event as MessageChannelEvent, { detail });
136+
}
137+
);
138+
}
130139
}
131140

132141
public static getMessageId(payload: Uint8Array): MessageId {
@@ -381,9 +390,8 @@ export class MessageChannel extends TypedEventEmitter<MessageChannelEvents> {
381390
public async sweepRepairIncomingBuffer(
382391
callback?: (message: Message) => Promise<boolean>
383392
): Promise<Message[]> {
384-
const repairsToSend = this.repairManager.sweepIncomingBuffer(
385-
this.localHistory
386-
);
393+
const repairsToSend =
394+
this.repairManager?.sweepIncomingBuffer(this.localHistory) ?? [];
387395

388396
if (callback) {
389397
for (const message of repairsToSend) {
@@ -426,9 +434,9 @@ export class MessageChannel extends TypedEventEmitter<MessageChannelEvents> {
426434
this.lamportTimestamp = lamportTimestampIncrement(this.lamportTimestamp);
427435

428436
// Get repair requests to include in sync message (SDS-R)
429-
const repairRequests = this.repairManager.getRepairRequests(
430-
MAX_REPAIR_REQUESTS_PER_MESSAGE
431-
);
437+
const repairRequests =
438+
this.repairManager?.getRepairRequests(MAX_REPAIR_REQUESTS_PER_MESSAGE) ??
439+
[];
432440

433441
const message = new SyncMessage(
434442
// does not need to be secure randomness
@@ -542,7 +550,7 @@ export class MessageChannel extends TypedEventEmitter<MessageChannelEvents> {
542550
}
543551

544552
// SDS-R: Handle received message in repair manager
545-
this.repairManager.markMessageReceived(message.messageId);
553+
this.repairManager?.markMessageReceived(message.messageId);
546554

547555
// SDS-R: Process incoming repair requests
548556
if (message.repairRequest && message.repairRequest.length > 0) {
@@ -554,7 +562,7 @@ export class MessageChannel extends TypedEventEmitter<MessageChannelEvents> {
554562
}
555563
});
556564

557-
this.repairManager.processIncomingRepairRequests(
565+
this.repairManager?.processIncomingRepairRequests(
558566
message.repairRequest,
559567
this.localHistory
560568
);
@@ -582,7 +590,7 @@ export class MessageChannel extends TypedEventEmitter<MessageChannelEvents> {
582590
);
583591

584592
// SDS-R: Track missing dependencies in repair manager
585-
this.repairManager.markDependenciesMissing(missingDependencies);
593+
this.repairManager?.markDependenciesMissing(missingDependencies);
586594

587595
this.safeSendEvent(MessageChannelEvent.InMessageMissing, {
588596
detail: Array.from(missingDependencies)
@@ -648,9 +656,10 @@ export class MessageChannel extends TypedEventEmitter<MessageChannelEvents> {
648656
log.info(this.senderId, "sending new message", messageId);
649657

650658
// Get repair requests to include in the message (SDS-R)
651-
const repairRequests = this.repairManager.getRepairRequests(
652-
MAX_REPAIR_REQUESTS_PER_MESSAGE
653-
);
659+
const repairRequests =
660+
this.repairManager?.getRepairRequests(
661+
MAX_REPAIR_REQUESTS_PER_MESSAGE
662+
) ?? [];
654663

655664
message = new ContentMessage(
656665
messageId,

packages/sds/src/message_channel/repair/repair.ts

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ export interface RepairConfig {
3737
numResponseGroups?: number;
3838
/** Maximum buffer size for repair requests */
3939
bufferSize?: number;
40-
/** Whether repair is enabled */
41-
enabled?: boolean;
4240
}
4341

4442
/**
@@ -48,8 +46,7 @@ export const DEFAULT_REPAIR_CONFIG: Required<RepairConfig> = {
4846
tMin: 30000, // 30 seconds
4947
tMax: 120000, // 120 seconds
5048
numResponseGroups: 1, // Recommendation is 1 group per PARTICIPANTS_PER_RESPONSE_GROUP participants
51-
bufferSize: 1000,
52-
enabled: true
49+
bufferSize: 1000
5350
};
5451

5552
/**
@@ -141,10 +138,6 @@ export class RepairManager {
141138
missingEntries: HistoryEntry[],
142139
currentTime = Date.now()
143140
): void {
144-
if (!this.config.enabled) {
145-
return;
146-
}
147-
148141
for (const entry of missingEntries) {
149142
// Calculate when to request this repair
150143
const tReq = this.calculateTReq(entry.messageId, currentTime);
@@ -198,10 +191,6 @@ export class RepairManager {
198191
maxRequests = 3,
199192
currentTime = Date.now()
200193
): HistoryEntry[] {
201-
if (!this.config.enabled) {
202-
return [];
203-
}
204-
205194
return this.outgoingBuffer.getEligible(currentTime, maxRequests);
206195
}
207196

@@ -214,10 +203,6 @@ export class RepairManager {
214203
localHistory: ILocalHistory,
215204
currentTime = Date.now()
216205
): void {
217-
if (!this.config.enabled) {
218-
return;
219-
}
220-
221206
for (const request of requests) {
222207
// Remove from our own outgoing buffer (someone else is requesting it)
223208
this.outgoingBuffer.remove(request.messageId);
@@ -278,10 +263,6 @@ export class RepairManager {
278263
maxRequests = 3,
279264
currentTime = Date.now()
280265
): HistoryEntry[] {
281-
if (!this.config.enabled) {
282-
return [];
283-
}
284-
285266
return this.getRepairRequests(maxRequests, currentTime);
286267
}
287268

@@ -293,10 +274,6 @@ export class RepairManager {
293274
localHistory: ILocalHistory,
294275
currentTime = Date.now()
295276
): Message[] {
296-
if (!this.config.enabled) {
297-
return [];
298-
}
299-
300277
const ready = this.incomingBuffer.getReady(currentTime);
301278
const messages: Message[] = [];
302279

@@ -321,13 +298,6 @@ export class RepairManager {
321298
this.incomingBuffer.clear();
322299
}
323300

324-
/**
325-
* Check if repair is enabled
326-
*/
327-
public get isEnabled(): boolean {
328-
return this.config.enabled;
329-
}
330-
331301
/**
332302
* Update number of response groups (e.g., when participants change)
333303
*/

0 commit comments

Comments
 (0)