-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathstate.ts
More file actions
77 lines (62 loc) · 1.6 KB
/
state.ts
File metadata and controls
77 lines (62 loc) · 1.6 KB
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
export type ScreenType = 'public' | 'operator' | 'maintenance'
export interface AppState {
currentScreen: ScreenType
timezone: string
locale: string
temperature: number | null
humidity: number | null
airPressure: number | null
}
const state: AppState = {
currentScreen: 'public',
timezone: 'UTC',
locale: 'en',
temperature: null,
humidity: null,
airPressure: null,
}
type Listener = (state: AppState) => void
const listeners: Listener[] = []
export function subscribe(listener: Listener) {
listeners.push(listener)
}
function notify() {
listeners.forEach((fn) => fn({ ...state }))
}
/* ===================== */
/* Mutations */
/* ===================== */
export function setScreen(screen: ScreenType) {
state.currentScreen = screen
notify()
}
export function setTimezone(tz: string) {
state.timezone = tz
notify()
}
export function setLocale(locale: string) {
state.locale = locale
notify()
}
export function setSensorReadings(readings: {
temperature?: number | null
humidity?: number | null
airPressure?: number | null
}) {
if (readings.temperature !== undefined)
state.temperature = readings.temperature
if (readings.humidity !== undefined) state.humidity = readings.humidity
if (readings.airPressure !== undefined)
state.airPressure = readings.airPressure
notify()
}
export function getState(): Readonly<AppState> {
return { ...state }
}
let lastPeripheralState: unknown = null
export function setLastPeripheralState(raw: unknown) {
lastPeripheralState = raw
}
export function getLastPeripheralState(): unknown {
return lastPeripheralState
}