From f0c6c16c3238069db30ad3d4e6059ba8dbf271c0 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sat, 18 Feb 2017 06:54:20 +0300 Subject: [PATCH] some refactoring -- now Action(..) is almost compatible with how actions are defiden in action set... Signed-off-by: Alex A. Naanou --- README.md | 2 +- actions.js | 39 +++++++++++++++++++++++++-------------- package.json | 2 +- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index f97415c..87c449c 100644 --- a/README.md +++ b/README.md @@ -363,7 +363,7 @@ _action_ is build-in. All action protocol details apply. **Notes:** - - there is not reliable way to call the post phase without first + - there is no reliable way to call the post phase without first calling the pre phase due to how the pre phase is defined (i.e. pre phase functions can return post phase functions). diff --git a/actions.js b/actions.js index 248137f..c83a4cc 100755 --- a/actions.js +++ b/actions.js @@ -291,6 +291,12 @@ function(func){ // Construct an action object... // +// Action(, ) +// Action([, [, ]][, ,] ) +// Action(, [ [[, ]][, ,] ]) +// -> +// +// // Action function format: // // // pre event code... @@ -356,13 +362,13 @@ function(func){ // XXX might be a good idea to add an option to return the full results... var Action = module.Action = -function Action(name, doc, ldoc, func){ +function Action(name, doc, ldoc, attrs, func){ // we got called without a 'new'... if(this == null || this.constructor !== Action){ // XXX using something like .apply(.., arguemnts) would be more // generel but have no time to figure out how to pass it // to new without the later complaining... - return new Action(name, doc, ldoc, func) + return new Action(name, doc, ldoc, attrs, func) } // prevent action overloading... @@ -375,6 +381,18 @@ function Action(name, doc, ldoc, func){ return meth.chainApply(this, null, arguments) } meth.__proto__ = this.__proto__ + // precess args... + var args = doc instanceof Array ? + doc + : [].slice.call(arguments) + .filter(function(e){ return e !== undefined }) + .slice(1) + func = args.pop() + last = args[args.length-1] + attrs = last != null && typeof(last) != typeof('str') ? args.pop() : {} + doc = typeof(args[0]) == typeof('str') ? args.shift() : null + ldoc = typeof(args[0]) == typeof('str') ? args.shift() : null + // populate the action attributes... //meth.name = name Object.defineProperty(meth, 'name', { @@ -388,6 +406,10 @@ function Action(name, doc, ldoc, func){ // make introspection be a bit better... meth.toString = func.toString.bind(func) + // setup attrs... + Object.keys(attrs) + .forEach(function(k){ meth[k] = attrs[k] }) + return meth } // this will make action instances behave like real functions... @@ -1520,19 +1542,8 @@ function Actions(a, b){ return } - var func = arg.pop() - - // attrs... - var last = arg[arg.length-1] - var attrs = last != null && typeof(last) != typeof('str') ? arg.pop() : {} - var ldoc = typeof(arg[1]) == typeof('str') ? arg[1] : null - // create a new action... - var a = obj[k] = new Action(k, arg[0], ldoc, func) - - // setup attrs... - Object.keys(attrs) - .forEach(function(k){ a[k] = attrs[k] }) + var a = obj[k] = Action(k, arg) }) if(proto != null){ diff --git a/package.json b/package.json index b48a111..d86e63e 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-actions", - "version": "3.0.4", + "version": "3.1.0", "description": "", "main": "actions.js", "scripts": {