changed action semantics to acomodate data threading through action call chains...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2015-12-05 04:38:15 +03:00
parent ebc579615f
commit 74b538c768
2 changed files with 46 additions and 12 deletions

View File

@ -51,6 +51,10 @@ define(function(require){ var module = {}
// handlers in its inheritance chain will be called before the // handlers in its inheritance chain will be called before the
// respective actions they are bound to and all the post handlers // respective actions they are bound to and all the post handlers
// are called directly after. // are called directly after.
// - pre handlers are passed the same arguments the original actions
// got when it was called.
// - post action handlers will get the root action result as first
// argument succeeded by the action arguments.
// //
// //
// //
@ -117,23 +121,23 @@ if(typeof(args2array) != 'function'){
// Action function format: // Action function format:
// //
// // pre event code... // // pre event code...
// function(){ // function(..){
// ... // pre code // ... // pre code
// } // }
// //
// // pre/post event code... // // pre/post event code...
// function(){ // function(..){
// ... // pre code // ... // pre code
// return function(){ // return function(<return>, ..){
// ... // post code // ... // post code
// } // }
// } // }
// //
// // same as above but using a deferred instead of a callback... // // same as above but using a deferred instead of a callback...
// function(){ // function(..){
// ... // pre code // ... // pre code
// return $.Deferred() // return $.Deferred()
// .done(function(){ // .done(function(<return>, ..){
// ... // post code // ... // post code
// }) // })
// } // }
@ -146,6 +150,9 @@ if(typeof(args2array) != 'function'){
// event is fired // event is fired
// post: if the action returns a callback function or a deferred // post: if the action returns a callback function or a deferred
// object it will be executed after the event is fired // object it will be executed after the event is fired
// NOTE: the signature if the post stage is the same as the
// action's with the added return value as first argument
// (the rest og the arguments are shifted by 1).
// //
// - actions automatically call the shadowed action, the pre stage is // - actions automatically call the shadowed action, the pre stage is
// executed down-up while the post stage is run in reverse order, // executed down-up while the post stage is run in reverse order,
@ -161,6 +168,10 @@ if(typeof(args2array) != 'function'){
// - an action will return the deepest (root) action's return, if that // - an action will return the deepest (root) action's return, if that
// return is undefined, then the action set is returned instead. // return is undefined, then the action set is returned instead.
// //
// - action arguments are "threaded" through the action chain down and
// root action return value and arguments are threaded back up the
// action chain.
//
// NOTE: if the root handler is instance of Toggler (jli) and the action // NOTE: if the root handler is instance of Toggler (jli) and the action
// is called with '?' as argument, then the toggler will be called // is called with '?' as argument, then the toggler will be called
// with the argument and return the result bypassing the handlers. // with the argument and return the result bypassing the handlers.
@ -235,17 +246,27 @@ function Action(name, doc, ldoc, func){
// call handlers -- post phase... // call handlers -- post phase...
// NOTE: post handlers need to get called last run pre first run post... // NOTE: post handlers need to get called last run pre first run post...
var results = handlers.reverse().map(function(h, i){ var results = []
handlers.reverse().forEach(function(h, i){
var res = h
// function... // function...
if(h instanceof Function){ if(h instanceof Function){
return h.apply(that, args) //res = h.apply(that, args)
res = h.apply(that,
[results[0] !== undefined ?
reults[0]
: that].concat(args))
// deferred... // deferred...
} else if(h != null && h.resolve instanceof Function){ } else if(h != null && h.resolve instanceof Function){
return h.resolve() //res = h.resolve()
res = h.resolve.apply(h,
[results[0] !== undefined ?
results[0]
: that].concat(args))
} }
return h results.push(res)
}) })
// XXX might be a good idea to add an option to return the full // XXX might be a good idea to add an option to return the full
@ -402,6 +423,12 @@ module.MetaActions = {
// 'post' - the handler is fired after the action is finished. // 'post' - the handler is fired after the action is finished.
// this is the default. // this is the default.
// //
// Handler Arguments:
// 'pre' - the handler will get the same arguments as the main
// action when called.
// 'post' - the handler will get the action return value followed
// by action arguments.
//
// The optional tag marks the handler to enable group removal via // The optional tag marks the handler to enable group removal via
// .off(..) // .off(..)
// //

View File

@ -276,10 +276,15 @@ actions.Actions({
json: ['File/Dump state as JSON object', json: ['File/Dump state as JSON object',
'This will collect JSON data from every available attribute ' 'This will collect JSON data from every available attribute '
+'supporting the .dumpJSON() method.', +'supporting the .dumpJSON() method.',
function(){ function(mode){
var res = {} var res = {}
for(var k in this){ for(var k in this){
if(this[k] != null && this[k].dumpJSON != null){ // dump the base crop state...
if(k == 'data' && this.crop_stack && this.crop_stack.length > 0){
res[k] = this.crop_stack[0].dumpJSON()
// dump current state...
} else if(this[k] != null && this[k].dumpJSON != null){
res[k] = this[k].dumpJSON() res[k] = this[k].dumpJSON()
} }
} }
@ -668,6 +673,8 @@ actions.Actions({
// crop... // crop...
// //
// XXX should we keep this isolated or should we connect stuff like
// tags, ...
crop: ['Crop/Crop image list', crop: ['Crop/Crop image list',
function(list, flatten){ function(list, flatten){
list = list || this.data.order list = list || this.data.order
@ -1856,7 +1863,7 @@ module.PartialRibbons = ImageGridFeatures.Feature({
this.updateRibbon(target) this.updateRibbon(target)
}], }],
['focusImage.post', ['focusImage.post',
function(target){ function(res, target){
this.preCacheJumpTargets(target) this.preCacheJumpTargets(target)
}], }],
['fitImage.pre', ['fitImage.pre',