-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
executable file
·102 lines (102 loc) · 3.36 KB
/
index.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
'use strict';
var __importStar =
(this && this.__importStar) ||
function(mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result['default'] = mod;
return result;
};
Object.defineProperty(exports, '__esModule', { value: true });
const React = __importStar(require('react'));
const react_native_1 = require('react-native');
class TimerCountdown extends React.Component {
constructor() {
super(...arguments);
this.state = {
secondsRemaining: this.props.initialSecondsRemaining,
timeoutId: undefined,
previousSeconds: undefined
};
this.tick = () => {
const currentSeconds = Date.now();
const dt = this.state.previousSeconds ? currentSeconds - this.state.previousSeconds : 0;
const interval = 1000;
const intervalSecondsRemaing = interval - (dt % interval);
let timeout = intervalSecondsRemaing;
if (intervalSecondsRemaing < interval / 2.0) {
timeout += interval;
}
const secondsRemaining = Math.max(this.state.secondsRemaining - dt, 0);
const isComplete = !!this.state.previousSeconds && secondsRemaining <= 0;
if (this.state.timeoutId !== undefined) {
clearTimeout(this.state.timeoutId);
}
this.setState({
timeoutId: isComplete ? undefined : setTimeout(this.tick, timeout),
previousSeconds: currentSeconds,
secondsRemaining
});
if (isComplete) {
if (this.props.onComplete) {
this.props.onComplete();
}
return;
}
if (this.props.onTick !== undefined) {
this.props.onTick(secondsRemaining);
}
};
this.getFormattedTime = milliseconds => {
if (this.props.formatSecondsRemaining !== undefined) {
return this.props.formatSecondsRemaining(milliseconds);
}
const remainingSec = Math.round(milliseconds / 1000);
const seconds = parseInt((remainingSec % 60).toString(), 10);
const minutes = parseInt(((remainingSec / 60) % 60).toString(), 10);
const hours = parseInt((remainingSec / 3600).toString(), 10);
const s = seconds < 10 ? '0' + seconds : seconds;
const m = minutes < 10 ? '0' + minutes : minutes;
let h = hours < 10 ? '0' + hours : hours;
h = h === '00' ? '' : h + ':';
return h + m + ':' + s;
};
}
componentDidMount() {
this.tick();
}
componentWillReceiveProps(newProps) {
if (this.state.timeoutId !== undefined) {
clearTimeout(this.state.timeoutId);
}
this.setState({
previousSeconds: undefined,
secondsRemaining: newProps.initialSecondsRemaining
});
}
componentDidUpdate() {
if (!this.state.previousSeconds && this.state.secondsRemaining > 0) {
this.tick();
}
}
componentWillUnmount() {
clearTimeout(this.state.timeoutId);
}
render() {
const secondsRemaining = this.state.secondsRemaining;
const allowFontScaling = !!this.props.allowFontScaling;
const style = this.props.style;
return (
<react_native_1.Text allowFontScaling={allowFontScaling} style={style}>
{this.getFormattedTime(secondsRemaining)}
</react_native_1.Text>
);
}
}
TimerCountdown.defaultProps = {
formatSecondsRemaining: undefined,
onTick: undefined,
onComplete: undefined
};
exports.default = TimerCountdown;