Skip to content

Commit 1b1514f

Browse files
authored
fix(directory): Fix SharedDirectory.forEach() (#25299)
## Description This PR fixes `SharedDirectory.forEach()` as it no longer tries to unbox the value. It also adds a test to prevent this regression in the future.
1 parent f3717b1 commit 1b1514f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

packages/dds/map/src/directory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1607,7 +1607,7 @@ class SubDirectory extends TypedEventEmitter<IDirectoryEvents> implements IDirec
16071607
): void {
16081608
this.throwIfDisposed();
16091609
for (const [key, localValue] of this.internalIterator()) {
1610-
callback((localValue as { value: unknown }).value, key, this);
1610+
callback(localValue, key, this);
16111611
}
16121612
}
16131613

packages/dds/map/src/test/mocha/directory.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,36 @@ describe("Directory", () => {
10481048
assert.equal(directory2.get("testKey2"), undefined);
10491049
});
10501050

1051+
it(".forEach() should iterate over all keys in the directory", () => {
1052+
const values = [
1053+
["a", "b"],
1054+
["c", "d"],
1055+
["e", "f"],
1056+
];
1057+
1058+
for (const [key, value] of values) {
1059+
directory1.set(key, value);
1060+
}
1061+
containerRuntimeFactory.processAllMessages();
1062+
1063+
let i = 0;
1064+
// eslint-disable-next-line unicorn/no-array-for-each
1065+
directory1.forEach((value, key) => {
1066+
assert(i < values.length, "forEach() should not have iterated more than i times");
1067+
assert.strictEqual(key, values[i][0], "key should match");
1068+
assert.strictEqual(value, values[i][1], "value should match");
1069+
i++;
1070+
});
1071+
i = 0;
1072+
// eslint-disable-next-line unicorn/no-array-for-each
1073+
directory2.forEach((value, key) => {
1074+
assert(i < values.length, "forEach() should not have iterated more than i times");
1075+
assert.strictEqual(key, values[i][0], "key should match");
1076+
assert.strictEqual(value, values[i][1], "value should match");
1077+
i++;
1078+
});
1079+
});
1080+
10511081
it("Shouldn't clear value if there is pending set", () => {
10521082
const valuesChanged: IDirectoryValueChanged[] = [];
10531083
let clearCount = 0;

0 commit comments

Comments
 (0)