Skip to content

Commit f190cef

Browse files
author
NI
committed
Better code (callback -> async) and protection aganist early tab close (Close before terminal is loaded)
1 parent 8bdfbeb commit f190cef

1 file changed

Lines changed: 110 additions & 45 deletions

File tree

ui/widgets/screen_console.vue

Lines changed: 110 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ class Term {
188188
oldCols = 0;
189189
190190
this.term.onResize(dim => {
191+
if (this.closed) {
192+
return;
193+
}
194+
191195
if (dim.cols === oldCols && dim.rows === oldRows) {
192196
return;
193197
}
@@ -220,16 +224,28 @@ class Term {
220224
}
221225
222226
setFont(value) {
227+
if (this.closed) {
228+
return;
229+
}
230+
223231
this.term.setOption("fontFamily", value);
224232
}
225233
226234
init(root, callbacks) {
235+
if (this.closed) {
236+
return;
237+
}
238+
227239
this.term.open(root);
228240
229241
this.term.textarea.addEventListener("focus", callbacks.focus);
230242
this.term.textarea.addEventListener("blur", callbacks.blur);
231243
232244
this.term.textarea.addEventListener("keyup", async ev => {
245+
if (this.closed) {
246+
return;
247+
}
248+
233249
if (ev.ctrlKey && ev.shiftKey) {
234250
switch (ev.keyCode) {
235251
case 86:
@@ -258,6 +274,10 @@ class Term {
258274
});
259275
260276
this.term.element.addEventListener("click", () => {
277+
if (this.closed) {
278+
return;
279+
}
280+
261281
this.term.textarea.blur();
262282
this.term.textarea.click();
263283
this.term.textarea.focus();
@@ -267,6 +287,10 @@ class Term {
267287
}
268288
269289
dispatch(event) {
290+
if (this.closed) {
291+
return;
292+
}
293+
270294
try {
271295
this.term.textarea.dispatchEvent(event);
272296
} catch (e) {
@@ -275,6 +299,10 @@ class Term {
275299
}
276300
277301
writeStr(d) {
302+
if (this.closed) {
303+
return;
304+
}
305+
278306
try {
279307
this.term.write(d);
280308
} catch (e) {
@@ -283,6 +311,10 @@ class Term {
283311
}
284312
285313
write(d) {
314+
if (this.closed) {
315+
return;
316+
}
317+
286318
try {
287319
this.term.write(d);
288320
} catch (e) {
@@ -291,6 +323,10 @@ class Term {
291323
}
292324
293325
fontSizeUp() {
326+
if (this.closed) {
327+
return;
328+
}
329+
294330
if (this.fontSize >= termMaxFontSize) {
295331
return;
296332
}
@@ -301,6 +337,10 @@ class Term {
301337
}
302338
303339
fontSizeDown() {
340+
if (this.closed) {
341+
return;
342+
}
343+
304344
if (this.fontSize <= termMinFontSize) {
305345
return;
306346
}
@@ -311,6 +351,10 @@ class Term {
311351
}
312352
313353
focus() {
354+
if (this.closed) {
355+
return;
356+
}
357+
314358
try {
315359
this.term.focus();
316360
} catch (e) {
@@ -319,6 +363,10 @@ class Term {
319363
}
320364
321365
blur() {
366+
if (this.closed) {
367+
return;
368+
}
369+
322370
try {
323371
this.term.blur();
324372
} catch (e) {
@@ -327,14 +375,26 @@ class Term {
327375
}
328376
329377
refit() {
378+
if (this.closed) {
379+
return;
380+
}
381+
330382
try {
331383
this.fit.fit();
332384
} catch (e) {
333385
process.env.NODE_ENV === "development" && console.trace(e);
334386
}
335387
}
336388
389+
destroyed() {
390+
return this.closed;
391+
}
392+
337393
destroy() {
394+
if (this.closed) {
395+
return;
396+
}
397+
338398
this.closed = true;
339399
340400
try {
@@ -381,7 +441,6 @@ export default {
381441
screenKeys: consoleScreenKeys,
382442
term: new Term(this.control),
383443
typeface: termTypeFace,
384-
running: true,
385444
runner: null
386445
};
387446
},
@@ -401,13 +460,10 @@ export default {
401460
}
402461
},
403462
async mounted() {
404-
this.running = true;
405-
406463
await this.init();
407464
},
408465
beforeDestroy() {
409466
this.deinit();
410-
this.running = false;
411467
},
412468
methods: {
413469
loadRemoteFont(typeface, timeout) {
@@ -418,62 +474,62 @@ export default {
418474
}).load(null, timeout)
419475
]);
420476
},
421-
retryLoadRemoteFont(typeface, timeout, onSuccess) {
477+
async retryLoadRemoteFont(typeface, timeout, onSuccess) {
422478
const self = this;
423479
424-
self
425-
.loadRemoteFont(typeface, timeout)
426-
.then(() => {
427-
onSuccess();
428-
})
429-
.catch(() => {
430-
if (!self.running) {
431-
return;
432-
}
480+
for (;;) {
481+
if (self.term.destroyed()) {
482+
return;
483+
}
433484
434-
self.retryLoadRemoteFont(typeface, timeout, onSuccess);
435-
});
485+
try {
486+
onSuccess(await self.loadRemoteFont(typeface, timeout));
487+
488+
return;
489+
} catch (e) {
490+
// Retry
491+
}
492+
}
436493
},
437494
async openTerm(root, callbacks) {
438495
const self = this;
439496
440-
return self
441-
.loadRemoteFont(termTypeFace, termTypeFaceLoadTimeout)
442-
.then(() => {
443-
if (!self.running) {
444-
return;
445-
}
497+
try {
498+
await self.loadRemoteFont(termTypeFace, termTypeFaceLoadTimeout);
446499
447-
root.innerHTML = "";
500+
if (self.term.destroyed()) {
501+
return;
502+
}
448503
449-
self.term.init(root, callbacks);
450-
})
451-
.catch(() => {
452-
if (!self.running) {
453-
return;
454-
}
504+
root.innerHTML = "";
455505
456-
root.innerHTML = "";
506+
self.term.init(root, callbacks);
507+
508+
return;
509+
} catch (e) {
510+
// Ignore
511+
}
457512
458-
callbacks.warn(termTypeFaceLoadError, false);
513+
if (self.term.destroyed()) {
514+
return;
515+
}
459516
460-
self.term.setFont(termFallbackTypeFace);
461-
self.term.init(root, callbacks);
517+
root.innerHTML = "";
462518
463-
self.retryLoadRemoteFont(
464-
termTypeFace,
465-
termTypeFaceLoadTimeout,
466-
() => {
467-
if (!self.running) {
468-
return;
469-
}
519+
callbacks.warn(termTypeFaceLoadError, false);
470520
471-
self.term.setFont(termTypeFace);
521+
self.term.setFont(termFallbackTypeFace);
522+
self.term.init(root, callbacks);
472523
473-
callbacks.warn(termTypeFaceLoadError, true);
474-
}
475-
);
476-
});
524+
self.retryLoadRemoteFont(termTypeFace, termTypeFaceLoadTimeout, () => {
525+
if (self.term.destroyed()) {
526+
return;
527+
}
528+
529+
self.term.setFont(termTypeFace);
530+
531+
callbacks.warn(termTypeFaceLoadError, true);
532+
});
477533
},
478534
triggerActive() {
479535
this.active ? this.activate() : this.deactivate();
@@ -506,6 +562,11 @@ export default {
506562
}
507563
}
508564
);
565+
566+
if (this.term.destroyed()) {
567+
return;
568+
}
569+
509570
this.triggerActive();
510571
this.runRunner();
511572
},
@@ -545,6 +606,10 @@ export default {
545606
this.runner = (async () => {
546607
try {
547608
for (;;) {
609+
if (this.term.destroyed()) {
610+
break;
611+
}
612+
548613
this.term.write(await this.control.receive());
549614
550615
self.$emit("updated");

0 commit comments

Comments
 (0)