Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Done homework tasks #3

Open
wants to merge 2 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
5 changes: 5 additions & 0 deletions HOMEWORK.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Homework
* Fixed bug with wrong array name in __3-callback.js__;
* Now timeout in __5-timeout.js__ can be called up to 5 times;
* Async timeout in __6-timeout.js__ can limit time for callbacks;
* General wrapper, pause(), resume() and timeout() have been implemented in __10-wrapper-class.js__.
2 changes: 1 addition & 1 deletion JavaScript/3-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

// const wrapAsync = (before, after, beforeCb, afterCb, fn) =>
// (...args) => {
// const callback = arr[arr.length -1];
// const callback = args[args.length -1];
// if (typeof callback === 'function') {
// args[args.length - 1] = (...pars) =>
// afterCb(callback(...beforeCb(...pars)));
Expand Down
14 changes: 12 additions & 2 deletions JavaScript/5-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ const timeout = (msec, fn) => {
if (timer) console.log('Function timedout');
timer = null;
}, msec);
let callCounter = 0;
return (...args) => {
if (timer) {
if (timer && callCounter < 4) {
callCounter++;
return fn(...args);
}
else if (timer) {
clearTimeout(timer);
timer = null;
return fn(...args);
Expand All @@ -27,5 +32,10 @@ const fn200 = timeout(200, fn);

setTimeout(() => {
fn100('first');
fn200('second');
fn200('1');
fn200('2');
fn200('3');
fn200('4');
fn200('5');
fn200('6');
}, 150);
34 changes: 27 additions & 7 deletions JavaScript/6-timeout-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,29 @@

// Wrapper will prevent call after timeout

const timeout = (msec, fn) => {
const timeout = (fntime, cbtime, fn) => {
let timer = setTimeout(() => {
if (timer) console.log('Function timedout');
timer = null;
}, msec);
}, fntime);
return (...args) => {
if (timer) {
const callback = args[args.length - 1];
if (typeof callback === 'function') {
let cbTimer = setTimeout(() => {
if (cbTimer) console.log('Callback timedout');
cbTimer = null;
}, cbtime);

args[args.length - 1] = (...pars) => {
if (cbTimer) {
clearTimeout(cbTimer);
cbTimer = null;
return callback(...pars);
}
};
}

clearTimeout(timer);
timer = null;
return fn(...args);
Expand All @@ -20,17 +36,21 @@ const timeout = (msec, fn) => {

const fn = (par, callback) => {
console.log('Function called, par:', par);
callback(null, par);
setTimeout(() => callback(null, par), 150);
};

const fn100 = timeout(100, fn);
const fn200 = timeout(200, fn);
const fn100 = timeout(100, 150, fn);
const fn150 = timeout(150, 100, fn);
const fn200 = timeout(200, 250, fn);

setTimeout(() => {
fn100('first', (err, data) => {
console.log('Callback first', data);
});
fn200('second', (err, data) => {
fn150('second', (err, data) => {
console.log('Callback second', data);
});
}, 150);
fn200('third', (err, data) => {
console.log('Callback third', data);
});
}, 100);
59 changes: 59 additions & 0 deletions JavaScript/d-wrapper-class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

class Wrapper {
constructor(limit, fn) {
this.count = limit;
this.calls = 0;
this.pause = false;
this.fn = fn;
this.timedout = false;
}

call(...args) {
if (this.timedout) return;
if (this.calls === this.count) throw new Error('Limit reached');
else if (!this.pause) {
this.calls++;
return this.fn(...args);
}
}

stop() {
if (this.pause) this.pause = false;
else this.pause = true;
return this;
}

timeout(msec) {
let timer = setTimeout(() => {
if (timer) {
timer = null;
console.log('Function timedout');
this.timedout = true;
}
}, msec);
return this;
}

print() {
console.log(`Calls: ${this.calls}\nFunction: ${this.fn}`);
return this;
}
}

//USAGE

const fn = par => {
console.log('Function called, par:', par);
};

const fnLim = new Wrapper(3, fn);
fnLim.call(1);
fnLim.print();
fnLim.stop();
fnLim.call(2);
fnLim
.stop()
.timeout(100);
setTimeout(() => fnLim.call(3), 150);
fnLim.call(4);