Skip to content

Commit

Permalink
feat: fix tie()
Browse files Browse the repository at this point in the history
  • Loading branch information
penpenpng committed Dec 3, 2023
1 parent 0b1a388 commit 3362d96
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/__test__/operator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,27 @@ test("tie()", async () => {
{
...packets[0],
seenOn: ["wss://aaa.example.com"],
isNew: true,
},
{
...packets[2],
seenOn: ["wss://aaa.example.com"],
isNew: true,
},
{
...packets[3],
seenOn: ["wss://aaa.example.com"],
isNew: true,
},
{
...packets[4],
seenOn: ["wss://aaa.example.com", "wss://bbb.example.com"],
isNew: false,
},
{
...packets[5],
seenOn: ["wss://aaa.example.com", "wss://bbb.example.com"],
isNew: false,
},
{
...packets[6],
Expand All @@ -117,6 +122,7 @@ test("tie()", async () => {
"wss://bbb.example.com",
"wss://ccc.example.com",
],
isNew: false,
}
)
);
Expand Down
4 changes: 3 additions & 1 deletion src/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export function tie<P extends EventPacket>(
* Create a customizable tie operator.
*/
export function createTie<P extends EventPacket>(): [
OperatorFunction<P, P & { seenOn: Set<string> }>,
OperatorFunction<P, P & { seenOn: Set<string>; isNew: boolean }>,
Map<string, Set<string>>
] {
const memo = new Map<string, Set<string>>();
Expand All @@ -111,13 +111,15 @@ export function createTie<P extends EventPacket>(): [
filter((packet) => !memo.get(packet.event.id)?.has(packet.from)),
map((packet) => {
const seenOn = memo.get(packet.event.id) ?? new Set<string>();
const isNew = seenOn.size <= 0;

seenOn.add(packet.from);
memo.set(packet.event.id, seenOn);

return {
...packet,
seenOn,
isNew,
};
})
),
Expand Down

0 comments on commit 3362d96

Please sign in to comment.