Skip to content

Commit

Permalink
Chore: Move reused code into method
Browse files Browse the repository at this point in the history
  • Loading branch information
pramodsum committed Nov 15, 2018
1 parent d39e4fe commit 558a7c9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
21 changes: 17 additions & 4 deletions src/controllers/AnnotationModeController.js
Expand Up @@ -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.
*
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();
}
});
});
Expand Down
9 changes: 2 additions & 7 deletions src/controllers/DrawingModeController.js
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
}

Expand Down
6 changes: 1 addition & 5 deletions src/controllers/PointModeController.js
Expand Up @@ -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;
}

Expand Down

0 comments on commit 558a7c9

Please sign in to comment.