This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
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
1 parent
7927575
commit d961465
Showing
14 changed files
with
355 additions
and
203 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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
*.orig | ||
node_modules | ||
lib | ||
dist | ||
npm-debug.log | ||
docs/literate | ||
docs |
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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
syntax: glob | ||
*.orig | ||
node_modules | ||
lib | ||
dist | ||
npm-debug.log | ||
docs/literate | ||
docs |
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
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,23 @@ | ||
{ | ||
"source": { | ||
"include": [ "lib", "README.md" ] | ||
}, | ||
"plugins": [ "plugins/markdown" ], | ||
"markdown": { | ||
"parser": "gfm" | ||
}, | ||
"opts": { | ||
"destination": "./docs/", | ||
"template": "./node_modules/ink-docstrap/template" | ||
}, | ||
"templates": { | ||
"systemName": "control.async", | ||
"theme": "cerulean", | ||
"linenums": true, | ||
"navType": "vertical", | ||
"copyright": "© 2014 Quildreen Motta", | ||
"default": { | ||
"outputSourceFiles": 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,90 @@ | ||
// Copyright (c) 2014 Quildreen Motta <[email protected]> | ||
// | ||
// 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. | ||
|
||
/** | ||
* Core operations for asynchronous control flow. | ||
* | ||
* @module async/core | ||
*/ | ||
|
||
// -- Dependencies ----------------------------------------------------- | ||
var Future = require('data.future') | ||
|
||
|
||
// -- Implementation --------------------------------------------------- | ||
|
||
/** | ||
* Returns an action that always fails with the given data. | ||
* | ||
* @method | ||
* @summary α → Future[α, β] | ||
*/ | ||
exports.fail = fail | ||
function fail(a) { | ||
return new Future(function(reject, resolve) { reject(a) }) | ||
} | ||
|
||
|
||
/** | ||
* Resolves all futures in parallel, and collects all of their values. | ||
* | ||
* @method | ||
* @summary Array[Future[α, β]] → Future[α, Array[β]] | ||
*/ | ||
exports.parallel = parallel | ||
function parallel(xs) { return new Future(function(reject, resolve) { | ||
var len = xs.length | ||
var result = new Array(len) | ||
var resolved = false | ||
|
||
xs.forEach(runComputation) | ||
|
||
function runComputation(x, i) { | ||
return x.fork( function(e) { | ||
if (resolved) return | ||
resolved = true | ||
reject(e) } | ||
|
||
, function(v) { | ||
if (resolved) return | ||
result[i] = v | ||
len = len - 1 | ||
if (len === 0) { resolved = true | ||
resolve(result) }})} | ||
})} | ||
|
||
|
||
/** | ||
* Returns the value of the first resolved or rejected future. | ||
* | ||
* @method | ||
* @summary Array[Future[α, β]] → Future[α, β] | ||
*/ | ||
exports.nondeterministicChoice = nondeterministicChoice | ||
function nondeterministicChoice(xs){ return new Future(function(reject, resolve) { | ||
var resolved = false | ||
xs.forEach(function(x){ x.fork( function(e){ transition(reject, e) } | ||
, function(v){ transition(resolve, v) }) }) | ||
|
||
function transition(f, a) { | ||
if (!resolved) { resolved = true | ||
f(a) }} | ||
})} |
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,29 @@ | ||
// Copyright (c) 2014 Quildreen Motta <[email protected]> | ||
// | ||
// 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. | ||
|
||
/** | ||
* @module async | ||
*/ | ||
var extend = require('xtend') | ||
|
||
module.exports = extend( require('./core') | ||
, require('./time') | ||
) |
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,76 @@ | ||
// Copyright (c) 2014 Quildreen Motta <[email protected]> | ||
// | ||
// 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. | ||
|
||
/** | ||
* Asynchronous actions related to time. | ||
* | ||
* @module async/time | ||
*/ | ||
|
||
// -- Dependencies ----------------------------------------------------- | ||
var Future = require('data.future') | ||
var flaw = require('flaw') | ||
|
||
|
||
// -- Helpers ---------------------------------------------------------- | ||
|
||
/** | ||
* Timeout, in ms. | ||
* | ||
* @private | ||
* @summary Number → Error | ||
*/ | ||
function TimeoutError(n) { | ||
return flaw( 'TimeoutError' | ||
, 'Timeoutted after ' + n + ' milliseconds.') | ||
} | ||
|
||
// -- Implementation --------------------------------------------------- | ||
|
||
/** | ||
* Returns a future that gets resolved after N milliseconds. | ||
* | ||
* The value of the future will be the delta of the time from its execution to | ||
* the resolution. | ||
* | ||
* @method | ||
* @summary Number → Future[α, Number] | ||
*/ | ||
exports.delay = delay | ||
function delay(n) { return new Future(function(reject, resolve) { | ||
var s = new Date | ||
setTimeout( function(){ resolve(new Date - s) } | ||
, n) | ||
})} | ||
|
||
|
||
/** | ||
* Returns a future that fails after N milliseconds. | ||
* | ||
* @method | ||
* @summary Number → Future[TimeoutError, α] | ||
*/ | ||
exports.timeout = timeout | ||
function timeout(n) { return new Future(function(reject, resolve) { | ||
var s = new Date | ||
setTimeout( function(){ reject(TimeoutError(n)) } | ||
, n) | ||
})} |
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
Oops, something went wrong.