mirror of
https://github.com/flynx/types.js.git
synced 2025-10-29 02:20:07 +00:00
added CooperativePromise(..)...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
c3c611c0bc
commit
344575daad
1
main.js
1
main.js
@ -19,6 +19,7 @@ module.patchDate = require('./Date').patchDate
|
||||
|
||||
// Additional types...
|
||||
module.containers = require('./containers')
|
||||
module.promise = require('./promise')
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ig-types",
|
||||
"version": "2.0.21",
|
||||
"version": "2.1.0",
|
||||
"description": "Generic JavaScript types and type extensions...",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
|
||||
78
promise.js
Normal file
78
promise.js
Normal file
@ -0,0 +1,78 @@
|
||||
/**********************************************************************
|
||||
*
|
||||
*
|
||||
*
|
||||
**********************************************/ /* c8 ignore next 2 */
|
||||
((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define)
|
||||
(function(require){ var module={} // make module AMD/node compatible...
|
||||
/*********************************************************************/
|
||||
|
||||
var object = require('ig-object')
|
||||
|
||||
|
||||
/*********************************************************************/
|
||||
|
||||
var CooperativePromise =
|
||||
module.CooperativePromise =
|
||||
object.Constructor('CooperativePromise', Promise, {
|
||||
// XXX do we actually need this???
|
||||
__promise: null,
|
||||
|
||||
set: function(promise){
|
||||
if(this.__promise === null){
|
||||
// setting a non-promise...
|
||||
if(promise.catch == null && promise.then == null){
|
||||
Object.defineProperty(this, '__promise', {
|
||||
value: false,
|
||||
enumerable: false,
|
||||
})
|
||||
this.__resolve(promise)
|
||||
|
||||
// setting a promise...
|
||||
} else {
|
||||
Object.defineProperty(this, '__promise', {
|
||||
value: promise,
|
||||
enumerable: false,
|
||||
})
|
||||
|
||||
// connect the base and the set promises...
|
||||
promise.catch(this.__reject.bind(this))
|
||||
promise.then(this.__resolve.bind(this)) }
|
||||
|
||||
// cleanup...
|
||||
delete this.__resolve
|
||||
delete this.__reject
|
||||
|
||||
} else {
|
||||
throw new Error('Setting a cooperative promise twice') } },
|
||||
|
||||
__new__: function(_, func){
|
||||
var methods = {}
|
||||
var obj = Reflect.construct(CooperativePromise.__proto__, [
|
||||
function(resolve, reject){
|
||||
methods.resolve = resolve
|
||||
methods.reject = reject
|
||||
func
|
||||
&& func.call(this, ...arguments)
|
||||
} ], CooperativePromise)
|
||||
// XXX revise...
|
||||
Object.defineProperties(obj, {
|
||||
__resolve: {
|
||||
value: methods.resolve,
|
||||
enumerable: false,
|
||||
configurable: true,
|
||||
},
|
||||
__reject: {
|
||||
value: methods.reject,
|
||||
enumerable: false,
|
||||
configurable: true,
|
||||
},
|
||||
})
|
||||
return obj },
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
/**********************************************************************
|
||||
* vim:set ts=4 sw=4 : */ return module })
|
||||
Loading…
x
Reference in New Issue
Block a user