Skip to content

Commit ec0dc7c

Browse files
authored
feat: moved time signals to a separate file (#278)
1 parent 5a2d893 commit ec0dc7c

3 files changed

Lines changed: 34 additions & 18 deletions

File tree

src/ClockHands.tsx

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
1-
import { createSignal, onCleanup } from 'solid-js';
1+
import { onCleanup } from 'solid-js';
22

33
import { ClockLine as ClockHand } from '@/ClockLine';
4-
import { hours, rotate, seconds } from '@/common';
4+
import { time } from '@/time';
55
import { getTestId } from '@/utilities';
66

7-
const getSecondsSinceMidnight = (): number =>
8-
(Date.now() - new Date().setHours(0, 0, 0, 0)) / 1000;
9-
107
export const ClockHands = () => {
11-
const [time, setTime] = createSignal(getSecondsSinceMidnight());
12-
13-
const subsecond = () => rotate(time() % 1, 0);
14-
const second = () => rotate((time() % seconds) / seconds);
15-
const minute = () => rotate(((time() / seconds) % seconds) / seconds);
16-
const hour = () => rotate(((time() / seconds ** 2) % hours) / hours);
17-
188
let frame = requestAnimationFrame(function loop() {
19-
setTime(getSecondsSinceMidnight());
9+
time.update();
2010
frame = requestAnimationFrame(loop);
2111
});
2212

@@ -30,22 +20,22 @@ export const ClockHands = () => {
3020
class="stroke-zinc-200 stroke-3 dark:stroke-zinc-600"
3121
data-testid={getTestId('subsecond')}
3222
length={82}
33-
transform={subsecond()}
23+
transform={time.milisecond}
3424
/>
3525
<ClockHand
3626
class="stroke-zinc-600 stroke-4 dark:stroke-zinc-200"
3727
length={46}
38-
transform={hour()}
28+
transform={time.hour}
3929
/>
4030
<ClockHand
4131
class="stroke-zinc-400 stroke-3"
4232
length={64}
43-
transform={minute()}
33+
transform={time.minute}
4434
/>
4535
<ClockHand
4636
class="stroke-solid-light stroke-2 dark:stroke-solid"
4737
length={76}
48-
transform={second()}
38+
transform={time.second}
4939
/>
5040
</>
5141
);

src/common.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export const seconds = 60;
2-
export const hours = seconds / 5;
32
export const rotate = (rotate: number, fractionDigits = 1) =>
43
`rotate(${(rotate * 360).toFixed(fractionDigits)})`;

src/time.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { createSignal } from 'solid-js';
2+
3+
import { rotate, seconds } from '@/common';
4+
5+
const hours = seconds / 5;
6+
const getSecondsSinceMidnight = (): number =>
7+
(Date.now() - new Date().setHours(0, 0, 0, 0)) / 1000;
8+
9+
const [clock, setClock] = createSignal(getSecondsSinceMidnight());
10+
11+
export const time = {
12+
get hour() {
13+
return rotate(((clock() / seconds ** 2) % hours) / hours);
14+
},
15+
get milisecond() {
16+
return rotate(clock() % 1, 0);
17+
},
18+
get minute() {
19+
return rotate(((clock() / seconds) % seconds) / seconds);
20+
},
21+
get second() {
22+
return rotate((clock() % seconds) / seconds);
23+
},
24+
update: () => {
25+
setClock(getSecondsSinceMidnight());
26+
},
27+
};

0 commit comments

Comments
 (0)