This repository has been archived by the owner on Aug 31, 2024. It is now read-only.
generated from IzK-ArcOS/v6-App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.ts
170 lines (127 loc) · 3.93 KB
/
runtime.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { AppRuntime } from "$ts/apps/runtime";
import { GlobeIcon, SpinnerIcon } from "$ts/images/general";
import { Process } from "$ts/process";
import { Store } from "$ts/writable";
import type { App, AppMutator } from "$types/app";
import axios from "axios";
export class Runtime extends AppRuntime {
public isHttpUrl = Store<boolean>(true);
public currentUrl = Store<string>("");
public currentHostname = Store<string>();
public loading = Store<boolean>(false);
public loadError = Store<boolean>(false);
public iframe: HTMLIFrameElement;
public loadStart = Store<number>(0);
public loadEnd = Store<number>(0);
public loadDuration = Store<number>(0);
public history = Store<string[]>([]);
public historyPointer = Store<number>(-1);
private _initialized = false;
constructor(app: App, mutator: AppMutator, process: Process) {
super(app, mutator, process);
this.currentUrl.subscribe((v) => {
if (!v) return;
this.isHttpUrl.set(true);
let hostname = "";
try {
hostname = new URL(v).hostname;
} catch {
hostname = "localhost";
this.isHttpUrl.set(false);
}
this.currentHostname.set(hostname);
});
}
public async navigate(url: string) {
this.loadError.set(false);
if (!url) return;
this.loading.set(true);
this.loadStart.set(performance.now());
if (!this.iframe) return;
this.iframe.src = "";
const loadable = await this.checkIfLoadable(url);
if (!loadable && this.isHttpUrl.get()) {
this.loadError.set(true);
this.loading.set(false);
return;
}
this.iframe.src = url;
}
public async Go() {
const address = this.currentUrl.get();
this.addToHistory(address);
this.navigate(address);
}
public refresh() {
this.currentUrl.set(this.iframe.src);
this.Go();
}
private _registerEventListeners() {
if (!this.iframe) return;
this.iframe.addEventListener("load", () => {
this.currentUrl.set(this.iframe.getAttribute("src"));
this.loading.set(false);
this.loadEnd.set(performance.now());
this.loadDuration.set(this.loadEnd.get() - this.loadStart.get());
});
}
public initialize(iframe: HTMLIFrameElement) {
if (!iframe || this._initialized) return;
this.iframe = iframe;
this._registerEventListeners();
this._initialized = true;
this.loading.subscribe((v) => {
console.log(v ? "Loading" : "No longer loading");
const app = this.appMutator.get();
app.metadata.icon = v ? SpinnerIcon : GlobeIcon;
this.appMutator.set(app);
});
setTimeout(() => {
const args = this.process.args;
if (!args.length || typeof args[0] !== "string") return;
this.navigate(args[0]);
});
}
public async checkIfLoadable(url: string) {
try {
const res = await axios.post("https://ifc.izaak-kuipers.workers.dev", { url });
return res.data.canLoadInIframe;
} catch (e) {
return true;
}
}
public truncateForwardHistory() {
const historyPointer = this.historyPointer.get();
let history = this.history.get();
if (historyPointer < history.length - 1) {
history = history.slice(0, historyPointer + 1);
}
this.history.set(history);
}
public addToHistory(url: string) {
const history = this.history.get();
let pointer = this.historyPointer.get();
history.push(url);
pointer++;
this.historyPointer.set(pointer);
this.history.set(history);
}
public goBack() {
const history = this.history.get();
let pointer = this.historyPointer.get();
if (pointer > 0) {
pointer--;
this.historyPointer.set(pointer);
this.navigate(history[pointer]);
}
}
public goForward() {
const history = this.history.get();
let pointer = this.historyPointer.get();
if (pointer < history.length - 1) {
pointer++;
this.historyPointer.set(pointer);
this.navigate(history[pointer]);
}
}
}