From ebee9f3213017e718a36a35910ff76148ee3a284 Mon Sep 17 00:00:00 2001 From: Michael Uskov Date: Wed, 1 Oct 2014 01:20:31 +0600 Subject: [PATCH 01/14] Initial --- trafficlights.html | 13 +++++++++++++ trafficlights.js | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 trafficlights.html create mode 100644 trafficlights.js diff --git a/trafficlights.html b/trafficlights.html new file mode 100644 index 0000000..7e4c1ff --- /dev/null +++ b/trafficlights.html @@ -0,0 +1,13 @@ + + +Traffic Lights + + + +

+ + + + + + \ No newline at end of file diff --git a/trafficlights.js b/trafficlights.js new file mode 100644 index 0000000..f54095c --- /dev/null +++ b/trafficlights.js @@ -0,0 +1,47 @@ +function TrafficLight(redTime, yellowTime, greenTime) { + this.__timer = null; + this.__state = null; + this.__redTime = redTime; + this.__yellowTime = yellowTime; + this.__greenTime = greenTime; + + var setnewtimer = function (delay, func) { + if (this.__timer) { + clearTimeout(this.__timer); + } + this.__timer = setTimeout(func, delay); + }; + + TrafficLight.prototype.toRed = function () { + this.__state = 'red'; + setnewtimer.call(this, this.__redTime, function () { + this.toGreen(); + }.bind(this)); + }; + + TrafficLight.prototype.toGreen = function () { + this.__state = 'green'; + setnewtimer.call(this, this.__greenTime, function () { + this.toYellow(); + }.bind(this)); + }; + + TrafficLight.prototype.toYellow = function () { + this.__state = 'yellow'; + setnewtimer.call(this, this.__yellowTime, function () { + this.toRed(); + }.bind(this)); + }; + + TrafficLight.prototype.state = function () { + return this.__state; + }; + + this.toGreen(); +} + +changeDOM = function() { + document.getElementById('colorParagraph').innerHTML = a.state(); +}; + +var a = new TrafficLight(5000, 5000, 5000); \ No newline at end of file From 9c422981e65391b85cd9c324b309bcd76cadfd9b Mon Sep 17 00:00:00 2001 From: Michael Uskov Date: Wed, 1 Oct 2014 12:11:57 +0600 Subject: [PATCH 02/14] Added EventEmitter --- trafficlights.html | 6 ++--- trafficlights.js | 56 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/trafficlights.html b/trafficlights.html index 7e4c1ff..4d41fa9 100644 --- a/trafficlights.html +++ b/trafficlights.html @@ -5,9 +5,9 @@

