Skip to content

Commit

Permalink
extract to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Jul 26, 2024
1 parent 8cdcbac commit c3aacf8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 66 deletions.
65 changes: 65 additions & 0 deletions src/execution/DeferredFragmentFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import type { Path } from '../jsutils/Path.js';

import type { DeferUsage } from './collectFields.js';
import { DeferredFragmentRecord } from './types.js';

/**
* @internal
*/
export class DeferredFragmentFactory {
private _rootDeferredFragments = new Map<
DeferUsage,
DeferredFragmentRecord
>();

get(deferUsage: DeferUsage, path: Path | undefined): DeferredFragmentRecord {
const deferUsagePath = this._pathAtDepth(path, deferUsage.depth);
let deferredFragmentRecords:
| Map<DeferUsage, DeferredFragmentRecord>
| undefined;
if (deferUsagePath === undefined) {
deferredFragmentRecords = this._rootDeferredFragments;
} else {
deferredFragmentRecords = (
deferUsagePath as unknown as {
deferredFragmentRecords: Map<DeferUsage, DeferredFragmentRecord>;
}
).deferredFragmentRecords;
if (deferredFragmentRecords === undefined) {
deferredFragmentRecords = new Map();
(
deferUsagePath as unknown as {
deferredFragmentRecords: Map<DeferUsage, DeferredFragmentRecord>;
}
).deferredFragmentRecords = deferredFragmentRecords;
}
}
let deferredFragmentRecord = deferredFragmentRecords.get(deferUsage);
if (deferredFragmentRecord === undefined) {
const { label, parentDeferUsage } = deferUsage;
deferredFragmentRecord = new DeferredFragmentRecord(
deferUsagePath,
label,
parentDeferUsage,
);
deferredFragmentRecords.set(deferUsage, deferredFragmentRecord);
}
return deferredFragmentRecord;
}

private _pathAtDepth(
path: Path | undefined,
depth: number,
): Path | undefined {
if (depth === 0) {
return;
}
const stack: Array<Path> = [];
let currentPath = path;
while (currentPath !== undefined) {
stack.unshift(currentPath);
currentPath = currentPath.prev;
}
return stack[depth - 1];
}
}
69 changes: 3 additions & 66 deletions src/execution/IncrementalGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { promiseWithResolvers } from '../jsutils/promiseWithResolvers.js';
import type { GraphQLError } from '../error/GraphQLError.js';

import type { DeferUsage } from './collectFields.js';
import { DeferredFragmentFactory } from './DeferredFragmentFactory.js';
import type {
DeferredFragmentRecord,
DeliveryGroup,
IncrementalDataRecord,
IncrementalDataRecordResult,
Expand All @@ -16,72 +18,7 @@ import type {
StreamRecord,
SuccessfulExecutionGroup,
} from './types.js';
import {
DeferredFragmentRecord,
isDeferredFragmentRecord,
isPendingExecutionGroup,
} from './types.js';

/**
* @internal
*/
class DeferredFragmentFactory {
private _rootDeferredFragments = new Map<
DeferUsage,
DeferredFragmentRecord
>();

get(deferUsage: DeferUsage, path: Path | undefined): DeferredFragmentRecord {
const deferUsagePath = this._pathAtDepth(path, deferUsage.depth);
let deferredFragmentRecords:
| Map<DeferUsage, DeferredFragmentRecord>
| undefined;
if (deferUsagePath === undefined) {
deferredFragmentRecords = this._rootDeferredFragments;
} else {
deferredFragmentRecords = (
deferUsagePath as unknown as {
deferredFragmentRecords: Map<DeferUsage, DeferredFragmentRecord>;
}
).deferredFragmentRecords;
if (deferredFragmentRecords === undefined) {
deferredFragmentRecords = new Map();
(
deferUsagePath as unknown as {
deferredFragmentRecords: Map<DeferUsage, DeferredFragmentRecord>;
}
).deferredFragmentRecords = deferredFragmentRecords;
}
}
let deferredFragmentRecord = deferredFragmentRecords.get(deferUsage);
if (deferredFragmentRecord === undefined) {
const { label, parentDeferUsage } = deferUsage;
deferredFragmentRecord = new DeferredFragmentRecord(
deferUsagePath,
label,
parentDeferUsage,
);
deferredFragmentRecords.set(deferUsage, deferredFragmentRecord);
}
return deferredFragmentRecord;
}

private _pathAtDepth(
path: Path | undefined,
depth: number,
): Path | undefined {
if (depth === 0) {
return;
}
const stack: Array<Path> = [];
let currentPath = path;
while (currentPath !== undefined) {
stack.unshift(currentPath);
currentPath = currentPath.prev;
}
return stack[depth - 1];
}
}
import { isDeferredFragmentRecord, isPendingExecutionGroup } from './types.js';

/**
* @internal
Expand Down

0 comments on commit c3aacf8

Please sign in to comment.