Started on node.js+socket.io+mongoDB on the backend for more responsivnes

This commit is contained in:
KasperRT
2015-04-09 00:18:13 +02:00
parent 076f8e821f
commit a8a705bd77
1889 changed files with 322175 additions and 68 deletions

4
server/node_modules/monk/node_modules/mpromise/.npmignore generated vendored Executable file
View File

@@ -0,0 +1,4 @@
*.sw*
node_modules/
.DS_Store
.idea

4
server/node_modules/monk/node_modules/mpromise/.travis.yml generated vendored Executable file
View File

@@ -0,0 +1,4 @@
language: node_js
node_js:
- 0.8
- 0.10

74
server/node_modules/monk/node_modules/mpromise/History.md generated vendored Executable file
View File

@@ -0,0 +1,74 @@
0.5.1 / 2014-01-20
==================
* fixed; `end` is much more consistent (especially for `then` chains)
0.4.4 / 2014-01-20
==================
* fixed; `end` is much more consistent (especially for `then` chains)
0.4.3 / 2013-12-17
==================
* fixed; non-A+ behavior on fulfill and reject [lbeschastny](https://github.com/lbeschastny)
* tests; simplified harness + compatible with travis + compatible with windows
0.5.0 / 2013-12-14
==================
* fixed; non-A+ behavior on fulfill and reject [lbeschastny](https://github.com/lbeschastny)
* tests; simplified harness + compatible with travis + compatible with windows
0.4.2 / 2013-11-26
==================
* fixed; enter the domain only if not the present domain
* added; `end` returns the promise
0.4.1 / 2013-10-26
==================
* Add `all`
* Longjohn for easier debugging
* can end a promise chain with an error handler
* Add ```chain```
0.4.0 / 2013-10-24
==================
* fixed; now plays nice with domains #3 [refack](https://github.com/refack)
* updated; compatibility for Promises A+ 2.0.0 [refack](https://github.com/refack)
* updated; guard against invalid arguments [refack](https://github.com/refack)
0.3.0 / 2013-07-25
==================
* updated; sliced to 0.0.5
* fixed; then is passed all fulfillment values
* use setImmediate if available
* conform to Promises A+ 1.1
0.2.1 / 2013-02-09
==================
* fixed; conformancy with A+ 1.2
0.2.0 / 2013-01-09
==================
* added; .end()
* fixed; only catch handler executions
0.1.0 / 2013-01-08
==================
* cleaned up API
* customizable event names
* docs
0.0.1 / 2013-01-07
==================
* original release

22
server/node_modules/monk/node_modules/mpromise/LICENSE generated vendored Executable file
View File

@@ -0,0 +1,22 @@
(The MIT License)
Copyright (c) 2012 [Aaron Heckmann](aaron.heckmann+github@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

224
server/node_modules/monk/node_modules/mpromise/README.md generated vendored Executable file
View File

@@ -0,0 +1,224 @@
#mpromise
==========
[![Build Status](https://travis-ci.org/aheckmann/mpromise.png)](https://travis-ci.org/aheckmann/mpromise)
A [promises/A+](https://github.com/promises-aplus/promises-spec) conformant implementation, written for [mongoose](http://mongoosejs.com).
## installation
```
$ npm install mpromise
```
## docs
An `mpromise` can be in any of three states, pending, fulfilled (success), or rejected (error). Once it is either fulfilled or rejected it's state can no longer be changed.
The exports object is the Promise constructor.
```js
var Promise = require('mpromise');
```
The constructor accepts an optional function which is executed when the promise is first resolved (either fulfilled or rejected).
```js
var promise = new Promise(fn);
```
This is the same as passing the `fn` to `onResolve` directly.
```js
var promise = new Promise;
promise.onResolve(function (err, args..) {
...
});
```
### Methods
####fulfill
Fulfilling a promise with values:
```js
var promise = new Promise;
promise.fulfill(args...);
```
If the promise has already been fulfilled or rejected, no action is taken.
####reject
Rejecting a promise with a reason:
```js
var promise = new Promise;
promise.reject(reason);
```
If the promise has already been fulfilled or rejected, no action is taken.
####resolve
Node.js callback style promise resolution `(err, args...)`:
```js
var promise = new Promise;
promise.resolve([reason], [arg1, arg2, ...]);
```
If the promise has already been fulfilled or rejected, no action is taken.
####onFulfill
To register a function for execution when the promise is fulfilled, pass it to `onFulfill`. When executed it will receive the arguments passed to `fulfill()`.
```js
var promise = new Promise;
promise.onFulfill(function (a, b) {
assert.equal(3, a + b);
});
promise.fulfill(1, 2);
```
The function will only be called once when the promise is fulfilled, never when rejected.
Registering a function with `onFulfill` after the promise has already been fulfilled results in the immediate execution of the function with the original arguments used to fulfill the promise.
```js
var promise = new Promise;
promise.fulfill(" :D ");
promise.onFulfill(function (arg) {
console.log(arg); // logs " :D "
})
```
####onReject
To register a function for execution when the promise is rejected, pass it to `onReject`. When executed it will receive the argument passed to `reject()`.
```js
var promise = new Promise;
promise.onReject(function (reason) {
assert.equal('sad', reason);
});
promise.reject('sad');
```
The function will only be called once when the promise is rejected, never when fulfilled.
Registering a function with `onReject` after the promise has already been rejected results in the immediate execution of the function with the original argument used to reject the promise.
```js
var promise = new Promise;
promise.reject(" :( ");
promise.onReject(function (reason) {
console.log(reason); // logs " :( "
})
```
####onResolve
Allows registration of node.js style callbacks `(err, args..)` to handle either promise resolution type (fulfill or reject).
```js
// fulfillment
var promise = new Promise;
promise.onResolve(function (err, a, b) {
console.log(a + b); // logs 3
});
promise.fulfill(1, 2);
// rejection
var promise = new Promise;
promise.onResolve(function (err) {
if (err) {
console.log(err.message); // logs "failed"
}
});
promise.reject(new Error('failed'));
```
####then
Creates a new promise and returns it. If `onFulfill` or `onReject` are passed, they are added as SUCCESS/ERROR callbacks to this promise after the nextTick.
Conforms to [promises/A+](https://github.com/promises-aplus/promises-spec) specification and passes its [tests](https://github.com/promises-aplus/promises-tests).
```js
// promise.then(onFulfill, onReject);
var p = new Promise;
p.then(function (arg) {
return arg + 1;
}).then(function (arg) {
throw new Error(arg + ' is an error!');
}).then(null, function (err) {
assert.ok(err instanceof Error);
assert.equal('2 is an error', err.message);
});
p.fullfill(1);
```
####end
Signifies that this promise was the last in a chain of `then()s`: if a handler passed to the call to `then` which produced this promise throws, the exception be rethrown.
You can pass an OnReject handler to `end` so that exceptions will be handled (like a final catch clause);
This method returns it's promise for easy use with `return`.
```js
var p = new Promise;
p.then(function(){ throw new Error('shucks') });
setTimeout(function () {
p.fulfill();
// error was caught and swallowed by the promise returned from
// p.then(). we either have to always register handlers on
// the returned promises or we can do the following...
}, 10);
// this time we use .end() which prevents catching thrown errors
var p = new Promise;
setTimeout(function () {
p.fulfill(); // throws "shucks"
}, 10);
return p.then(function(){ throw new Error('shucks') }).end(); // <--
```
### chain
Allows direct promise to promise chaining (especially useful by a outside aggregating function). It doesn't use the asynchronous `resolve` algorithm and so excepts only another Promise as it's argument.
```js
function makeMeAPromise(i) {
var p = new Promise;
p.fulfill(i);
return p;
}
var returnPromise = initialPromise = new Promise;
for (i=0; i<10; ++i)
returnPromise = returnPromise.chain(makeMeAPromise(i));
initialPromise.fulfill();
return returnPromise;
```
###Event names
If you'd like to alter this implementations event names used to signify success and failure you may do so by setting `Promise.SUCCESS` or `Promise.FAILURE` respectively.
```js
Promise.SUCCESS = 'complete';
Promise.FAILURE = 'err';
```
###Luke, use the Source
For more ideas read the [source](https://github.com/aheckmann/mpromise/blob/master/lib), [tests](https://github.com/aheckmann/mpromise/blob/master/test), or the [mongoose implementation](https://github.com/LearnBoost/mongoose/blob/3.6x/lib/promise.js).
## license
[MIT](https://github.com/aheckmann/mpromise/blob/master/LICENSE)

1
server/node_modules/monk/node_modules/mpromise/index.js generated vendored Executable file
View File

@@ -0,0 +1 @@
module.exports = exports = require('./lib/promise');

459
server/node_modules/monk/node_modules/mpromise/lib/promise.js generated vendored Executable file
View File

@@ -0,0 +1,459 @@
'use strict';
/*!
* Module dependencies.
*/
var slice = function (arr, start, end) {
return Array.prototype.slice.call(arr, start, end)
};
var EventEmitter = require('events').EventEmitter;
/**
* Promise constructor.
*
* _NOTE: The success and failure event names can be overridden by setting `Promise.SUCCESS` and `Promise.FAILURE` respectively._
*
* @param {Function} back a function that accepts `fn(err, ...){}` as signature
* @inherits NodeJS EventEmitter http://nodejs.org/api/events.html#events_class_events_eventemitter
* @event `reject`: Emits when the promise is rejected (event name may be overridden)
* @event `fulfill`: Emits when the promise is fulfilled (event name may be overridden)
* @api public
*/
function Promise(back) {
EventEmitter.call(this);
this.emitted = {};
this.ended = false;
if ('function' == typeof back)
this.onResolve(back);
}
/*!
* event names
*/
Promise.SUCCESS = 'fulfill';
Promise.FAILURE = 'reject';
/*!
* Inherits from EventEmitter.
*/
Promise.prototype.__proto__ = EventEmitter.prototype;
/**
* Adds `listener` to the `event`.
*
* If `event` is either the success or failure event and the event has already been emitted, the`listener` is called immediately and passed the results of the original emitted event.
*
* @param {String} event
* @param {Function} callback
* @return {Promise} this
* @api public
*/
Promise.prototype.on = function (event, callback) {
if (this.emitted[event])
callback.apply(this, this.emitted[event]);
else
EventEmitter.prototype.on.call(this, event, callback);
return this;
}
/**
* Keeps track of emitted events to run them on `on`.
*
* @api private
*/
Promise.prototype.emit = function (event) {
// ensures a promise can't be fulfill() or reject() more than once
var success = this.constructor.SUCCESS;
var failure = this.constructor.FAILURE;
if (event == success || event == failure) {
if (this.emitted[success] || this.emitted[failure]) {
return this;
}
this.emitted[event] = slice(arguments, 1);
}
return EventEmitter.prototype.emit.apply(this, arguments);
}
/**
* Fulfills this promise with passed arguments.
*
* If this promise has already been fulfilled or rejected, no action is taken.
*
* @api public
*/
Promise.prototype.fulfill = function () {
var args = slice(arguments);
return this.emit.apply(this, [this.constructor.SUCCESS].concat(args));
}
/**
* Rejects this promise with `reason`.
*
* If this promise has already been fulfilled or rejected, no action is taken.
*
* @api public
* @param {Object|String} reason
* @return {Promise} this
*/
Promise.prototype.reject = function (reason) {
if (this.ended && !this.hasRejectListeners()) throw reason;
return this.emit(this.constructor.FAILURE, reason);
}
/**
* Resolves this promise to a rejected state if `err` is passed or
* fulfilled state if no `err` is passed.
*
* @param {Error} [err] error or null
* @param {Object} [val] value to fulfill the promise with
* @api public
*/
Promise.prototype.resolve = function (err, val) {
if (err) return this.reject(err);
return this.fulfill(val);
}
/**
* Adds a listener to the SUCCESS event.
*
* @return {Promise} this
* @api public
*/
Promise.prototype.onFulfill = function (fn) {
if (!fn) return this;
if ('function' != typeof fn) throw new TypeError("fn should be a function");
return this.on(this.constructor.SUCCESS, fn);
}
Promise.prototype.hasRejectListeners = function () {
return this.listeners(this.constructor.FAILURE).length > 0;
};
/**
* Adds a listener to the FAILURE event.
*
* @return {Promise} this
* @api public
*/
Promise.prototype.onReject = function (fn) {
if (!fn) return this;
if ('function' != typeof fn) throw new TypeError("fn should be a function");
return this.on(this.constructor.FAILURE, fn);
}
/**
* Adds a single function as a listener to both SUCCESS and FAILURE.
*
* It will be executed with traditional node.js argument position:
* function (err, args...) {}
*
* @param {Function} fn
* @return {Promise} this
*/
Promise.prototype.onResolve = function (fn) {
if (!fn) return this;
if ('function' != typeof fn) throw new TypeError("fn should be a function");
this.on(this.constructor.FAILURE, function (err) {
fn.apply(this, [err]);
});
this.on(this.constructor.SUCCESS, function () {
var args = slice(arguments);
fn.apply(this, [null].concat(args));
});
return this;
}
/**
* Creates a new promise and returns it. If `onFulfill` or
* `onReject` are passed, they are added as SUCCESS/ERROR callbacks
* to this promise after the next tick.
*
* Conforms to [promises/A+](https://github.com/promises-aplus/promises-spec) specification. Read for more detail how to use this method.
*
* ####Example:
*
* var p = new Promise;
* p.then(function (arg) {
* return arg + 1;
* }).then(function (arg) {
* throw new Error(arg + ' is an error!');
* }).then(null, function (err) {
* assert.ok(err instanceof Error);
* assert.equal('2 is an error', err.message);
* });
* p.complete(1);
*
* @see promises-A+ https://github.com/promises-aplus/promises-spec
* @param {Function} onFulFill
* @param {Function} [onReject]
* @return {Promise} newPromise
*/
Promise.prototype.then = function (onFulfill, onReject) {
var self = this
, retPromise = new Promise;
if ('function' == typeof onReject) {
self.onReject(handler(retPromise, onReject));
} else {
self.onReject(retPromise.reject.bind(retPromise));
}
if ('function' == typeof onFulfill) {
self.onFulfill(handler(retPromise, onFulfill));
} else {
self.onFulfill(retPromise.fulfill.bind(retPromise));
}
return retPromise;
};
function handler(retPromise, fn) {
return function handler() {
var args = arguments;
process.nextTick(
function in_the_handler() {
if (retPromise.domain && retPromise.domain !== process.domain) retPromise.domain.enter();
var x;
try {
x = fn.apply(undefined, args);
} catch (err) {
return retPromise.reject(err);
}
resolve(retPromise, x);
return;
}
);
}
}
function resolve(promise, x) {
var then;
var type;
var done;
var reject_;
var resolve_;
type = typeof x;
if ('undefined' == type) {
return promise.fulfill(x);
}
if (promise === x) {
return promise.reject(new TypeError("promise and x are the same"));
}
if (null != x) {
if ('object' == type || 'function' == type) {
try {
then = x.then;
} catch (err) {
return promise.reject(err);
}
if ('function' == typeof then) {
try {
resolve_ = function () {var args = slice(arguments); resolve.apply(this, [promise].concat(args));};
reject_ = promise.reject.bind(promise);
done = false;
return then.call(
x
, function fulfill() {
if (done) return;
done = true;
return resolve_.apply(this, arguments);
}
, function reject() {
if (done) return;
done = true;
return reject_.apply(this, arguments);
})
} catch (err) {
if (done) return;
done = true;
if (promise.ended) throw err;
return promise.reject(err);
}
}
}
}
promise.fulfill(x);
}
/**
* Signifies that this promise was the last in a chain of `then()s`: if a handler passed to the call to `then` which produced this promise throws, the exception will go uncaught.
*
* ####Example:
*
* var p = new Promise;
* p.then(function(){ throw new Error('shucks') });
* setTimeout(function () {
* p.fulfill();
* // error was caught and swallowed by the promise returned from
* // p.then(). we either have to always register handlers on
* // the returned promises or we can do the following...
* }, 10);
*
* // this time we use .end() which prevents catching thrown errors
* var p = new Promise;
* var p2 = p.then(function(){ throw new Error('shucks') }).end(); // <--
* setTimeout(function () {
* p.fulfill(); // throws "shucks"
* }, 10);
*
* @api public
* @param {Function} [onReject]
* @return {Promise} this
*/
Promise.prototype.end = function (onReject) {
this.onReject(onReject);
this.ended = true;
return this;
};
/**
* A debug utility function that adds handlers to a promise that will log some output to the `console`
*
* ####Example:
*
* var p = new Promise;
* p.then(function(){ throw new Error('shucks') });
* setTimeout(function () {
* p.fulfill();
* // error was caught and swallowed by the promise returned from
* // p.then(). we either have to always register handlers on
* // the returned promises or we can do the following...
* }, 10);
*
* // this time we use .end() which prevents catching thrown errors
* var p = new Promise;
* var p2 = p.then(function(){ throw new Error('shucks') }).end(); // <--
* setTimeout(function () {
* p.fulfill(); // throws "shucks"
* }, 10);
*
* @api public
* @param {Promise} p
* @param {String} name
* @return {Promise} this
*/
Promise.trace = function (p, name) {
p.then(
function () {
console.log("%s fulfill %j", name, slice(arguments));
}
,
function () {
console.log("%s reject %j", name, slice(arguments));
}
)
};
Promise.prototype.chain = function (p2) {
var p1 = this;
p1.onFulfill(p2.fulfill.bind(p2));
p1.onReject(p2.reject.bind(p2));
return p2;
};
Promise.deferred = function () {
var p = new Promise;
return {
promise: p,
reject: p.reject.bind(p),
fulfill: p.fulfill.bind(p),
callback: p.resolve.bind(p)
}
};
/*!
* Module exports.
*/
Promise.prototype.all = function (promiseOfArr) {
var pRet = new Promise;
this.then(promiseOfArr).then(
function (promiseArr) {
var count = 0;
var ret = [];
var errSentinel;
if (!promiseArr.length) pRet.resolve();
promiseArr.forEach(function (promise, index) {
if (errSentinel) return;
count++;
promise.then(
function (val) {
if (errSentinel) return;
ret[index] = val;
--count;
if (count == 0) pRet.fulfill(ret);
},
function (err) {
if (errSentinel) return;
errSentinel = err;
pRet.reject(err);
}
);
});
return pRet;
}
, pRet.reject.bind(pRet)
);
return pRet;
};
Promise.hook = function(arr) {
var p1 = new Promise;
var pFinal = new Promise;
var signalP = function () {
--count;
if (count == 0)
pFinal.fulfill();
return pFinal;
};
var count = 1;
var ps = p1;
arr.forEach(function (hook) {
ps = ps.then(
function () {
var p = new Promise;
count++;
hook(p.resolve.bind(p), signalP);
return p;
}
)
});
ps = ps.then(signalP);
p1.resolve();
return ps;
};
module.exports = Promise;

54
server/node_modules/monk/node_modules/mpromise/package.json generated vendored Executable file
View File

@@ -0,0 +1,54 @@
{
"name": "mpromise",
"version": "0.5.1",
"description": "Promises A+ conformant implementation",
"main": "index.js",
"scripts": {
"test": "node node_modules/mocha/bin/_mocha"
},
"devDependencies": {
"longjohn": "0.2.2",
"promises-aplus-tests": "2.0.3",
"mocha": "1.13.0"
},
"repository": {
"type": "git",
"url": "git://github.com/aheckmann/mpromise"
},
"keywords": [
"promise",
"mongoose",
"aplus",
"a+",
"plus"
],
"author": {
"name": "Aaron Heckmann",
"email": "aaron.heckmann+github@gmail.com"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/aheckmann/mpromise/issues"
},
"homepage": "https://github.com/aheckmann/mpromise",
"_id": "mpromise@0.5.1",
"dist": {
"shasum": "ef1890a5c24e8b239f009474f0a65a040cca0e61",
"tarball": "http://registry.npmjs.org/mpromise/-/mpromise-0.5.1.tgz"
},
"_from": "mpromise@0.5.1",
"_npmVersion": "1.3.23",
"_npmUser": {
"name": "refack",
"email": "refael@empeeric.com"
},
"maintainers": [
{
"name": "refack",
"email": "refael@empeeric.com"
}
],
"directories": {},
"_shasum": "ef1890a5c24e8b239f009474f0a65a040cca0e61",
"_resolved": "https://registry.npmjs.org/mpromise/-/mpromise-0.5.1.tgz"
}

View File

@@ -0,0 +1,32 @@
var Promise = require('../')
, Domain = require('domain')
, assert = require('assert');
var next = 'function' == typeof setImmediate
? setImmediate
: process.nextTick;
describe("domains", function () {
it("exceptions should not breakout of domain bounderies", function (done) {
if (process.version.indexOf('v0.8') == 0) return done();
var d = Domain.create();
d.once('error', function (err) {
assert.equal(err.message, 'gaga');
done()
});
var p = new Promise();
d.run(function () {
p.then(function () {
}).then(function () {
throw new Error('gaga');
}).end();
});
next(function () {
p.fulfill();
})
});
});

View File

@@ -0,0 +1,494 @@
/*global describe,it */
if (process.version.indexOf('v0.11') == -1) require("longjohn");
/**
* Module dependencies.
*/
var assert = require('assert');
var Promise = require('../');
/**
* Test.
*/
describe('promise', function(){
it('events fire right after fulfill()', function(done){
var promise = new Promise()
, called = 0;
promise.on('fulfill', function (a, b) {
assert.equal(a, '1');
assert.equal(b, '2');
called++;
});
promise.fulfill('1', '2');
promise.on('fulfill', function (a, b) {
assert.equal(a, '1');
assert.equal(b, '2');
called++;
});
assert.equal(2, called);
done();
});
it('events fire right after reject()', function(done){
var promise = new Promise()
, called = 0;
promise.on('reject', function (err) {
assert.ok(err instanceof Error);
called++;
});
promise.reject(new Error('booyah'));
promise.on('reject', function (err) {
assert.ok(err instanceof Error);
called++;
});
assert.equal(2, called);
done()
});
describe('onResolve()', function(){
it('from constructor works', function(done){
var called = 0;
var promise = new Promise(function (err) {
assert.ok(err instanceof Error);
called++;
})
promise.reject(new Error('dawg'));
assert.equal(1, called);
done();
});
it('after fulfill()', function(done){
var promise = new Promise()
, called = 0;
promise.fulfill('woot');
promise.onResolve(function (err, data){
assert.equal(data,'woot');
called++;
});
promise.onResolve(function (err, data){
assert.strictEqual(err, null);
called++;
});
assert.equal(2, called);
done();
})
});
describe('onFulfill shortcut', function(){
it('works', function(done){
var promise = new Promise()
, called = 0;
promise.onFulfill(function (woot) {
assert.strictEqual(woot, undefined);
called++;
});
promise.fulfill();
assert.equal(1, called);
done();
})
})
describe('onReject shortcut', function(){
it('works', function(done){
var promise = new Promise()
, called = 0;
promise.onReject(function (err) {
assert.ok(err instanceof Error);
called++;
});
promise.reject(new Error);
assert.equal(1, called);
done();
})
});
describe('return values', function(){
it('on()', function(done){
var promise = new Promise()
assert.ok(promise.on('jump', function(){}) instanceof Promise);
done()
});
it('onFulfill()', function(done){
var promise = new Promise()
assert.ok(promise.onFulfill(function(){}) instanceof Promise);
done();
})
it('onReject()', function(done){
var promise = new Promise()
assert.ok(promise.onReject(function(){}) instanceof Promise);
done();
})
it('onResolve()', function(done){
var promise = new Promise()
assert.ok(promise.onResolve(function(){}) instanceof Promise);
done();
})
})
describe('casting errors', function(){
describe('reject()', function(){
it('does not cast arguments to Error', function(done){
var p = new Promise(function (err, arg) {
assert.equal(3, err);
done();
});
p.reject(3);
})
})
})
describe('then', function(){
describe('catching', function(){
it('should not catch returned promise fulfillments', function(done){
var errorSentinal
, p = new Promise
, p2 = p.then(function () { throw errorSentinal = new Error("boo!") });
p.fulfill();
done();
});
it('should not catch returned promise fulfillments even async', function (done) {
var errorSentinal
, p = new Promise
, p2 = p.then(function () { throw errorSentinal = new Error("boo!") });
setTimeout(function () {
p.fulfill();
done();
}, 10);
});
it('can be disabled using .end()', function(done){
if (process.version.indexOf('v0.8') == 0) return done();
var errorSentinal
, overTimeout
, domain = require('domain').create();
domain.once('error', function (err) {
assert(err, errorSentinal);
clearTimeout(overTimeout);
done()
});
domain.run(function () {
var p = new Promise;
var p2 = p.then(function () {
throw errorSentinal = new Error('shucks')
});
p2.end();
p.fulfill();
});
overTimeout = setTimeout(function () { done(new Error('error was swallowed')); }, 10);
});
it('can be disabled using .end() even when async', function (done) {
if (process.version.indexOf('v0.8') == 0) return done();
var errorSentinal
, overTimeout
, domain = require('domain').create();
domain.on('error', function (err) {
assert(err, errorSentinal);
clearTimeout(overTimeout);
done()
});
domain.run(function () {
var p = new Promise;
var p2 = p.then(function () {
throw errorSentinal = new Error("boo!")
});
p2.end();
setTimeout(function () {p.fulfill();}, 10);
});
overTimeout = setTimeout(function () { done(new Error('error was swallowed')); }, 20);
});
it('can be handled using .end() so no throwing', function (done) {
var errorSentinal
, overTimeout
, domain = require('domain').create();
domain.run(function () {
var p = new Promise;
var p2 = p.then(function () {
throw errorSentinal = new Error("boo!")
});
p2.end(function (err) {
assert.equal(err, errorSentinal);
clearTimeout(overTimeout);
done()
});
setTimeout(function () {p.fulfill();}, 10);
});
overTimeout = setTimeout(function () { done(new Error('error was swallowed')); }, 20);
});
});
it('persistent', function(done){
var p = new Promise
v = null;
function ensure (val) {
v = v || val;
assert.equal(v, val);
}
function guard () {
throw new Error('onReject should not be called');
}
p.then(ensure, guard).end();
p.fulfill('foo');
p.fulfill('bar');
p.reject(new Error('baz'));
p.then(ensure, guard).end();
setTimeout(done, 0);
});
it('accepts multiple completion values', function(done){
var p = new Promise;
p.then(function (a, b) {
assert.equal(2, arguments.length);
assert.equal('hi', a);
assert.equal(4, b);
done();
}, done).end();
p.fulfill('hi', 4);
})
});
describe('end', function () {
it("should return the promise", function (done) {
var p = new Promise;
var p1 = p.end();
assert.equal(p, p1);
done();
});
it("should throw for chain", function (done) {
var p = new Promise;
p.then().then().then().then().end();
try {
p.reject('bad');
} catch (e) {
done();
}
});
it("should not throw for chain with reject handler", function (done) {
var p = new Promise;
p.then().then().then().then().end(function () {
done();
});
try {
p.reject('bad');
} catch (e) {
done(e);
}
});
});
describe('chain', function () {
it('should propagate fulfillment', function (done) {
var varSentinel = {a:'a'};
var p1 = new Promise;
var p2 = p1.chain(new Promise(function (err, doc) {
assert.equal(doc, varSentinel);
done();
}));
p1.fulfill(varSentinel);
});
it('should propagate rejection', function (done) {
var e = new Error("gaga");
var p1 = new Promise;
var p2 = p1.chain(new Promise(function (err) {
assert.equal(err, e);
done();
}));
p1.reject(e);
});
it('should propagate resolution err', function (done) {
var e = new Error("gaga");
var p1 = new Promise;
var p2 = p1.chain(new Promise(function (err) {
assert.equal(err, e);
done();
}));
p1.resolve(e);
});
it('should propagate resolution val', function (done) {
var varSentinel = {a:'a'};
var p1 = new Promise;
var p2 = p1.chain(new Promise(function (err, val) {
assert.equal(val, varSentinel);
done();
}));
p1.resolve(null, varSentinel);
})
});
describe("all", function () {
it("works", function (done) {
var count = 0;
var p = new Promise;
var p2 = p.all(function () {
return [
(function () {var p = new Promise(); count++ ;p.resolve(); return p;})()
, (function () {var p = new Promise(); count++ ;p.resolve(); return p;})()
];
});
p2.then(function () {
assert.equal(count, 2);
done();
});
p.resolve();
});
it("handles rejects", function (done) {
var count = 0;
var p = new Promise;
var p2 = p.all(function () {
return [
(function () {var p = new Promise(); count++; p.resolve(); return p;})()
, (function () {var p = new Promise(); count++; throw new Error("gaga");})()
];
});
p2.onReject(function (err) {
assert(err.message, "gaga");
assert.equal(count, 2);
done();
});
p.resolve();
});
});
describe("deferred", function () {
it("works", function (done) {
var d = Promise.deferred();
assert.ok(d.promise instanceof Promise);
assert.ok(d.reject instanceof Function);
assert.ok(d.fulfill instanceof Function);
assert.ok(d.callback instanceof Function);
done();
});
});
describe("hook", function () {
it("works", function (done) {
var run = 0;
var l1 = function (ser, par) {
run++;
ser();
par();
};
Promise.hook([l1, l1, l1]).then(function () {
assert(run, 3);
done();
})
});
it("works with async serial hooks", function (done) {
this.timeout(800);
var run = 0;
var l1 = function (ser, par) {
run++;
setTimeout(function () {ser();}, 200);
par();
};
Promise.hook([l1, l1, l1]).then(function () {
assert(run, 3);
done();
})
});
it("works with async parallel hooks", function (done) {
this.timeout(400);
var run = 0;
var l1 = function (ser, par) {
run++;
ser();
setTimeout(function () {par();}, 200);
};
Promise.hook([l1, l1, l1]).then(function () {
assert(run, 3);
done();
})
});
it("catches errors in hook logic", function (done) {
var run = 0;
var l1 = function (ser, par) {
run++;
ser();
par();
};
var l2 = function (ser, par) {
run++;
ser();
par();
throw new Error("err")
};
Promise.hook([l1, l2, l1]).end(function (err) {
assert(run, 2);
done();
});
});
});
});

View File

@@ -0,0 +1,39 @@
/**
* Module dependencies.
*/
var Promise = require('../lib/promise');
var aplus = require('promises-aplus-tests');
// tests
var adapter = {};
adapter.fulfilled = function (value) {
var p = new Promise;
p.fulfill(value);
return p;
};
adapter.rejected = function (reason) {
var p = new Promise;
p.reject(reason);
return p;
}
adapter.deferred = function () {
var p = new Promise;
return {
promise: p, reject: p.reject.bind(p), resolve: p.fulfill.bind(p)
}
}
it("run A+ suite", function (done) {
this.timeout(60000);
aplus(adapter, {
reporter: 'dot', slow: 1
// , bail:true
// , grep:'2.3.1: If `promise` and `x` refer to the same object, reject `promise` with a `TypeError` as the reason. via return from a fulfilled promise'
}, function (err) {
done(err);
});
});