From b7c57744df3d0f784a8b67292123447538cb2ed8 Mon Sep 17 00:00:00 2001 From: Daan Vanden Bosch Date: Sun, 27 Sep 2020 01:05:37 +0200 Subject: [PATCH] Added unit tests for try_finally and added dispose override to LogStore. --- src/core/util.test.ts | 37 +++++++++++++++++++++++++++++ src/quest_editor/stores/LogStore.ts | 22 ++++++++++------- 2 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 src/core/util.test.ts diff --git a/src/core/util.test.ts b/src/core/util.test.ts new file mode 100644 index 00000000..3ab53266 --- /dev/null +++ b/src/core/util.test.ts @@ -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); +}); diff --git a/src/quest_editor/stores/LogStore.ts b/src/quest_editor/stores/LogStore.ts index 1dce7e1e..67447bef 100644 --- a/src/quest_editor/stores/LogStore.ts +++ b/src/quest_editor/stores/LogStore.ts @@ -8,6 +8,7 @@ import { Store } from "../../core/stores/Store"; const logger = LogManager.get("quest_editor/stores/LogStore"); export class LogStore extends Store { + private animation_frame?: number; private readonly log_buffer: LogEntry[] = []; private readonly logger_name_buffer: string[] = []; @@ -23,6 +24,15 @@ export class LogStore extends Store { 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 { const logger = LogManager.get(name); logger.handler = this.handler; @@ -40,16 +50,10 @@ export class LogStore extends Store { this.add_buffered_log_entries(); } - private adding_log_entries?: number; - private add_buffered_log_entries(): void { - if (this.adding_log_entries != undefined) return; - - this.adding_log_entries = requestAnimationFrame(() => { - if (this.disposed) { - return; - } + if (this.animation_frame != undefined || this.disposed) return; + this.animation_frame = requestAnimationFrame(() => { const DROP_THRESHOLD = 500; const DROP_THRESHOLD_HALF = DROP_THRESHOLD / 2; const BATCH_SIZE = 200; @@ -89,7 +93,7 @@ export class LogStore extends Store { this._log.splice(0, 1000); } - this.adding_log_entries = undefined; + this.animation_frame = undefined; if (this.log_buffer.length) { this.add_buffered_log_entries();