diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-05-24 13:50:34 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-24 13:50:34 -0400 |
commit | 66e1185686f98f1cc4493298b5b1d4e0be7d826a (patch) | |
tree | 7d67a8bbbfd3d32235dcc5fbfe94c34e61132f3e /ext/mixed/js/task-accumulator.js | |
parent | c800444a0d4aa0177242da51e0f9716ebe882587 (diff) |
Settings binder (#542)
* Fix _modifySetting being async
* Return values for modifySettings's set and splice actions
* Add apiGetSettings
* Create a class which can accumulate tasks to run in bulk
* Create a class which binds input elements to customizable sources
* Create class which binds input elements to settings
* Add support for value transforms
* Remove redundant ObjectPropertyAccessor.getPathArray
* Fix not using correct types for input.min/max/step
* Fix wrong condition
* Use api object
Diffstat (limited to 'ext/mixed/js/task-accumulator.js')
-rw-r--r-- | ext/mixed/js/task-accumulator.js | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/ext/mixed/js/task-accumulator.js b/ext/mixed/js/task-accumulator.js new file mode 100644 index 00000000..5c6fe312 --- /dev/null +++ b/ext/mixed/js/task-accumulator.js @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2020 Yomichan Authors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +class TaskAccumulator { + constructor(runTasks) { + this._deferPromise = null; + this._activePromise = null; + this._tasks = []; + this._tasksActive = []; + this._uniqueTasks = new Map(); + this._uniqueTasksActive = new Map(); + this._runTasksBind = this._runTasks.bind(this); + this._tasksCompleteBind = this._tasksComplete.bind(this); + this._runTasks = runTasks; + } + + enqueue(key, data) { + if (this._deferPromise === null) { + const promise = this._activePromise !== null ? this._activePromise : Promise.resolve(); + this._deferPromise = promise.then(this._runTasksBind); + } + + const task = {data, stale: false}; + if (key !== null) { + const activeTaskInfo = this._uniqueTasksActive.get(key); + if (typeof activeTaskInfo !== 'undefined') { + activeTaskInfo.stale = true; + } + + this._uniqueTasks.set(key, task); + } else { + this._tasks.push(task); + } + + return this._deferPromise; + } + + _runTasks() { + this._deferPromise = null; + + // Swap + [this._tasks, this._tasksActive] = [this._tasksActive, this._tasks]; + [this._uniqueTasks, this._uniqueTasksActive] = [this._uniqueTasksActive, this._uniqueTasks]; + + const promise = this._runTasksAsync(); + this._activePromise = promise.then(this._tasksCompleteBind); + return this._activePromise; + } + + async _runTasksAsync() { + try { + const allTasks = [ + ...this._tasksActive.map((taskInfo) => [null, taskInfo]), + ...this._uniqueTasksActive.entries() + ]; + await this._runTasks(allTasks); + } catch (e) { + yomichan.logError(e); + } + } + + _tasksComplete() { + this._tasksActive.length = 0; + this._uniqueTasksActive.clear(); + this._activePromise = null; + } +} |