- - - + + + \ No newline at end of file diff --git a/trafficlights.js b/trafficlights.js index f54095c..b14146f 100644 --- a/trafficlights.js +++ b/trafficlights.js @@ -1,11 +1,41 @@ -function TrafficLight(redTime, yellowTime, greenTime) { +function EventEmitter() { + this.listeners = {}; + + EventEmitter.prototype.addListener = function (event, listener) { + if (this.listeners[event] === undefined) { + this.listeners[event] = []; + } + this.listeners[event].push(listener); + }; + + EventEmitter.prototype.removeListener = function (event, listener) { + if (this.listeners[event] !== undefined) { + for (var i = 0; i < this.listeners[event].length; i++) { + if (this.listeners[event][i] === listener) + this.listeners[event].splice(i,1); + } + } + }; + + EventEmitter.prototype.emit = function (event, data) { + if (this.listeners[event] !== undefined) { + for (var i = 0; i < this.listeners[event].length; i++) { + this.listeners[event][i](data); + } + } + } + +} + + +function TrafficLight(redTime, yellowTime, greenTime) { this.__timer = null; this.__state = null; this.__redTime = redTime; this.__yellowTime = yellowTime; this.__greenTime = greenTime; - var setnewtimer = function (delay, func) { + this.__setnewtimer = function (delay, func) { if (this.__timer) { clearTimeout(this.__timer); } @@ -14,24 +44,32 @@ function TrafficLight(redTime, yellowTime, greenTime) { TrafficLight.prototype.toRed = function () { this.__state = 'red'; - setnewtimer.call(this, this.__redTime, function () { + this.__setnewtimer(this.__redTime, function () { this.toGreen(); }.bind(this)); }; TrafficLight.prototype.toGreen = function () { this.__state = 'green'; - setnewtimer.call(this, this.__greenTime, function () { + this.__setnewtimer(this.__greenTime, function () { this.toYellow(); }.bind(this)); }; TrafficLight.prototype.toYellow = function () { this.__state = 'yellow'; - setnewtimer.call(this, this.__yellowTime, function () { + this.__setnewtimer(this.__yellowTime, function () { this.toRed(); }.bind(this)); }; + + TrafficLight.prototype.tramcallback = function (data) { + console.log('TRAM'); + }; + + TrafficLight.prototype.tramsubscribe = function (event) { + event.addListener('tram', this.tramcallback); + }; TrafficLight.prototype.state = function () { return this.__state; @@ -40,8 +78,10 @@ function TrafficLight(redTime, yellowTime, greenTime) { this.toGreen(); } -changeDOM = function() { - document.getElementById('colorParagraph').innerHTML = a.state(); +changeDOM = function () { + document.getElementById('colorParagraph').innerHTML = tl.state(); }; -var a = new TrafficLight(5000, 5000, 5000); \ No newline at end of file +var tl = new TrafficLight(5000, 5000, 5000); +var event = new EventEmitter(); +tl.tramsubscribe(event); \ No newline at end of file From a6497237b9aa7fa2affbf8e269296008b83170bd Mon Sep 17 00:00:00 2001 From: Michael Uskov Date: Wed, 1 Oct 2014 13:16:37 +0600 Subject: [PATCH 03/14] console.log --- trafficlights.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/trafficlights.js b/trafficlights.js index b14146f..464d103 100644 --- a/trafficlights.js +++ b/trafficlights.js @@ -35,6 +35,7 @@ function TrafficLight(redTime, yellowTime, greenTime) { this.__yellowTime = yellowTime; this.__greenTime = greenTime; + this.__setnewtimer = function (delay, func) { if (this.__timer) { clearTimeout(this.__timer); @@ -43,6 +44,7 @@ function TrafficLight(redTime, yellowTime, greenTime) { }; TrafficLight.prototype.toRed = function () { + console.log('Changed to red'); this.__state = 'red'; this.__setnewtimer(this.__redTime, function () { this.toGreen(); @@ -50,6 +52,7 @@ function TrafficLight(redTime, yellowTime, greenTime) { }; TrafficLight.prototype.toGreen = function () { + console.log('Changed to green'); this.__state = 'green'; this.__setnewtimer(this.__greenTime, function () { this.toYellow(); @@ -57,6 +60,7 @@ function TrafficLight(redTime, yellowTime, greenTime) { }; TrafficLight.prototype.toYellow = function () { + console.log('Changed to yellow'); this.__state = 'yellow'; this.__setnewtimer(this.__yellowTime, function () { this.toRed(); From be2a053a35402bfb943105c9a83198f0600e3516 Mon Sep 17 00:00:00 2001 From: Michael Uskov Date: Wed, 1 Oct 2014 14:33:14 +0600 Subject: [PATCH 04/14] Starting writing listener --- trafficlights.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/trafficlights.js b/trafficlights.js index 464d103..fbe24f5 100644 --- a/trafficlights.js +++ b/trafficlights.js @@ -28,8 +28,11 @@ } -function TrafficLight(redTime, yellowTime, greenTime) { +function TrafficLight(redTime, yellowTime, greenTime) { + this.__trammode = false; + this.__tramtimer = null; this.__timer = null; + this.__tramstate = null; this.__state = null; this.__redTime = redTime; this.__yellowTime = yellowTime; @@ -67,15 +70,26 @@ function TrafficLight(redTime, yellowTime, greenTime) { }.bind(this)); }; - TrafficLight.prototype.tramcallback = function (data) { - console.log('TRAM'); + TrafficLight.prototype.__entertrammode = function () { + console.log('Entering tram mode'); + }; + + TrafficLight.prototype.__tramcallback = function (data) { + console.log('Handling tram event'); + setTimeout(function () { + this.__entertrammode(); + }.bind(this), 3000); }; TrafficLight.prototype.tramsubscribe = function (event) { - event.addListener('tram', this.tramcallback); + event.addListener('tram', function() { + this.__tramcallback(); + }.bind(this)); }; TrafficLight.prototype.state = function () { + if (this.__trammode) + return this.__tramstate; return this.__state; }; @@ -86,6 +100,7 @@ changeDOM = function () { document.getElementById('colorParagraph').innerHTML = tl.state(); }; + var tl = new TrafficLight(5000, 5000, 5000); var event = new EventEmitter(); tl.tramsubscribe(event); \ No newline at end of file From 9736713f53c9a1b7e08233caea5c0079d5b9595f Mon Sep 17 00:00:00 2001 From: Michael Uskov Date: Wed, 1 Oct 2014 14:39:19 +0600 Subject: [PATCH 05/14] Tram mode --- trafficlights.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/trafficlights.js b/trafficlights.js index fbe24f5..aff2edc 100644 --- a/trafficlights.js +++ b/trafficlights.js @@ -70,8 +70,19 @@ function TrafficLight(redTime, yellowTime, greenTime) { }.bind(this)); }; + TrafficLight.prototype.__exittrammode = function () { + console.log('Exit tram mode'); + this.__tramstate = null; + this.__trammode = false; + }; + TrafficLight.prototype.__entertrammode = function () { console.log('Entering tram mode'); + this.__trammode = true; + this.__tramstate = 'green'; + setTimeout(function () { + this.__exittrammode(); + }.bind(this), 10000); }; TrafficLight.prototype.__tramcallback = function (data) { From 742f2051613d1617ba2905aa590c7b8e8c133eb6 Mon Sep 17 00:00:00 2001 From: Michael Uskov Date: Wed, 1 Oct 2014 14:46:31 +0600 Subject: [PATCH 06/14] Only one tram event --- trafficlights.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/trafficlights.js b/trafficlights.js index aff2edc..dcb842c 100644 --- a/trafficlights.js +++ b/trafficlights.js @@ -29,7 +29,7 @@ function TrafficLight(redTime, yellowTime, greenTime) { - this.__trammode = false; + this.__trammode = null; this.__tramtimer = null; this.__timer = null; this.__tramstate = null; @@ -73,12 +73,12 @@ function TrafficLight(redTime, yellowTime, greenTime) { TrafficLight.prototype.__exittrammode = function () { console.log('Exit tram mode'); this.__tramstate = null; - this.__trammode = false; + this.__trammode = null; }; TrafficLight.prototype.__entertrammode = function () { console.log('Entering tram mode'); - this.__trammode = true; + this.__trammode = 'using'; this.__tramstate = 'green'; setTimeout(function () { this.__exittrammode(); @@ -86,10 +86,13 @@ function TrafficLight(redTime, yellowTime, greenTime) { }; TrafficLight.prototype.__tramcallback = function (data) { - console.log('Handling tram event'); - setTimeout(function () { - this.__entertrammode(); - }.bind(this), 3000); + if (!this.__trammode) { + console.log('Handling tram event'); + this.__trammode = 'waiting'; + setTimeout(function () { + this.__entertrammode(); + }.bind(this), 3000); + } }; TrafficLight.prototype.tramsubscribe = function (event) { @@ -99,7 +102,7 @@ function TrafficLight(redTime, yellowTime, greenTime) { }; TrafficLight.prototype.state = function () { - if (this.__trammode) + if (this.__trammode == 'using') return this.__tramstate; return this.__state; }; From d313542c3d634e87659ad0128462f80d3191d4a5 Mon Sep 17 00:00:00 2001 From: Michael Uskov Date: Wed, 1 Oct 2014 15:01:12 +0600 Subject: [PATCH 07/14] Minor changes for JSHint --- trafficlights.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/trafficlights.js b/trafficlights.js index dcb842c..ccac616 100644 --- a/trafficlights.js +++ b/trafficlights.js @@ -7,7 +7,7 @@ } this.listeners[event].push(listener); }; - + EventEmitter.prototype.removeListener = function (event, listener) { if (this.listeners[event] !== undefined) { for (var i = 0; i < this.listeners[event].length; i++) { @@ -23,7 +23,7 @@ this.listeners[event][i](data); } } - } + }; } From 192dff16aa270a2950aa74215cd70a36fa617416 Mon Sep 17 00:00:00 2001 From: Michael Uskov Date: Wed, 1 Oct 2014 15:08:44 +0600 Subject: [PATCH 08/14] Moved constants to private fields --- trafficlights.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/trafficlights.js b/trafficlights.js index ccac616..4e3e03d 100644 --- a/trafficlights.js +++ b/trafficlights.js @@ -37,6 +37,8 @@ function TrafficLight(redTime, yellowTime, greenTime) { this.__redTime = redTime; this.__yellowTime = yellowTime; this.__greenTime = greenTime; + this.__tramWaitTime = 3000; + this.__tramGreenTime = 10000; this.__setnewtimer = function (delay, func) { @@ -82,7 +84,7 @@ function TrafficLight(redTime, yellowTime, greenTime) { this.__tramstate = 'green'; setTimeout(function () { this.__exittrammode(); - }.bind(this), 10000); + }.bind(this), this.__tramGreenTime); }; TrafficLight.prototype.__tramcallback = function (data) { @@ -91,7 +93,7 @@ function TrafficLight(redTime, yellowTime, greenTime) { this.__trammode = 'waiting'; setTimeout(function () { this.__entertrammode(); - }.bind(this), 3000); + }.bind(this), this.__tramWaitTime); } }; From e12be4739f9a57169db3efa7dfd542fae5964c6a Mon Sep 17 00:00:00 2001 From: Michael Uskov Date: Wed, 1 Oct 2014 20:41:12 +0600 Subject: [PATCH 09/14] Stubbed useless change handling --- trafficlights.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/trafficlights.js b/trafficlights.js index 4e3e03d..85cd065 100644 --- a/trafficlights.js +++ b/trafficlights.js @@ -34,19 +34,26 @@ function TrafficLight(redTime, yellowTime, greenTime) { this.__timer = null; this.__tramstate = null; this.__state = null; + this.__stateChangedTime = null; this.__redTime = redTime; this.__yellowTime = yellowTime; this.__greenTime = greenTime; this.__tramWaitTime = 3000; this.__tramGreenTime = 10000; + this.__usefulCoefficient = 0.9; - this.__setnewtimer = function (delay, func) { + TrafficLight.prototype.__setnewtimer = function (delay, func) { if (this.__timer) { clearTimeout(this.__timer); } + this.__stateChangedTime = new Date(); this.__timer = setTimeout(func, delay); }; + + TrafficLight.prototype.__currentStateTime = function () { + return this['__' + this.__state + 'Time']; + }; TrafficLight.prototype.toRed = function () { console.log('Changed to red'); @@ -76,13 +83,16 @@ function TrafficLight(redTime, yellowTime, greenTime) { console.log('Exit tram mode'); this.__tramstate = null; this.__trammode = null; + if (new Date() - this.__stateChangedTime > this.__usefulCoefficient * this.__currentStateTime()) { + console.log('Useless change'); + } }; TrafficLight.prototype.__entertrammode = function () { console.log('Entering tram mode'); this.__trammode = 'using'; this.__tramstate = 'green'; - setTimeout(function () { + this.__tramtimer = setTimeout(function () { this.__exittrammode(); }.bind(this), this.__tramGreenTime); }; @@ -91,16 +101,16 @@ function TrafficLight(redTime, yellowTime, greenTime) { if (!this.__trammode) { console.log('Handling tram event'); this.__trammode = 'waiting'; - setTimeout(function () { + this.__tramtimer = setTimeout(function () { this.__entertrammode(); - }.bind(this), this.__tramWaitTime); + }.bind(this), this.__tramWaitTime); } }; TrafficLight.prototype.tramsubscribe = function (event) { event.addListener('tram', function() { this.__tramcallback(); - }.bind(this)); + }.bind(this)); }; TrafficLight.prototype.state = function () { From 7be20ecb2ca395c1b5c5520c65e155d92a4c171b Mon Sep 17 00:00:00 2001 From: Michael Uskov Date: Wed, 1 Oct 2014 20:45:32 +0600 Subject: [PATCH 10/14] Added working useless change handler --- trafficlights.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/trafficlights.js b/trafficlights.js index 85cd065..3795102 100644 --- a/trafficlights.js +++ b/trafficlights.js @@ -40,7 +40,7 @@ function TrafficLight(redTime, yellowTime, greenTime) { this.__greenTime = greenTime; this.__tramWaitTime = 3000; this.__tramGreenTime = 10000; - this.__usefulCoefficient = 0.9; + this.__usefulCoefficient = 0.7; TrafficLight.prototype.__setnewtimer = function (delay, func) { @@ -84,7 +84,11 @@ function TrafficLight(redTime, yellowTime, greenTime) { this.__tramstate = null; this.__trammode = null; if (new Date() - this.__stateChangedTime > this.__usefulCoefficient * this.__currentStateTime()) { - console.log('Useless change'); + switch (this.__state) { + case 'red': this.toGreen(); break; + case 'yellow': this.toRed(); break; + case 'green': this.toYellow(); break; + } } }; From f326777a1120f9283983026bf642c860a12c7db0 Mon Sep 17 00:00:00 2001 From: Michael Uskov Date: Wed, 1 Oct 2014 20:46:52 +0600 Subject: [PATCH 11/14] Switched from tabs to spaces --- trafficlights.js | 116 +++++++++++++++++++++++------------------------ 1 file changed, 58 insertions(+), 58 deletions(-) diff --git a/trafficlights.js b/trafficlights.js index 3795102..b848531 100644 --- a/trafficlights.js +++ b/trafficlights.js @@ -21,42 +21,42 @@ if (this.listeners[event] !== undefined) { for (var i = 0; i < this.listeners[event].length; i++) { this.listeners[event][i](data); - } - } - }; + } + } + }; } function TrafficLight(redTime, yellowTime, greenTime) { - this.__trammode = null; - this.__tramtimer = null; + this.__trammode = null; + this.__tramtimer = null; this.__timer = null; - this.__tramstate = null; + this.__tramstate = null; this.__state = null; - this.__stateChangedTime = null; + this.__stateChangedTime = null; this.__redTime = redTime; this.__yellowTime = yellowTime; this.__greenTime = greenTime; - this.__tramWaitTime = 3000; - this.__tramGreenTime = 10000; - this.__usefulCoefficient = 0.7; + this.__tramWaitTime = 3000; + this.__tramGreenTime = 10000; + this.__usefulCoefficient = 0.7; TrafficLight.prototype.__setnewtimer = function (delay, func) { if (this.__timer) { clearTimeout(this.__timer); } - this.__stateChangedTime = new Date(); + this.__stateChangedTime = new Date(); this.__timer = setTimeout(func, delay); }; - - TrafficLight.prototype.__currentStateTime = function () { - return this['__' + this.__state + 'Time']; - }; + + TrafficLight.prototype.__currentStateTime = function () { + return this['__' + this.__state + 'Time']; + }; TrafficLight.prototype.toRed = function () { - console.log('Changed to red'); + console.log('Changed to red'); this.__state = 'red'; this.__setnewtimer(this.__redTime, function () { this.toGreen(); @@ -64,7 +64,7 @@ function TrafficLight(redTime, yellowTime, greenTime) { }; TrafficLight.prototype.toGreen = function () { - console.log('Changed to green'); + console.log('Changed to green'); this.__state = 'green'; this.__setnewtimer(this.__greenTime, function () { this.toYellow(); @@ -72,54 +72,54 @@ function TrafficLight(redTime, yellowTime, greenTime) { }; TrafficLight.prototype.toYellow = function () { - console.log('Changed to yellow'); + console.log('Changed to yellow'); this.__state = 'yellow'; this.__setnewtimer(this.__yellowTime, function () { this.toRed(); }.bind(this)); }; - - TrafficLight.prototype.__exittrammode = function () { - console.log('Exit tram mode'); - this.__tramstate = null; - this.__trammode = null; - if (new Date() - this.__stateChangedTime > this.__usefulCoefficient * this.__currentStateTime()) { - switch (this.__state) { - case 'red': this.toGreen(); break; - case 'yellow': this.toRed(); break; - case 'green': this.toYellow(); break; - } - } - }; - - TrafficLight.prototype.__entertrammode = function () { - console.log('Entering tram mode'); - this.__trammode = 'using'; - this.__tramstate = 'green'; - this.__tramtimer = setTimeout(function () { - this.__exittrammode(); - }.bind(this), this.__tramGreenTime); - }; - - TrafficLight.prototype.__tramcallback = function (data) { - if (!this.__trammode) { - console.log('Handling tram event'); - this.__trammode = 'waiting'; - this.__tramtimer = setTimeout(function () { - this.__entertrammode(); - }.bind(this), this.__tramWaitTime); - } - }; - - TrafficLight.prototype.tramsubscribe = function (event) { - event.addListener('tram', function() { - this.__tramcallback(); - }.bind(this)); - }; + + TrafficLight.prototype.__exittrammode = function () { + console.log('Exit tram mode'); + this.__tramstate = null; + this.__trammode = null; + if (new Date() - this.__stateChangedTime > this.__usefulCoefficient * this.__currentStateTime()) { + switch (this.__state) { + case 'red': this.toGreen(); break; + case 'yellow': this.toRed(); break; + case 'green': this.toYellow(); break; + } + } + }; + + TrafficLight.prototype.__entertrammode = function () { + console.log('Entering tram mode'); + this.__trammode = 'using'; + this.__tramstate = 'green'; + this.__tramtimer = setTimeout(function () { + this.__exittrammode(); + }.bind(this), this.__tramGreenTime); + }; + + TrafficLight.prototype.__tramcallback = function (data) { + if (!this.__trammode) { + console.log('Handling tram event'); + this.__trammode = 'waiting'; + this.__tramtimer = setTimeout(function () { + this.__entertrammode(); + }.bind(this), this.__tramWaitTime); + } + }; + + TrafficLight.prototype.tramsubscribe = function (event) { + event.addListener('tram', function() { + this.__tramcallback(); + }.bind(this)); + }; TrafficLight.prototype.state = function () { - if (this.__trammode == 'using') - return this.__tramstate; + if (this.__trammode == 'using') + return this.__tramstate; return this.__state; }; From 92c8dfeb26edaf5ca929f380cbd809e416b3931c Mon Sep 17 00:00:00 2001 From: Michael Uskov Date: Thu, 2 Oct 2014 14:53:00 +0600 Subject: [PATCH 12/14] Emit events with setTimeout --- trafficlights.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/trafficlights.js b/trafficlights.js index b848531..f0e40a5 100644 --- a/trafficlights.js +++ b/trafficlights.js @@ -16,11 +16,17 @@ } } }; + + EventEmitter.prototype.__returnFunction = function (event, position, data) { + return function () { + this.listeners[event][position](data); + }.bind(this); + }; EventEmitter.prototype.emit = function (event, data) { if (this.listeners[event] !== undefined) { for (var i = 0; i < this.listeners[event].length; i++) { - this.listeners[event][i](data); + setTimeout(this.__returnFunction(event, i, data), 0); } } }; From ae9a16948ad4fb8ed08df23bc36c8f1969832125 Mon Sep 17 00:00:00 2001 From: Michael Uskov Date: Thu, 2 Oct 2014 15:10:52 +0600 Subject: [PATCH 13/14] Added JSDoc --- trafficlights.js | 178 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 145 insertions(+), 33 deletions(-) diff --git a/trafficlights.js b/trafficlights.js index f0e40a5..b740d15 100644 --- a/trafficlights.js +++ b/trafficlights.js @@ -1,31 +1,71 @@ -function EventEmitter() { - this.listeners = {}; +/** + * Constructs EventEmitter + * + * @constructor + * @this {EventEmitter} + */ +function EventEmitter() { + /** @private */ this.__listeners = {}; + +/** + * Adds event listener + * + * + * @this {EventEmitter} + * @param {string} event + * @param {func} listener + */ EventEmitter.prototype.addListener = function (event, listener) { - if (this.listeners[event] === undefined) { - this.listeners[event] = []; + if (this.__listeners[event] === undefined) { + this.__listeners[event] = []; } - this.listeners[event].push(listener); + this.__listeners[event].push(listener); }; - + +/** + * Removes event listener + * + * + * @this {EventEmitter} + * @param {string} event + * @param {func} listener + */ EventEmitter.prototype.removeListener = function (event, listener) { - if (this.listeners[event] !== undefined) { - for (var i = 0; i < this.listeners[event].length; i++) { - if (this.listeners[event][i] === listener) - this.listeners[event].splice(i,1); + if (this.__listeners[event] !== undefined) { + for (var i = 0; i < this.__listeners[event].length; i++) { + if (this.__listeners[event][i] === listener) + this.__listeners[event].splice(i,1); } } }; +/** + * Returns function to call in setTimeout + * + * @private + * @this {EventEmitter} + * @param {string} event + * @param {int} position + * @param {string} data Optional data + */ EventEmitter.prototype.__returnFunction = function (event, position, data) { return function () { - this.listeners[event][position](data); + this.__listeners[event][position](data); }.bind(this); }; - + +/** + * Emits event + * + * + * @this {EventEmitter} + * @param {string} event + * @param {string} data Optional data + */ EventEmitter.prototype.emit = function (event, data) { - if (this.listeners[event] !== undefined) { - for (var i = 0; i < this.listeners[event].length; i++) { + if (this.__listeners[event] !== undefined) { + for (var i = 0; i < this.__listeners[event].length; i++) { setTimeout(this.__returnFunction(event, i, data), 0); } } @@ -33,22 +73,41 @@ } - +/** + * Constructs new TrafficLight object + * + * + * @this {TrafficLight} + * @constructor + * @param {int} redTime + * @param {int} yellowTime + * @param {int} greenTime + */ + function TrafficLight(redTime, yellowTime, greenTime) { - this.__trammode = null; - this.__tramtimer = null; - this.__timer = null; - this.__tramstate = null; - this.__state = null; - this.__stateChangedTime = null; - this.__redTime = redTime; - this.__yellowTime = yellowTime; - this.__greenTime = greenTime; - this.__tramWaitTime = 3000; - this.__tramGreenTime = 10000; - this.__usefulCoefficient = 0.7; + /** @private */ this.__trammode = null; + /** @private */ this.__tramtimer = null; + /** @private */ this.__timer = null; + /** @private */ this.__tramstate = null; + /** @private */ this.__state = null; + /** @private */ this.__stateChangedTime = null; + /** @private */ this.__redTime = redTime; + /** @private */ this.__yellowTime = yellowTime; + /** @private */ this.__greenTime = greenTime; + /** @private */ this.__tramWaitTime = 3000; + /** @private */ this.__tramGreenTime = 10000; + /** @private */ this.__usefulCoefficient = 0.7; +/** + * Sets new timer for color changes and checks if the is only one timer + * + * @private + * @this {TrafficLight} + * @param {int} delay + * @param {func} func + * @param {string} data Optional data + */ TrafficLight.prototype.__setnewtimer = function (delay, func) { if (this.__timer) { clearTimeout(this.__timer); @@ -56,11 +115,22 @@ function TrafficLight(redTime, yellowTime, greenTime) { this.__stateChangedTime = new Date(); this.__timer = setTimeout(func, delay); }; - + +/** + * Returns amount of time this color should be active + * + * @private + * @this {TrafficLight} + */ TrafficLight.prototype.__currentStateTime = function () { return this['__' + this.__state + 'Time']; }; +/** + * Changes color to red + * + * @this {TrafficLight} + */ TrafficLight.prototype.toRed = function () { console.log('Changed to red'); this.__state = 'red'; @@ -68,7 +138,12 @@ function TrafficLight(redTime, yellowTime, greenTime) { this.toGreen(); }.bind(this)); }; - + +/** + * Changes color to green + * + * @this {TrafficLight} + */ TrafficLight.prototype.toGreen = function () { console.log('Changed to green'); this.__state = 'green'; @@ -77,6 +152,11 @@ function TrafficLight(redTime, yellowTime, greenTime) { }.bind(this)); }; +/** + * Changes color to yellow + * + * @this {TrafficLight} + */ TrafficLight.prototype.toYellow = function () { console.log('Changed to yellow'); this.__state = 'yellow'; @@ -84,7 +164,13 @@ function TrafficLight(redTime, yellowTime, greenTime) { this.toRed(); }.bind(this)); }; - + +/** + * Exits tram mode + * + * @private + * @this {TrafficLight} + */ TrafficLight.prototype.__exittrammode = function () { console.log('Exit tram mode'); this.__tramstate = null; @@ -97,7 +183,13 @@ function TrafficLight(redTime, yellowTime, greenTime) { } } }; - + +/** + * Enters tram mode + * + * @private + * @this {TrafficLight} + */ TrafficLight.prototype.__entertrammode = function () { console.log('Entering tram mode'); this.__trammode = 'using'; @@ -106,7 +198,13 @@ function TrafficLight(redTime, yellowTime, greenTime) { this.__exittrammode(); }.bind(this), this.__tramGreenTime); }; - + +/** + * Tram mode enter callback for Tram event + * + * @private + * @this {TrafficLight} + */ TrafficLight.prototype.__tramcallback = function (data) { if (!this.__trammode) { console.log('Handling tram event'); @@ -117,12 +215,26 @@ function TrafficLight(redTime, yellowTime, greenTime) { } }; +/** + * Subscribes to Tram event of specified TrafficLight + * + * + * @this {TrafficLight} + * @param {object} event + */ TrafficLight.prototype.tramsubscribe = function (event) { event.addListener('tram', function() { this.__tramcallback(); }.bind(this)); - }; + }; + +/** + * Returns current state + * + * + * @this {TrafficLight} + */ TrafficLight.prototype.state = function () { if (this.__trammode == 'using') return this.__tramstate; From 73ed12bccfdccc67f6a5d802e637652d8d3dd691 Mon Sep 17 00:00:00 2001 From: Michael Uskov Date: Tue, 28 Oct 2014 19:23:41 +0500 Subject: [PATCH 14/14] Now emits Tram event --- trafficlights.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/trafficlights.js b/trafficlights.js index b740d15..d05f2f8 100644 --- a/trafficlights.js +++ b/trafficlights.js @@ -251,4 +251,6 @@ changeDOM = function () { var tl = new TrafficLight(5000, 5000, 5000); var event = new EventEmitter(); -tl.tramsubscribe(event); \ No newline at end of file +tl.tramsubscribe(event); +console.log('Tram event will emit in 3 seconds'); +setTimeout(function(){event.emit('tram')}, 3000); \ No newline at end of file