-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootloader.js
199 lines (171 loc) · 6.47 KB
/
bootloader.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
'use strict';
(function () {
function Aborted() {}
/////////////////////////////////////////////////////
function Utils() {}
Utils.prototype.clamp = function(x, min, max) {
return Math.max(Math.min(x, max), min);
};
Utils.prototype.get = function(url, progress) {
progress = progress || (frac => {});
return new Promise((resolve, reject) => {
progress(0);
var req = new XMLHttpRequest();
req.onload = function() {
if (this.status >= 200 && this.status < 300) {
progress(1);
resolve(this.responseText);
}
else {
reject(new Error(this.statusText + "\n" + this.responseText));
}
};
req.onerror = function (ev) {
console.error(ev);
if (ev.type === 'error') {
reject(new Error("interrupted"));
} else {
reject(new Error("unknown error"));
}
};
req.onabort = function (ev) {
console.error(ev);
reject(new Aborted());
};
req.onprogress = function (ev) {
if (ev.lengthComputable) progress(ev.loaded / ev.total);
};
req.open("GET", url);
req.overrideMimeType("text/plain");
req.send();
});
};
Utils.prototype.later = function(ms) {
return new Promise((resolve, reject) => {
window.setTimeout(() => resolve(), ms);
});
};
Utils.prototype.decodeTiddlerFields = function(text) {
text = text.split("\n");
var title = text.splice(0, 1)[0];
var fields = JSON.parse(text.splice(0, 1)[0]);
text = text.join("\n");
fields.title = title;
fields.text = text;
return fields;
};
/////////////////////////////////////////////////////
function Bootloader(elBootloader)
{
this.elBootloader = elBootloader;
this.elProgress = elBootloader.querySelector(".progress");
this.elError = elBootloader.querySelector(".error");
this.utils = new Utils();
this.config = {
tiddlersList: "./tiddlers.list",
kernelPrefix: "$:/boot/bootprefix.js",
kernel: "$:/boot/boot.js",
};
}
Bootloader.prototype.boot = function () {
this.loadTiddlersList()
.then(list => this.loadTiddlers(list))
.then(tiddlers => this.bootKernelPrefix(tiddlers))
.then(tiddlers => this.bootTiddlers(tiddlers))
.then(kernelTiddler => this.bootKernel(kernelTiddler))
.then(() => this.done())
.catch(err => {
console.error(err);
this.showError("ABORTED");
this.utils.later(400).then(() => {
if (!(err instanceof Aborted)) alert(err);
debugger;
throw err; // stop the rest of script from execution
});
})
;
};
Bootloader.prototype.loadTiddlersList = function () {
this.setProgressInit();
return this.utils.get(this.config.tiddlersList)
.then(titles => titles.split(/\n+/).filter(x => x.length));
};
Bootloader.prototype.loadTiddlers = function (pathList) {
this.setProgressLoading(0, 1);
var progresses = [];
var pending = [];
var rejected = false;
pathList.forEach((path, i) => {
var load = this.utils.get(path, frac => {
if (rejected) return;
progresses[i] = frac;
var progress = progresses.reduce((x, y) => x + y, 0);
this.setProgressLoading(progress, pathList.length);
}).catch(err => {
rejected = true;
throw err;
});
pending.push(load);
});
return Promise.all(pending)
.then(tiddlers => tiddlers.map(text => this.utils.decodeTiddlerFields(text)));
};
Bootloader.prototype.bootKernelPrefix = function (tiddlers) {
return this.utils.later().then(() => {
var tiddler = tiddlers.find(x => x.title === this.config.kernelPrefix);
if (!tiddler) throw new Error("kernel prefix not found: " + this.config.kernelPrefix);
eval.call(window, tiddler.text);
return tiddlers;
});
};
Bootloader.prototype.bootTiddlers = function (tiddlers) {
return this.utils.later().then(() => {
var kernel = tiddlers.find(x => x.title === this.config.kernel);
window.$tw.preloadTiddlerArray(tiddlers);
return kernel;
});
};
Bootloader.prototype.bootKernel = function (kernelTiddler) {
return this.utils.later(400).then(() => {
if (!kernelTiddler) throw new Error("kernel not found: " + this.config.kernel);
eval.call(window, kernelTiddler.text);
});
};
Bootloader.prototype.done = function () {
console.log("bootloader done.");
this.setProgress(1);
this.utils.later(200)
.then(() => this.setBootloaderOpacity(0))
.then(() => this.utils.later(600))
.then(() => this.hideBootloader());
;
};
Bootloader.prototype.setProgressInit = function () {
this.setProgress(0.05);
};
// initial progress is 10%, last progress is 99%
Bootloader.prototype.setProgressLoading = function (i, total) {
this.setProgress(i / total * 0.89 + 0.10);
};
Bootloader.prototype.setProgress = function (frac) {
frac = Math.exp((frac - 1) * 5);
var percent = this.utils.clamp(parseInt(frac * 100), 0, 100);
this.elProgress.style.width = percent + "%";
};
Bootloader.prototype.setBootloaderOpacity = function (opacity) {
this.elBootloader.style.backgroundColor = "rgba(255, 255, 255, " + opacity + ")";
this.utils.later(200).then(() => this.elBootloader.style.opacity = opacity);
};
Bootloader.prototype.hideBootloader = function () {
this.elBootloader.style.display = "none";
};
Bootloader.prototype.showError = function (err) {
this.elError.textContent = err;
};
this.$bootloader = "tearly";
window.onload = ev => {
var el = document.querySelector("#bootloader");
var bl = new Bootloader(el);
bl.boot();
};
}).call(this);