Skip to content

Commit

Permalink
Added a 'Workflow' class to replace similar codes in the project
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed Jun 14, 2017
1 parent 0bc3801 commit eb22a3d
Show file tree
Hide file tree
Showing 11 changed files with 277 additions and 219 deletions.
15 changes: 15 additions & 0 deletions lib/helper/Workflow.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export declare class Workflow {
counter: number;
data: any[];
errorFn: (error?: any) => void;
doneFn: (data?: any[]) => void;
intermediateFn: (subject: any, e?: any, data?: any) => void;
exitOnError: boolean;
constructor(exitOnError?: boolean);
protected _done(subject: any, e?: any, data?: any): void;
each<T>(subjects: T[], fn: (subject: T, done: (error?: any, data?: any) => void) => void): this;
eachProperties(object: any, fn: (name: string, value: any, done: (error?: any, data?: any) => void) => void): this;
intermediate(fn: (subject: any, e?: any, data?: any) => void): this;
error(fn: (error?: any) => void): this;
done(fn: (data?: any[]) => void): this;
}
70 changes: 70 additions & 0 deletions lib/helper/Workflow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Workflow = (function () {
function Workflow(exitOnError) {
if (exitOnError === void 0) { exitOnError = true; }
this.exitOnError = !!exitOnError;
this.intermediateFn = null;
this.counter = 0;
this.data = [];
this.errorFn = null;
this.doneFn = null;
}
Workflow.prototype._done = function (subject, e, data) {
if (this.counter <= 0)
return;
if (e) {
if (this.exitOnError)
this.counter = -1000;
if (this.errorFn)
this.errorFn(e);
if (this.exitOnError)
return;
}
if (this.intermediateFn)
this.intermediateFn(subject, e, data);
--this.counter;
this.data.push(data);
if (this.counter === 0 && this.doneFn)
this.doneFn(this.data);
};
Workflow.prototype.each = function (subjects, fn) {
var _this = this;
this.counter = subjects.length;
subjects.forEach(function (s) { return process.nextTick(function () { return fn(s, function (e, d) { return _this._done(s, e, d); }); }); });
return this;
};
Workflow.prototype.eachProperties = function (object, fn) {
var _this = this;
this.counter = Object.keys(object).length;
process.nextTick(function () {
var _loop_1 = function (name_1) {
fn(name_1, object[name_1], function (e, d) {
return _this._done((_a = {}, _a[name_1] = object[name_1], _a), e, d);
var _a;
});
};
for (var name_1 in object) {
_loop_1(name_1);
}
});
return this;
};
Workflow.prototype.intermediate = function (fn) {
this.intermediateFn = fn;
return this;
};
Workflow.prototype.error = function (fn) {
this.errorFn = fn;
return this;
};
Workflow.prototype.done = function (fn) {
var _this = this;
this.doneFn = fn;
if (this.counter === 0)
process.nextTick(function () { return _this.doneFn(_this.data); });
return this;
};
return Workflow;
}());
exports.Workflow = Workflow;
27 changes: 7 additions & 20 deletions lib/resource/std/StandardResource.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var LockScope_1 = require("../lock/LockScope");
var Workflow_1 = require("../../helper/Workflow");
var LockType_1 = require("../lock/LockType");
var LockKind_1 = require("../lock/LockKind");
var LockBag_1 = require("../lock/LockBag");
Expand All @@ -20,26 +21,12 @@ var StandardResource = (function () {
callback(e, null);
return;
}
if (children.length === 0) {
callback(null, 0);
return;
}
var size = 0;
var nb = children.length;
function go(e, s) {
if (nb <= 0)
return;
if (e) {
nb = -1;
callback(e, size);
return;
}
size += s;
--nb;
if (nb === 0)
callback(null, size);
}
children.forEach(function (c) { return c.size(targetSource, go); });
new Workflow_1.Workflow()
.each(children, function (child, cb) {
child.size(targetSource, cb);
})
.error(function (e) { return callback(e, 0); })
.done(function (sizes) { return callback(null, sizes.reduce(function (o, s) { return o + s; }, 0)); });
});
};
StandardResource.prototype.isSame = function (resource, callback) {
Expand Down
61 changes: 16 additions & 45 deletions lib/server/commands/Copy.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,20 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var WebDAVRequest_1 = require("../WebDAVRequest");
var Workflow_1 = require("../../helper/Workflow");
var FSManager_1 = require("../../manager/FSManager");
function copyAllProperties(source, destination, callback) {
source.getProperties(function (e, props) {
if (e) {
callback(e);
return;
}
var nb = Object.keys(props).length;
function go(error) {
if (nb <= 0)
return;
if (error) {
nb = -1;
callback(error);
return;
}
--nb;
if (nb === 0)
callback(null);
}
if (nb === 0) {
callback(null);
return;
}
for (var name_1 in props) {
if (nb <= 0)
break;
destination.setProperty(name_1, JSON.parse(JSON.stringify(props[name_1])), go);
}
new Workflow_1.Workflow()
.eachProperties(props, function (name, value, cb) {
destination.setProperty(name, JSON.parse(JSON.stringify(value)), cb);
})
.error(callback)
.done(function () { return callback(null); });
});
}
function copy(arg, source, rDest, destination, callback) {
Expand Down Expand Up @@ -72,33 +57,19 @@ function copy(arg, source, rDest, destination, callback) {
return;
}
source.getChildren(function (e, children) { return _(e, function () {
var nb = children.length;
function done(error) {
if (nb <= 0)
return;
if (error) {
nb = -1;
callback(e);
return;
}
--nb;
if (nb === 0) {
arg.invokeEvent('copy', source, dest);
callback(null);
}
}
if (nb === 0) {
callback(null);
arg.invokeEvent('copy', source, dest);
return;
}
children.forEach(function (child) {
new Workflow_1.Workflow()
.each(children, function (child, cb) {
child.webName(function (e, name) { return process.nextTick(function () {
if (e)
done(e);
cb(e);
else
copy(arg, child, dest, destination.getChildPath(name), done);
copy(arg, child, dest, destination.getChildPath(name), cb);
}); });
})
.error(callback)
.done(function () {
arg.invokeEvent('copy', source, dest);
callback(null);
});
}); });
}
Expand Down
33 changes: 17 additions & 16 deletions lib/server/commands/Propfind.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
var WebDAVRequest_1 = require("../WebDAVRequest");
var IResource_1 = require("../../resource/IResource");
var XML_1 = require("../../helper/XML");
var Workflow_1 = require("../../helper/Workflow");
var FSPath_1 = require("../../manager/FSPath");
var Errors_1 = require("../../Errors");
var http = require("http");
Expand Down Expand Up @@ -114,25 +115,25 @@ function default_1(arg, callback) {
}
arg.requirePrivilege('canGetChildren', resource, function () {
resource.getChildren(function (e, children) { return process.nextTick(function () {
var nb = children.length + 1;
function nbOut(error) {
if (nb > 0 && error) {
nb = -1;
if (error === Errors_1.Errors.BadAuthentication)
arg.setCode(WebDAVRequest_1.HTTPCodes.Unauthorized);
else
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
function err(e) {
if (e === Errors_1.Errors.BadAuthentication)
arg.setCode(WebDAVRequest_1.HTTPCodes.Unauthorized);
else
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
}
addXMLInfo(resource, multistatus, function (e) {
if (e) {
err(e);
return;
}
--nb;
if (nb === 0)
new Workflow_1.Workflow()
.each(children, function (r, cb) { return addXMLInfo(r, multistatus, cb); })
.error(err)
.done(function () {
done(multistatus);
}
addXMLInfo(resource, multistatus, nbOut);
children.forEach(function (child) { return process.nextTick(function () {
addXMLInfo(child, multistatus, nbOut);
}); });
});
});
}); });
});
}); });
Expand Down
24 changes: 9 additions & 15 deletions lib/server/commands/Proppatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Object.defineProperty(exports, "__esModule", { value: true });
var WebDAVRequest_1 = require("../WebDAVRequest");
var http_1 = require("http");
var Workflow_1 = require("../../helper/Workflow");
var XML_1 = require("../../helper/XML");
function default_1(arg, callback) {
arg.getResource(function (e, r) {
Expand Down Expand Up @@ -41,21 +42,14 @@ function default_1(arg, callback) {
}
list.forEach(function (el) {
var els = el.find('DAV:prop').elements;
if (els.length === 0) {
finalize_1();
return;
}
var nb = els.length;
els.forEach(function (el) {
fnProp(el, function (e) { return process.nextTick(function () {
if (!e)
arg.invokeEvent(eventName, r, el);
notify_1(el, e);
--nb;
if (nb === 0)
finalize_1();
}); });
});
new Workflow_1.Workflow(false)
.each(els, fnProp)
.intermediate(function (el, e) {
if (!e)
arg.invokeEvent(eventName, r, el);
notify_1(el, e);
})
.done(function () { return finalize_1(); });
});
};
execute('DAV:set', 'setProperty', function (el, callback) {
Expand Down
89 changes: 89 additions & 0 deletions src/helper/Workflow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { IResource, ReturnCallback, ETag } from '../resource/IResource'
import { MethodCallArgs } from '../server/MethodCallArgs'
import { Errors } from '../Errors'
import * as url from 'url'

export class Workflow
{
counter : number;
data : any[];
errorFn : (error ?: any) => void;
doneFn : (data ?: any[]) => void;
intermediateFn : (subject : any, e ?: any, data ?: any) => void;

exitOnError : boolean;

constructor(exitOnError : boolean = true)
{
this.exitOnError = !!exitOnError;
this.intermediateFn = null;
this.counter = 0;
this.data = [];
this.errorFn = null;
this.doneFn = null;
}

protected _done(subject : any, e ?: any, data ?: any)
{
if(this.counter <= 0)
return;

if(e)
{
if(this.exitOnError)
this.counter = -1000;
if(this.errorFn)
this.errorFn(e);
if(this.exitOnError)
return;
}

if(this.intermediateFn)
this.intermediateFn(subject, e, data);

--this.counter;
this.data.push(data);
if(this.counter === 0 && this.doneFn)
this.doneFn(this.data);
}

each<T>(subjects : T[], fn : (subject : T, done : (error ?: any, data ?: any) => void) => void)
{
this.counter = subjects.length;
subjects.forEach((s) => process.nextTick(() => fn(s, (e, d) => this._done(s, e, d))));
return this;
}

eachProperties(object, fn : (name : string, value : any, done : (error ?: any, data ?: any) => void) => void)
{
this.counter = Object.keys(object).length;
process.nextTick(() =>
{
for(const name in object)
fn(name, object[name], (e, d) => this._done({ [name]: object[name] }, e, d));
})
return this;
}

intermediate(fn : (subject : any, e ?: any, data ?: any) => void)
{
this.intermediateFn = fn;
return this;
}

error(fn : (error ?: any) => void)
{
this.errorFn = fn;
return this;
}

done(fn : (data ?: any[]) => void)
{
this.doneFn = fn;

if(this.counter === 0)
process.nextTick(() => this.doneFn(this.data));

return this;
}
}

0 comments on commit eb22a3d

Please sign in to comment.