Skip to content

Commit

Permalink
Add .unaryValue to ExecutionValue (#2324)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie authored Jan 17, 2025
2 parents 8b113a7 + 68926ab commit 56f899a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 6 deletions.
8 changes: 8 additions & 0 deletions .changeset/fast-phones-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"grafast": patch
---

🚨 ExecutionValue no longer exposes .value and .entries (you need to narrow to
access these). Added new `.unaryValue()` that can be used to assert the value is
unary and retrieve its value - this should be used instead of `.at(0)` in
general.
5 changes: 3 additions & 2 deletions grafast/grafast/__tests__/unaryDeps-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ class GetRecordsStep<T extends Record<string, any>> extends ExecutableStep {
indexMap,
values,
}: ExecutionDetails): Promise<GrafastResultsList<any>> {
const db = values[this.dbDepId].value as sqlite3.Database;
const first = this.firstUDI != null ? values[this.firstUDI].value : null;
const db = values[this.dbDepId].unaryValue() as sqlite3.Database;
const first =
this.firstUDI != null ? values[this.firstUDI].unaryValue() : null;

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

Expand Down
8 changes: 8 additions & 0 deletions grafast/grafast/src/engine/executeBucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,12 @@ export function bucketToString(this: Bucket) {
return `Bucket<${this.layerPlan}>`;
}

function throwNotUnary(): never {
throw new Error(
`This is not a unary value so we cannot get the single value - there may be more than one!`,
);
}

// TODO: memoize?
export function batchExecutionValue<TData>(
entries: TData[],
Expand All @@ -1256,6 +1262,7 @@ export function batchExecutionValue<TData>(
at: batchEntriesAt,
isBatch: true,
entries,
unaryValue: throwNotUnary,
_flags,
_flagsAt: batchFlagsAt,
_getStateUnion() {
Expand Down Expand Up @@ -1313,6 +1320,7 @@ export function unaryExecutionValue<TData>(
at: unaryAt,
isBatch: false,
value,
unaryValue: () => value,
_entryFlags,
_flagsAt: unaryFlagsAt,
_getStateUnion: unaryGetStateUnion,
Expand Down
8 changes: 6 additions & 2 deletions grafast/grafast/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,8 @@ export type ExecutionValue<TData = any> =
interface ExecutionValueBase<TData = any> {
at(i: number): TData;
isBatch: boolean;
/** Returns this.value for a unary execution value; throws if non-unary */
unaryValue(): TData;
/** @internal */
_flagsAt(i: number): ExecutionEntryFlags;
/** bitwise OR of all the entry states @internal */
Expand All @@ -866,15 +868,17 @@ export interface BatchExecutionValue<TData = any>
extends ExecutionValueBase<TData> {
isBatch: true;
entries: ReadonlyArray<TData>;
value?: never;
/** Always throws, since this should only be called on unary execution values */
unaryValue(): never;
/** @internal */
readonly _flags: Array<ExecutionEntryFlags>;
}
export interface UnaryExecutionValue<TData = any>
extends ExecutionValueBase<TData> {
isBatch: false;
value: TData;
entries?: never;
/** Same as getting .value */
unaryValue(): TData;
/** @internal */
_entryFlags: ExecutionEntryFlags;
}
Expand Down
2 changes: 1 addition & 1 deletion grafast/grafast/src/steps/applyTransforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class ApplyTransformsStep extends ExecutableStep {
);
}
if (itemStep._isUnary) {
store.set(itemStepId, unaryExecutionValue(values0.at(0)));
store.set(itemStepId, unaryExecutionValue(values0.unaryValue()));
} else {
store.set(itemStepId, batchExecutionValue([]));
}
Expand Down
2 changes: 1 addition & 1 deletion grafast/grafast/src/steps/listTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export class __ListTransformStep<
const listStepValue = values[this.listStepDepId];

if (itemStep._isUnary) {
const list = listStepValue.at(0);
const list = listStepValue.unaryValue();
store.set(
itemStepId,
unaryExecutionValue(Array.isArray(list) ? list[0] : list),
Expand Down

0 comments on commit 56f899a

Please sign in to comment.