forked from vitejs/devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogs.ts
More file actions
85 lines (72 loc) · 2.22 KB
/
logs.ts
File metadata and controls
85 lines (72 loc) · 2.22 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
78
79
80
81
82
83
84
85
import type { DevToolsLogEntry, DevToolsRpcClientFunctions } from '@vitejs/devtools-kit'
import type { DocksContext } from '@vitejs/devtools-kit/client'
import type { Reactive } from 'vue'
import { reactive } from 'vue'
import { addToast } from './toasts'
export interface LogsState {
entries: DevToolsLogEntry[]
unreadCount: number
pendingSelectId: string | null
}
let _logsState: Reactive<LogsState> | undefined
export function useLogs(context: DocksContext): Reactive<LogsState> {
if (_logsState)
return _logsState
const state: Reactive<LogsState> = _logsState = reactive({
entries: [],
unreadCount: 0,
pendingSelectId: null,
})
const entryMap = new Map<string, DevToolsLogEntry>()
let isInitialFetch = true
let lastVersion: number | undefined
async function updateLogs() {
const result = await context.rpc.call('devtoolskit:internal:logs:list', lastVersion)
let newCount = 0
// Apply removals
for (const id of result.removedIds)
entryMap.delete(id)
// Apply new/updated entries
for (const entry of result.entries) {
const prev = entryMap.get(entry.id)
if (!prev) {
newCount++
if (isInitialFetch) {
// On initial fetch (page refresh), only toast entries still loading
if (entry.notify && entry.status === 'loading')
addToast(entry)
}
else {
if (entry.notify)
addToast(entry)
}
}
else if (entry.notify && JSON.stringify(entry) !== JSON.stringify(prev)) {
addToast(entry)
}
entryMap.set(entry.id, entry)
}
state.entries = Array.from(entryMap.values())
state.unreadCount += newCount
lastVersion = result.version
isInitialFetch = false
}
context.rpc.client.register({
name: 'devtoolskit:internal:logs:updated' satisfies keyof DevToolsRpcClientFunctions,
type: 'action',
handler: () => {
if (context.rpc.isTrusted)
updateLogs()
},
})
context.rpc.ensureTrusted().then(() => updateLogs())
return state
}
export function markLogsAsRead(): void {
if (_logsState)
_logsState.unreadCount = 0
}
export function selectLog(id: string): void {
if (_logsState)
_logsState.pendingSelectId = id
}