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.2 KiB
JavaScript

"use strict";
const Promise = require("bluebird");
const memoizee = require("memoizee");
// FIXME: Figure out a reasonable way to make this symbol its own (conflict-free) package, given that it'll be used all across both sysquery and CVM
const All = require("../graphql-interface/symbols/all");
// This generates a (memoized) source function for commands that always produce an entire list, that needs to be filtered for the desired item(s)
module.exports = function evaluateAndPick({ command, selectResult, selectID, many }) {
let commandOnce = memoizee(command);
return function (ids) {
return Promise.try(() => {
return commandOnce();
}).then((result) => {
if (selectResult != null) {
return selectResult(result);
} else {
return result;
}
}).then((items) => {
return ids.map((id) => {
if (id === All) {
return items;
} else if (many === true) {
// NOTE: This produces nested arrays! One array for each input ID.
return items.filter((item) => selectID(item) === id);
} else {
// TODO: Can this be more performant? Currently it is a nested loop
return items.find((item) => selectID(item) === id);
}
});
});
};
};