-
Notifications
You must be signed in to change notification settings - Fork 243
/
pomodoro.js
132 lines (109 loc) · 3.55 KB
/
pomodoro.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var States = {
RUNNING: 'running',
STOPPED: 'stopped',
IN_BREAK: 'in_break',
PAUSED: 'paused',
};
// options (object)
// options.onTick (function) - Runs on every tick
// options.onBreakStarts (function) - Runs when break starts
// options.onBreakEnds (function) - Runs when break ends
var pomodoro = function(options) {
var _setIntervalId = null;
var _runningDuration = options.runningDuration; // Default pomodoro duration: 20 Min
var _breakDuration = options.breakDuration; // Default break duration: 5 Min
var _runningDurationRemaining = 0; // In seconds
var _breakDurationRemaining = 0; // In seconds
var _currentState = States.STOPPED;
var _onTick = function() {
switch (_currentState) {
case States.RUNNING: _handleTickOnRunning(); break;
case States.IN_BREAK: _handleTickOnBreak(); break;
case States.STOPPED: _handleTickOnStopped(); break;
case States.PAUSED: return;
}
}
var _handleTickOnRunning = function() {
if (_runningDurationRemaining < 1) {
_runningDurationRemaining = 0;
_breakDurationRemaining = _breakDuration * 60;
_currentState = States.IN_BREAK
options.onBreakStarts && options.onBreakStarts();
} else {
_runningDurationRemaining -= 1;
if (options.onTick) options.onTick()
}
};
var _handleTickOnBreak = function() {
if (_breakDurationRemaining < 1) {
_breakDurationRemaining = 0;
_runningDurationRemaining = _runningDuration * 60;
_currentState = States.RUNNING
options.onBreakEnds && options.onBreakEnds();
} else {
_breakDurationRemaining -= 1;
if (options.onTick) options.onTick()
}
};
var _handleTickOnStopped = function() {
clearInterval(_setIntervalId)
_runningDurationRemaining = 0;
_breakDurationRemaining = 0;
_setIntervalId = null;
};
var exports = {
start: function() {
if (_setIntervalId !== null) clearInterval(_setIntervalId);
_runningDurationRemaining = _runningDuration * 60;
_setIntervalId = setInterval(_onTick, 1000);
_currentState = States.RUNNING;
},
stop: function() {
_currentState = States.STOPPED;
},
pause: function() {
_currentState = States.PAUSED;
},
resume: function() {
_currentState = _breakDurationRemaining ? States.IN_BREAK : States.RUNNING;
},
updateRunningDuration() {
if (_runningDuration >= 60) _runningDuration = 1;
else _runningDuration += 1;
},
updateBreakDuration() {
if (_breakDuration >= 60) _breakDuration = 1;
else _breakDuration += 1;
},
getRunningDuration() {
return _runningDuration;
},
getBreakDuration() {
return _breakDuration;
},
getRemainingTime() {
var remainingTime;
switch (_currentState) {
case States.RUNNING: remainingTime = _runningDurationRemaining; break;
case States.IN_BREAK: remainingTime = _breakDurationRemaining; break;
case States.STOPPED: remainingTime = _runningDuration * 60; break;
case States.PAUSED: remainingTime = _runningDurationRemaining || _breakDurationRemaining;
}
return ('0' + Math.floor(remainingTime/60)).slice(-2) + ':' + ('0' + remainingTime % 60).slice(-2);
},
isRunning() {
return _currentState === States.RUNNING;
},
isInBreak() {
return _currentState === States.IN_BREAK;
},
isPaused() {
return _currentState === States.PAUSED;
},
isStopped() {
return _currentState === States.STOPPED;
},
}
return exports;
}
module.exports = pomodoro;