Skip to content

Commit

Permalink
Made optional the 'parent' and 'fsManager' on constructor of resource…
Browse files Browse the repository at this point in the history
…s -> parent is not affected on 'addChild()' call
  • Loading branch information
AdrienCastex committed May 12, 2017
1 parent aa02333 commit 9c9a3bc
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 26 deletions.
6 changes: 3 additions & 3 deletions lib/resource/PhysicalResource.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FSPath, FSPath>): void;
Expand All @@ -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<ResourceType>): void;
create(callback: SimpleCallback): void;
delete(callback: SimpleCallback): void;
Expand All @@ -35,7 +35,7 @@ export declare class PhysicalFolder extends PhysicalResource {
getChildren(callback: ReturnCallback<IResource[]>): void;
}
export declare class PhysicalFile extends PhysicalResource {
constructor(realPath: string, parent: IResource, fsManager: FSManager);
constructor(realPath: string, parent?: IResource, fsManager?: FSManager);
type(callback: ReturnCallback<ResourceType>): void;
create(callback: SimpleCallback): void;
delete(callback: SimpleCallback): void;
Expand Down
7 changes: 6 additions & 1 deletion lib/resource/PhysicalResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions lib/resource/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
7 changes: 6 additions & 1 deletion lib/resource/RootResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions lib/resource/VirtualResource.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FSPath, FSPath>): void;
Expand All @@ -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<ResourceType>): void;
append(data: Int8Array, callback: SimpleCallback): void;
write(data: Int8Array, callback: SimpleCallback): void;
Expand All @@ -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<ResourceType>): void;
create(callback: SimpleCallback): void;
delete(callback: SimpleCallback): void;
Expand Down
7 changes: 6 additions & 1 deletion lib/resource/VirtualResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 8 additions & 4 deletions src/resource/PhysicalResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
}
Expand Down
8 changes: 6 additions & 2 deletions src/resource/Resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 5 additions & 1 deletion src/resource/RootResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
12 changes: 8 additions & 4 deletions src/resource/VirtualResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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)
{
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion test/tests/listRootFolder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/tests/makeFolder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/tests/readVirtualFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/tests/statVirtualFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9c9a3bc

Please sign in to comment.