Skip to content

Commit 2be1edb

Browse files
committed
fix: Weakly reference parents in file tree to avoid cycles
1 parent c331a16 commit 2be1edb

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/files/browser.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class BIDSFileBrowser implements BIDSFile {
1212
#file: File
1313
name: string
1414
path: string
15-
parent: FileTree
15+
#parent!: WeakRef<FileTree>
1616
viewed: boolean = false
1717

1818
constructor(file: File, ignore: FileIgnoreRules, parent?: FileTree) {
@@ -25,6 +25,14 @@ export class BIDSFileBrowser implements BIDSFile {
2525
this.parent = parent ?? new FileTree('', '/', undefined)
2626
}
2727

28+
get parent(): FileTree {
29+
return this.#parent.deref() as FileTree
30+
}
31+
32+
set parent(tree: FileTree) {
33+
this.#parent = new WeakRef(tree)
34+
}
35+
2836
get size(): number {
2937
return this.#file.size
3038
}

src/files/deno.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class BIDSFileDeno implements BIDSFile {
1717
#ignore: FileIgnoreRules
1818
name: string
1919
path: string
20-
parent: FileTree
20+
#parent!: WeakRef<FileTree>
2121
#fileInfo?: Deno.FileInfo
2222
#datasetAbsPath: string
2323
viewed: boolean = false
@@ -41,6 +41,14 @@ export class BIDSFileDeno implements BIDSFile {
4141
return join(this.#datasetAbsPath, this.path)
4242
}
4343

44+
get parent(): FileTree {
45+
return this.#parent.deref() as FileTree
46+
}
47+
48+
set parent(tree: FileTree) {
49+
this.#parent = new WeakRef(tree)
50+
}
51+
4452
get size(): number {
4553
return this.#fileInfo ? this.#fileInfo.size : -1
4654
}

src/types/filetree.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class FileTree {
3232
files: BIDSFile[]
3333
directories: FileTree[]
3434
viewed: boolean
35-
parent?: FileTree
35+
#parent?: WeakRef<FileTree>
3636
#ignore: FileIgnoreRules
3737

3838
constructor(path: string, name: string, parent?: FileTree, ignore?: FileIgnoreRules) {
@@ -45,6 +45,14 @@ export class FileTree {
4545
this.#ignore = ignore ?? new FileIgnoreRules([])
4646
}
4747

48+
get parent(): FileTree | undefined {
49+
return this.#parent?.deref()
50+
}
51+
52+
set parent(tree: FileTree | undefined) {
53+
this.#parent = tree ? new WeakRef(tree) : undefined
54+
}
55+
4856
get ignored(): boolean {
4957
if (!this.parent) return false
5058
return this.#ignore.test(this.path)

0 commit comments

Comments
 (0)