Added logging, changed some directory structure

This commit is contained in:
2018-01-13 21:33:40 -05:00
parent f079a5f067
commit 8e72ffb917
73656 changed files with 35284 additions and 53718 deletions

View File

@@ -0,0 +1,77 @@
# thunky
Delay the evaluation of a paramless async function and cache the result (see [thunk](http://en.wikipedia.org/wiki/Thunk_%28functional_programming%29)).
npm install thunky
## Example
Let's make a simple function that returns a random number 1 second after it is called for the first time
``` js
var thunky = require('thunky');
var test = thunky(function(callback) { // the inner function should only accept a callback
console.log('waiting 1s and returning random number');
setTimeout(function() {
callback(Math.random());
}, 1000);
});
test(function(num) { // inner function is called the first time we call test
console.log(num); // prints random number
});
test(function(num) { // subsequent calls waits for the first call to finish and return the same value
console.log(num); // prints the same random number as above
});
```
## Lazy evaluation
Thunky makes it easy to implement a lazy evaluation pattern.
``` js
var getDb = thunky(function(callback) {
db.open(myConnectionString, callback);
});
var queryDb = function(query, callback) {
getDb(function(err, db) {
if (err) return callback(err);
db.query(query, callback);
});
};
queryDb('some query', function(err, result) { ... } );
queryDb('some other query', function(err, result) { ... } );
```
The first time `getDb` is called it will try do open a connection to the database.
Any subsequent calls will just wait for the first call to complete and then call your callback.
A nice property of this pattern is that it *easily* allows us to pass any error caused by `getDb` to the `queryDb` callback.
## Error → No caching
If the thunk callback is called with an `Error` object as the first argument it will not cache the result
``` js
var fails = thunky(function(callback) {
console.log('returning an error');
callback(new Error('bad stuff'));
});
fails(function(err) { // inner function is called
console.log(err);
});
fails(function(err) { // inner function is called again as it returned an error before
console.log(err);
});
```
## License
MIT

View File

@@ -0,0 +1,31 @@
var isError = function(err) { // inlined from util so this works in the browser
return Object.prototype.toString.call(err) === '[object Error]';
};
var thunky = function(fn) {
var run = function(callback) {
var stack = [callback];
state = function(callback) {
stack.push(callback);
};
fn(function(err) {
var args = arguments;
var apply = function(callback) {
if (callback) callback.apply(null, args);
};
state = isError(err) ? run : apply;
while (stack.length) apply(stack.shift());
});
};
var state = run;
return function(callback) {
state(callback);
};
};
module.exports = thunky;

View File

@@ -0,0 +1,54 @@
{
"_args": [
[
"thunky@0.1.0",
"C:\\Users\\deranjer\\go\\src\\github.com\\deranjer\\goTorrent\\torrent-project"
]
],
"_from": "thunky@0.1.0",
"_id": "thunky@0.1.0",
"_inBundle": false,
"_integrity": "sha1-vzAUaCTituZ7Dy16Ssi+smkIaE4=",
"_location": "/react-scripts/thunky",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "thunky@0.1.0",
"name": "thunky",
"escapedName": "thunky",
"rawSpec": "0.1.0",
"saveSpec": null,
"fetchSpec": "0.1.0"
},
"_requiredBy": [
"/react-scripts/multicast-dns"
],
"_resolved": "https://registry.npmjs.org/thunky/-/thunky-0.1.0.tgz",
"_spec": "0.1.0",
"_where": "C:\\Users\\deranjer\\go\\src\\github.com\\deranjer\\goTorrent\\torrent-project",
"author": {
"name": "Mathias Buus Madsen",
"email": "mathiasbuus@gmail.com"
},
"bugs": {
"url": "https://github.com/mafintosh/thunky/issues"
},
"description": "delay the evaluation of a paramless async function and cache the result",
"homepage": "https://github.com/mafintosh/thunky#readme",
"keywords": [
"memo",
"thunk",
"async",
"lazy",
"control",
"flow",
"cache"
],
"name": "thunky",
"repository": {
"type": "git",
"url": "git://github.com/mafintosh/thunky.git"
},
"version": "0.1.0"
}