-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Clock.tsx
77 lines (62 loc) · 1.6 KB
/
Clock.tsx
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
import { IReactionPublic, observable } from 'mobx';
import { Second } from 'web-utility';
import {
attribute,
component,
observer,
on,
reaction,
WebCell
} from '../source';
import { renderMode } from './utility';
class ClockModel {
@observable
accessor time = new Date();
constructor() {
setInterval(() => (this.time = new Date()), Second);
}
}
const clockStore = new ClockModel();
export const FunctionClock = observer(() => {
const { time } = clockStore;
return (
<time dateTime={time.toJSON()}>
Function Clock: {time.toLocaleString()}
</time>
);
});
export interface ClassClock extends WebCell {}
@component({
tagName: 'class-clock',
mode: 'open',
renderMode
})
@observer
export class ClassClock extends HTMLElement implements WebCell {
@attribute
@observable
accessor time = new Date();
private timer: number;
connectedCallback() {
this.timer = window.setInterval(() => (this.time = new Date()), Second);
}
disconnectedCallback() {
clearInterval(this.timer);
}
@reaction(({ time }) => time)
handleReaction(newValue: Date, oldValue: Date, reaction: IReactionPublic) {
console.log(newValue, oldValue, reaction);
}
@on('click', 'time')
handleClick(event: MouseEvent, currentTarget: HTMLTimeElement) {
console.log(event, currentTarget);
}
render() {
const { time } = this;
return (
<time dateTime={time.toJSON()}>
Class Clock: {time.toLocaleString()}
</time>
);
}
}