-
-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathapp.ts
155 lines (136 loc) · 3.65 KB
/
app.ts
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { Capacitor } from '@capacitor/core'
import { Network } from '@capacitor/network'
import { Keyboard } from '@capacitor/keyboard'
import { App, AppState, AppInfo } from '@capacitor/app'
import { DeviceInfo, DeviceId } from '@capacitor/device'
import debounce from 'lodash-es/debounce'
import { hasNetwork, requestIdleCallback } from './utils'
import redraw from './utils/redraw'
import session from './session'
import * as xhr from './xhr'
import challengesApi from './lichess/challenges'
import * as helper from './ui/helper'
import lobby from './ui/lobby'
import Badge from './badge'
import push from './push'
import router from './router'
import socket from './socket'
import sound from './sound'
import { isForeground, setForeground, setBackground } from './utils/appMode'
let firstConnection = true
export default function appInit(
appInfo: Pick<AppInfo, 'version'>,
deviceInfo: DeviceInfo,
deviceId: DeviceId,
cpuCores: number,
sfMaxMem: number,
cpuArch: string,
): void {
window.lichess.cpuArch = cpuArch
window.deviceInfo = {
platform: deviceInfo.platform,
osVersion: deviceInfo.osVersion,
uuid: deviceId.uuid,
appVersion: appInfo.version,
cpuCores,
stockfishMaxMemory: Math.ceil(sfMaxMem / 16.0) * 16,
}
if (Capacitor.getPlatform() === 'ios') {
Keyboard.setAccessoryBarVisible({ isVisible: true })
}
requestIdleCallback(() => {
// cache viewport dims
helper.viewportDim()
sound.load()
})
App.addListener('appStateChange', (state: AppState) => {
if (state.isActive) {
sound.resume()
setForeground()
session.refresh().then(() => {
if (Capacitor.getPlatform() === 'ios') {
Badge.setNumber({ badge: session.myTurnGames().length })
}
})
socket.cancelDelayedDisconnect()
socket.connect()
redraw()
}
else {
setBackground()
socket.delayedDisconnect(3 * 60 * 1000)
lobby.appCancelSeeking()
}
})
Network.addListener('networkStatusChange', s => {
if (s.connected) {
onOnline()
}
else {
onOffline()
}
})
App.addListener('backButton', router.backbutton)
window.addEventListener('resize', debounce(onResize), false)
// pull session data once (to log in user automatically thanks to cookie)
// and also listen to online event in case network was disconnected at app
// startup
if (hasNetwork()) {
onOnline()
} else {
session.restoreStoredSession()
}
}
function onResize() {
helper.clearCachedViewportDim()
redraw()
}
function onOnline() {
if (isForeground()) {
if (firstConnection) {
firstConnection = false
xhr.status()
getPools()
session.rememberLogin()
.then(() => {
push.register()
challengesApi.refresh()
if (Capacitor.getPlatform() === 'ios') {
Badge.setNumber({ badge: session.myTurnGames().length })
}
redraw()
})
.catch(() => {
console.log('connected as anonymous')
if (Capacitor.getPlatform() === 'ios') {
Badge.setNumber({ badge: 0 })
}
})
} else {
socket.connect()
session.refresh()
}
}
}
function onOffline() {
// TODO check this behavior with capacitor
// offline event fires every time the network connection changes
// it doesn't mean necessarily the network is off
if (isForeground() && !hasNetwork()) {
socket.disconnect()
redraw()
}
}
// pre fetch and cache available pools
// retry 5 times
let nbRetries = 1
function getPools() {
return xhr.lobby()
.then(redraw)
.catch(() => {
if (nbRetries <= 5) {
nbRetries++
setTimeout(getPools, nbRetries * 1000)
}
})
}