Skip to content

Commit

Permalink
Merge 09b0566 into 0f16a8e
Browse files Browse the repository at this point in the history
  • Loading branch information
bergus committed Jul 29, 2016
2 parents 0f16a8e + 09b0566 commit 098a97f
Show file tree
Hide file tree
Showing 30 changed files with 516 additions and 188 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
experiments/
node_modules/
coverage/
perf/logs/
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@
],
"scripts": {
"build-dist": "mkdirp dist && rollup -c",
"build": "npm run build-dist && uglifyjs -c 'warnings=false' -m -o dist/creed.min.js -- dist/creed.js",
"build-es6": "mkdirp dist && rollup -f cjs -o dist/creed.js src/main.js",
"build": "npm run build-dist && uglifyjs -c \"warnings=false\" -m -o dist/creed.min.js -- dist/creed.js",
"preversion": "npm run build",
"check-coverage": "istanbul check-coverage --statements 100 --branches 100 --lines 100 --functions 100 coverage/coverage*.json",
"lint": "jsinspect src && eslint src",
"pretest": "npm run lint",
"test": "istanbul cover _mocha",
"test": "istanbul cover node_modules/mocha/bin/_mocha",
"posttest": "npm run test-aplus",
"test-aplus": "promises-aplus-tests test/aplus.js --reporter dot"
},
Expand Down
2 changes: 1 addition & 1 deletion perf/doxbee-sequential-errors/promises-creed-algebraic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ global.useCreed = true;
global.useQ = false;
global.useBluebird = false;

var creed = require('../../dist/creed');
var creed = require('../..');

require('../lib/fakesP');

Expand Down
2 changes: 1 addition & 1 deletion perf/doxbee-sequential-errors/promises-creed-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ global.useCreed = true;
global.useQ = false;
global.useBluebird = false;

var creed = require('../../dist/creed');
var creed = require('../..');

require('../lib/fakesP');

Expand Down
2 changes: 1 addition & 1 deletion perf/doxbee-sequential-errors/promises-creed.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ global.useCreed = true;
global.useQ = false;
global.useBluebird = false;

var creed = require('../../dist/creed');
var creed = require('../..');

require('../lib/fakesP');

Expand Down
4 changes: 2 additions & 2 deletions perf/doxbee-sequential/promises-creed-algebraic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ global.useCreed = true;
global.useQ = false;
global.useBluebird = false;

var creed = require('../../dist/creed');
var creed = require('../..');

require('../lib/fakesP');

