Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 864dc7d
Showing
7 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Copyright (c) 2013 Greg Spurrier | ||
|
||
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# PFunc | ||
> We want the func... gotta have that func. (with apologies to George Clinton) | ||
PFunc is a [Promises/A+](http://promises-aplus.github.com/promises-spec/) compliant promise implementation. PFunc's promise is a Node.js-style callback function accepting error and result arguments. When invoked, the promise is fulfilled or rejected based on the arguments. The promise also has fulfill() and reject() methods for more traditional resolution. | ||
|
||
[![Build Status](https://travis-ci.org/gregspurrier/pfunc.png)](https://travis-ci.org/gregspurrier/pfunc) | ||
|
||
## License | ||
PFunc is Copyright 2013 Greg Spurrier. It is released under the MIT license. See LICENSE.txt for details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
function create() { | ||
var state = 'pending'; | ||
var values; | ||
var downstreamPromises = []; | ||
|
||
function isPromise(p) { | ||
return p && p.then instanceof Function; | ||
} | ||
|
||
function invoke(cb, args, promise) { | ||
try { | ||
var result = cb.apply(null, args); | ||
if (isPromise(result)) { | ||
result.then(promise.fulfill, promise.reject); | ||
} else { | ||
promise.fulfill(result); | ||
} | ||
} catch(e) { | ||
promise.reject(e); | ||
} | ||
} | ||
|
||
function fulfillDownstream(ds, args) { | ||
if(ds.onFulfilled) { | ||
invoke(ds.onFulfilled, args, ds.promise); | ||
} else { | ||
// Cascade to the next promise | ||
ds.promise.fulfill.apply(null, args); | ||
} | ||
} | ||
|
||
function fulfill(val) { | ||
if (state === 'pending') { | ||
state = 'fulfilled'; | ||
values = arguments; | ||
downstreamPromises.forEach(function(ds) { | ||
fulfillDownstream(ds, values); | ||
}); | ||
} | ||
} | ||
|
||
function rejectDownstream(ds, args) { | ||
if(ds.onRejected) { | ||
invoke(ds.onRejected, args, ds.promise); | ||
} else { | ||
// Cascade to the next promise | ||
ds.promise.reject.apply(null, args); | ||
} | ||
} | ||
|
||
function reject() { | ||
if (state === 'pending') { | ||
state = 'rejected'; | ||
values = arguments; | ||
downstreamPromises.forEach(function(ds) { | ||
rejectDownstream(ds, values); | ||
}); | ||
} | ||
} | ||
|
||
var cbPromise = function(err) { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
fulfill.apply(null, Array.prototype.slice.call(arguments, 1)); | ||
} | ||
} | ||
cbPromise.then = function(onFulfilled, onRejected) { | ||
onFulfilled = onFulfilled instanceof Function && onFulfilled; | ||
onRejected = onRejected instanceof Function && onRejected; | ||
var nextPromise = create(); | ||
var ds = {onFulfilled: onFulfilled, | ||
onRejected: onRejected, | ||
promise: nextPromise} | ||
if (state === 'pending') { | ||
downstreamPromises.push(ds); | ||
} else if (state === 'fulfilled') { | ||
process.nextTick(fulfillDownstream.bind(null, ds, values)); | ||
} else { | ||
process.nextTick(rejectDownstream.bind(null, ds, values)); | ||
} | ||
return nextPromise; | ||
} | ||
cbPromise.fulfill = fulfill; | ||
cbPromise.reject = reject; | ||
return cbPromise; | ||
} | ||
|
||
module.exports.create = create; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "pfunc", | ||
"version": "0.0.0", | ||
"description": "A simple Promises/A+ compliant library for Node.js", | ||
"main": "lib/pfunc.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/gregspurrier/pfunc.git" | ||
}, | ||
"keywords": [ | ||
"promise" | ||
], | ||
"author": "Greg Spurrier", | ||
"license": "MIT", | ||
"readmeFilename": "README.md", | ||
"devDependencies": { | ||
"promises-aplus-tests": "*" | ||
}, | ||
"scripts": { | ||
"test": "promises-aplus-tests test/test_adapter.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
0 info it worked if it ends with ok | ||
1 verbose cli [ '/Users/greg/.nvm/v0.8.22/bin/node', | ||
1 verbose cli '/Users/greg/.nvm/v0.8.22/bin/npm', | ||
1 verbose cli 'test' ] | ||
2 info using npm@1.2.14 | ||
3 info using node@v0.8.22 | ||
4 verbose read json /Users/greg/proj/pfunc/test/package.json | ||
5 error Error: ENOENT, open '/Users/greg/proj/pfunc/test/package.json' | ||
6 error If you need help, you may report this log at: | ||
6 error <http://github.com/isaacs/npm/issues> | ||
6 error or email it to: | ||
6 error <npm-@googlegroups.com> | ||
7 error System Darwin 12.3.0 | ||
8 error command "/Users/greg/.nvm/v0.8.22/bin/node" "/Users/greg/.nvm/v0.8.22/bin/npm" "test" | ||
9 error cwd /Users/greg/proj/pfunc/test | ||
10 error node -v v0.8.22 | ||
11 error npm -v 1.2.14 | ||
12 error path /Users/greg/proj/pfunc/test/package.json | ||
13 error code ENOENT | ||
14 error errno 34 | ||
15 verbose exit [ 34, true ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var pfunc = require('../lib/pfunc'); | ||
|
||
// By testing the callback-style interface we also exercise the underlying | ||
// fulfill/reject-style interface. | ||
module.exports.pending = function() { | ||
var promise = pfunc.create(); | ||
return { | ||
promise: promise, | ||
fulfill: function(val) { promise(null, val); }, | ||
reject: function(val) { promise(val); } | ||
}; | ||
}; |