-
Notifications
You must be signed in to change notification settings - Fork 13
"Умный" светофор с 2 бонусами #3
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
Open
michaeluskov
wants to merge
14
commits into
cri-2014:master
Choose a base branch
from
michaeluskov:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ebee9f3
Initial
michaeluskov 9c42298
Added EventEmitter
michaeluskov a649723
console.log
michaeluskov be2a053
Starting writing listener
michaeluskov 9736713
Tram mode
michaeluskov 742f205
Only one tram event
michaeluskov d313542
Minor changes for JSHint
michaeluskov 192dff1
Moved constants to private fields
michaeluskov e12be47
Stubbed useless change handling
michaeluskov 7be20ec
Added working useless change handler
michaeluskov f326777
Switched from tabs to spaces
michaeluskov 92c8dfe
Emit events with setTimeout
michaeluskov ae9a169
Added JSDoc
michaeluskov 73ed12b
Now emits Tram event
michaeluskov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <html> | ||
| <head> | ||
| <title>Traffic Lights</title> | ||
| </head> | ||
| <body> | ||
| <button onclick="javascript:changeDOM()">Color?</button> | ||
| <p id="colorParagraph"></p> | ||
| <button onclick="javascript:tl.toRed()">To red</button> | ||
| <button onclick="javascript:tl.toYellow()">To yellow</button> | ||
| <button onclick="javascript:tl.toGreen()">To green</button> | ||
| </body> | ||
| <script type="text/javascript" src="trafficlights.js"></script> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,256 @@ | ||
| /** | ||
| * 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] = []; | ||
| } | ||
| 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); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * 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); | ||
| }.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++) { | ||
| setTimeout(this.__returnFunction(event, i, data), 0); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Constructs new TrafficLight object | ||
| * | ||
| * | ||
| * @this {TrafficLight} | ||
| * @constructor | ||
| * @param {int} redTime | ||
| * @param {int} yellowTime | ||
| * @param {int} greenTime | ||
| */ | ||
|
|
||
| function TrafficLight(redTime, yellowTime, greenTime) { | ||
| /** @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); | ||
| } | ||
| 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'; | ||
| this.__setnewtimer(this.__redTime, function () { | ||
| this.toGreen(); | ||
| }.bind(this)); | ||
| }; | ||
|
|
||
| /** | ||
| * Changes color to green | ||
| * | ||
| * @this {TrafficLight} | ||
| */ | ||
| TrafficLight.prototype.toGreen = function () { | ||
| console.log('Changed to green'); | ||
| this.__state = 'green'; | ||
| this.__setnewtimer(this.__greenTime, function () { | ||
| this.toYellow(); | ||
| }.bind(this)); | ||
| }; | ||
|
|
||
| /** | ||
| * Changes color to yellow | ||
| * | ||
| * @this {TrafficLight} | ||
| */ | ||
| TrafficLight.prototype.toYellow = function () { | ||
| console.log('Changed to yellow'); | ||
| this.__state = 'yellow'; | ||
| this.__setnewtimer(this.__yellowTime, function () { | ||
| this.toRed(); | ||
| }.bind(this)); | ||
| }; | ||
|
|
||
| /** | ||
| * Exits tram mode | ||
| * | ||
| * @private | ||
| * @this {TrafficLight} | ||
| */ | ||
| 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; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Enters tram mode | ||
| * | ||
| * @private | ||
| * @this {TrafficLight} | ||
| */ | ||
| 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); | ||
| }; | ||
|
|
||
| /** | ||
| * Tram mode enter callback for Tram event | ||
| * | ||
| * @private | ||
| * @this {TrafficLight} | ||
| */ | ||
| 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); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * 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; | ||
| return this.__state; | ||
| }; | ||
|
|
||
| this.toGreen(); | ||
| } | ||
|
|
||
| changeDOM = function () { | ||
| document.getElementById('colorParagraph').innerHTML = tl.state(); | ||
| }; | ||
|
|
||
|
|
||
| var tl = new TrafficLight(5000, 5000, 5000); | ||
| var event = new EventEmitter(); | ||
| tl.tramsubscribe(event); | ||
| console.log('Tram event will emit in 3 seconds'); | ||
| setTimeout(function(){event.emit('tram')}, 3000); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Вот тут не понял, если условие выполняется - переключаемся в следующий цвет.
А если не выполняется, то ... ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Если светофор "прогорел в фоне" больше, чем this.__usefulCoefficient * кол-во времени, которое по настройкам он должен прогореть, то переключиться на следующий. если нет, то ничего не делать, вернуться в то состояние, которое было в фоне, ибо и без этого возврат будет адекватным.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Все, понял. Норм!