Skip to content

Commit 434728f

Browse files
chore: address comments, reduce diff
1 parent 3e3c511 commit 434728f

File tree

5 files changed

+17
-42
lines changed

5 files changed

+17
-42
lines changed

packages/sds/src/message_channel/mem_local_history.spec.ts renamed to packages/sds/src/message_channel/local_history.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { expect } from "chai";
22

3-
import { MemLocalHistory } from "./mem_local_history.js";
3+
import { MemLocalHistory } from "./local_history.js";
44
import { ContentMessage } from "./message.js";
55

66
describe("MemLocalHistory", () => {
77
it("Cap max size when messages are pushed one at a time", () => {
88
const maxSize = 2;
99

10-
const hist = new MemLocalHistory({ maxSize: maxSize });
10+
const hist = new MemLocalHistory({ maxSize });
1111

1212
hist.push(
1313
new ContentMessage("1", "c", "a", [], 1n, undefined, new Uint8Array([1]))
@@ -31,7 +31,7 @@ describe("MemLocalHistory", () => {
3131
it("Cap max size when a pushed array is exceeding the cap", () => {
3232
const maxSize = 2;
3333

34-
const hist = new MemLocalHistory({ maxSize: maxSize });
34+
const hist = new MemLocalHistory({ maxSize });
3535

3636
hist.push(
3737
new ContentMessage("1", "c", "a", [], 1n, undefined, new Uint8Array([1]))

packages/sds/src/message_channel/mem_local_history.ts renamed to packages/sds/src/message_channel/local_history.ts

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Logger } from "@waku/utils";
22
import _ from "lodash";
33

44
import { type ChannelId, ContentMessage, isContentMessage } from "./message.js";
5+
import { ILocalHistory } from "./message_channel.js";
56
import { PersistentStorage } from "./persistent_storage.js";
67

78
export const DEFAULT_MAX_LENGTH = 10_000;
@@ -19,44 +20,15 @@ export const DEFAULT_MAX_LENGTH = 10_000;
1920
* If an array of items longer than `maxLength` is pushed, dropping will happen
2021
* at next push.
2122
*/
22-
export interface ILocalHistory {
23-
length: number;
24-
push(...items: ContentMessage[]): number;
25-
some(
26-
predicate: (
27-
value: ContentMessage,
28-
index: number,
29-
array: ContentMessage[]
30-
) => unknown,
31-
thisArg?: any
32-
): boolean;
33-
slice(start?: number, end?: number): ContentMessage[];
34-
find(
35-
predicate: (
36-
value: ContentMessage,
37-
index: number,
38-
obj: ContentMessage[]
39-
) => unknown,
40-
thisArg?: any
41-
): ContentMessage | undefined;
42-
findIndex(
43-
predicate: (
44-
value: ContentMessage,
45-
index: number,
46-
obj: ContentMessage[]
47-
) => unknown,
48-
thisArg?: any
49-
): number;
50-
}
5123

52-
export type MemLocalHistoryOptions = {
24+
export type LocalHistoryOptions = {
5325
storage?: ChannelId | PersistentStorage;
5426
maxSize?: number;
5527
};
5628

5729
const log = new Logger("sds:local-history");
5830

59-
export class MemLocalHistory implements ILocalHistory {
31+
export class LocalHistory implements ILocalHistory {
6032
private items: ContentMessage[] = [];
6133
private readonly storage?: PersistentStorage;
6234
private readonly maxSize: number;
@@ -68,7 +40,7 @@ export class MemLocalHistory implements ILocalHistory {
6840
* - storage: Optional persistent storage backend for message persistence or channelId to use with PersistentStorage.
6941
* - maxSize: The maximum number of messages to store. Optional, defaults to DEFAULT_MAX_LENGTH.
7042
*/
71-
public constructor(opts: MemLocalHistoryOptions = {}) {
43+
public constructor(opts: LocalHistoryOptions = {}) {
7244
const { storage, maxSize } = opts;
7345
this.maxSize = maxSize ?? DEFAULT_MAX_LENGTH;
7446
if (storage instanceof PersistentStorage) {

packages/sds/src/message_channel/message_channel.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { expect } from "chai";
44
import { DefaultBloomFilter } from "../bloom_filter/bloom.js";
55

66
import { MessageChannelEvent } from "./events.js";
7-
import { MemLocalHistory } from "./mem_local_history.js";
8-
import { ILocalHistory } from "./mem_local_history.js";
7+
import { MemLocalHistory } from "./local_history.js";
8+
import { ILocalHistory } from "./local_history.js";
99
import {
1010
ContentMessage,
1111
HistoryEntry,

packages/sds/src/message_channel/message_channel.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { DefaultBloomFilter } from "../bloom_filter/bloom.js";
77

88
import { Command, Handlers, ParamsByAction, Task } from "./command_queue.js";
99
import { MessageChannelEvent, MessageChannelEvents } from "./events.js";
10-
import { ILocalHistory, MemLocalHistory } from "./mem_local_history.js";
10+
import { LocalHistory } from "./local_history.js";
1111
import {
1212
ChannelId,
1313
ContentMessage,
@@ -23,8 +23,6 @@ import {
2323
} from "./message.js";
2424
import { RepairConfig, RepairManager } from "./repair/repair.js";
2525

26-
export type { ILocalHistory };
27-
2826
export const DEFAULT_BLOOM_FILTER_OPTIONS = {
2927
capacity: 10000,
3028
errorRate: 0.001
@@ -40,6 +38,11 @@ const DEFAULT_POSSIBLE_ACKS_THRESHOLD = 2;
4038

4139
const log = new Logger("sds:message-channel");
4240

41+
export type ILocalHistory = Pick<
42+
Array<ContentMessage>,
43+
"some" | "push" | "slice" | "find" | "length" | "findIndex"
44+
>;
45+
4346
export interface MessageChannelOptions {
4447
causalHistorySize?: number;
4548
/**
@@ -115,7 +118,7 @@ export class MessageChannel extends TypedEventEmitter<MessageChannelEvents> {
115118
this.possibleAcks = new Map();
116119
this.incomingBuffer = [];
117120
this.localHistory =
118-
localHistory ?? new MemLocalHistory({ storage: channelId });
121+
localHistory ?? new LocalHistory({ storage: channelId });
119122
this.causalHistorySize =
120123
options.causalHistorySize ?? DEFAULT_CAUSAL_HISTORY_SIZE;
121124
// TODO: this should be determined based on the bloom filter parameters and number of hashes

packages/sds/src/message_channel/persistent_storage.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from "chai";
22

3-
import { MemLocalHistory } from "./mem_local_history.js";
3+
import { MemLocalHistory } from "./local_history.js";
44
import { ContentMessage } from "./message.js";
55
import { HistoryStorage, PersistentStorage } from "./persistent_storage.js";
66

0 commit comments

Comments
 (0)