Expand Down Expand Up @@ -52,7 +52,7 @@ module.exports = function upload(stream, idOrPath, tag, done) {
}).chain(function() {
return File.whereUpdate({id: fileId}, {version: version.id})
.execWithin(tx);
}).map(function() {
}).then(function() {
tx.commit();
return done();
}, function(err) {
Expand Down
2 changes: 1 addition & 1 deletion perf/doxbee-sequential/promises-creed-generator.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
global.useBluebird = false;
global.useQ = false;
global.useCreed = true;
var creed = require('../../dist/creed');
var creed = require('../..');
require('../lib/fakesP');

module.exports = creed.coroutine(function* upload(stream, idOrPath, tag, done) {
Expand Down
2 changes: 1 addition & 1 deletion perf/doxbee-sequential/promises-creed.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ global.useCreed = true;
global.useQ = false;
global.useBluebird = false;

var creed = require('../../dist/creed');
var creed = require('../..');

require('../lib/fakesP');

Expand Down
2 changes: 1 addition & 1 deletion perf/lib/fakesP.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ else if (global.useNative) {
};
}
else if (global.useCreed) {
var lifter = require('../../dist/creed').fromNode;
var lifter = require('../..').fromNode;
}
else {
var lifter = require('when/node').lift;
Expand Down
2 changes: 1 addition & 1 deletion perf/madeup-parallel/promises-creed-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ global.useQ = false;
global.useWhen = false;

global.useCreed = true;
var creed = require('../../dist/creed');
var creed = require('../..');
require('../lib/fakesP');

module.exports = creed.coroutine(function* upload(stream, idOrPath, tag, done) {
Expand Down
2 changes: 1 addition & 1 deletion perf/madeup-parallel/promises-creed.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ global.useWhen = false;

global.useCreed = true;

var creed = require('../../dist/creed');
var creed = require('../..');

require('../lib/fakesP');

Expand Down
3 changes: 1 addition & 2 deletions perf/performance.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

var args = require('optimist').argv;

var path = require('path');

global.LIKELIHOOD_OF_REJECTION = args.e || 0.1;
Expand Down Expand Up @@ -138,7 +137,7 @@ function measure(files, requests, time, parg, callback) {
async.mapSeries(files, function(f, done) {
console.log("benchmarking", f);
var logFile = path.basename(f) + ".log";
var profileFlags = ["--prof", "--logfile=C:/etc/v8/" + logFile];
var profileFlags = ["--prof", "--logfile=logs/" + logFile];

var argsFork = [__filename,
'--n', requests,
Expand Down
29 changes: 29 additions & 0 deletions src/Action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export default class Action {
constructor (promise) {
this.promise = promise
}

// default onFulfilled action
/* istanbul ignore next */
fulfilled (p) {
this.promise._become(p)
}

// default onRejected action
rejected (p) {
this.promise._become(p)
return false
}

tryCall (f, x) {
let result
// test if `f` (and only it) throws
try {
result = f(x)
} catch (e) {
this.promise._reject(e)
return
} // else
this.handle(result)
}
}
26 changes: 9 additions & 17 deletions src/chain.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
import Action from './Action'
import maybeThenable from './maybeThenable'

export default function (f, p, promise) {
p._when(new Chain(f, promise))
return promise
}

class Chain {
class Chain extends Action {
constructor (f, promise) {
super(promise)
this.f = f
this.promise = promise
}

fulfilled (p) {
try {
runChain(this.f, p.value, this.promise)
} catch (e) {
this.promise._reject(e)
}
this.tryCall(this.f, p.value)
}

rejected (p) {
this.promise._become(p)
}
}
handle (y) {
if (!(maybeThenable(y) && typeof y.then === 'function')) {
this.promise._reject(new TypeError('f must return a promise'))
}

function runChain (f, x, p) {
const y = f(x)
if (!(maybeThenable(y) && typeof y.then === 'function')) {
throw new TypeError('f must return a promise')
this.promise._resolve(y)
}

p._resolve(y)
}
26 changes: 16 additions & 10 deletions src/coroutine.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,47 @@
import Action from './Action'

export default function (resolve, iterator, promise) {
new Coroutine(resolve, iterator, promise).run()
return promise
}

class Coroutine {
class Coroutine extends Action {
constructor (resolve, iterator, promise) {
super(promise)
this.resolve = resolve
this.iterator = iterator
this.promise = promise
this.generator = iterator
}

run () {
this.step(this.iterator.next, void 0)
this.tryStep(this.generator.next, void 0)
}

step (continuation, x) {
tryStep (resume, x) {
let result
// test if `resume` (and only it) throws
try {
this.handle(continuation.call(this.iterator, x))
result = resume.call(this.generator, x)
} catch (e) {
this.promise._reject(e)
}
return
} // else
this.handle(result)
}

handle (result) {
if (result.done) {
return this.promise._resolve(result.value)
}

this.resolve(result.value)._runAction(this)
this.resolve(result.value)._when(this)
}

fulfilled (ref) {
this.step(this.iterator.next, ref.value)
this.tryStep(this.generator.next, ref.value)
}

rejected (ref) {
this.step(this.iterator.throw, ref.value)
this.tryStep(this.generator.throw, ref.value)
return true
}
}
10 changes: 4 additions & 6 deletions src/delay.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import Action from './Action'

export default function (ms, p, promise) {
p._runAction(new Delay(ms, promise))
return promise
}

class Delay {
class Delay extends Action {
constructor (time, promise) {
super(promise)
this.time = time
this.promise = promise
}

fulfilled (p) {
/*global setTimeout*/
setTimeout(become, this.time, p, this.promise)
}

rejected (p) {
this.promise._become(p)
}
}

function become (p, promise) {
Expand Down
1 change: 1 addition & 0 deletions src/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function silenceError (p) {
p._runAction(silencer)
}

// implements Action
const silencer = {
fulfilled () {},
rejected: setHandled
Expand Down
23 changes: 14 additions & 9 deletions src/iterable.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isFulfilled, isRejected, silenceError } from './inspect'
import Action from './Action'
import maybeThenable from './maybeThenable'

export function resultsArray (iterable) {
Expand Down Expand Up @@ -58,18 +59,22 @@ function handleItem (resolve, handler, x, i, promise) {
} else if (isRejected(p)) {
handler.rejectAt(p, i, promise)
} else {
settleAt(p, handler, i, promise)
p._runAction(new Indexed(handler, i, promise))
}
}

function settleAt (p, handler, i, promise) {
p._runAction({handler, i, promise, fulfilled, rejected})
}
class Indexed extends Action {
constructor (handler, i, promise) {
super(promise)
this.i = i
this.handler = handler
}

function fulfilled (p) {
this.handler.fulfillAt(p, this.i, this.promise)
}
fulfilled (p) {
this.handler.fulfillAt(p, this.i, this.promise)
}

function rejected (p) {
return this.handler.rejectAt(p, this.i, this.promise)
rejected (p) {
return this.handler.rejectAt(p, this.i, this.promise)
}
}
4 changes: 2 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export {
// coroutine :: Generator e a -> (...* -> Promise e a)
// Make a coroutine from a promise-yielding generator
export function coroutine (generator) {
return function (...args) {
return function coroutinified (...args) {
return runGenerator(generator, this, args)
}
}
Expand All @@ -50,7 +50,7 @@ function runGenerator (generator, thisArg, args) {
// fromNode :: NodeApi e a -> (...args -> Promise e a)
// Turn a Node API into a promise API
export function fromNode (f) {
return function (...args) {
return function promisified (...args) {
return runResolver(_runNode, f, this, args, new Future())
}
}
Expand Down
17 changes: 7 additions & 10 deletions src/map.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import Action from './Action'

export default function (f, p, promise) {
p._when(new Map(f, promise))
return promise
}

class Map {
class Map extends Action {
constructor (f, promise) {
super(promise)
this.f = f
this.promise = promise
}

fulfilled (p) {
try {
const f = this.f
this.promise._fulfill(f(p.value))
} catch (e) {
this.promise._reject(e)
}
this.tryCall(this.f, p.value)
}

rejected (p) {
this.promise._become(p)
handle (result) {
this.promise._fulfill(result)
}
}

0 comments on commit 098a97f

Please sign in to comment.