Socket
Socket
Sign inDemoInstall

oath

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

oath - npm Package Compare versions

Comparing version 0.2.3 to 1.0.0

examples/co.js

11

History.md
1.0.0 / 2013-12-29
==================
* pkg/docs: update description
* travis: no nextgen node
* Merge branch 'refactor/thunks'
* ignore: modern
* travis: modern node
* code cleanup
* rewrite as thunks
0.2.3 / 2012-01-30

@@ -3,0 +14,0 @@ ==================

185

index.js

@@ -1,1 +0,184 @@

module.exports = require('./lib/oath');
/*!
* Oath - Node.js / browser event emitter.
* Copyright(c) 2011 Jake Luer <jake@alogicalparadox.com>
* MIT Licensed
*/
/*!
* Module dependencies
*/
var assert = require('simple-assert');
/*!
* Primary exports
*/
var exports = module.exports = createThunk;
/**
* ### thunk(cb, [ctx])
*
* Create a thunk that can later be "completed".
*
* ```js
* function async(cb) {
* var done = thunk(cb);
* setImmediate(done);
* return done.thunk;
* }
*
* var res = async(next);
* res(alsoNext);
* ```
*
* @param {Function} first callback
* @param {Object} context to invoke callback(s) with
* @return {Function} completed handle
* @api public
*/
function createThunk(cb, ctx) {
var t = {
ctx: ctx || null,
res: null,
progress: { total: null, complete: null },
listeners: { complete: [], error: [], progress: [] }
};
/*!
* Scoped `.call` for invoke the callback chain.
* Returns a function that is to be called with
* an `{Array}` of callbacks to invoke.
*
* @param {Object} store
* @return {Function}
* @api private
*/
var call = (function(t) {
return function call(fns) {
for(var i = 0; i < fns.length; i++) {
fns[i].apply(t.ctx, t.res);
}
};
})(t);
/**
* #### done(err, [...])
*
* The function returned from `thunk` signals
* the completion of the thunk and is to be used
* within the async function that the thunk represents.
*
* @param {Error|null} if error
* @param {Mixed} repeatable result
* @api public
*/
var done = (function(t) {
return function thunk() {
assert(!Array.isArray(t.res), 'thunk has already completed');
var argv = t.res = [].slice.call(arguments);
var q = argv[0] ? t.listeners.error : t.listeners.complete;
return call(q);
};
})(t);
done.progress = (function(t) {
return function(total, current) {
t.progess = { total: total, current: current };
call(t.listeners.progress);
return this;
}
})(t);
/**
* #### done.thunk(cb)
*
* This method is used to stack callbacks
* for invocation upon completion. A function/method
* implementing thunks should return this function.
*
* @param {Function} callback to add to stack
* @api public
*/
done.thunk = (function(t) {
function addListener(ev, fn) {
assert(~[ 'error', 'complete', 'progress' ].indexOf(ev), 'invalid event listener key');
assert('function' === typeof fn, 'invalid event listener callback');
t.listeners[ev].push(fn);
}
function Oath(fn) {
if (!fn) return Oath;
if (Array.isArray(t.res)) {
assert('function' === typeof fn, 'invalid event listener callback');
call([ fn ]);
} else {
addListener('complete', fn);
addListener('error', fn);
}
return Oath;
}
Object.defineProperty(Oath, 'progress', {
get: function() { return t.progress; }
});
Object.defineProperty(Oath, 'onerror', {
set: function(fn) { addListener('error', fn); }
});
Object.defineProperty(Oath, 'oncomplete', {
set: function(fn) { addListener('complete', fn); }
});
Object.defineProperty(Oath, 'onprogress', {
set: function(fn) { addListener('progress', fn); }
});
return Oath;
})(t);
if (cb) done.thunk(cb);
return done;
}
/**
* ### thunk.wrap(method, [source context], [target context])
*
* Convert a node-compatible standard async function
* to a thunk-style function.
*
* ```js
* // no context required
* var read = thunk.wrap(fs.read);
*
* // contexts required
* function Server() {
* var serv = this._handle = http.createServer();
* this.listen = thunk.wrap(serv.listen, serv, this);
* }
* ```
*
* @param {Function} function or method
* @param {Mixed} source context (for method)
* @param {Mixed} target context (for callbacks)
* @return {Function} add callback to stack
* @api public
*/
exports.wrap = function(method, sctx, tctx) {
return function() {
var argv = [].slice.call(arguments);
var done = createThunk(null, tctx);
argv[argv.length] = done;
sctx = sctx || null;
method.apply(sctx, argv);
return done.thunk;
};
};

