Comparing version 1.2.1 to 2.0.0-beta.1
285
fluture.js
@@ -13,15 +13,11 @@ //// ____ _ _ | ||
/*istanbul ignore next*/ | ||
if(typeof module !== 'undefined'){ | ||
module.exports = f(require('fantasy-land'), require('inspect-f')); | ||
if(module && typeof module.exports !== 'undefined'){ | ||
module.exports = f(require('inspect-f')); | ||
} | ||
else if(typeof global.define === 'function' && global.define.amd){ | ||
global.define(['fantasy-land', 'inspect-f'], f); | ||
} | ||
else{ | ||
global.Fluture = f(global.FantasyLand, global.inspectf); | ||
global.Fluture = f(global.inspectf); | ||
} | ||
}(/*istanbul ignore next*/(global || window || this), function(FL, inspectf){ | ||
}(/*istanbul ignore next*/(global || window || this), function(inspectf){ | ||
@@ -35,2 +31,9 @@ 'use strict'; | ||
const TYPEOF_FUTURE = 'fluture/Future'; | ||
const FL = { | ||
map: 'map', | ||
bimap: 'bimap', | ||
chain: 'chain', | ||
ap: 'ap', | ||
of: 'of' | ||
}; | ||
@@ -103,2 +106,4 @@ function isForkable(m){ | ||
const noop = function noop(){}; | ||
const call = function call(f){f()}; | ||
const padf = (sf, s) => s.replace(/^/gm, sf).replace(sf, ''); | ||
@@ -111,3 +116,3 @@ const showf = f => padf(' ', inspectf(2, f)); | ||
function unaryPartial(f, a){ | ||
const g = function partial(b, c, d){ | ||
return function partial(b, c, d){ | ||
switch(arguments.length){ | ||
@@ -119,5 +124,2 @@ case 1: return f(a, b); | ||
}; | ||
g.toString = () => `${inspectf(2, f)}.bind(null, ${show(a)})`; | ||
g.inspect = () => `[Function: unaryPartial$${fid(f)}]`; | ||
return g; | ||
} | ||
@@ -127,6 +129,3 @@ | ||
function binaryPartial(f, a, b){ | ||
const g = function partial(c, d){ return arguments.length === 1 ? f(a, b, c) : f(a, b, c, d) }; | ||
g.toString = () => `${inspectf(2, f)}.bind(null, ${show(a)}, ${show(b)})`; | ||
g.inspect = () => `[Function: binaryPartial$${fid(f)}]`; | ||
return g; | ||
return function partial(c, d){ return arguments.length === 1 ? f(a, b, c) : f(a, b, c, d) }; | ||
} | ||
@@ -136,6 +135,3 @@ | ||
function ternaryPartial(f, a, b, c){ | ||
const g = function partial(d){ return f(a, b, c, d) }; | ||
g.toString = () => `${inspectf(2, f)}.bind(null, ${show(a)}, ${show(b)}, ${show(c)})`; | ||
g.inspect = () => `[Function: ternaryPartial$${fid(f)}]`; | ||
return g; | ||
return function partial(d){ return f(a, b, c, d) }; | ||
} | ||
@@ -170,2 +166,9 @@ | ||
function check$fork$f(f, c){ | ||
if(!(f === undefined || (isFunction(f) && f.length === 0))) throw new TypeError( | ||
'Future#fork expected the computation to return a nullary function or void' | ||
+ `\n Actual: ${show(f)}\n From calling: ${showf(c)}` | ||
); | ||
} | ||
function check$chain(it, f){ | ||
@@ -176,2 +179,7 @@ if(!isFuture(it)) error$invalidContext('Future#chain', it); | ||
function check$recur(it, f){ | ||
if(!isFuture(it)) error$invalidContext('Future#recur', it); | ||
if(!isFunction(f)) error$invalidArgument('Future#recur', 0, 'be a function', f); | ||
} | ||
function check$chain$f(m, f, x){ | ||
@@ -184,2 +192,9 @@ if(!isFuture(m)) throw new TypeError( | ||
function check$recur$f(m, f, x){ | ||
if(!isFuture(m)) throw new TypeError( | ||
'Future#recur expects the function its given to return a Future' | ||
+ `\n Actual: ${show(m)}\n From calling: ${showf(f)}\n With: ${show(x)}` | ||
); | ||
} | ||
function check$chainRej(it, f){ | ||
@@ -282,12 +297,2 @@ if(!isFuture(it)) error$invalidContext('Future.chainRej', it); | ||
function check$cache$settle(oldState, newState, oldValue, newValue){ | ||
if(oldState > 1) throw new Error( | ||
'Future.cache expects the Future it wraps to only resolve or reject once; ' | ||
+ ' a cached Future tried to ' + (newState === 2 ? 'reject' : 'resolve') + ' a second time.' | ||
+ ' Please check your cached Future and make sure it does not call res or rej multiple times' | ||
+ '\n It was ' + (oldState === 2 ? 'rejected' : 'resolved') + ' with: ' + show(oldValue) | ||
+ '\n It got ' + (newState === 2 ? 'rejected' : 'resolved') + ' with: ' + show(newValue) | ||
); | ||
} | ||
function check$after(n){ | ||
@@ -358,4 +363,5 @@ if(typeof n !== 'number') error$invalidArgument('Future.after', 0, 'be a number', n); | ||
function FutureClass(f){ | ||
this._f = f; | ||
function FutureClass(f, guard){ | ||
this._f = guard === true ? Future$guardedFork : Future$rawFork; | ||
this._raw = f; | ||
} | ||
@@ -365,23 +371,5 @@ | ||
check$Future(f); | ||
return new FutureClass(f); | ||
return new FutureClass(f, true); | ||
} | ||
function Guarded(f){ | ||
check$Future(f); | ||
return new FutureClass(function Guarded$fork(rej, res){ | ||
let open = true; | ||
f(function Guarded$rej(x){ | ||
if(open){ | ||
open = false; | ||
rej(x); | ||
} | ||
}, function Guarded$res(x){ | ||
if(open){ | ||
open = false; | ||
res(x); | ||
} | ||
}) | ||
}); | ||
} | ||
function Future$of(x){ | ||
@@ -395,5 +383,31 @@ return new FutureClass(function Future$of$fork(rej, res){ | ||
check$fork(this, rej, res); | ||
this._f(rej, res); | ||
return this._f(rej, res); | ||
} | ||
function Future$rawFork(rej, res){ | ||
const f = this._raw(rej, res); | ||
check$fork$f(f, this._raw); | ||
return f || noop; | ||
} | ||
function Future$guardedFork(rej, res){ | ||
let open = true; | ||
const f = this._raw(function Future$guard$rej(x){ | ||
if(open){ | ||
open = false; | ||
rej(x); | ||
} | ||
}, function Future$guard$res(x){ | ||
if(open){ | ||
open = false; | ||
res(x); | ||
} | ||
}); | ||
check$fork$f(f, this._raw); | ||
return function Future$guardedFork$cancel(){ | ||
open = false; | ||
f && f(); | ||
}; | ||
} | ||
function Future$chain(f){ | ||
@@ -403,5 +417,19 @@ check$chain(this, f); | ||
return new FutureClass(function Future$chain$fork(rej, res){ | ||
_this._f(rej, function Future$chain$res(x){ | ||
let cancel; | ||
const r = _this._f(rej, function Future$chain$res(x){ | ||
const m = f(x); | ||
check$chain$f(m, f, x); | ||
cancel = m._f(rej, res); | ||
}); | ||
return cancel ? cancel : (cancel = r, function Future$chain$cancel(){ cancel() }); | ||
}); | ||
} | ||
function Future$recur(f){ | ||
check$recur(this, f); | ||
const _this = this; | ||
return new FutureClass(function Future$chain$fork(rej, res){ | ||
return _this._f(rej, function Future$chain$res(x){ | ||
const m = f(x); | ||
check$recur$f(m, f, x); | ||
m._f(rej, res); | ||
@@ -416,7 +444,9 @@ }); | ||
return new FutureClass(function Future$chainRej$fork(rej, res){ | ||
_this._f(function Future$chainRej$rej(x){ | ||
let cancel; | ||
const r = _this._f(function Future$chainRej$rej(x){ | ||
const m = f(x); | ||
check$chainRej$f(m, f, x); | ||
m._f(rej, res); | ||
cancel = m._f(rej, res); | ||
}, res); | ||
return cancel ? cancel : (cancel = r, function Future$chainRej$cancel(){ cancel() }); | ||
}); | ||
@@ -429,3 +459,3 @@ } | ||
return new FutureClass(function Future$map$fork(rej, res){ | ||
_this._f(rej, function Future$map$res(x){ | ||
return _this._f(rej, function Future$map$res(x){ | ||
res(f(x)); | ||
@@ -440,3 +470,3 @@ }); | ||
return new FutureClass(function Future$mapRej$fork(rej, res){ | ||
_this._f(function Future$mapRej$rej(x){ | ||
return _this._f(function Future$mapRej$rej(x){ | ||
rej(f(x)); | ||
@@ -451,3 +481,3 @@ }, res); | ||
return new FutureClass(function Future$bimap$fork(rej, res){ | ||
_this._f(function Future$bimap$rej(x){ | ||
return _this._f(function Future$bimap$rej(x){ | ||
rej(f(x)); | ||
@@ -466,3 +496,3 @@ }, function Future$bimap$res(x){ | ||
const rej = x => ko || (ko = 1, g(x)); | ||
_this._f(rej, function Future$ap$resThis(f){ | ||
const c1 = _this._f(rej, function Future$ap$resThis(f){ | ||
if(!ok2) return void (ok1 = 1, _f = f); | ||
@@ -472,3 +502,3 @@ check$ap$f(f); | ||
}); | ||
m._f(rej, function Future$ap$resThat(x){ | ||
const c2 = m._f(rej, function Future$ap$resThat(x){ | ||
if(!ok1) return void (ok2 = 1, _x = x) | ||
@@ -478,2 +508,3 @@ check$ap$f(_f); | ||
}); | ||
return function Future$ap$cancel(){ c1(); c2() }; | ||
}); | ||
@@ -486,3 +517,3 @@ } | ||
return new FutureClass(function Future$swap$fork(rej, res){ | ||
_this._f(res, rej); | ||
return _this._f(res, rej); | ||
}); | ||
@@ -492,3 +523,3 @@ } | ||
function Future$toString(){ | ||
return `Future(${this._f.toString()})`; | ||
return `Future(${this._raw.toString()})`; | ||
} | ||
@@ -500,6 +531,7 @@ | ||
return new FutureClass(function Future$race$fork(rej, res){ | ||
let settled = false; | ||
const once = f => a => settled || (settled = true, f(a)); | ||
_this._f(once(rej), once(res)); | ||
m._f(once(rej), once(res)); | ||
let settled = false, c1 = noop, c2 = noop; | ||
const once = f => a => settled || (settled = true, c1(), c2(), f(a)); | ||
c1 = _this._f(once(rej), once(res)); | ||
c2 = m._f(once(rej), once(res)); | ||
return function Future$race$cancel(){ c1(); c2() }; | ||
}); | ||
@@ -513,10 +545,11 @@ } | ||
let ok = false, ko = false, val, err; | ||
_this._f( | ||
const c1 = _this._f( | ||
() => ko ? rej(err) : ok ? res(val) : (ko = true), | ||
x => (ok = true, res(x)) | ||
); | ||
m._f( | ||
const c2 = m._f( | ||
e => ok || (ko ? rej(e) : (err = e, ko = true)), | ||
x => ok || (ko ? res(x) : (val = x, ok = true)) | ||
); | ||
return function Future$or$cancel(){ c1(); c2() }; | ||
}); | ||
@@ -529,23 +562,26 @@ } | ||
return new FutureClass(function Future$fold$fork(rej, res){ | ||
_this._f(e => res(f(e)), x => res(g(x))); | ||
return _this._f(e => res(f(e)), x => res(g(x))); | ||
}); | ||
} | ||
function Future$hook(f, g){ | ||
check$hook(this, f, g); | ||
function Future$hook(dispose, consume){ | ||
check$hook(this, dispose, consume); | ||
const _this = this; | ||
return new FutureClass(function Future$hook$fork(rej, res){ | ||
_this.fork(rej, function Future$hook$res(r){ | ||
const m = g(r); | ||
check$hook$g(m, g, r); | ||
m._f(e => { | ||
const c = f(r); | ||
check$hook$f(c, f, r); | ||
c._f(rej, _ => rej(e)) | ||
let cancel; | ||
const ret = _this._f(rej, function Future$hook$res(resource){ | ||
const m = consume(resource); | ||
check$hook$g(m, consume, resource); | ||
cancel = m._f(e => { | ||
const c = dispose(resource); | ||
check$hook$f(c, dispose, resource); | ||
c._f(rej, _ => rej(e)); | ||
}, x => { | ||
const c = f(r); | ||
check$hook$f(c, f, r); | ||
c._f(rej, _ => res(x)) | ||
}) | ||
const c = dispose(resource); | ||
check$hook$f(c, dispose, resource); | ||
c._f(rej, _ => res(x)); | ||
}); | ||
}); | ||
cancel = cancel || ret; | ||
return function Future$hook$cancel(){ cancel() }; | ||
}); | ||
@@ -558,11 +594,9 @@ } | ||
return new FutureClass(function Future$finally$fork(rej, res){ | ||
_this._f(function Future$finally$rej(e){ | ||
m._f(rej, function Future$finally$rej$res(){ | ||
rej(e); | ||
}); | ||
let cancel; | ||
const r = _this._f(function Future$finally$rej(e){ | ||
cancel = m._f(rej, function Future$finally$rej$res(){ rej(e) }); | ||
}, function Future$finally$res(x){ | ||
m._f(rej, function Future$finally$res$res(){ | ||
res(x); | ||
}); | ||
cancel = m._f(rej, function Future$finally$res$res(){ res(x) }); | ||
}); | ||
return cancel ? cancel : (cancel = r, function Future$finally$cancel(){ cancel() }); | ||
}); | ||
@@ -573,3 +607,3 @@ } | ||
check$value(this, f); | ||
this._f( | ||
return this._f( | ||
function Future$value$rej(e){ | ||
@@ -588,3 +622,3 @@ throw new Error( | ||
return new Promise(function Future$promise$do(resolve, reject){ | ||
_this.fork(reject, resolve); | ||
_this._f(reject, resolve); | ||
}); | ||
@@ -599,3 +633,2 @@ } | ||
const settleWith = newState => function Future$cache$settle(newValue){ | ||
check$cache$settle(state, newState, value, newValue); | ||
value = newValue; state = newState; | ||
@@ -609,2 +642,3 @@ for(let i = 0, l = que.length; i < l; i++){ | ||
return new FutureClass(function Future$cache$fork(rej, res){ | ||
let cancel = noop; | ||
switch(state){ | ||
@@ -617,4 +651,10 @@ case 1: que.push({2: rej, 3: res}); break; | ||
que.push({2: rej, 3: res}); | ||
_this.fork(settleWith(2), settleWith(3)); | ||
cancel = _this._f(settleWith(2), settleWith(3)); | ||
} | ||
return function Future$cache$cancel(){ | ||
que = []; | ||
value = undefined; | ||
state = undefined; | ||
cancel(); | ||
}; | ||
}); | ||
@@ -631,2 +671,3 @@ } | ||
chain: Future$chain, | ||
recur: Future$recur, | ||
chainRej: Future$chainRej, | ||
@@ -682,9 +723,6 @@ [FL.map]: Future$map, | ||
function createNullaryDispatcher(method){ | ||
const f = function nullaryDispatch(m){ | ||
return function nullaryDispatch(m){ | ||
if(m && typeof m[method] === 'function') return m[method](); | ||
error$invalidArgument(`Future.${method}`, 1, `have a "${method}" method`, m); | ||
}; | ||
f.toString = () => `function dispatch$${method}(m){ m.${method}() }`; | ||
f.inspect = () => `[Function: dispatch$${method}]`; | ||
return f; | ||
} | ||
@@ -694,10 +732,7 @@ | ||
function createUnaryDispatcher(method){ | ||
const f = function unaryDispatch(a, m){ | ||
if(arguments.length === 1) return unaryPartial(f, a); | ||
return function unaryDispatch(a, m){ | ||
if(arguments.length === 1) return unaryPartial(unaryDispatch, a); | ||
if(m && typeof m[method] === 'function') return m[method](a); | ||
error$invalidArgument(`Future.${method}`, 1, `have a "${method}" method`, m); | ||
}; | ||
f.toString = () => `function dispatch$${method}(a, m){ m.${method}(a) }`; | ||
f.inspect = () => `[Function: dispatch$${method}]`; | ||
return f; | ||
} | ||
@@ -707,10 +742,7 @@ | ||
function createInvertedUnaryDispatcher(method){ | ||
const f = function invertedUnaryDispatch(m, a){ | ||
if(arguments.length === 1) return unaryPartial(f, m); | ||
return function invertedUnaryDispatch(m, a){ | ||
if(arguments.length === 1) return unaryPartial(invertedUnaryDispatch, m); | ||
if(m && typeof m[method] === 'function') return m[method](a); | ||
error$invalidArgument(`Future.${method}`, 0, `have a "${method}" method`, m); | ||
}; | ||
f.toString = () => `function dispatch$${method}(m, a){ m.${method}(a) }`; | ||
f.inspect = () => `[Function: dispatch$${method}]`; | ||
return f; | ||
} | ||
@@ -720,11 +752,8 @@ | ||
function createBinaryDispatcher(method){ | ||
const f = function binaryDispatch(a, b, m){ | ||
if(arguments.length === 1) return unaryPartial(f, a); | ||
if(arguments.length === 2) return binaryPartial(f, a, b); | ||
return function binaryDispatch(a, b, m){ | ||
if(arguments.length === 1) return unaryPartial(binaryDispatch, a); | ||
if(arguments.length === 2) return binaryPartial(binaryDispatch, a, b); | ||
if(m && typeof m[method] === 'function') return m[method](a, b); | ||
error$invalidArgument(`Future.${method}`, 2, `have a "${method}" method`, m); | ||
}; | ||
f.toString = () => `function dispatch$${method}(a, b, m){ m.${method}(a, b) }`; | ||
f.inspect = () => `[Function: dispatch$${method}]`; | ||
return f; | ||
} | ||
@@ -734,14 +763,12 @@ | ||
function createInvertedBinaryDispatcher(method){ | ||
const f = function invertedBinaryDispatch(m, a, b){ | ||
if(arguments.length === 1) return unaryPartial(f, m); | ||
if(arguments.length === 2) return binaryPartial(f, m, a); | ||
return function invertedBinaryDispatch(m, a, b){ | ||
if(arguments.length === 1) return unaryPartial(invertedBinaryDispatch, m); | ||
if(arguments.length === 2) return binaryPartial(invertedBinaryDispatch, m, a); | ||
if(m && typeof m[method] === 'function') return m[method](a, b); | ||
error$invalidArgument(`Future.${method}`, 0, `have a "${method}" method`, m); | ||
}; | ||
f.toString = () => `function dispatch$${method}(m, a, b){ m.${method}(a, b) }`; | ||
f.inspect = () => `[Function: dispatch$${method}]`; | ||
return f; | ||
} | ||
Future.chain = createUnaryDispatcher('chain'); | ||
Future.recur = createUnaryDispatcher('recur'); | ||
Future.chainRej = createUnaryDispatcher('chainRej'); | ||
@@ -767,3 +794,2 @@ Future.map = createUnaryDispatcher('map'); | ||
Future.Guarded = Guarded; | ||
Future.isFuture = isFuture; | ||
@@ -782,4 +808,5 @@ Future.isForkable = isForkable; | ||
return new FutureClass(function Future$after$fork(rej, res){ | ||
setTimeout(res, n, x); | ||
}) | ||
const t = setTimeout(res, n, x); | ||
return function Future$after$cancel(){ clearTimeout(t) }; | ||
}); | ||
}; | ||
@@ -791,3 +818,3 @@ | ||
m.fork(rej, res); | ||
}); | ||
}, true); | ||
}; | ||
@@ -850,4 +877,6 @@ | ||
return new FutureClass(function Future$node$fork(rej, res){ | ||
f((a, b) => a ? rej(a) : res(b)); | ||
}); | ||
f(function Future$node$done(a, b){ | ||
a ? rej(a) : res(b); | ||
}); | ||
}, true); | ||
}; | ||
@@ -862,5 +891,6 @@ | ||
let ok = 0; | ||
const cs = []; | ||
const out = new Array(l); | ||
const next = j => i < l ? fork(ms[i], i++) : (j === l && res(out)); | ||
const fork = (m, j) => (check$parallel$m(m, j), m._f( | ||
const fork = (m, j) => (check$parallel$m(m, j), cs[j] = m._f( | ||
e => ko || (rej(e), ko = true), | ||
@@ -870,2 +900,3 @@ x => ko || (out[j] = x, next(++ok)) | ||
ms.slice(0, i).forEach(fork); | ||
return function Future$parallel$cancel(){ cs.forEach(call) }; | ||
}); | ||
@@ -884,3 +915,3 @@ }; | ||
}; | ||
return next().fork(rej, res); | ||
return next()._f(rej, res); | ||
}); | ||
@@ -887,0 +918,0 @@ }; |
{ | ||
"name": "fluture", | ||
"version": "1.2.1", | ||
"version": "2.0.0-beta.1", | ||
"description": "FantasyLand compliant (monadic) alternative to Promises", | ||
@@ -17,2 +17,3 @@ "main": "fluture.js", | ||
"test:opt": "node --allow-natives-syntax --trace-opt --trace-deopt --trace-inlining scripts/test-opt", | ||
"test:mem": "node scripts/test-mem", | ||
"test:unit": "node ./node_modules/.bin/_mocha --ui bdd --reporter spec --check-leaks --full-trace", | ||
@@ -53,3 +54,2 @@ "test:coverage": "node node_modules/.bin/istanbul cover --report html ./node_modules/.bin/_mocha -- --ui bdd --reporter dot --bail --check-leaks && codecov" | ||
"dependencies": { | ||
"fantasy-land": "^0.3.0", | ||
"inspect-f": "^1.1.0" | ||
@@ -63,2 +63,4 @@ }, | ||
"eslint": "^3.0.1", | ||
"fun-task": "^1.1.1", | ||
"heapdump": "^0.3.7", | ||
"istanbul": "^0.4.2", | ||
@@ -65,0 +67,0 @@ "jsverify": "^0.7.1", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1
766
29538
18
3
1
1
0
- Removedfantasy-land@^0.3.0
- Removeddaggy@0.0.1(transitive)
- Removedfantasy-combinators@0.0.1(transitive)
- Removedfantasy-land@0.3.0(transitive)