Skip to content

Commit

Permalink
Add recursive firing of dispose events of sub directory deletion (mic…
Browse files Browse the repository at this point in the history
  • Loading branch information
jatgarg authored Mar 14, 2022
1 parent 47e2bd1 commit eeb358a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/dds/map/src/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,19 @@ 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;
}
// Dispose the subdirectory tree. This will dispose the subdirectories from bottom to top.
const subDirectories = directory.subdirectories();
for (const [_, subDirectory] of subDirectories) {
this.disposeSubDirectoryTree(subDirectory);
}
directory.dispose();
}
}
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

0 comments on commit eeb358a

Please sign in to comment.