reworked how inner return values are handled by .chainApply(..)...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2016-11-07 18:15:53 +03:00
parent 1c0965fe56
commit b9e5d34254
2 changed files with 33 additions and 12 deletions

View File

@ -204,9 +204,17 @@ module.UNDEFINED = ['undefined placeholder']
// 1) pre phase of protocol action (outer) // 1) pre phase of protocol action (outer)
// 2) implementation action (inner) // 2) implementation action (inner)
// 3) post phase of protocol action (outer) // 3) post phase of protocol action (outer)
//
// Differences form the base action protocol:
// - returning undefined from inner will keep the outer return value.
// ...this is different from the base action protocol where returning
// undefined will get replaces by the context (this), here to
// guarantee returning the context, it should be returned explicitly,
// otherwise the responsibility is shifted to the outer action.
// - returning anything else will override the outer return
// //
// NOTE: this will not affect to protocol/signature of the outer action // NOTE: this will not affect to protocol/signature of the outer action
// in any way. // in any way other than the ability to override the return value.
// NOTE: both the inner and outer actions will get passed the same // NOTE: both the inner and outer actions will get passed the same
// arguments. // arguments.
// NOTE: another use-case is testing/debugging actions. // NOTE: another use-case is testing/debugging actions.
@ -363,8 +371,6 @@ function Action(name, doc, ldoc, func){
} }
// create the actual instance we will be returning... // create the actual instance we will be returning...
//var meth = function(){
// return meth.chainApply(this, null, arguments) }
var meth = function(){ var meth = function(){
return meth.chainApply(this, null, arguments) } return meth.chainApply(this, null, arguments) }
meth.__proto__ = this.__proto__ meth.__proto__ = this.__proto__
@ -513,23 +519,29 @@ Action.prototype.post = function(context, data){
} }
// Chaining... // Chaining...
//
// For docs see: MetaActions.chainApply(..) and the base module doc.
Action.prototype.chainApply = function(context, inner, args){ Action.prototype.chainApply = function(context, inner, args){
args = [].slice.call(args || []) args = [].slice.call(args || [])
var res = context
var outer = this.name var outer = this.name
var data = this.pre(context, args) var data = this.pre(context, args)
// call the inner action/function if preset.... // call the inner action/function if preset....
if(inner){ if(inner){
//res = inner instanceof Function ? // XXX need a way to pass data.result to inner... (???)
inner instanceof Function ? var res = inner instanceof Function ?
inner.call(context, args) inner.apply(context, args)
: inner instanceof Array && inner.length > 0 ? : inner instanceof Array && inner.length > 0 ?
context[inner.pop()].chainCall(context, inner, args) context[inner.pop()].chainApply(context, inner, args)
: typeof(inner) == typeof('str') ? : typeof(inner) == typeof('str') ?
context[inner].chainCall(context, null, args) context[inner].chainApply(context, null, args)
: null : undefined
// push the inner result into the chian...
if(res !== undefined){
data.result = res
}
} }
return this.post(context, data) return this.post(context, data)
@ -1051,9 +1063,18 @@ module.MetaActions = {
// //
// The given arguments are passed as-is to both the outer and inner // The given arguments are passed as-is to both the outer and inner
// actions. // actions.
// The base inner action return value is passed to the outer action // The inner action return value is passed to the outer action
// .post handlers. // .post handlers.
// //
// inner return value is handling slightly differs from the base
// action protocol in two respects:
// 1) to keep the outer return value, inner must return undefined.
// 2) to guarantee returning the context regardless of outer's return
// value, the inner must return the context (this) explicilty.
//
// NOTE: as a restriction of the action protocol the inner return will
// override the return value of outer, but there is no way to
// see that value.
// NOTE: these call the action's .chainApply(..) and .chainCall(..) // NOTE: these call the action's .chainApply(..) and .chainCall(..)
// methods, thus is not compatible with non-action methods... // methods, thus is not compatible with non-action methods...
// NOTE: .chianCall('action', ..) is equivalent to .action.chianCall(..) // NOTE: .chianCall('action', ..) is equivalent to .action.chianCall(..)

View File

@ -1,6 +1,6 @@
{ {
"name": "ig-actions", "name": "ig-actions",
"version": "1.6.3", "version": "1.7.0",
"description": "", "description": "",
"main": "actions.js", "main": "actions.js",
"scripts": { "scripts": {