Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chainjs

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chainjs - npm Package Compare versions

Comparing version 0.0.0 to 0.0.1

CHANGELOG.md

52

chain.js
var _ = require('underscore');
var util = {
// 批处理handler数组
batch: function (handlers/*, params*/) {
var args = Array.prototype.slice.call(arguments);
var args = this.slice(arguments);
args.shift();

@@ -10,2 +12,6 @@ _.each(handlers, function (handler) {

});
},
// 封装Array.slice
slice: function (array) {
return Array.prototype.slice.call(array);
}

@@ -15,9 +21,14 @@ }

module.exports = function (startHandler/*, arg1, [arg2, ...]*/) {
var chainHandlers = [],
chainEndHandlers = [],
beforeHandlers = [],
afterHandlers = [],
isEnd = false,
args = Array.prototype.slice.call(arguments),
args = util.slice(arguments),
startParams = [],
cursor = 0,
_data = {};
// 链调用结束的拦截器
function filter () {

@@ -30,3 +41,2 @@ return isEnd;

then: function (handler) {
if (filter()) return;

@@ -38,9 +48,9 @@ chainHandlers.push(handler);

next: function () {
if (filter()) return;
if (filter()) return chain;
if (isEnd) return;
var handler = chainHandlers.shift();
var handler = chainHandlers[cursor];
cursor ++;
var handlerArgs = [],
argParams = Array.prototype.slice.call(arguments);
argParams = util.slice(arguments);

@@ -51,6 +61,9 @@ handlerArgs.push(chain);

if (handler) {
util.batch.apply(util, [beforeHandlers].concat(handlerArgs));
handler.apply(this, handlerArgs);
} else {
this.end.apply(this, argParams);
}
return chain;

@@ -60,3 +73,2 @@ },

final: function (handler) {
if (filter()) return;

@@ -76,3 +88,3 @@ chainEndHandlers.push(handler);

start: function () {
if (filter()) return;
if (startHandler) {

@@ -95,12 +107,20 @@ startHandler.apply(this, startParams);

},
filter: function () {
// 每个链节点的拦截方法
filter: function (filterHandler) {
// TBD
return chain;
},
before: function () {
// 每个链节点handler的函数切面-before
before: function (beforeHandler) {
// TBD
beforeHandlers.push(beforeHandler)
return chain;
},
after: function () {
// 每个链节点handler的函数切面-after
after: function (afterHandler) {
// TBD
return chain;
}
}
// 修理初始参数
args.shift();

@@ -107,0 +127,0 @@ startParams.push(chain);

{
"name": "chainjs",
"version": "0.0.0",
"version": "0.0.1",
"description": "Use chaining call to resolve the problem of javascript async callback handle ",

@@ -5,0 +5,0 @@ "main": "chain.js",

@@ -1,4 +0,134 @@

chain
=====
chainjs
=======
Use chaining call to resolve the problem of javascript async callback handle
Chaining of functions call to resolve the problem of javascript async callback handling.
## Install
```bash
npm install chainjs
```
## Usage
__use in node:__
```javascript
var Chain = require('chainjs'),
param = {message: 'Hello world';}
function beginStep (chain, param) {
console.log(param.message); //Hello world
chain.next('none');
}
function step1 (chain, data) {
chain.next('say hello');
}
Chain(beginStep, param)
.then(step1)
.final(function (chain, data) {
console.log(data); //say hello
});
```
## API
### Chain(beginHandler)
```javascript
Chain(func, param)
```
### then(stepHandler)
```javascript
Chain(func).then(func1).then(func2)
```
### start()
```javascript
Chain(func).then(func1).then(func2).start();
```
### next(nextParams)
### end(finalParams)
### final(finalHandler)
### data(savingData)
Saving data in current chain
```javascript
Chain(function (chain, param) {
chain.data('param', param);
chain.next();
}, {data: ''})
.then(function (chain) {
var param = chain.data('param');
console.log(param); // {data:''}
})
.start();
```
### filter(filterHandler)
TBD
### before(beforeHandler)
Will be invoked before each step
```javascript
Chain(function (chain, param) {
chain.data('param', param);
chain.next();
}, {data: ''})
.then(function (chain) { // Step 1
var param = chain.data('param');
console.log(param); // {data:''}
})
.before(function (chain) { // Step before
console.log('It will be invoked before each chain step')
})
.start();
```
### after(afterHandler)
TBD
## Testing
```bash
npm test
```
## Example
```javascript
var Chain = require('chainjs');
Chain(function (chain, msg) {
// save param
chain.data('chain:param', msg);
console.log(msg); //Hello world
chain.next({message: 'Next step'});
}, 'Hello world')
.then(function (chain, param) {
console.log(param.message); // Next step
chain.end({name: 'switer'})
})
.then(function (chain) {
//will be skiped
})
.final(function (chain, author) {
var param = chain.data('chain:param');
console.log(param); // Hello world
console.log(author.name); // switer
})
.start(); // starting chain execute
```
## Change Log
## License
MIT
var Chain = require('../chain');
Chain(function (chain, msg) {
console.log('Chain initialize');
// save param

@@ -12,10 +13,24 @@ chain.data('chain:param', msg);

.then(function (chain, param) {
console.log('Chain step 1');
console.log(param.message); // Next step
chain.end({name: 'switer'})
chain.next({name: 'switer'});
})
.then(function (chain, param) {
console.log('Chain step 2');
console.log(param.name); // Next step
chain.end({name: 'switer.github.io'});
})
.then(function (chain) {
//will be skiped
console.log('Chain step 3');
})
.before(function (chain) {
console.log('In each chain-node before handlers');
console.log('==================================');
})
.final(function (chain, author) {
console.log('Chain step final');
var param = chain.data('chain:param');

@@ -22,0 +37,0 @@

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