From a16f6d607919814cc9c02c307dc21def5f19f30b Mon Sep 17 00:00:00 2001 From: Pavlo Andrusiak Date: Wed, 19 Oct 2016 20:20:32 +0300 Subject: [PATCH] Update sync wrapper added: beforeCall, afterCall args --- JavaScript/1-wrap.js | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/JavaScript/1-wrap.js b/JavaScript/1-wrap.js index e90b5d6..c926e18 100644 --- a/JavaScript/1-wrap.js +++ b/JavaScript/1-wrap.js @@ -1,29 +1,34 @@ 'use strict'; -function cloneInterface(anInterface) { - let fn, clone = {}; - for (let key in anInterface) { - fn = anInterface[key]; - clone[key] = wrapFunction(fn); - } - return clone; +function cloneInterface(anInterface, beforeCall, afterCall) { + let fn, clone = {}; + for (let key in anInterface) { + fn = anInterface[key]; + clone[key] = wrapFunction(fn, beforeCall, afterCall); + } + return clone; } -function wrapFunction(fn) { - console.log('Wrap function: ' + fn.name); - return (...args) => { - console.log('Called wrapper for: ' + fn.name); - console.dir({ args }); - fn.apply(undefined, args); - console.log('Ended wrapper for: ' + fn.name); - }; +function wrapFunction(fn, beforeCall, afterCall) { + console.log('Wrap function: ' + fn.name); + return (...args) => { + beforeCall(); + console.log('Called wrapper for: ' + fn.name); + console.dir({ args }); + fn.apply(undefined, args); + console.log('Ended wrapper for: ' + fn.name); + afterCall(); + }; } let interfaceName = { - methodName: function(par1, par2) { - console.dir({ method: { par1, par2 } }); - } + methodName: function(par1, par2) { + console.dir({ method: { par1, par2 } }); + } }; -let cloned = cloneInterface(interfaceName); +let cloned = cloneInterface(interfaceName, + ()=>console.log('aaaaa'), + ()=>console.log('zzzzz') + ); cloned.methodName('Uno', 'Due');