Skip to content

Commit

Permalink
fix test expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalmi committed Sep 9, 2023
1 parent e19bc90 commit 6522c37
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
21 changes: 14 additions & 7 deletions src/js/state/Node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,23 @@ describe('Node', () => {
it('should return children of children when called recursively', async () => {
const mockCallback: Callback = vi.fn();

await node.put({ chat1: { latest: { id: 'messageId', text: 'hi' } } });
await node.put({ chat2: { latest: { id: 'messageId2', text: 'hi2' } } });
await node
.get('chat1')
.put({ latest: { id: 'alienMessage', text: 'Take me to your leader' } });
await node
.get('chat2')
.put({ latest: { id: 'cowMessage', text: "Mooove over, I'm coming too!" } });

node.map(mockCallback, 3);
expect(mockCallback).toHaveBeenCalledWith(
{
chat1: { latest: { id: 'messageId', text: 'hi' } },
chat2: { latest: { id: 'messageId2', text: 'hi2' } },
},
'test',
{ latest: { id: 'alienMessage', text: 'Take me to your leader' } },
'test/chat1',
expect.any(Number),
expect.any(Function),
);
expect(mockCallback).toHaveBeenCalledWith(
{ latest: { id: 'cowMessage', text: "Mooove over, I'm coming too!" } },
'test/chat2',
expect.any(Number),
expect.any(Function),
);
Expand Down
10 changes: 6 additions & 4 deletions src/js/state/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ export default class Node {
const latestMap = new Map<string, NodeValue>();

let adapterSubs: Unsubscribe[] = [];
const openUnsubs: Record<string, Unsubscribe> = {}; // Changed to a dictionary

const unsubscribeFromAdapters = () => {
adapterSubs.forEach((unsub) => unsub());
};

let openUnsub: Unsubscribe | undefined;
const cb = (value: any, path: string, updatedAt: number | undefined) => {
const latest = latestMap.get(path);
if (updatedAt !== undefined && latest && latest.updatedAt >= updatedAt) {
Expand All @@ -199,14 +199,15 @@ export default class Node {
this.get(childName).put(value, updatedAt);

if (recursion > 0 && value === DIR_VALUE) {
if (!openUnsub) {
openUnsub = this.get(childName).open(callback, recursion - 1);
if (!openUnsubs[childName]) {
// Check if an Unsubscribe exists for this child
openUnsubs[childName] = this.get(childName).open(callback, recursion - 1);
}
} else {
callback(value, path, updatedAt, () => {
this.map_subscriptions.delete(id);
unsubscribeFromAdapters();
openUnsub?.();
Object.values(openUnsubs).forEach((unsub) => unsub()); // Unsubscribe all
});
}
};
Expand All @@ -216,6 +217,7 @@ export default class Node {
const unsubscribe = () => {
this.map_subscriptions.delete(id);
unsubscribeFromAdapters();
Object.values(openUnsubs).forEach((unsub) => unsub()); // Unsubscribe all
};

return unsubscribe;
Expand Down

0 comments on commit 6522c37

Please sign in to comment.