-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
275 lines (265 loc) · 8.05 KB
/
main.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* eslint-disable no-new */
const { momentTime, meta, gql, uuidv4, get, query, invoke, send, on: rawOn } = window.app
const ipc = new EventTarget()
const on = (name, callback) => {
rawOn(name)
ipc.addEventListener(name, ({ detail }) => callback(undefined, ...detail))
}
window.addEventListener('electron-ipc', ({ detail: { name, args } }) => ipc.dispatchEvent(new CustomEvent(name, { detail: args })))
const Vue = window.Vue
const updates = [
['1.0.1', '- 修复macOS一些兼容性问题'],
['1.1.0', '- 显示日志'],
['1.2.1', '- Electron升级到v7'],
['1.2.2', '- 降低并行, 加大间隔过小的警告范围'],
['1.2.3', '- 降低并行, 增加间隔'],
['1.5.0', '- 核心依赖DD@Nodejs'],
['1.6.0', '- 发弹幕功能'],
['1.7.0', '- 删去Pulls, 显示DD力'],
['1.7.4', '- 改进macOS的Notarize过程'],
['1.8.0', `
- 改进Main-Renderer进程的IPC弹幕同步以及渲染弹幕的逻辑
- 记住50个弹幕历史→_→!
第一次写更新日志
有时间把之前的补上吧`],
['1.8.1', '- 更新electron-updater修复一个导致软件无法更新的问题'],
['1.9.0', `
- 把API改成GraphQL了
- 记住数据的功能
- 补上了一部分之前的更新记录`],
['1.9.1', `
- 修好了弹幕会重叠的问题,感谢@ouuan
- 显示弹幕时间
- 自己的状态粗体显示`],
['1.9.2', '- 修好了1.9.1引入的导致不能显示界面问题,感谢@ouuan'],
['1.9.3', `
- 降低electron版本尝试修复macOS不能公证的问题
- 弹幕太长的提示`],
['1.9.4', `
- 弹幕是空的时候不能发送@ouuan
- 改变了弹幕时间的位置@ouuan`],
['1.9.6', '- 添加了命令行版本的下载链接'],
['1.10.0', `
嘿嘿 好久不见
- 更新了一部分依赖包
- Electron 从8更新到10
- 更新了DD@Home核心`],
['1.10.1', '- Electron 从10更新到8 (?)'],
['1.11.0', `
- 加入了转发连接直播间的功能`],
['1.12.0', '- 展示直播间监听状态,设置直播间监听数上限'],
['1.13.0', '- 更新Electron@15'],
['1.14.0', '- 更新ddatnodejs'],
['1.14.2', '- 修复了一个导致状态不刷新的问题']
].map(([version, message]) => [version, message.split('\n')]).reverse()
new Vue({
el: '#main',
data: {
version: meta.version,
state: {
completeNum: undefined,
completeNumNow: undefined,
delay: undefined,
INTERVAL: undefined,
url: undefined,
update: undefined,
updateProgress: undefined,
updateDownloaded: undefined,
nickname: undefined,
log: undefined,
pending: undefined,
power: 0,
online: undefined,
homes: [],
danmakuLength: 0,
uuid: undefined,
id: undefined,
danmaku: undefined,
roomLength: 0,
totalActive: 0,
livedRooms: 0,
wsLimit: undefined
},
displayDanmaku: {
danmakuPack: [],
showingPack: [],
observer: undefined
},
logs: [],
uptime: undefined,
interval: undefined,
nickname: undefined,
uuid: undefined,
wsLimit: undefined,
danmaku: '',
danmakuWait: false,
now: Date.now(),
updates
},
watch: {
interval(value) {
if (value) {
invoke('updateInterval', Number(value))
}
},
nickname(value) {
invoke('updateNickname', value)
},
wsLimit(limit) {
invoke('updateWebSocketLimit', limit)
},
'state.log'(log) {
this.logs.unshift(log)
if (this.logs.length > 233) {
this.logs.pop()
}
},
async danmakuDetectors(value) {
await this.$nextTick()
value.forEach(({ i }) => {
this.displayDanmaku.observer.observe(document.getElementById(`danmakuDetectors_${i}`))
})
},
'displayDanmaku.showingPack'(value) {
value.forEach((w, i) => {
if (w) {
if (!this.displayDanmaku.danmakuPack[i]) {
this.getDanmaku(i)
}
}
})
},
'state.danmakuLength'() {
if (this.displayDanmaku.danmakuPack.length) {
this.getDanmaku(this.displayDanmaku.danmakuPack.length - 1)
}
}
},
methods: {
send(e) {
e.preventDefault()
if (this.danmaku && !this.danmakuWait) {
this.danmakuWait = true
setTimeout(() => {
this.danmakuWait = false
}, 1000)
send('danmaku', this.danmaku)
this.danmaku = ''
}
},
close() {
if (this.uuid) {
invoke('updateUUID', this.uuid)
}
invoke('close')
},
restart() {
invoke('restart')
},
randomUUID() {
this.uuid = uuidv4()
},
async getDanmaku(i) {
const number = Math.min(100, this.state.danmakuLength - i * 100)
const skip = i * 100
const { danmaku: { danmaku } = {} } = await query(gql`query getDanmaku($number:Int!,$skip:Int!) {danmaku{danmaku(number:$number, skip:$skip){name text timestamp}}}`, { number, skip })
.catch(() => ({}))
if (danmaku) {
Vue.set(this.displayDanmaku.danmakuPack, i, danmaku
.map(({ name, text, timestamp }, i) => ({ name, text, timestamp: Number(timestamp), n: i + skip }))
.map(({ name, text, timestamp, n }) => ({ name, text, timestamp, n, bottom: n * 24, i: n % 300 })))
}
}
},
computed: {
intervalWarning() {
return this.interval && Number(this.interval) < 500
},
homes() {
return this.state.homes
.map(({ runtime = 'Home', version = '', docker, platform = '', name = 'DD', resolves, rejects, id }) => ({
id,
name,
sum: resolves + rejects,
resolves,
runtime,
platform: docker || platform,
version
}))
.sort(({ resolves: a }, { resolves: b }) => b - a)
},
powerSec() {
const round = this.state.power / 60
if (round < 1) {
return Math.round(round * 10) / 10
}
return Math.round(round)
},
longDanmaku() {
return this.danmaku.length > 256
},
emptyDanmaku() {
return this.danmaku.length === 0
},
danmakuHeight() {
return this.state.danmakuLength * 24
},
danmakuDetectors() {
const num = Math.trunc(this.state.danmakuLength / 100) + 1
const height = 100 * 24
return Array(num)
.fill(height)
.map((height, i) => ({ height, bottom: i * height, i }))
},
danmakus() {
const blank = Array(300).fill(-100).map((bottom, i) => ({ bottom, i }))
const showing = this.displayDanmaku.danmakuPack.filter((_, i) => this.displayDanmaku.showingPack[i])
showing.flat().forEach(({ name, text, timestamp, bottom, i }) => {
const { absoluteTime, calendar, fromNow } = momentTime(timestamp)
blank[i] = { name, text, absoluteTime, relativeTime: this.now - timestamp > 1000 * 60 * 60 ? calendar : fromNow, bottom, i }
})
return blank
},
averageActive() {
const { roomLength, totalActive } = this.state
return Math.round(totalActive / roomLength * 10) / 10
}
},
async created() {
const logs = await get('logs')
logs.shift()
this.logs = logs
on('stateUpdate', (_events, key, value) => {
this.state[key] = value
})
Object.keys(this.state).forEach(async key => {
this.state[key] = await get(key)
})
const interval = () => {
(async () => {
this.uptime = await invoke('uptime')
})()
this.now = Date.now()
return interval
}
setInterval(interval(), 1000)
this.displayDanmaku.observer = new IntersectionObserver(entries => entries.forEach(({ isIntersecting, target }) => {
const n = Number(target.getAttribute('n'))
if (isIntersecting) {
Vue.set(this.displayDanmaku.showingPack, n, true)
} else {
Vue.set(this.displayDanmaku.showingPack, n, false)
}
}), {
root: document.getElementById('danmakuBox'),
rootMargin: '600px 0px 600px 1000000px',
thresholds: [0]
})
},
async mounted() {
document.getElementById('main').style.display = 'block'
await this.$nextTick()
invoke('ready')
}
})