-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathappState.ts
More file actions
208 lines (175 loc) · 6.22 KB
/
Copy pathappState.ts
File metadata and controls
208 lines (175 loc) · 6.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
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
import { mkdirSync, readdirSync, statSync } from 'node:fs'
import { basename, dirname, join, relative } from 'node:path'
import { log } from 'node:console'
import { debounce } from 'perfect-debounce'
import { type Ref, type ShallowRef, type UnwrapRef, nextTick, watch as plainWatch, ref, shallowRef } from '@vue/runtime-core'
import * as vscode from 'vscode'
import type { TraceData } from '../shared/src/traceData'
import { getTracePanel, isTraceViewAlive, postMessage } from './webview'
import { getProjectName, getWorkspacePath } from './storage'
import { setStatusBarState } from './statusBar'
import { sendTraceDir } from './commands'
export const afterWatches = nextTick
export const workspacePath = ref('')
export const projectName = ref('')
export const projectPath = ref('')
export const saveName = ref('')
export const savePath = ref('')
export const saveNames: Ref<string[]> = ref([])
export const projectNames: Ref<string[]> = ref([])
export const traceFiles: ShallowRef<Record<string, TraceData>> = shallowRef({})
export const traceRunning = ref(false)
export const tracePath = ref('')
export const state = {
workspacePath,
projectName,
projectPath,
saveName,
savePath,
saveNames,
projectNames,
traceFiles,
traceRunning,
tracePath,
} as const
type State = typeof state
type StateType<K extends keyof State> = State[K] extends Ref<any> ? UnwrapRef<State[K]> : State[K]
let context: vscode.ExtensionContext
let storagePath: string
let tracePathWatcher: vscode.FileSystemWatcher | undefined
const refreshTraceDir = debounce(async () => {
if (tracePath.value)
await sendTraceDir(tracePath.value)
}, 500)
export async function initAppState(extensionContext: vscode.ExtensionContext) {
context = extensionContext
storagePath = context.globalStorageUri.fsPath
watchT('saveNames', noop, (names) => {
postMessage({ message: 'saveNames', names })
})
watchT('projectNames', noop, names => postMessage({ message: 'projectNames', names }))
watchT('projectName', (name) => {
if (!name)
return
if (!projectNames.value.includes(name))
projectNames.value.push(name)
setStatusBarState('projectName', projectName.value)
getProjectPath()
if (isTraceViewAlive())
getTracePanel().title = `Trace View - ${name}`
function getSaves(atPath: string) {
const files = readdirSync(atPath)
for (const file of files) {
const fullPath = join(atPath, file)
const stat = statSync(fullPath)
if (stat.isDirectory()) {
if (basename(file) === 'traces') {
const savePath = dirname(fullPath)
const saveName = relative(projectPath.value, savePath)
if (!saveNames.value.includes(saveName)) {
saveNames.value.push(saveName)
}
}
else {
getSaves(fullPath)
}
// TODO: check for project config file, particularly to get the last used save name
}
}
}
getSaves(projectPath.value)
saveName.value = 'default'
}, name => postMessage({ message: 'projectOpen', name }))
watchT('savePath', (path) => {
if (!path)
return
mkdirSync(path, { recursive: true })
}, noop)
watchT('tracePath', (path) => {
if (!path)
return
mkdirSync(path, { recursive: true })
tracePathWatcher?.dispose()
tracePathWatcher = vscode.workspace.createFileSystemWatcher(new vscode.RelativePattern(path, '*.json'))
tracePathWatcher.onDidCreate(refreshTraceDir)
tracePathWatcher.onDidChange(refreshTraceDir)
tracePathWatcher.onDidDelete(refreshTraceDir)
context.subscriptions.push(tracePathWatcher)
}, noop)
watchT('saveName', (name) => {
if (!name)
return
if (!saveNames.value.includes(name)) {
saveNames.value.push(name)
triggerWatchRemote('saveNames')
}
postMessage({ message: 'saveOpen', name })
setStatusBarState('saveName', saveName.value)
tracePath.value = join(projectPath.value, name, 'traces')
void sendTraceDir(tracePath.value)
}, (name) => {
postMessage({ message: 'saveOpen', name })
})
watchT('traceFiles', noop, (files) => {
postMessage({ message: 'traceFileLoaded', fileName: '', dirName: tracePath.value, resetFileList: true })
nextTick(() =>
Object.keys(files).forEach((fileName) => {
postMessage({ message: 'traceFileLoaded', fileName, dirName: tracePath.value, resetFileList: false })
}),
)
})
watchT('traceRunning', (running: boolean) => setStatusBarState('tracing', running), (running: boolean) => {
postMessage({ message: running ? 'traceStart' : 'traceStop' })
})
workspacePath.value = getWorkspacePath()
projectName.value = getProjectName()
saveName.value = 'default'
}
const triggers: Partial<Record<keyof State, { handler: ((arg: any) => void | Promise<void>), remoteHandler: (arg: any) => void }>> = {}
export function watchT<K extends keyof State>(triggerName: K, handler: (arg: StateType<K>) => void, remoteHandler: (arg: StateType<K>) => void) {
triggers[triggerName] = { handler, remoteHandler }
plainWatch(state[triggerName], (value: any) => {
try {
handler(value)
remoteHandler(value)
}
catch (e) {
log(`${e}`)
}
})
}
export function triggerWatchRemote(watchName: keyof State) {
triggerWatch(watchName, false, true)
}
export function triggerWatchLocal(watchName: keyof State) {
triggerWatch(watchName, true, false)
}
export function triggerWatch(watchName: keyof State, local = true, remote = true) {
const trigger = triggers[watchName]
if (!trigger)
throw new Error(`watchName not found${watchName}`)
const value = state[watchName].value
if (local) {
const result = trigger.handler(value)
if (remote) {
if (result instanceof Promise)
result.then(() => trigger.remoteHandler(value))
else
trigger.remoteHandler(value)
}
}
else if (remote) {
trigger.remoteHandler(value)
}
}
export function triggerAll(local: boolean, remote: boolean) {
for (const watchName in triggers) {
triggerWatch(watchName as keyof State, local, remote)
}
}
export async function noop() {}
function getProjectPath() {
projectPath.value = join(storagePath, projectName.value)
mkdirSync(projectPath.value, { recursive: true })
return projectPath.value
}