Inlining the ScheduledCommit class

This commit is contained in:
Jonathan Putney
2019-11-19 15:22:14 -05:00
parent 4abe1f3420
commit df029a4fda
4 changed files with 35 additions and 116 deletions
+23 -76
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+10 -38
View File
@@ -11,6 +11,7 @@ import {unflatten} from './utilities';
*/ */
export default class BaseAPI { export default class BaseAPI {
#timeout; #timeout;
#scheduleCancelled = false;
#error_codes; #error_codes;
#settings = { #settings = {
autocommit: false, autocommit: false,
@@ -905,8 +906,8 @@ export default class BaseAPI {
* @param {number} when - the number of milliseconds to wait before committing * @param {number} when - the number of milliseconds to wait before committing
*/ */
scheduleCommit(when: number) { scheduleCommit(when: number) {
this.#timeout = new ScheduledCommit(this, when); this.#timeout = setTimeout(this.scheduledCallback, when);
this.apiLog('scheduleCommit', null, null, global_constants.LOG_LEVEL_DEBUG); this.apiLog('scheduleCommit', '', 'scheduled', global_constants.LOG_LEVEL_DEBUG);
} }
/** /**
@@ -914,48 +915,19 @@ export default class BaseAPI {
*/ */
clearScheduledCommit() { clearScheduledCommit() {
if (this.#timeout) { if (this.#timeout) {
this.#timeout.cancel(); this.#scheduleCancelled = true;
this.#timeout = null; clearTimeout(this.#timeout);
this.apiLog('clearScheduledCommit', null, null, this.apiLog('clearScheduledCommit', '', 'cleared',
global_constants.LOG_LEVEL_DEBUG); global_constants.LOG_LEVEL_DEBUG);
} }
} }
}
/** /**
* Private class that wraps a timeout call to the commit() function * Callback for scheduled commit timeout
*/ */
class ScheduledCommit { scheduledCallback() {
#API; if (!this.#scheduleCancelled) {
#cancelled = false; this.commit('Commit', false);
#timeout;
/**
* Constructor for ScheduledCommit
* @param {BaseAPI} API
* @param {number} when
*/
constructor(API: any, when: number) {
this.#API = API;
this.#timeout = setTimeout(this.wrapper, when);
}
/**
* Cancel any currently scheduled commit
*/
cancel() {
this.#cancelled = true;
if (this.#timeout) {
clearTimeout(this.#timeout);
}
}
/**
* Wrap the API commit call to check if the call has already been cancelled
*/
wrapper() {
if (!this.#cancelled) {
this.#API.commit();
} }
} }
} }