Skip to content

Commit bcfe711

Browse files
committed
states are more readable. browserAction shows time in the icon.
1 parent ab5e629 commit bcfe711

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

background.js

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/**
22
* Global Variables
33
*/
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;
1212

1313
/**
1414
* Executed Initially
@@ -23,17 +23,17 @@ chrome.browserAction.setPopup({
2323
chrome.runtime.onMessage.addListener(
2424
function(request, sender, sendResponse) {
2525
// Only start timer if timer was initially off. No delay.
26-
if (request.command == "startTimer" && stateInd == 0) {
26+
if (request.command === "startTimer" && stateKey === "off") {
2727
changeToNextState(false);
2828
sendResponse({message: "Timer started."});
2929
}
3030
// Only clear timers if timer is not off.
31-
else if (request.command == "endTimer" && stateInd != 0) {
31+
else if (request.command === "endTimer" && stateKey !== "off") {
3232
if (timer) clearInterval(timer);
3333
if (timeout) clearTimeout(timeout);
3434
timeout = null;
3535
timer = null;
36-
changeState(0, false); // Change to off state
36+
changeState("off", false); // Change to off state
3737
chrome.runtime.sendMessage({
3838
command: "timerEnded"
3939
});
@@ -70,6 +70,7 @@ function sendUpdatedTime(difference) {
7070
"command": "updateTime",
7171
"time": time
7272
});
73+
chrome.browserAction.setBadgeText({"text" : time});
7374
}
7475

7576
/**
@@ -101,8 +102,8 @@ function notifyUser() {
101102
* Called during a change of state during usual flow.
102103
*/
103104
function changeToNextState(isDelayed) {
104-
nextStateInd = currentState.nextState || (stateInd + 1) % timerStates.length;
105-
changeState(nextStateInd, isDelayed);
105+
nextStateKey = currentState.nextState;
106+
changeState(nextStateKey, isDelayed);
106107
}
107108

108109
/**
@@ -111,9 +112,9 @@ function changeToNextState(isDelayed) {
111112
* between the pomodoro period is over and the break begins to give user time to
112113
* wrap up.).
113114
*/
114-
function changeState(nextStateInd, isDelayed) {
115-
stateInd = nextStateInd;
116-
currentState = timerStates[stateInd];
115+
function changeState(nextStateKey, isDelayed) {
116+
stateKey = nextStateKey;
117+
currentState = timerStates[stateKey];
117118
chrome.browserAction.setPopup({
118119
"popup": currentState.html
119120
});
@@ -126,4 +127,4 @@ function changeState(nextStateInd, isDelayed) {
126127
}
127128
else startTimer();
128129
}
129-
}
130+
}

breakState.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ function BreakState() {
1111
iconUrl: "icon.png"
1212
};
1313
this.notificationBaseId = "breakOver";
14-
this.nextState = 1;
15-
}
14+
this.nextState = "pomodoro";
15+
}

pomodoroState.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ function PomodoroState() {
1111
iconUrl: "icon.png"
1212
};
1313
this.notificationBaseId = "pomodoroOver";
14-
this.nextState = 2;
15-
}
14+
this.nextState = "break";
15+
}

timer.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ function addOnClick() {
4242
chrome.runtime.sendMessage({
4343
"command": "endTimer"
4444
});
45+
document.location = chrome.runtime.getURL("popup.html");
46+
chrome.browserAction.setBadgeText({"text" : ""});
4547
}
4648
}
4749

48-
document.addEventListener('DOMContentLoaded', init);
50+
document.addEventListener('DOMContentLoaded', init);

0 commit comments

Comments
 (0)