Comparing version 1.0.4 to 1.0.5
1.0.5 / 2016-02-02 | ||
================== | ||
* shared context across internal action calls | ||
1.0.4 / 2016-02-02 | ||
@@ -3,0 +8,0 @@ ================== |
25
index.js
@@ -23,3 +23,3 @@ /** | ||
if (!(this instanceof internal)) return new internal(state) | ||
this.state = assign(actions || {}, state || {}) | ||
this.state = bind(actions, state || {}) | ||
this.queue = [] | ||
@@ -82,3 +82,4 @@ } | ||
internal.prototype.__defineGetter__(name, function () { | ||
var child = Child(this.state) | ||
var child = Child() | ||
child.state = this.state | ||
child.queue = this.queue | ||
@@ -122,2 +123,22 @@ return child | ||
function bind (actions, state) { | ||
var obj = walk(actions, function(fn) { | ||
return fn.bind(state) | ||
}) | ||
Object.keys(obj).forEach(function(key) { | ||
state[key] = state[key] || obj[key] | ||
}) | ||
return state | ||
} | ||
function walk (obj, fn) { | ||
Object.keys(obj).forEach(function (key) { | ||
if (typeof obj[key] === 'function') obj[key] = fn(obj[key]) | ||
else walk(obj[key], fn) | ||
}) | ||
return obj | ||
} | ||
/** | ||
@@ -124,0 +145,0 @@ * Prettify a function |
{ | ||
"name": "internal", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "internal queue for your public libraries and APIs", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,7 +5,16 @@ var Internal = require('../') | ||
echo(msg, fn) { | ||
this.redis.message(msg, fn) | ||
this.test = 'test' | ||
this.hi(msg, fn) | ||
}, | ||
hi(msg, fn) { | ||
this.test2 = 'whatever!!' | ||
fn() | ||
}, | ||
redis: { | ||
message(msg, fn) { | ||
fn(null, 'hi') | ||
var self = this | ||
this.echo(msg, function() { | ||
console.log('here...') | ||
fn(null, msg + ':' + self.test + ':' + self.test2) | ||
}) | ||
} | ||
@@ -17,4 +26,4 @@ } | ||
api.echo('hi') | ||
api.redis.message('hi') | ||
.then(v => console.log(v)) | ||
.catch(e => console.log(e.stack)) |
9757
294