-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.ts
More file actions
93 lines (76 loc) · 2.42 KB
/
Copy pathapp.ts
File metadata and controls
93 lines (76 loc) · 2.42 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
namespace microcode {
// Auto-save slot
export const SAVESLOT_AUTO = "sa"
export interface SavedState {
progdef: any
version?: string
}
export class App {
private host_: AppHost
constructor() {
// One interval delay to ensure all static constructors have executed.
basic.pause(500)
const buf = this.load(SAVESLOT_AUTO)
if (buf) {
const prog = ProgramDefn.fromBuffer(new BufferReader(buf))
runProgram(prog)
}
this.host_ = new AppHost(this)
this.openHome()
}
public save(slot: string, buf: Buffer) {
// console.log(`save to ${slot}: ${buf.length}b`)
profile()
settings.writeBuffer(slot, buf)
return true
}
public load(slot: string): Buffer {
try {
return settings.readBuffer(slot)
} catch (e) {
console.log(e)
}
return undefined
}
public openHome() {
this.host_.launchHome()
}
public runFromEditor() {
const hostedProgram = this.host_.currentEditorProgram()
if (hostedProgram) {
runProgramIfStopped(hostedProgram)
}
}
}
let theInterpreter: Interpreter = undefined
export function runProgram(prog: ProgramDefn) {
if (theInterpreter) theInterpreter.stop()
theInterpreter = new Interpreter(prog, runtimeHost)
}
export function runProgramIfStopped(prog: ProgramDefn): boolean {
if (isProgramRunning()) return false
runProgram(prog)
return true
}
export function replaceAutoProgram(buf: Buffer): void {
const wasRunning = isProgramRunning()
if (wasRunning) stopProgram()
settings.writeBuffer(SAVESLOT_AUTO, buf)
if (wasRunning)
runProgram(ProgramDefn.fromBuffer(new BufferReader(buf)))
}
export function stopProgram() {
if (theInterpreter) theInterpreter.stop()
theInterpreter = undefined
}
export function stopProgramIfRunning(): boolean {
if (!isProgramRunning()) return false
stopProgram()
basic.showIcon(IconNames.No, 100)
basic.clearScreen()
return true
}
export function isProgramRunning() {
return theInterpreter != undefined
}
}