Skip to content

Commit 56f899a

Browse files
authored
Add .unaryValue to ExecutionValue (#2324)
2 parents 8b113a7 + 68926ab commit 56f899a

File tree

6 files changed

+27
-6
lines changed

6 files changed

+27
-6
lines changed

.changeset/fast-phones-change.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"grafast": patch
3+
---
4+
5+
🚨 ExecutionValue no longer exposes .value and .entries (you need to narrow to
6+
access these). Added new `.unaryValue()` that can be used to assert the value is
7+
unary and retrieve its value - this should be used instead of `.at(0)` in
8+
general.

grafast/grafast/__tests__/unaryDeps-test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ class GetRecordsStep<T extends Record<string, any>> extends ExecutableStep {
8686
indexMap,
8787
values,
8888
}: ExecutionDetails): Promise<GrafastResultsList<any>> {
89-
const db = values[this.dbDepId].value as sqlite3.Database;
90-
const first = this.firstUDI != null ? values[this.firstUDI].value : null;
89+
const db = values[this.dbDepId].unaryValue() as sqlite3.Database;
90+
const first =
91+
this.firstUDI != null ? values[this.firstUDI].unaryValue() : null;
9192

9293
const identifierCols = Object.keys(this.depIdByIdentifier);
9394

grafast/grafast/src/engine/executeBucket.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,12 @@ export function bucketToString(this: Bucket) {
12461246
return `Bucket<${this.layerPlan}>`;
12471247
}
12481248

1249+
function throwNotUnary(): never {
1250+
throw new Error(
1251+
`This is not a unary value so we cannot get the single value - there may be more than one!`,
1252+
);
1253+
}
1254+
12491255
// TODO: memoize?
12501256
export function batchExecutionValue<TData>(
12511257
entries: TData[],
@@ -1256,6 +1262,7 @@ export function batchExecutionValue<TData>(
12561262
at: batchEntriesAt,
12571263
isBatch: true,
12581264
entries,
1265+
unaryValue: throwNotUnary,
12591266
_flags,
12601267
_flagsAt: batchFlagsAt,
12611268
_getStateUnion() {
@@ -1313,6 +1320,7 @@ export function unaryExecutionValue<TData>(
13131320
at: unaryAt,
13141321
isBatch: false,
13151322
value,
1323+
unaryValue: () => value,
13161324
_entryFlags,
13171325
_flagsAt: unaryFlagsAt,
13181326
_getStateUnion: unaryGetStateUnion,

grafast/grafast/src/interfaces.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,8 @@ export type ExecutionValue<TData = any> =
849849
interface ExecutionValueBase<TData = any> {
850850
at(i: number): TData;
851851
isBatch: boolean;
852+
/** Returns this.value for a unary execution value; throws if non-unary */
853+
unaryValue(): TData;
852854
/** @internal */
853855
_flagsAt(i: number): ExecutionEntryFlags;
854856
/** bitwise OR of all the entry states @internal */
@@ -866,15 +868,17 @@ export interface BatchExecutionValue<TData = any>
866868
extends ExecutionValueBase<TData> {
867869
isBatch: true;
868870
entries: ReadonlyArray<TData>;
869-
value?: never;
871+
/** Always throws, since this should only be called on unary execution values */
872+
unaryValue(): never;
870873
/** @internal */
871874
readonly _flags: Array<ExecutionEntryFlags>;
872875
}
873876
export interface UnaryExecutionValue<TData = any>
874877
extends ExecutionValueBase<TData> {
875878
isBatch: false;
876879
value: TData;
877-
entries?: never;
880+
/** Same as getting .value */
881+
unaryValue(): TData;
878882
/** @internal */
879883
_entryFlags: ExecutionEntryFlags;
880884
}

grafast/grafast/src/steps/applyTransforms.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class ApplyTransformsStep extends ExecutableStep {
105105
);
106106
}
107107
if (itemStep._isUnary) {
108-
store.set(itemStepId, unaryExecutionValue(values0.at(0)));
108+
store.set(itemStepId, unaryExecutionValue(values0.unaryValue()));
109109
} else {
110110
store.set(itemStepId, batchExecutionValue([]));
111111
}

grafast/grafast/src/steps/listTransform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export class __ListTransformStep<
231231
const listStepValue = values[this.listStepDepId];
232232

233233
if (itemStep._isUnary) {
234-
const list = listStepValue.at(0);
234+
const list = listStepValue.unaryValue();
235235
store.set(
236236
itemStepId,
237237
unaryExecutionValue(Array.isArray(list) ? list[0] : list),

0 commit comments

Comments
 (0)