Comparing version 0.0.2 to 0.0.3
@@ -39,6 +39,17 @@ /* | ||
this.____items = []; //this remembers the callback functions list | ||
this.____next = function() //execute next function | ||
this.____finally = null; | ||
this.____next = function(jump) //execute next function | ||
{ | ||
var func = self.____items.shift(); | ||
if (func) func.call(); | ||
if (!jump) | ||
{ | ||
var func = self.____items.shift(); | ||
if (func) func.call(); | ||
} | ||
else self.____items = []; | ||
if (self.____items.length == 0 && typeof self.____finally == 'function') | ||
{ | ||
self.____finally.call(obj); | ||
self.____finally = null; | ||
} | ||
}; | ||
@@ -48,5 +59,4 @@ | ||
{ | ||
var args = [].slice.call(arguments,1); | ||
var args = [].slice.call(arguments,1),func = arguments[0]; | ||
args.push(self.____next); | ||
var func = arguments[0]; | ||
self.____items.push(function() | ||
@@ -59,2 +69,8 @@ { | ||
this.end = function(func) //support for final function | ||
{ | ||
self.____finally = func; | ||
return self; | ||
}; | ||
//copy and wrap the methods of obj | ||
@@ -69,3 +85,3 @@ for(var func in obj) | ||
{ | ||
var args = [].slice.call(arguments); //change arguments as an array | ||
var args = [].slice.call(arguments); | ||
args.push(self.____next); //pass next callback to the last argument | ||
@@ -83,7 +99,3 @@ self.____items.push(function() //wrap the function and push into callbacks array | ||
//start execute the chained functions in next tick | ||
setTimeout(function() | ||
{ | ||
self.____next(); | ||
},0); | ||
setTimeout(self.____next,0); | ||
return this; | ||
@@ -90,0 +102,0 @@ } |
@@ -0,1 +1,5 @@ | ||
var JSChain = require('./jschain'); | ||
new JSChain( | ||
@@ -17,3 +21,3 @@ { | ||
setTimeout(next,1000); | ||
}).exec(function() | ||
}).end(function() | ||
{ | ||
@@ -30,3 +34,3 @@ | ||
console.log('done'); | ||
next(); | ||
next( url == 'http://example.com/4' ); | ||
},1000); | ||
@@ -37,69 +41,17 @@ } | ||
var chain = new JSChain({getURL: getURL}); | ||
for(var i=1;i<100;i++) | ||
chain.end(function() | ||
{ | ||
console.log('chain.end'); | ||
}); | ||
for(var i=1;i<10;i++) | ||
{ | ||
chain.getURL('http://example.com/'+i); | ||
} | ||
chain.exec(function() | ||
chain.exec(function(next) | ||
{ | ||
console.log('DONE!!!'); | ||
console.log('before end'); | ||
next(); | ||
}); | ||
}); | ||
function JSChain(obj) | ||
{ | ||
var self = this; | ||
this.____items = []; | ||
this.____next = function() | ||
{ | ||
var func = self.____items.shift(); | ||
if (func) func.call(); | ||
} | ||
this.exec = function() | ||
{ | ||
var args = [].slice.call(arguments,1); | ||
args.push(self.____next); | ||
var func = arguments[0]; | ||
self.____items.push(function() | ||
{ | ||
func.apply(obj,args); | ||
}); | ||
return self; | ||
} | ||
for(var func in obj) | ||
{ | ||
if (typeof obj[func] == 'function' && obj.hasOwnProperty(func)) | ||
{ | ||
(function(func) | ||
{ | ||
self[func] = function() | ||
{ | ||
var args = [].slice.call(arguments); | ||
args.push(self.____next); | ||
self.____items.push(function() | ||
{ | ||
obj[func].apply(obj,args); | ||
}); | ||
return self; | ||
} | ||
})(func); | ||
} | ||
} | ||
//start execute the chained functions in next tick | ||
setTimeout(function() | ||
{ | ||
self.____next(); | ||
},0); | ||
return this; | ||
} | ||
@@ -10,3 +10,3 @@ { | ||
], | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"author": "Chunlong Liu <longbill.cn@gmail.com>", | ||
@@ -19,3 +19,2 @@ "engines": [ | ||
"directories": { | ||
"test": "test" | ||
}, | ||
@@ -22,0 +21,0 @@ "scripts": { |
@@ -12,3 +12,3 @@ # Javascript Chain # | ||
```javascript | ||
new JSChain( | ||
var _map = | ||
{ | ||
@@ -25,7 +25,9 @@ foo: function(next) | ||
} | ||
}).foo().bar('hello','world').exec(function(next) | ||
}; | ||
new JSChain(_map).foo().bar('hello','world').exec(function(next) | ||
{ | ||
console.log('cumtome function'); | ||
setTimeout(next,1000); | ||
}).exec(function() | ||
}).end(function() | ||
{ | ||
@@ -36,6 +38,6 @@ console.log('done'); | ||
##When to use it?## | ||
## Background ## | ||
Javascript callbacks may looks like this: | ||
Old days, you might write something like this: | ||
```javascript | ||
@@ -81,3 +83,3 @@ ajaxGet('http://something',function(a) | ||
setTimeout(next,1000); | ||
}).exec(function() | ||
}).end(function() | ||
{ | ||
@@ -89,2 +91,3 @@ console.log('done'); | ||
With the help of JSChain, the first demo code could be written like this: | ||
```javascript | ||
@@ -95,3 +98,3 @@ var myController = | ||
saveToDatabase: function(a,next){ ... ... next(); }, | ||
notifyUser: function() { ... arguments[arguments.length-1].call(); ... }, | ||
notifyUser: function() { ... next(); }, | ||
markNotified: function(next){ ... next(); ... } | ||
@@ -104,2 +107,3 @@ }; | ||
And JSChain is very useful for data scraping projects: | ||
```javascript | ||
@@ -124,3 +128,3 @@ function getURL(url,next) | ||
The JSChain itself is very tiny. The full documented source code of JSChain is only 1.6KB. I can paste the source code here, so you can read the source code. This could help you understand how it works. | ||
The JSChain itself is very small. The full documented source code of JSChain is only 1.6KB. I can paste the source code here, so you can read the source code. This could help you understand how it works. | ||
@@ -132,6 +136,17 @@ ```javascript | ||
this.____items = []; //this remembers the callback functions list | ||
this.____next = function() //execute next function | ||
this.____finally = null; | ||
this.____next = function(jump) //execute next function | ||
{ | ||
var func = self.____items.shift(); | ||
if (func) func.call(); | ||
if (!jump) | ||
{ | ||
var func = self.____items.shift(); | ||
if (func) func.call(); | ||
} | ||
else self.____items = []; | ||
if (self.____items.length == 0 && typeof self.____finally == 'function') | ||
{ | ||
self.____finally.call(obj); | ||
self.____finally = null; | ||
} | ||
}; | ||
@@ -141,5 +156,4 @@ | ||
{ | ||
var args = [].slice.call(arguments,1); | ||
var args = [].slice.call(arguments,1),func = arguments[0]; | ||
args.push(self.____next); | ||
var func = arguments[0]; | ||
self.____items.push(function() | ||
@@ -152,2 +166,8 @@ { | ||
this.end = function(func) //support for final function | ||
{ | ||
self.____finally = func; | ||
return self; | ||
}; | ||
//copy and wrap the methods of obj | ||
@@ -162,3 +182,3 @@ for(var func in obj) | ||
{ | ||
var args = [].slice.call(arguments); //change arguments as an array | ||
var args = [].slice.call(arguments); | ||
args.push(self.____next); //pass next callback to the last argument | ||
@@ -171,3 +191,3 @@ self.____items.push(function() //wrap the function and push into callbacks array | ||
} | ||
})(func); // this is the closure trick | ||
})(func); // this is the closure tricks | ||
} | ||
@@ -177,7 +197,3 @@ } | ||
//start execute the chained functions in next tick | ||
setTimeout(function() | ||
{ | ||
self.____next(); | ||
},0); | ||
setTimeout(self.____next,0); | ||
return this; | ||
@@ -187,3 +203,40 @@ } | ||
Good luck. | ||
##Advanced features## | ||
###end method### | ||
An `end` method can add a function to the end of the chain. You can not define this in the object when new JSChian(). | ||
###jump to the end instead of going to next### | ||
When you call `next` method, if you pass a `true` value into `next()` method, that means JUMP TO THE END. | ||
###data passing along functions### | ||
``` | ||
var obj = | ||
{ | ||
a: function(i,next) | ||
{ | ||
this.A = i.toUpperCase(); | ||
next(); | ||
}, | ||
b: function(next) | ||
{ | ||
this.B = this.A+'B'; | ||
next(); | ||
} | ||
}; | ||
var c = new JSChain(obj); | ||
c.a('a') | ||
.b() | ||
.exec(function() | ||
{ | ||
console.log(this); | ||
}); | ||
``` | ||
this will output: | ||
``` | ||
{ a: [Function], b: [Function], A: 'A', B: 'AB' } | ||
``` |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
10772
228
0
130
1