golike-defer
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -1,133 +0,2 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var push = Array.prototype.push; | ||
var isPromise = function isPromise(value) { | ||
return value != null && typeof value.then === 'function'; | ||
}; | ||
var toDecorator = function toDecorator(wrap) { | ||
var decorator = function decorator(target, key, descriptor) { | ||
return key === undefined ? wrap(target) : _extends({}, descriptor, { | ||
value: wrap(descriptor.value) | ||
}); | ||
}; | ||
Object.assign(decorator, wrap); | ||
return decorator; | ||
}; | ||
// ------------------------------------------------------------------- | ||
var errorWrapper = { error: null }; | ||
var forwardResult = function forwardResult(result) { | ||
if (result === errorWrapper) { | ||
var error = errorWrapper.error; | ||
errorWrapper.error = null; | ||
throw error; | ||
} | ||
return result; | ||
}; | ||
var tryCatch = function tryCatch(fn, thisArg, args) { | ||
try { | ||
return fn.apply(thisArg, args); | ||
} catch (error) { | ||
errorWrapper.error = error; | ||
return errorWrapper; | ||
} | ||
}; | ||
// =================================================================== | ||
var defaultOnError = function defaultOnError(error) { | ||
console.error(error); | ||
}; | ||
function Deferred(fn, thisArg, args) { | ||
this.args = args; | ||
this.fn = fn; | ||
this.thisArg = thisArg; | ||
} | ||
Deferred.prototype.run = function () { | ||
var args = this.args, | ||
fn = this.fn, | ||
thisArg = this.thisArg; | ||
return args === undefined && thisArg === undefined ? fn() : fn.apply(thisArg, args); | ||
}; | ||
var makeDefer = function makeDefer(onSuccess, onFailure) { | ||
var defer = function defer(fn) { | ||
var onError = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultOnError; | ||
return function () { | ||
var deferreds = []; | ||
var args = [function (deferred) { | ||
var args = void 0; | ||
if (typeof deferred !== 'function') { | ||
deferred = this[deferred]; | ||
} | ||
var nArgs = arguments.length - 1; | ||
if (nArgs !== 0) { | ||
args = new Array(nArgs); | ||
for (var i = 0; i < nArgs; ++i) { | ||
args[i] = arguments[i + 1]; | ||
} | ||
} | ||
deferreds.push(new Deferred(deferred, this, args)); | ||
}]; | ||
push.apply(args, arguments); | ||
var result = tryCatch(fn, this, args); | ||
if (isPromise(result)) { | ||
var executeAndForward = function executeAndForward() { | ||
var i = deferreds.length; | ||
var loop = function loop() { | ||
return i > 0 ? new Promise(function (resolve) { | ||
return resolve(deferreds[--i].run()); | ||
}).then(loop, reportAndLoop) : result; | ||
}; | ||
var reportAndLoop = function reportAndLoop(error) { | ||
onError(error); | ||
return loop(); | ||
}; | ||
return loop(); | ||
}; | ||
return result.then(onSuccess && executeAndForward, onFailure && executeAndForward); | ||
} | ||
if (result === errorWrapper ? onFailure : onSuccess) { | ||
var i = deferreds.length; | ||
while (i > 0) { | ||
try { | ||
deferreds[--i].run(); | ||
} catch (error) { | ||
onError(error); | ||
} | ||
} | ||
} | ||
return forwardResult(result); | ||
}; | ||
}; | ||
defer.onError = function (cb) { | ||
return toDecorator(function (fn) { | ||
return defer(fn, cb); | ||
}); | ||
}; | ||
return defer; | ||
}; | ||
var defer = toDecorator(makeDefer(true, true)); | ||
exports.default = defer; | ||
var onFailure = exports.onFailure = defer.onFailure = toDecorator(makeDefer(false, true)); | ||
var onSuccess = exports.onSuccess = defer.onSuccess = toDecorator(makeDefer(true, false)); | ||
"use strict";exports.__esModule=true;exports.default=void 0;var push=Array.prototype.push;var toDecorator=function toDecorator(wrap){return function(target,key,descriptor){if(key===undefined){return wrap(target);}descriptor.value=wrap(descriptor.value);return descriptor;};};var defaultOnError=function defaultOnError(error){console.error(error);};var SUCCESS=1<<0;var FAILURE=1<<1;function Deferred(fn,thisArg,args,when){this.args=args;this.fn=fn;this.thisArg=thisArg;this.when=when;}Deferred.prototype.run=function(when){if((when&this.when)!==0){return this.fn.apply(this.thisArg,this.args);}};var defer=function defer(fn,onError){if(onError===void 0){onError=defaultOnError;}return function(){var deferreds=[];var makeAddDeferred=function makeAddDeferred(when){return function $defer(deferred){var args;if(typeof deferred!=='function'){deferred=this[deferred];}var nArgs=arguments.length-1;if(nArgs!==0){args=new Array(nArgs);for(var _i=0;_i<nArgs;++_i){args[_i]=arguments[_i+1];}}deferreds.push(new Deferred(deferred,this,args,when));};};var $defer=makeAddDeferred(FAILURE|SUCCESS);$defer.onFailure=makeAddDeferred(FAILURE);$defer.onSuccess=makeAddDeferred(SUCCESS);var args=[$defer];push.apply(args,arguments);var hasThrown,result;try{result=fn.apply(this,args);var then;if(result!=null&&typeof(then=result.then)==='function'){var executeAndForward=function executeAndForward(when){var i=deferreds.length;var loop=function loop(){if(i===0){return result;}try{var _result=deferreds[--i].run(when);var _then;return _result!=null&&typeof(_then=_result.then)==='function'?_then.call(_result,loop,reportAndLoop):loop();}catch(error){return reportAndLoop(error);}};var reportAndLoop=function reportAndLoop(error){onError(error);return loop();};return loop();};return then.call(result,function(){return executeAndForward(SUCCESS);},function(){return executeAndForward(FAILURE);});}hasThrown=false;}catch(error){result=error;hasThrown=true;}var when=hasThrown?FAILURE:SUCCESS;var i=deferreds.length;while(i>0){try{deferreds[--i].run(when);}catch(error){onError(error);}}if(hasThrown){throw result;}return result;};};var decorator=toDecorator(defer);exports.default=decorator;decorator.onError=function(cb){return toDecorator(function(fn){return defer(fn,cb);});}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "golike-defer", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"license": "ISC", | ||
@@ -35,15 +35,22 @@ "description": "go's defer statement in JavaScript", | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"@babel/polyfill": "7.0.0-beta.34" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.24.1", | ||
"babel-eslint": "^7.2.3", | ||
"babel-plugin-lodash": "^3.2.11", | ||
"babel-preset-env": "^1.5.2", | ||
"babel-preset-stage-3": "^6.24.1", | ||
"cross-env": "^5.0.1", | ||
"dependency-check": "^2.9.1", | ||
"husky": "^0.14.1", | ||
"jest": "^20.0.4", | ||
"rimraf": "^2.6.1", | ||
"standard": "^10.0.2" | ||
"@babel/cli": "7.0.0-beta.34", | ||
"@babel/core": "7.0.0-beta.34", | ||
"@babel/plugin-proposal-decorators": "7.0.0-beta.34", | ||
"@babel/preset-env": "7.0.0-beta.34", | ||
"babel-7-jest": "^21.3.2", | ||
"babel-eslint": "^8.0.3", | ||
"cross-env": "^5.1.1", | ||
"eslint": "^4.13.1", | ||
"eslint-config-standard": "^11.0.0-beta.0", | ||
"eslint-plugin-import": "^2.8.0", | ||
"eslint-plugin-node": "^5.2.1", | ||
"eslint-plugin-promise": "^3.6.0", | ||
"eslint-plugin-standard": "^3.0.1", | ||
"husky": "^0.14.3", | ||
"jest": "^22.0.3", | ||
"rimraf": "^2.6.2" | ||
}, | ||
@@ -55,32 +62,14 @@ "scripts": { | ||
"dev-test": "jest --bail --watch", | ||
"posttest": "standard && dependency-check ./package.json", | ||
"prebuild": "rimraf dist/", | ||
"predev": "npm run prebuild", | ||
"prepublish": "npm run build", | ||
"prepublishOnly": "npm run build", | ||
"pretest": "eslint --ignore-path .gitignore --fix .", | ||
"test": "jest" | ||
}, | ||
"babel": { | ||
"env": { | ||
"test": { | ||
"ignore": null | ||
} | ||
}, | ||
"ignore": "*.spec.js", | ||
"plugins": [ | ||
"lodash" | ||
], | ||
"presets": [ | ||
[ | ||
"env", | ||
{ | ||
"targets": { | ||
"node": 4 | ||
} | ||
} | ||
], | ||
"stage-3" | ||
] | ||
}, | ||
"jest": { | ||
"collectCoverage": true, | ||
"testEnvironment": "node", | ||
"transform": { | ||
"\\.jsx?$": "babel-7-jest" | ||
}, | ||
"roots": [ | ||
@@ -90,9 +79,3 @@ "<rootDir>/src" | ||
"testRegex": "\\.spec\\.js$" | ||
}, | ||
"standard": { | ||
"ignore": [ | ||
"dist" | ||
], | ||
"parser": "babel-eslint" | ||
} | ||
} |
@@ -25,7 +25,30 @@ # golike-defer [](https://travis-ci.org/JsCommunity/golike-defer) | ||
- `$defer(cb)`: `cb` will be called at the end of the function | ||
- `$defer(cb, arg1, arg2)`: `cb` will be called with `arg1` and `arg2` arguments | ||
- `$defer.call(thisArg, cb)`: `cb` will be called with the context `thisArg` | ||
- `$defer.call(thisArg, 'method')`: `thisArg.method` will be called at the end of the function | ||
```js | ||
const fn = defer( | ||
// Works both with sync and async functions | ||
async function ($defer, ...args) { | ||
$defer(() => { | ||
console.log('always called at the end of the function') | ||
}) | ||
$defer.onFailure(() => { | ||
console.log('called at the end of the function only on failure') | ||
}) | ||
$defer.onSuccess(() => { | ||
console.log('called at the end of the function only on success') | ||
}) | ||
} | ||
) | ||
``` | ||
Context and arguments can be passed to the deferred function: | ||
- `$defer(cb)`: called without context nor arguments | ||
- `$defer(cb, arg1, arg2)`: called with arguments `arg1` and `arg2` | ||
- `$defer.call(thisArg, cb)`: called with context `thisArg` | ||
- `$defer.call(thisArg, 'method')`: `thisArg.method` called with context `thisArg` | ||
## Example | ||
```js | ||
@@ -71,6 +94,2 @@ import defer from 'golike-defer' | ||
There are also two decorators, `defer.onSuccess()` and | ||
`defer.onFailure()` which run the deferreds only, respectively, in | ||
case of success or in case of failure. | ||
### On error | ||
@@ -97,15 +116,15 @@ | ||
# Install dependencies | ||
> npm install | ||
> yarn | ||
# Run the tests | ||
> npm test | ||
> yarn test | ||
# Continuously compile | ||
> npm run dev | ||
> yarn dev | ||
# Continuously run the tests | ||
> npm run dev-test | ||
> yarn dev-test | ||
# Build for production (automatically called by npm install) | ||
> npm run build | ||
> yarn build | ||
``` | ||
@@ -112,0 +131,0 @@ |
Sorry, the diff of this file is not supported yet
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
143
13982
1
16
9
1
+ Added@babel/polyfill@7.0.0-beta.34(transitive)
+ Addedcore-js@2.6.12(transitive)
+ Addedregenerator-runtime@0.11.1(transitive)