From 0459aff37804a39e7afb0971ba186fbc058d719c Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Wed, 25 Nov 2020 19:11:25 +0300 Subject: [PATCH] added support for action paths in aliases... Signed-off-by: Alex A. Naanou --- actions.js | 53 +++++++++++++++++++++++++++++++--------------------- package.json | 2 +- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/actions.js b/actions.js index bdc57ca..9d16d9e 100755 --- a/actions.js +++ b/actions.js @@ -185,21 +185,32 @@ Object.assign( rest != null && args.splice(rest, 1, ...call_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??? callAction: function(context, action, ...args){ action = typeof(action) == typeof('str') ? this(action) : action - // XXX should this break if action does not exist??? - return context[action.action] instanceof Function ? - context[action.action] - .apply(context, this.resolveArgs(context, action.arguments, args)) - // action not found or is not callable... (XXX) + var {context, name} = this.resolveAction(context, action.action) + return (context && context[name] instanceof Function) ? + context[name](context, ...this.resolveArgs(context, action.arguments, args)) + // action not found or is not callable... + // XXX should this break if action does not exist??? : undefined }, - applyAction: function(context, action, args){ - return this.callAction(context, action, ...args) }, - // XXX make this stricter... isStringAction: function(txt){ try{ @@ -747,11 +758,14 @@ object.Constructor('Alias', Action, { if(target == ''){ return } + var parser = + this.parseStringAction + || parseStringAction var p = parsed - || (this.parseStringAction || parseStringAction)(target) + || parser(target) - return p.action in this ? - (this.parseStringAction || parseStringAction).callAction(this, p, ...arguments) + return parser.isActionReachable(this, p.action) ? + parser.callAction(this, p, ...arguments) // error... : console.error(`${alias}: Unknown alias target action: ${p.action}`) } func.toString = function(){ @@ -822,7 +836,7 @@ module.MetaActions = { action.apply(this, args) : this[action] ? this[action].apply(this, args) - : this.parseStringAction.applyAction(this, action, args) }, + : this.parseStringAction.callAction(this, action, ...args) }, apply: function(action, args){ return this.call(action, ...args)}, @@ -1247,18 +1261,15 @@ module.MetaActions = { _handler // alias handler... : function(...args){ - parsed = parsed - || (this.parseStringAction - || parseStringAction)(_handler) - - return parsed.action in this ? - (this.parseStringAction - || parseStringAction) - .callAction(this, parsed, ...arguments) + var parser = this.parseStringAction || parseStringAction + parsed = parsed || parser(_handler) + return parser.isActionReachable(this, parsed.action) ? + parser.callAction(this, parsed, ...arguments) // error... : console.error( `.on(..): Unknown handler target action: ${parsed.action}`) } + // actions as a string/array... actions = typeof(actions) == 'string' ? actions.split(/\s+/) : actions diff --git a/package.json b/package.json index 329c56c..d98328b 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-actions", - "version": "3.24.13", + "version": "3.24.14", "description": "", "main": "actions.js", "scripts": {