Skip to content

Added files d-create-function and e-timeout-callback as home work. #6

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions JavaScript/d-create-function.js
Original file line number Diff line number Diff line change
@@ -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}`);
}
const createFn = new Function(...args, `
const fn = ${fn};
const before = ${before};
const after = ${after};
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,
});
86 changes: 86 additions & 0 deletions JavaScript/e-timeout-callback.js
Original file line number Diff line number Diff line change
@@ -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;
});