UNPKG

grafast

Version:

Cutting edge GraphQL planning and execution engine

75 lines 2.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.each = each; exports.isSkippableEach = isSkippableEach; const tslib_1 = require("tslib"); const chalk_1 = tslib_1.__importDefault(require("chalk")); const step_js_1 = require("../step.js"); const __item_js_1 = require("./__item.js"); const listTransform_js_1 = require("./listTransform.js"); const eachReduceCallback = (memo, item) => { memo.push(item); return memo; }; const eachItemPlanCallback = (itemPlan) => itemPlan; const eachInitialState = () => []; const outerCache = new WeakMap(); const eachCallbackForListPlan = (listPlan, mapper) => { let innerCache = outerCache.get(listPlan); if (!innerCache) { innerCache = new WeakMap(); outerCache.set(listPlan, innerCache); } let result = innerCache.get(mapper); if (!result) { result = (itemPlan) => mapper(listPlan.listItem(itemPlan)); innerCache.set(mapper, result); } return result; }; function eachOptimize() { const layerPlan = this.subroutineLayer; const rootStep = layerPlan.rootStep; if (rootStep instanceof __item_js_1.__ItemStep && rootStep.getParentStep().layerPlan !== layerPlan) { // We don't do anything; replace ourself with our parent return this.getListStep(); } return this; } /** * Transforms a list by wrapping each element in the list with the given mapper. */ function each(listStep, mapper) { return (0, listTransform_js_1.listTransform)({ listStep, itemPlanCallback: eachItemPlanCallback, initialState: eachInitialState, reduceCallback: eachReduceCallback, listItem: (0, step_js_1.isListCapableStep)(listStep) ? eachCallbackForListPlan(listStep, mapper) : mapper, meta: `each:${chalk_1.default.yellow(listStep.id)}${mapper.name ? `/${mapper.name}` : ""}`, optimize: eachOptimize, ...(listStep.connectionClone != null ? { connectionClone(...args) { const $list = this.getListStep(); const $clonedList = $list.connectionClone(...args); return each($clonedList, mapper); }, } : null), }); } function isSkippableEach($step) { if ($step instanceof listTransform_js_1.__ListTransformStep) { return ($step.itemPlanCallback === eachItemPlanCallback && $step.initialState === eachInitialState && $step.reduceCallback === eachReduceCallback && $step.optimize === eachOptimize && $step.optimize() === $step.getListStep()); } return false; } //# sourceMappingURL=each.js.map