UNPKG

grafast

Version:

Cutting edge GraphQL planning and execution engine

100 lines 3.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildOptimizedExecute = buildOptimizedExecute; const error_ts_1 = require("./error.js"); const utils_ts_1 = require("./utils.js"); /** * Calls `callback` with an execute function that's optimized for handling * `depCount` dependencies (where the step `isSyncAndSafe` or not). */ function buildOptimizedExecute(depCount, isSyncAndSafe, callback) { if (depCount === 0) { callback(isSyncAndSafe ? execute0nocatch : execute0withcatch); } else if (depCount === 1) { callback(isSyncAndSafe ? execute1nocatch : execute1withcatch); } else if (depCount === 2) { callback(isSyncAndSafe ? execute2nocatch : execute2withcatch); } else { callback(executeN); } } function execute0nocatch({ count, extra }) { const results = []; for (let i = 0; i < count; i++) { results[i] = this.unbatchedExecute(extra); } return results; } function execute0withcatch({ count, extra }) { const results = []; for (let i = 0; i < count; i++) { try { results[i] = this.unbatchedExecute(extra); } catch (e) { results[i] = (0, error_ts_1.flagError)(e); } } return results; } function execute1nocatch({ count, values: [value0], extra }) { const results = []; for (let i = 0; i < count; i++) { results[i] = this.unbatchedExecute(extra, value0.at(i)); } return results; } function execute1withcatch({ count, values: [value0], extra }) { const results = []; for (let i = 0; i < count; i++) { try { results[i] = this.unbatchedExecute(extra, value0.at(i)); } catch (e) { results[i] = (0, error_ts_1.flagError)(e); } } return results; } function execute2nocatch({ count, values: [value0, value1], extra }) { const results = []; for (let i = 0; i < count; i++) { results[i] = this.unbatchedExecute(extra, value0.at(i), value1.at(i)); } return results; } function execute2withcatch({ count, values: [value0, value1], extra }) { const results = []; for (let i = 0; i < count; i++) { try { results[i] = this.unbatchedExecute(extra, value0.at(i), value1.at(i)); } catch (e) { results[i] = (0, error_ts_1.flagError)(e); } } return results; } function executeN({ count, values, extra }) { const results = []; // Warning: this `args` array gets reused over and over again to avoid memory // allocation. const args = (0, utils_ts_1.arrayOfLength)(values.length + 1); args[0] = extra; for (let dataIndex = 0; dataIndex < count; dataIndex++) { for (let valueIndex = 0; valueIndex < values.length; valueIndex++) { args[valueIndex + 1] = values[valueIndex].at(dataIndex); } try { results[dataIndex] = this.unbatchedExecute.apply(this, args); } catch (e) { results[dataIndex] = (0, error_ts_1.flagError)(e); } } return results; } //# sourceMappingURL=unbatchedStepExecute.js.map