Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recursive firing of dispose events of sub directory deletion #9451

Merged
merged 2 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion packages/dds/map/src/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,27 @@ class SubDirectory extends TypedEventEmitter<IDirectoryEvents> implements IDirec
// This should make the subdirectory structure unreachable so it can be GC'd and won't appear in snapshots
// Might want to consider cleaning out the structure more exhaustively though?
const successfullyRemoved = this._subdirectories.delete(subdirName);
previousValue?.dispose();
this.disposeSubDirectoryTree(previousValue);
return successfullyRemoved;
}

private disposeSubDirectoryTree(directory: IDirectory | undefined) {
if (!directory) {
return;
}
const stack: [{node: IDirectory, visited: boolean}] = [{node: directory, visited: false}];
jatgarg marked this conversation as resolved.
Show resolved Hide resolved
while (stack.length > 0) {
const node = stack.pop();
assert(node !== undefined, "Sub directory should be present");
if (node.visited) {
node.node.dispose();
} else {
stack.push({node: node.node, visited: true});
const subDirectories = node.node.subdirectories();
for (const [_, subDirectory] of subDirectories) {
stack.push({node: subDirectory, visited: false});
}
}
}
}
}
16 changes: 16 additions & 0 deletions packages/dds/map/src/test/directory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,22 @@ describe("Directory", () => {
} catch (error) {
assert.strictEqual(error.errorType, "usageError", "Should throw usage error");
}

// Check recursive dispose event firing
const subSubDirectory = newSubDirectory.createSubDirectory("rockChild");
let rockSubDirectoryDisposed = false;
let subSubDirectoryDisposed = false;
newSubDirectory.on("disposed", (value: IDirectory) => {
rockSubDirectoryDisposed = true;
assert.equal(value.disposed, true, "rock sub directory not deleted");
});
subSubDirectory.on("disposed", (value: IDirectory) => {
subSubDirectoryDisposed = true;
assert.equal(value.disposed, true, "sub sub directory not deleted");
});
directory.deleteSubDirectory("rock");
assert(rockSubDirectoryDisposed, "Rock sub directory should be disposed");
assert(subSubDirectoryDisposed, "sub sub directory should be disposed");
});

it("Rejects a undefined and null key set", () => {
Expand Down