Added unit tests for try_finally and added dispose override to LogStore.

This commit is contained in:
Daan Vanden Bosch 2020-09-27 01:05:37 +02:00
parent 864bf40da3
commit b7c57744df
2 changed files with 50 additions and 9 deletions

37
src/core/util.test.ts Normal file
View File

@ -0,0 +1,37 @@
import { try_finally } from "./util";
test("try_finally with synchronous function", () => {
let after_called = false;
const value = try_finally(
() => 999,
() => (after_called = true),
);
expect(value).toBe(999);
expect(after_called).toBe(true);
});
test("try_finally with asynchronous function", async () => {
let after_called = false;
const value = await try_finally(
async () => 567,
() => (after_called = true),
);
expect(value).toBe(567);
expect(after_called).toBe(true);
});
test("try_finally with promise-returning function", async () => {
let after_called = false;
const value = await try_finally(
() => new Promise(resolve => setTimeout(() => resolve(849), 10)),
() => (after_called = true),
);
expect(value).toBe(849);
expect(after_called).toBe(true);
});

View File

@ -8,6 +8,7 @@ import { Store } from "../../core/stores/Store";
const logger = LogManager.get("quest_editor/stores/LogStore"); const logger = LogManager.get("quest_editor/stores/LogStore");
export class LogStore extends Store { export class LogStore extends Store {
private animation_frame?: number;
private readonly log_buffer: LogEntry[] = []; private readonly log_buffer: LogEntry[] = [];
private readonly logger_name_buffer: string[] = []; private readonly logger_name_buffer: string[] = [];
@ -23,6 +24,15 @@ export class LogStore extends Store {
this.severity.map(severity => message => message.severity >= severity), this.severity.map(severity => message => message.severity >= severity),
); );
dispose(): void {
super.dispose();
if (this.animation_frame != undefined) {
cancelAnimationFrame(this.animation_frame);
this.animation_frame = undefined;
}
}
get_logger(name: string): Logging { get_logger(name: string): Logging {
const logger = LogManager.get(name); const logger = LogManager.get(name);
logger.handler = this.handler; logger.handler = this.handler;
@ -40,16 +50,10 @@ export class LogStore extends Store {
this.add_buffered_log_entries(); this.add_buffered_log_entries();
} }
private adding_log_entries?: number;
private add_buffered_log_entries(): void { private add_buffered_log_entries(): void {
if (this.adding_log_entries != undefined) return; if (this.animation_frame != undefined || this.disposed) return;
this.adding_log_entries = requestAnimationFrame(() => {
if (this.disposed) {
return;
}
this.animation_frame = requestAnimationFrame(() => {
const DROP_THRESHOLD = 500; const DROP_THRESHOLD = 500;
const DROP_THRESHOLD_HALF = DROP_THRESHOLD / 2; const DROP_THRESHOLD_HALF = DROP_THRESHOLD / 2;
const BATCH_SIZE = 200; const BATCH_SIZE = 200;
@ -89,7 +93,7 @@ export class LogStore extends Store {
this._log.splice(0, 1000); this._log.splice(0, 1000);
} }
this.adding_log_entries = undefined; this.animation_frame = undefined;
if (this.log_buffer.length) { if (this.log_buffer.length) {
this.add_buffered_log_entries(); this.add_buffered_log_entries();