Fixing bind on original ScheduledCommit class

This commit is contained in:
Jonathan Putney
2019-11-19 15:32:41 -05:00
parent d3f7184507
commit 8d8831954e
4 changed files with 115 additions and 37 deletions
+76 -25
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
+37 -10
View File
@@ -11,7 +11,6 @@ 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,
@@ -195,7 +194,7 @@ export default class BaseAPI {
// If we didn't have any errors while setting the data, go ahead and // If we didn't have any errors while setting the data, go ahead and
// schedule a commit, if autocommit is turned on // schedule a commit, if autocommit is turned on
if (String(this.lastErrorCode) === '0') { if (String(this.lastErrorCode) === '0') {
if (this.settings.autocommit) { if (this.settings.autocommit && !this.#timeout) {
this.scheduleCommit(this.settings.autocommitSeconds * 1000); this.scheduleCommit(this.settings.autocommitSeconds * 1000);
} }
} }
@@ -906,8 +905,7 @@ 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.clearScheduledCommit(); this.#timeout = new ScheduledCommit(this, when);
this.#timeout = setTimeout(this.scheduledCallback.bind(this), when);
this.apiLog('scheduleCommit', '', 'scheduled', global_constants.LOG_LEVEL_DEBUG); this.apiLog('scheduleCommit', '', 'scheduled', global_constants.LOG_LEVEL_DEBUG);
} }
@@ -916,19 +914,48 @@ export default class BaseAPI {
*/ */
clearScheduledCommit() { clearScheduledCommit() {
if (this.#timeout) { if (this.#timeout) {
this.#scheduleCancelled = true; this.#timeout.cancel();
clearTimeout(this.#timeout); this.#timeout = null;
this.apiLog('clearScheduledCommit', '', 'cleared', this.apiLog('clearScheduledCommit', '', 'cleared',
global_constants.LOG_LEVEL_DEBUG); global_constants.LOG_LEVEL_DEBUG);
} }
} }
}
/** /**
* Callback for scheduled commit timeout * Private class that wraps a timeout call to the commit() function
*/ */
scheduledCallback() { class ScheduledCommit {
if (!this.#scheduleCancelled) { #API;
this.commit('Commit', false); #cancelled = false;
#timeout;
/**
* Constructor for ScheduledCommit
* @param {BaseAPI} API
* @param {number} when
*/
constructor(API: any, when: number) {
this.#API = API;
this.#timeout = setTimeout(this.wrapper.bind(this), 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();
} }
} }
} }