Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Handles #9

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ experiments/
node_modules/
build/
coverage/
perf/logs/
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"jsnext:main": "dist/creed.es.js",
"files": [
"dist/creed.js",
"dist/creed.es.js"
"dist/creed.es.js",
"dist/creed.node.js"
],
"repository": {
"type": "git",
Expand All @@ -28,13 +29,13 @@
"scripts": {
"compile": "npm run compile-src",
"compile-src": "mkdirp build/src && buble -m -i src -o build/src --no modules",
"build-dist": "npm run compile && mkdirp dist && rollup -c",
"build": "npm run build-dist && uglifyjs -c 'warnings=false' -m -o dist/creed.min.js -- dist/creed.js",
"build-dist": "npm run compile && mkdirp dist && rollup -c && rollup -f cjs -o dist/creed.node.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
57 changes: 57 additions & 0 deletions src/Action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Handle, ShareHandle } from './Handle'

export default class Action extends Handle {
constructor (promise) {
super(null) // ref will be set when used as handle
this.promise = promise
}

_concat (action) {
if (!action._isReused() && (this._isReused() || action instanceof ShareHandle)) {
return action._concat(this)
} else {
return new ShareHandle(this.ref)._concat(this)._concat(action)
}
}
run () {
const settled = this.ref
if (this._isReused()) {
this.ref = null // make action reusable elsewhere
}
settled._runAction(this)
}

// 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
try {
result = f(x)
} catch (e) {
this.promise._reject(e)
return
} // else
this.handle(result)
}

tryCallContext (f, c, x) {
let result
try {
result = f.call(c, x)
} catch (e) {
this.promise._reject(e)
return
} // else
this.handle(result)
}
}
2 changes: 1 addition & 1 deletion src/Any.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { silenceError } from './inspect.js'
import { silenceError } from './Promise' // deferred

export default class Any {
constructor () {
Expand Down
36 changes: 19 additions & 17 deletions src/ErrorHandler.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { silenceError, isHandled } from './inspect'
import { silenceError } from './Promise' // deferred
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh @coveralls, why don't you like circular dependencies? Everything builds, tests and covers fine on my machine.

import { isHandled } from './inspect'

const UNHANDLED_REJECTION = 'unhandledRejection'
const HANDLED_REJECTION = 'rejectionHandled'
export const UNHANDLED_REJECTION = 'unhandledRejection'
export const HANDLED_REJECTION = 'rejectionHandled'

export default class ErrorHandler {
constructor (emitEvent, reportError) {
this.errors = []
this.emit = emitEvent
this.reportError = reportError
this.report = () => this._reportErrors()
}

track (e) {
if (!this.emit(UNHANDLED_REJECTION, e, e.value)) {
/* istanbul ignore else */
if (this.errors.length === 0) {
setTimeout(reportErrors, 1, this.reportError, this.errors)
setTimeout(this.report, 1)
}
this.errors.push(e)
}
Expand All @@ -24,22 +26,22 @@ export default class ErrorHandler {
silenceError(e)
this.emit(HANDLED_REJECTION, e)
}
}

function reportErrors (report, errors) {
try {
reportAll(errors, report)
} finally {
errors.length = 0
_reportErrors () {
try {
this._reportAll(this.errors)
} finally {
this.errors.length = 0
}
}
}

function reportAll (errors, report) {
for (let i = 0; i < errors.length; ++i) {
const e = errors[i]
/* istanbul ignore else */
if (!isHandled(e)) {
report(e)
_reportAll (errors) {
for (let i = 0; i < errors.length; ++i) {
const e = errors[i]
/* istanbul ignore else */
if (!isHandled(e)) {
this.reportError(e)
}
}
}
}
33 changes: 33 additions & 0 deletions src/Handle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export class Handle {
constructor (ref) {
this.ref = ref // a ShareHandle, known Promise or unresolved Future
}
_getRef () {
return this.ref
}
// the ref will be lost, e.g. when an action is used multiple times
_isReused () {
return false // ref is stable by default
}
}

export class ShareHandle extends Handle {
constructor (ref) {
// assert: ref != null
super(ref)
this.length = 0
}
_concat (action) {
action.ref = this
this[this.length++] = action
// potential for flattening the tree here
return this
}
run () {
for (let i = 0; i < this.length; ++i) {
this.ref._runAction(this[i])
this[i] = void 0
}
this.length = 0
}
}
Loading