added support for action paths in aliases...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-11-25 19:11:25 +03:00
parent e9775d7334
commit 0459aff378
2 changed files with 33 additions and 22 deletions

View File

@ -185,21 +185,32 @@ Object.assign(
rest != null rest != null
&& args.splice(rest, 1, ...call_args) && args.splice(rest, 1, ...call_args)
return args }, return args },
resolveAction: function(context, name){
var path = name.split(/\./g)
return {
name: path.pop(),
context: path
.reduce(function(res, n){
return res != null ?
res[n]
: res
}, context),
} },
isActionReachable: function(context, name){
var {context, name} = this.resolveAction(context, name)
return context
&& name in context },
// XXX should this break if action does not exist??? // XXX should this break if action does not exist???
callAction: function(context, action, ...args){ callAction: function(context, action, ...args){
action = typeof(action) == typeof('str') ? action = typeof(action) == typeof('str') ?
this(action) this(action)
: action : action
// XXX should this break if action does not exist??? var {context, name} = this.resolveAction(context, action.action)
return context[action.action] instanceof Function ? return (context && context[name] instanceof Function) ?
context[action.action] context[name](context, ...this.resolveArgs(context, action.arguments, args))
.apply(context, this.resolveArgs(context, action.arguments, args)) // action not found or is not callable...
// action not found or is not callable... (XXX) // XXX should this break if action does not exist???
: undefined }, : undefined },
applyAction: function(context, action, args){
return this.callAction(context, action, ...args) },
// XXX make this stricter... // XXX make this stricter...
isStringAction: function(txt){ isStringAction: function(txt){
try{ try{
@ -747,11 +758,14 @@ object.Constructor('Alias', Action, {
if(target == ''){ if(target == ''){
return } return }
var parser =
this.parseStringAction
|| parseStringAction
var p = parsed var p = parsed
|| (this.parseStringAction || parseStringAction)(target) || parser(target)
return p.action in this ? return parser.isActionReachable(this, p.action) ?
(this.parseStringAction || parseStringAction).callAction(this, p, ...arguments) parser.callAction(this, p, ...arguments)
// error... // error...
: console.error(`${alias}: Unknown alias target action: ${p.action}`) } : console.error(`${alias}: Unknown alias target action: ${p.action}`) }
func.toString = function(){ func.toString = function(){
@ -822,7 +836,7 @@ module.MetaActions = {
action.apply(this, args) action.apply(this, args)
: this[action] ? : this[action] ?
this[action].apply(this, args) this[action].apply(this, args)
: this.parseStringAction.applyAction(this, action, args) }, : this.parseStringAction.callAction(this, action, ...args) },
apply: function(action, args){ apply: function(action, args){
return this.call(action, ...args)}, return this.call(action, ...args)},
@ -1247,18 +1261,15 @@ module.MetaActions = {
_handler _handler
// alias handler... // alias handler...
: function(...args){ : function(...args){
parsed = parsed var parser = this.parseStringAction || parseStringAction
|| (this.parseStringAction parsed = parsed || parser(_handler)
|| parseStringAction)(_handler) return parser.isActionReachable(this, parsed.action) ?
parser.callAction(this, parsed, ...arguments)
return parsed.action in this ?
(this.parseStringAction
|| parseStringAction)
.callAction(this, parsed, ...arguments)
// error... // error...
: console.error( : console.error(
`.on(..): Unknown handler target action: ${parsed.action}`) } `.on(..): Unknown handler target action: ${parsed.action}`) }
// actions as a string/array...
actions = typeof(actions) == 'string' ? actions = typeof(actions) == 'string' ?
actions.split(/\s+/) actions.split(/\s+/)
: actions : actions

View File

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