-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
1,225 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
anti:gagarin@0.3.3 | ||
anti:gagarin@0.4.0 | ||
[email protected] | ||
[email protected] | ||
[email protected].3 | ||
[email protected].13 | ||
[email protected].4 | ||
[email protected].14 | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
|
@@ -14,5 +14,5 @@ [email protected] | |
[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected].4 | ||
[email protected].5 | ||
[email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
var genericPromiseChain = require('./genericPromiseChain'); | ||
var Promise = require('es6-promise').Promise; | ||
var either = require('./tools').either; | ||
|
||
module.exports = function generic(methods, myPrototype) { | ||
|
||
var GenericPromiseChain = genericPromiseChain(methods, myPrototype); | ||
|
||
function Generic (operand) { | ||
"use strict"; | ||
|
||
this._operand = operand; | ||
} | ||
|
||
Generic.prototype = Object.create(myPrototype, { | ||
methods: { value: GenericPromiseChain.prototype.methods } | ||
}); | ||
|
||
GenericPromiseChain.prototype.methods.forEach(function (name) { | ||
"use strict"; | ||
|
||
Generic.prototype[name] = function () { | ||
var chain = new GenericPromiseChain(this._operand); | ||
return chain[name].apply(chain, arguments); | ||
}; | ||
|
||
}); | ||
|
||
return Generic; | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
var Promise = require('es6-promise').Promise; | ||
var either = require('./tools').either; | ||
|
||
module.exports = function genericPromiseChain(methods, myPrototype) { | ||
|
||
function GenericPromiseChain (operand) { | ||
"use strict"; | ||
|
||
var self = this; | ||
|
||
this._operand = operand; | ||
this._promise = typeof operand === 'function' ? operand() : operand; | ||
} | ||
|
||
GenericPromiseChain.prototype = Object.create(myPrototype); | ||
|
||
[ 'then', 'catch' ].forEach(function (name) { | ||
|
||
GenericPromiseChain.prototype[name] = function () { | ||
"use strict"; | ||
this._promise = this._promise[name].apply(this._promise, arguments); | ||
return this; | ||
}; | ||
|
||
}); | ||
|
||
GenericPromiseChain.prototype.always = function (callback) { | ||
"use strict"; | ||
|
||
return this.then(function (result) { callback(null, result) }, callback); | ||
}; | ||
|
||
GenericPromiseChain.prototype.sleep = function (timeout) { | ||
"use strict"; | ||
|
||
var self = this; | ||
return self.then(function () { | ||
return new Promise(function (resolve) { | ||
setTimeout(resolve, timeout); | ||
}); | ||
}); | ||
}; | ||
|
||
GenericPromiseChain.prototype.expectError = function (callback) { | ||
"use strict"; | ||
|
||
var self = this; | ||
return self.then(function () { | ||
throw new Error('exception was not thrown'); | ||
}, callback); | ||
}; | ||
|
||
GenericPromiseChain.prototype.noWait = function () { | ||
"use strict"; | ||
|
||
return new GenericPromiseChain(this._operand); | ||
}; | ||
|
||
GenericPromiseChain.prototype.branch = function () { | ||
"use strict"; | ||
|
||
return new GenericPromiseChain(this._operand, this._promise); | ||
}; | ||
|
||
GenericPromiseChain.prototype.yet = function (code, args) { | ||
"use strict"; | ||
|
||
var args = Array.prototype.slice.call(arguments, 0); | ||
var self = this; | ||
//-------------------------------- | ||
return self.catch(function (err) { | ||
return self.noWait().execute(code, args).then(function (errMessage) { | ||
throw new Error(err.message + ' ' + errMessage); | ||
}); | ||
}); | ||
}; | ||
|
||
GenericPromiseChain.prototype.methods = methods.concat([ | ||
'__custom__', | ||
'catch', | ||
'then', | ||
'always', | ||
'sleep', | ||
'expectError', | ||
'noWait', | ||
'branch', | ||
'yet', | ||
]); | ||
|
||
GenericPromiseChain.prototype.__custom__ = function (action) { | ||
var self = this; | ||
self._promise = Promise.all([ | ||
typeof self._operand === 'function' ? self._operand() : self._operand, self._promise | ||
]).then(function (all) { | ||
return new Promise(function (resolve, reject) { | ||
var operand = all[0]; | ||
if (!operand || typeof operand !== 'object') { | ||
reject(new Error('GenericPromiseChain: invalid operand')); | ||
} | ||
action(operand, either(cleanError(reject)).or(resolve)); | ||
}); | ||
}); | ||
return self; | ||
}; | ||
|
||
methods.forEach(function (name) { | ||
"use strict"; | ||
|
||
/** | ||
* Update the current promise and return this to allow chaining. | ||
*/ | ||
GenericPromiseChain.prototype[name] = function () { | ||
var args = Array.prototype.slice.call(arguments, 0); | ||
return this.__custom__(function (operand) { | ||
if (!operand[name]) { | ||
reject(new Error('GenericPromiseChain: operand does not implement method: ' + name)); | ||
} else { | ||
args.push(either(cleanError(reject)).or(resolve)); | ||
operand[name].apply(operand, args); | ||
} | ||
}); | ||
}; | ||
|
||
}); | ||
|
||
return GenericPromiseChain; | ||
|
||
} | ||
|
||
function cleanError(reject) { | ||
return function (err) { | ||
if (err && !(err instanceof Error)) { | ||
err = new Error(err.message || err.toString()); | ||
} | ||
reject(err); | ||
} | ||
} |
Oops, something went wrong.