diff --git a/src/controllers/AnnotationModeController.js b/src/controllers/AnnotationModeController.js index 17acbc047..0aa12d6c3 100644 --- a/src/controllers/AnnotationModeController.js +++ b/src/controllers/AnnotationModeController.js @@ -599,6 +599,21 @@ class AnnotationModeController extends EventEmitter { Object.keys(this.annotations).forEach((pageNum) => this.renderPage(pageNum)); } + /** + * Unregisters and destroys the specified thread + * + * @param {AnnotationThread} thread thread to destroy + * @return {void} + */ + destroyThread(thread: AnnotationThread) { + if (!thread) { + return; + } + + this.unregisterThread(thread); + thread.destroy(); + } + /** * Renders annotations from memory for a specified page. * @@ -614,8 +629,7 @@ class AnnotationModeController extends EventEmitter { pageThreads.forEach((thread, index) => { // Destroy any pending threads that may exist on re-render if (thread.state === STATES.pending || thread.id === this.pendingThreadID) { - this.unregisterThread(thread); - thread.destroy(); + this.destroyThread(thread); return; } @@ -645,10 +659,9 @@ class AnnotationModeController extends EventEmitter { const pageThreads = this.annotations[pageNum].all() || []; pageThreads.forEach((thread) => { if (thread.state === STATES.pending || thread.id === this.pendingThreadID) { - this.unregisterThread(thread); + this.destroyThread(thread); hadPendingThreads = true; this.pendingThreadID = null; - thread.destroy(); } }); }); diff --git a/src/controllers/DrawingModeController.js b/src/controllers/DrawingModeController.js index 0cd5ed118..2ddc5746a 100644 --- a/src/controllers/DrawingModeController.js +++ b/src/controllers/DrawingModeController.js @@ -92,11 +92,7 @@ class DrawingModeController extends AnnotationModeController { * @return {void} */ cancelDrawing(): void { - if (this.currentThread) { - this.unregisterThread(this.currentThread); - this.currentThread.destroy(); - } - + this.destroyThread(this.currentThread); this.exit(); } @@ -307,8 +303,7 @@ class DrawingModeController extends AnnotationModeController { this.unbindListeners(); this.bindListeners(); } else { - this.unregisterThread(thread); - thread.destroy(); + this.destroyThread(thread); this.renderPage(thread.location.page); } diff --git a/src/controllers/PointModeController.js b/src/controllers/PointModeController.js index e7aaf8449..4041232af 100644 --- a/src/controllers/PointModeController.js +++ b/src/controllers/PointModeController.js @@ -64,11 +64,7 @@ class PointModeController extends AnnotationModeController { this.annotatedElement.classList.remove(CLASS_ANNOTATION_POINT_MODE); const thread = this.getThreadByID(this.pendingThreadID); - if (thread) { - this.unregisterThread(thread); - thread.destroy(); - } - + this.destroyThread(thread); this.pendingThreadID = null; }