19

package.json
{
"name": "oath",
"version": "1.0.0",
"description": "Thunk-based flow control with fallback support for callbacks.",
"author": "Jake Luer <jake@alogicalparadox.com>",
"name": "oath",
"description": "Tiny library for node and the browser that makes it easy to build and interact with promise/future based APIs.",
"version": "0.2.3",
"homepage": "http://alogicalparadox.com/oath",

@@ -13,13 +13,14 @@ "repository": {

"scripts": {
"test": "make test"
"test": "node_modules/goodwin/bin/goodwin"
},
"engines": {
"node": ">= 0.4.8"
"node": ">= 0.8.0"
},
"dependencies": {},
"dependencies": {
"simple-assert": "~1.0.0"
},
"devDependencies": {
"mocha": "*",
"chai": "*",
"folio": "0.1.x"
"co": "~3.0.1",
"goodwin": "https://github.com/logicalparadox/goodwin/tarball/master"
}
}

@@ -1,20 +0,6 @@

[![Build Status](https://secure.travis-ci.org/logicalparadox/oath.png)](http://travis-ci.org/logicalparadox/oath)
# Oath [![Build Status](https://secure.travis-ci.org/logicalparadox/oath.png)](http://travis-ci.org/logicalparadox/oath)
# Oath
> Thunk-based flow control with fallback support for callbacks. Primarily for use
> with [co](https://github.com/visionmedia/co).
Oath is a tiny javascript library for [node](http://nodejs.org) and the browser that makes
it easy to build and interact with promise/future based APIs.
## What is a future/promise anyway?
A future (or promise), is an alternative method to callbacks when working with asyncronous
code. For more information check out the very information Wikipedia article
on [Futures and Promises](http://en.wikipedia.org/wiki/Futures_and_promises).
## About version 0.2.x
Version 0.2.x and the master branch represent a shift in oath's core fucus. Instead of providing helper
functions for manipulating the result set and deep chaining, this version allows you to manipulate the
result set directly, and provides helpers if this is to be done asyncronously. Docs coming shortly.
## Installation

@@ -30,20 +16,2 @@

### Browser
Download the package and include either the normal or minimized build in your HTML header.
<script src="/public/js/oath.js" type="text/javascript"></script>
<script src="/public/js/oath.min.js" type="text/javascript"></script>
## Help, resources, and issues?
* The annotated source / full API documentation for versions 0.1.x is available at [alogicalparadox.com/oath](http://alogicalparadox.com/oath/).
* If you have questions or issues, please use this projects [Github Issues](https://github.com/logicalparadox/oath/issues).
## Contributors
Interested in contributing? Fork to get started. Contact [@logicalparadox](http://github.com/logicalparadox) if you are interested in being a regular contributor.
* Jake Luer [[Github: @logicalparadox](http://github.com/logicalparadox)] [[Twitter: @jakeluer](http://twitter.com/jakeluer)] [[Website](http://alogicalparadox.com)]
## License

@@ -53,3 +21,3 @@

Copyright (c) 2011 Jake Luer <jake@alogicalparadox.com>
Copyright (c) 2011-2014 Jake Luer <jake@alogicalparadox.com>

@@ -72,2 +40,2 @@ Permission is hereby granted, free of charge, to any person obtaining a copy

OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc