diff --git a/lib/resource/PhysicalResource.d.ts b/lib/resource/PhysicalResource.d.ts index 3340ef88..abfd9be1 100644 --- a/lib/resource/PhysicalResource.d.ts +++ b/lib/resource/PhysicalResource.d.ts @@ -3,7 +3,7 @@ import { ResourceChildren } from './ResourceChildren'; import { FSManager, FSPath } from '../manager/FSManager'; export declare abstract class PhysicalResource extends StandardResource { realPath: string; - constructor(realPath: string, parent: IResource, fsManager: FSManager); + constructor(realPath: string, parent?: IResource, fsManager?: FSManager); abstract create(callback: SimpleCallback): any; abstract delete(callback: SimpleCallback): any; moveTo(to: FSPath, callback: Return2Callback): void; @@ -21,7 +21,7 @@ export declare abstract class PhysicalResource extends StandardResource { } export declare class PhysicalFolder extends PhysicalResource { children: ResourceChildren; - constructor(realPath: string, parent: IResource, fsManager: FSManager); + constructor(realPath: string, parent?: IResource, fsManager?: FSManager); type(callback: ReturnCallback): void; create(callback: SimpleCallback): void; delete(callback: SimpleCallback): void; @@ -35,7 +35,7 @@ export declare class PhysicalFolder extends PhysicalResource { getChildren(callback: ReturnCallback): void; } export declare class PhysicalFile extends PhysicalResource { - constructor(realPath: string, parent: IResource, fsManager: FSManager); + constructor(realPath: string, parent?: IResource, fsManager?: FSManager); type(callback: ReturnCallback): void; create(callback: SimpleCallback): void; delete(callback: SimpleCallback): void; diff --git a/lib/resource/PhysicalResource.js b/lib/resource/PhysicalResource.js index d621beb4..cf7bd7d5 100644 --- a/lib/resource/PhysicalResource.js +++ b/lib/resource/PhysicalResource.js @@ -93,7 +93,12 @@ var PhysicalFolder = (function (_super) { Resource_1.StandardResource.sizeOfSubFiles(this, callback); }; PhysicalFolder.prototype.addChild = function (resource, callback) { - this.children.add(resource, callback); + var _this = this; + this.children.add(resource, function (e) { + if (!e) + resource.parent = _this; + callback(e); + }); }; PhysicalFolder.prototype.removeChild = function (resource, callback) { this.children.remove(resource, callback); diff --git a/lib/resource/Resource.js b/lib/resource/Resource.js index ae387278..ca3cb52d 100644 --- a/lib/resource/Resource.js +++ b/lib/resource/Resource.js @@ -125,8 +125,13 @@ var StandardResource = (function () { this.dateLastModified = Date.now(); }; StandardResource.prototype.removeFromParent = function (callback) { - if (this.parent) - this.parent.removeChild(this, callback); + var _this = this; + var parent = this.parent; + if (parent) + parent.removeChild(this, function (e) { + if (_this.parent === parent) + _this.parent = null; + }); else callback(null); }; diff --git a/lib/resource/RootResource.js b/lib/resource/RootResource.js index 5fe592d9..54058a78 100644 --- a/lib/resource/RootResource.js +++ b/lib/resource/RootResource.js @@ -54,7 +54,12 @@ var RootResource = (function (_super) { Resource_1.StandardResource.sizeOfSubFiles(this, callback); }; RootResource.prototype.addChild = function (resource, callback) { - this.children.add(resource, callback); + var _this = this; + this.children.add(resource, function (e) { + if (!e) + resource.parent = _this; + callback(e); + }); }; RootResource.prototype.removeChild = function (resource, callback) { this.children.remove(resource, callback); diff --git a/lib/resource/VirtualResource.d.ts b/lib/resource/VirtualResource.d.ts index ea23fbc5..df34ce12 100644 --- a/lib/resource/VirtualResource.d.ts +++ b/lib/resource/VirtualResource.d.ts @@ -3,7 +3,7 @@ import { ResourceChildren } from './ResourceChildren'; import { FSManager, FSPath } from '../manager/FSManager'; export declare abstract class VirtualResource extends StandardResource { name: string; - constructor(name: string, parent: IResource, fsManager?: FSManager); + constructor(name: string, parent?: IResource, fsManager?: FSManager); create(callback: SimpleCallback): void; delete(callback: SimpleCallback): void; moveTo(to: FSPath, callback: Return2Callback): void; @@ -21,7 +21,7 @@ export declare abstract class VirtualResource extends StandardResource { } export declare class VirtualFolder extends VirtualResource { children: ResourceChildren; - constructor(name: string, parent: IResource, fsManager?: FSManager); + constructor(name: string, parent?: IResource, fsManager?: FSManager); type(callback: ReturnCallback): void; append(data: Int8Array, callback: SimpleCallback): void; write(data: Int8Array, callback: SimpleCallback): void; @@ -34,7 +34,7 @@ export declare class VirtualFolder extends VirtualResource { } export declare class VirtualFile extends VirtualResource { content: Int8Array; - constructor(name: string, parent: IResource, fsManager?: FSManager); + constructor(name: string, parent?: IResource, fsManager?: FSManager); type(callback: ReturnCallback): void; create(callback: SimpleCallback): void; delete(callback: SimpleCallback): void; diff --git a/lib/resource/VirtualResource.js b/lib/resource/VirtualResource.js index ac6c0bff..97db341c 100644 --- a/lib/resource/VirtualResource.js +++ b/lib/resource/VirtualResource.js @@ -67,7 +67,12 @@ var VirtualFolder = (function (_super) { Resource_1.StandardResource.sizeOfSubFiles(this, callback); }; VirtualFolder.prototype.addChild = function (resource, callback) { - this.children.add(resource, callback); + var _this = this; + this.children.add(resource, function (e) { + if (!e) + resource.parent = _this; + callback(e); + }); }; VirtualFolder.prototype.removeChild = function (resource, callback) { this.children.remove(resource, callback); diff --git a/src/resource/PhysicalResource.ts b/src/resource/PhysicalResource.ts index ede4c49e..6cc04c7a 100644 --- a/src/resource/PhysicalResource.ts +++ b/src/resource/PhysicalResource.ts @@ -9,7 +9,7 @@ export abstract class PhysicalResource extends StandardResource { realPath : string - constructor(realPath : string, parent : IResource, fsManager : FSManager) + constructor(realPath : string, parent ?: IResource, fsManager ?: FSManager) { super(parent, fsManager); @@ -63,7 +63,7 @@ export class PhysicalFolder extends PhysicalResource { children : ResourceChildren - constructor(realPath : string, parent : IResource, fsManager : FSManager) + constructor(realPath : string, parent ?: IResource, fsManager ?: FSManager) { super(realPath, parent, fsManager); @@ -128,7 +128,11 @@ export class PhysicalFolder extends PhysicalResource // ****************************** Children ****************************** // addChild(resource : IResource, callback : SimpleCallback) { - this.children.add(resource, callback); + this.children.add(resource, (e) => { + if(!e) + resource.parent = this; + callback(e); + }); } removeChild(resource : IResource, callback : SimpleCallback) { @@ -142,7 +146,7 @@ export class PhysicalFolder extends PhysicalResource export class PhysicalFile extends PhysicalResource { - constructor(realPath : string, parent : IResource, fsManager : FSManager) + constructor(realPath : string, parent ?: IResource, fsManager ?: FSManager) { super(realPath, parent, fsManager); } diff --git a/src/resource/Resource.ts b/src/resource/Resource.ts index ec8217f4..93ed3833 100644 --- a/src/resource/Resource.ts +++ b/src/resource/Resource.ts @@ -246,8 +246,12 @@ export abstract class StandardResource implements IResource protected removeFromParent(callback : SimpleCallback) { - if(this.parent) - this.parent.removeChild(this, callback); + const parent = this.parent; + if(parent) + parent.removeChild(this, (e) => { + if(this.parent === parent) // this.parent didn't change + this.parent = null; + }); else callback(null); } diff --git a/src/resource/RootResource.ts b/src/resource/RootResource.ts index 95518bf8..9f6e5d12 100644 --- a/src/resource/RootResource.ts +++ b/src/resource/RootResource.ts @@ -67,7 +67,11 @@ export class RootResource extends StandardResource // ****************************** Children ****************************** // addChild(resource : IResource, callback : SimpleCallback) { - this.children.add(resource, callback); + this.children.add(resource, (e) => { + if(!e) + resource.parent = this; + callback(e); + }); } removeChild(resource : IResource, callback : SimpleCallback) { diff --git a/src/resource/VirtualResource.ts b/src/resource/VirtualResource.ts index 69767ca6..408f540d 100644 --- a/src/resource/VirtualResource.ts +++ b/src/resource/VirtualResource.ts @@ -10,7 +10,7 @@ export abstract class VirtualResource extends StandardResource { name : string - constructor(name : string, parent : IResource, fsManager ?: FSManager) + constructor(name : string, parent ?: IResource, fsManager ?: FSManager) { super(parent, fsManager ? fsManager : new VirtualFSManager()); @@ -61,7 +61,7 @@ export class VirtualFolder extends VirtualResource { children : ResourceChildren - constructor(name : string, parent : IResource, fsManager ?: FSManager) + constructor(name : string, parent ?: IResource, fsManager ?: FSManager) { super(name, parent, fsManager); @@ -99,7 +99,11 @@ export class VirtualFolder extends VirtualResource // ****************************** Children ****************************** // addChild(resource : IResource, callback : SimpleCallback) { - this.children.add(resource, callback); + this.children.add(resource, (e) => { + if(!e) + resource.parent = this; + callback(e); + }); } removeChild(resource : IResource, callback : SimpleCallback) { @@ -115,7 +119,7 @@ export class VirtualFile extends VirtualResource { content : Int8Array - constructor(name : string, parent : IResource, fsManager ?: FSManager) + constructor(name : string, parent ?: IResource, fsManager ?: FSManager) { super(name, parent, fsManager); diff --git a/test/tests/listRootFolder.js b/test/tests/listRootFolder.js index 55963039..011129ca 100644 --- a/test/tests/listRootFolder.js +++ b/test/tests/listRootFolder.js @@ -11,7 +11,7 @@ module.exports = (test, options, index) => test('list root folder', _isValid => } var server = new webdav.WebDAVServer(); - server.rootResource.addChild(new webdav.VirtualFile('file.txt', server.rootResource, null), e => { + server.rootResource.addChild(new webdav.VirtualFile('file.txt', server.rootResource), e => { if(e) { isValid(false, e) diff --git a/test/tests/makeFolder.js b/test/tests/makeFolder.js index 9ea637e1..988b3674 100644 --- a/test/tests/makeFolder.js +++ b/test/tests/makeFolder.js @@ -21,7 +21,7 @@ module.exports = (test, options, index) => test('make a folder', _isValid => } var server = new webdav.WebDAVServer(); - server.rootResource.addChild(new webdav.VirtualFile('testFile.txt', server.rootResource, null), e => { + server.rootResource.addChild(new webdav.VirtualFile('testFile.txt', server.rootResource), e => { if(e) { isValid(false, e) diff --git a/test/tests/readVirtualFile.js b/test/tests/readVirtualFile.js index f4eaf729..e3d3e2e9 100644 --- a/test/tests/readVirtualFile.js +++ b/test/tests/readVirtualFile.js @@ -23,7 +23,7 @@ module.exports = (test, options, index) => test('read a virtual file', _isValid var testValue = 'this is the content!'; var server = new webdav.WebDAVServer(); - var file = new webdav.VirtualFile('testFile.txt', server.rootResource, null); + var file = new webdav.VirtualFile('testFile.txt', server.rootResource); file.content = testValue; server.rootResource.addChild(file, e => { if(e) diff --git a/test/tests/statVirtualFile.js b/test/tests/statVirtualFile.js index 05d1f9d3..9d5ba2f4 100644 --- a/test/tests/statVirtualFile.js +++ b/test/tests/statVirtualFile.js @@ -21,7 +21,7 @@ module.exports = (test, options, index) => test('stat of virtual file', _isValid } var server = new webdav.WebDAVServer(); - server.rootResource.addChild(new webdav.VirtualFile('testFile.txt', server.rootResource, null), e => { + server.rootResource.addChild(new webdav.VirtualFile('testFile.txt', server.rootResource), e => { if(e) { isValid(false, e)