You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.3 KiB
JavaScript
37 lines
1.3 KiB
JavaScript
"use strict";
|
|
|
|
const Promise = require("bluebird");
|
|
const consumable = require("@joepie91/consumable");
|
|
const syncpipe = require("syncpipe");
|
|
|
|
const createMutationAPIWrapper = require("./mutation-api/wrapper");
|
|
|
|
module.exports = function (state) {
|
|
const createDatabaseMutationAPI = require("./mutation-api/database")(state);
|
|
|
|
return function createDatabaseQueue(context) {
|
|
let databaseMutationAPI = createDatabaseMutationAPI(context);
|
|
let mutationAPI = createMutationAPIWrapper(context, databaseMutationAPI);
|
|
|
|
let queue = consumable([]);
|
|
|
|
return {
|
|
api: syncpipe(Object.keys(mutationAPI), [
|
|
(_) => _.map((method) => [ method, function() { queue.peek().push([ method, arguments ]); } ]),
|
|
(_) => Object.fromEntries(_)
|
|
]),
|
|
execute: function () {
|
|
if (!queue.peek().some((method) => method[0] === "updateMetadata")) {
|
|
// Doing an updateMetadata call is necessary to mark a task 'completed', so we inject a dummy call that doesn't actually change the metadata itself
|
|
// FIXME: Split apart 'markTaskCompleted' and 'updateMetadata' queries so that this hack is no longer necessary
|
|
queue.peek().push([ "updateMetadata", [ (data) => data ]]);
|
|
}
|
|
|
|
return Promise.each(queue.consume(), ([ method, args ]) => {
|
|
return mutationAPI[method](... args);
|
|
});
|
|
}
|
|
};
|
|
};
|
|
};
|