From cfce55579475bea6a4653c84a8d4e408f202a7db Mon Sep 17 00:00:00 2001 From: ser Date: Wed, 8 Jan 2020 22:20:17 +0200 Subject: [PATCH 1/3] Added file d-create-function as home work. --- JavaScript/d-create-function.js | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 JavaScript/d-create-function.js diff --git a/JavaScript/d-create-function.js b/JavaScript/d-create-function.js new file mode 100644 index 0000000..58bc80d --- /dev/null +++ b/JavaScript/d-create-function.js @@ -0,0 +1,40 @@ +'use strict'; + +const wrap = (before, after, fn) => { + const args = []; + for (let i = 1; i <= fn.length; i++) { + args.push(`a${i}`); + } + global.fn = fn; + global.before = before; + global.after = after; + const createFn = new Function(...args, ` + return after(fn(...before(${args.join(', ')}))); + `); + return createFn; +}; + +// Usage + +const func = (par1, par2) => { + console.dir({ par1, par2 }); + return [par1, par2]; +}; + +const before = (...args) => { + console.log('before'); + return args; +}; + +const after = res => { + console.log('after'); + return res; +}; + +const wrapped = wrap(before, after, func); +const res = wrapped('Uno', 'Due'); +console.dir({ + res, + func: func.length, + wrapped: wrapped.length, +}); From 777fd3be23869623113fba7c4f5709e5c0cd71cc Mon Sep 17 00:00:00 2001 From: ser Date: Thu, 9 Jan 2020 23:27:49 +0200 Subject: [PATCH 2/3] added example with timeout for callback --- JavaScript/e-timeout-callback.js | 86 ++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 JavaScript/e-timeout-callback.js diff --git a/JavaScript/e-timeout-callback.js b/JavaScript/e-timeout-callback.js new file mode 100644 index 0000000..41abb9e --- /dev/null +++ b/JavaScript/e-timeout-callback.js @@ -0,0 +1,86 @@ +'use strict'; + +// Wrapper will prevent call after timeout + +const timeout = (msec, f) => { + let timer = setTimeout(() => { + if (timer) console.log('Function timedout'); + timer = null; + }, msec); + return (...args) => { + if (timer) { + clearTimeout(timer); + timer = null; + return f(...args); + } else { + return [new Error('timeout for callback'), null]; + } + }; +}; + +const wrapFunction = f => { + console.log('Wrap function:', f.name); + return (...args) => { + console.log('Called wrapper for:', f.name); + console.dir({ args }); + if (args.length > 0) { + const callback = args[args.length - 1]; + if (typeof callback === 'function') { + const timeoutCallback = timeout(500, callback); + args[args.length - 1] = (...args) => { + console.log('Callback:', f.name); + const cbRes = timeoutCallback(...args); + console.log('Callback results:', cbRes); + return cbRes; + }; + } + } + console.log('Call:', f.name); + console.dir(args); + const result = f(...args); + console.log('Ended wrapper for:', f.name); + console.dir({ result }); + return result; + }; +}; + +const cloneInterface = anInterface => { + const clone = {}; + const keys = Object.keys(anInterface); + for (const key of keys) { + const fn = anInterface[key]; + clone[key] = wrapFunction(fn); + } + return clone; +}; + +// Usage + +const interfaceName = { + methodName(par1, par2, callback) { + console.dir({ par1, par2 }); + setTimeout(() => { + callback(null, { field: 'value' }); + }, 300); + return par1; + }, + methodName2(par1, par2, callback) { + console.dir({ par1, par2 }); + setTimeout(() => { + callback(null, { field: 'value2' }); + }, 700); + return par1; + }, +}; + +const cloned = cloneInterface(interfaceName); + +cloned.methodName('Uno', 'Due', (err, data) => { + console.log({ err, data }); + return true; +}); + +cloned.methodName2('Tre', 'Quattro', (err, data) => { + console.log({ err, data }); + return true; +}); From 238e5cdeefe03f3be6703f06839f2229a993159b Mon Sep 17 00:00:00 2001 From: ser Date: Fri, 10 Jan 2020 00:17:57 +0200 Subject: [PATCH 3/3] Functions fn, before, after removed from global --- JavaScript/d-create-function.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/JavaScript/d-create-function.js b/JavaScript/d-create-function.js index 58bc80d..d79b13a 100644 --- a/JavaScript/d-create-function.js +++ b/JavaScript/d-create-function.js @@ -5,10 +5,10 @@ const wrap = (before, after, fn) => { for (let i = 1; i <= fn.length; i++) { args.push(`a${i}`); } - global.fn = fn; - global.before = before; - global.after = after; const createFn = new Function(...args, ` + const fn = ${fn}; + const before = ${before}; + const after = ${after}; return after(fn(...before(${args.join(', ')}))); `); return createFn;