Socket
Socket
Sign inDemoInstall

switchback

Package Overview
Dependencies
Maintainers
2
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

switchback - npm Package Compare versions

Comparing version 1.0.1 to 1.1.0

42

lib/index.js

@@ -22,2 +22,3 @@ /**

* - a switchback definition obj or a standard 1|2-ary node callback function.
*
* @param {Object} [defaultHandlers]

@@ -27,14 +28,30 @@ * - '*': supply a special callback for when none of the other handlers match

* - otherwise a function should be supplied, e.g. { 'error': res.serverError }
*
* @param {Object} [callbackContext]
* - optional `this` context for callbacks
*
* @param {Boolean} immediate
* - optional don't wait for setTimeout(0) before triggering callback.
* this breaks event-emitter-style usage.
* defaults to false
*/
var switchback = function(callback, defaultHandlers, callbackContext) {
var switchback = function(callback, defaultHandlers, callbackContext, immediate) {
/**
* @api private
* @param {Function} cb [description]
*/
function _maybeWaitForNextTick(cb){
if (immediate) return cb();
setTimeout(cb, 0);
}
// Track whether a single tick of the event loop has elapsed yet since
// this switchback was instantiated.
var atLeastOneTickHasElapsed;
setTimeout(function (){
_maybeWaitForNextTick(function (){
atLeastOneTickHasElapsed = true;
}, 0);
});

@@ -61,8 +78,15 @@ // Build switchback

else {
setTimeout(function (){
_maybeWaitForNextTick(function (){
return e.emit.apply(e, args);
}, 0);
});
}
};
Switchback.on = function(evName, handler) {
if (immediate) {
throw (function (){
var _err = new Error('Switchback can not be used as an EventEmitter (i.e. `.on()`) when 4th argument (`immediate`) is set to true.');
_err.code = 'SWITCHBACK:E_USAGE';
return _err;
})();
}
return e.on.apply(e, Array.prototype.slice.call(arguments));

@@ -147,5 +171,5 @@ };

else {
setTimeout(function (){
_maybeWaitForNextTick(function (){
runtimeHandler.apply(runtimeCtx, runtimeArgs);
}, 0);
});
}

@@ -178,2 +202,6 @@

module.exports = switchback;

2

package.json
{
"name": "switchback",
"version": "1.0.1",
"version": "1.1.0",
"description": "Normalize callback fns to switchbacks and vice versa",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

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

# [<img title="switchback - Delinearizes flow control into a more realistic directed graph" src="http://i.imgur.com/Jgrc9k2.png" width="75px" alt="image of a mountain switchback"/>](https://github.com/balderdashy/switchback) Switchback
# [<img title="switchback - Delinearizes flow control into a more realistic directed graph" src="http://i.imgur.com/Jgrc9k2.png" width="75px" alt="image of a mountain switchback"/>](https://github.com/node-machine/switchback) Switchback
[![Bower version](https://badge.fury.io/bo/switchback.png)](http://badge.fury.io/bo/switchback)
[![NPM version](https://badge.fury.io/js/node-switchback.png)](http://badge.fury.io/js/node-switchback) &nbsp; &nbsp;
[![Build Status](https://travis-ci.org/balderdashy/switchback.svg?branch=master)](https://travis-ci.org/balderdashy/switchback)
[![NPM version](https://badge.fury.io/js/switchback.png)](http://badge.fury.io/js/switchback) &nbsp; &nbsp;
[![Build Status](https://travis-ci.org/node-machine/switchback.svg?branch=master)](https://travis-ci.org/node-machine/switchback)

@@ -97,3 +97,3 @@ Normalize a callback to a "switchback" and vice versa.

```sh
$ npm install node-switchback --save
$ npm install switchback --save
```

@@ -104,3 +104,3 @@

```js
var switchback = require('node-switchback');
var switchback = require('switchback');
```

@@ -123,3 +123,3 @@

```javascript
var switchback = require('node-switchback');
var switchback = require('switchback');

@@ -269,2 +269,2 @@ function myFunction (stuff, cb) {

[![githalytics.com alpha](https://cruel-carlota.pagodabox.com/a22d3919de208c90c898986619efaa85 "githalytics.com")](http://githalytics.com/balderdashy/switchback)
[![githalytics.com alpha](https://cruel-carlota.pagodabox.com/a22d3919de208c90c898986619efaa85 "githalytics.com")](http://githalytics.com/node-machine/switchback)

@@ -88,3 +88,90 @@ /**

});
////////
describe('synchronous function w/ a switchback + `immediate` set to true ::', function() {
var someSynchronousFn = function(foobar, cb) {
var sb = switchback(cb, {}, {}, true);
sb.success('some stuff');
return sb;
};
describe('when that function is called using a traditional node callback', function() {
it('should NOT wait for one cycle of the event loop', function() {
var data;
someSynchronousFn({
blah: 'this other argument doesnt matter',
blahblah: 'its the callback we care about'
}, function (err, _data) {
if (err) throw err;
data = _data;
});
assert.equal(data, 'some stuff');
});
});
describe('when that function is called using a handlers object', function() {
it('should NOT wait for one cycle of the event loop', function() {
var data;
someSynchronousFn({
blah: 'this other argument doesnt matter',
blahblah: 'its the callback we care about'
}, {
error: function(err) {
throw err;
},
success: function(_data) {
data = _data;
}
});
assert.equal(data, 'some stuff');
});
});
describe('when that function returns the switchback and relies on its EventEmitter properties', function() {
it('should fail with an error', function() {
try {
someSynchronousFn({
blah: 'this other argument doesnt matter',
blahblah: 'its the callback we care about'
})
.on('error', function(err) {
throw new Error('should never get here');
})
.on('success', function(data) {
throw new Error('should never get here');
});
throw new Error('should have thrown error when trying to use .on()');
}
catch (e) {
if (e.code !== 'SWITCHBACK:E_USAGE') {
throw e;
}
}
});
});
});
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