1
1
/**
2
2
* Global Variables
3
3
*/
4
- var timerStates = [
5
- { "state" : "off" , "html" : "popup.html" } ,
6
- new PomodoroState ( ) ,
7
- new BreakState ( ) ] ,
8
- stateInd = 0 ,
9
- currentState = timerStates [ stateInd ] ,
10
- timer ,
11
- timeout ;
4
+ var timerStates = {
5
+ "off" : { "state" : "off" , "html" : "popup.html" , "nextState" : "pomodoro "} ,
6
+ "pomodoro" : new PomodoroState ( ) ,
7
+ "break" : new BreakState ( ) } ,
8
+ stateKey = "off" ,
9
+ currentState = timerStates [ stateKey ] ,
10
+ timer ,
11
+ timeout ;
12
12
13
13
/**
14
14
* Executed Initially
@@ -23,17 +23,17 @@ chrome.browserAction.setPopup({
23
23
chrome . runtime . onMessage . addListener (
24
24
function ( request , sender , sendResponse ) {
25
25
// Only start timer if timer was initially off. No delay.
26
- if ( request . command == "startTimer" && stateInd == 0 ) {
26
+ if ( request . command === "startTimer" && stateKey === "off" ) {
27
27
changeToNextState ( false ) ;
28
28
sendResponse ( { message : "Timer started." } ) ;
29
29
}
30
30
// Only clear timers if timer is not off.
31
- else if ( request . command == "endTimer" && stateInd != 0 ) {
31
+ else if ( request . command === "endTimer" && stateKey !== "off" ) {
32
32
if ( timer ) clearInterval ( timer ) ;
33
33
if ( timeout ) clearTimeout ( timeout ) ;
34
34
timeout = null ;
35
35
timer = null ;
36
- changeState ( 0 , false ) ; // Change to off state
36
+ changeState ( "off" , false ) ; // Change to off state
37
37
chrome . runtime . sendMessage ( {
38
38
command : "timerEnded"
39
39
} ) ;
@@ -70,6 +70,7 @@ function sendUpdatedTime(difference) {
70
70
"command" : "updateTime" ,
71
71
"time" : time
72
72
} ) ;
73
+ chrome . browserAction . setBadgeText ( { "text" : time } ) ;
73
74
}
74
75
75
76
/**
@@ -101,8 +102,8 @@ function notifyUser() {
101
102
* Called during a change of state during usual flow.
102
103
*/
103
104
function changeToNextState ( isDelayed ) {
104
- nextStateInd = currentState . nextState || ( stateInd + 1 ) % timerStates . length ;
105
- changeState ( nextStateInd , isDelayed ) ;
105
+ nextStateKey = currentState . nextState ;
106
+ changeState ( nextStateKey , isDelayed ) ;
106
107
}
107
108
108
109
/**
@@ -111,9 +112,9 @@ function changeToNextState(isDelayed) {
111
112
* between the pomodoro period is over and the break begins to give user time to
112
113
* wrap up.).
113
114
*/
114
- function changeState ( nextStateInd , isDelayed ) {
115
- stateInd = nextStateInd ;
116
- currentState = timerStates [ stateInd ] ;
115
+ function changeState ( nextStateKey , isDelayed ) {
116
+ stateKey = nextStateKey ;
117
+ currentState = timerStates [ stateKey ] ;
117
118
chrome . browserAction . setPopup ( {
118
119
"popup" : currentState . html
119
120
} ) ;
@@ -126,4 +127,4 @@ function changeState(nextStateInd, isDelayed) {
126
127
}
127
128
else startTimer ( ) ;
128
129
}
129
- }
130
+ }
0 commit comments