Skip to content

Commit 8fce6b9

Browse files
committed
Fix view signature
1 parent ec1798a commit 8fce6b9

File tree

3 files changed

+54
-20
lines changed

3 files changed

+54
-20
lines changed

crates/bindings-typescript/src/lib/option.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { AlgebraicType } from './algebraic_type';
22

3-
export type OptionAlgebraicType = {
3+
export type OptionAlgebraicType<T extends AlgebraicType = AlgebraicType> = {
44
tag: 'Sum';
55
value: {
66
variants: [
7-
{ name: 'some'; algebraicType: AlgebraicType },
7+
{ name: 'some'; algebraicType: T },
88
{
99
name: 'none';
1010
algebraicType: { tag: 'Product'; value: { elements: [] } };
@@ -14,9 +14,13 @@ export type OptionAlgebraicType = {
1414
};
1515

1616
export const Option: {
17-
getAlgebraicType(innerType: AlgebraicType): OptionAlgebraicType;
17+
getAlgebraicType<T extends AlgebraicType = AlgebraicType>(
18+
innerType: T
19+
): OptionAlgebraicType<T>;
1820
} = {
19-
getAlgebraicType(innerType: AlgebraicType): OptionAlgebraicType {
21+
getAlgebraicType<T extends AlgebraicType = AlgebraicType>(
22+
innerType: T
23+
): OptionAlgebraicType<T> {
2024
return AlgebraicType.Sum({
2125
variants: [
2226
{ name: 'some', algebraicType: innerType },

crates/bindings-typescript/src/server/type_builders.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,18 +1101,21 @@ export class ArrayBuilder<Element extends TypeBuilder<any, any>>
11011101
export class OptionBuilder<Value extends TypeBuilder<any, any>>
11021102
extends TypeBuilder<
11031103
InferTypeOfTypeBuilder<Value> | undefined,
1104-
OptionAlgebraicType
1104+
OptionAlgebraicType<InferSpacetimeTypeOfTypeBuilder<Value>>
11051105
>
11061106
implements
1107-
Defaultable<InferTypeOfTypeBuilder<Value> | undefined, OptionAlgebraicType>
1107+
Defaultable<
1108+
InferTypeOfTypeBuilder<Value> | undefined,
1109+
OptionAlgebraicType<InferSpacetimeTypeOfTypeBuilder<Value>>
1110+
>
11081111
{
11091112
/**
11101113
* The phantom value type of the option for TypeScript
11111114
*/
11121115
readonly value!: Value;
11131116

11141117
constructor(value: Value) {
1115-
let innerType: AlgebraicType;
1118+
let innerType: InferSpacetimeTypeOfTypeBuilder<Value>;
11161119
if (value instanceof ColumnBuilder) {
11171120
innerType = value.typeBuilder.algebraicType;
11181121
} else {
@@ -2294,11 +2297,14 @@ export class OptionColumnBuilder<
22942297
>
22952298
extends ColumnBuilder<
22962299
InferTypeOfTypeBuilder<Value> | undefined,
2297-
OptionAlgebraicType,
2300+
OptionAlgebraicType<InferSpacetimeTypeOfTypeBuilder<Value>>,
22982301
M
22992302
>
23002303
implements
2301-
Defaultable<InferTypeOfTypeBuilder<Value> | undefined, OptionAlgebraicType>
2304+
Defaultable<
2305+
InferTypeOfTypeBuilder<Value> | undefined,
2306+
OptionAlgebraicType<InferSpacetimeTypeOfTypeBuilder<Value>>
2307+
>
23022308
{
23032309
default(
23042310
value: InferTypeOfTypeBuilder<Value> | undefined

crates/bindings-typescript/src/server/views.ts

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
import type {
1+
import {
22
AlgebraicType,
3-
AlgebraicTypeVariants,
4-
ProductType,
3+
type AlgebraicTypeVariants,
4+
type ProductType,
55
} from '../lib/algebraic_type';
66
import type { Identity } from '../lib/identity';
7+
import type { OptionAlgebraicType } from '../lib/option';
78
import type { ParamsObj } from './reducers';
89
import { MODULE_DEF, type UntypedSchemaDef } from './schema';
910
import type { ReadonlyTable } from './table';
10-
import type { Infer, InferTypeOfRow, TypeBuilder } from './type_builders';
11+
import type {
12+
ArrayBuilder,
13+
Infer,
14+
InferTypeOfRow,
15+
OptionBuilder,
16+
ProductBuilder,
17+
TypeBuilder,
18+
} from './type_builders';
1119
import { bsatnBaseSize } from './util';
1220

1321
export type ViewCtx<S extends UntypedSchemaDef> = Readonly<{
@@ -35,11 +43,15 @@ export type AnonymousViewFn<
3543
Ret extends ViewReturnTypeBuilder,
3644
> = (ctx: AnonymousViewCtx<S>, params: InferTypeOfRow<Params>) => Infer<Ret>;
3745

38-
export type ViewReturnTypeBuilder = TypeBuilder<
39-
any,
40-
| { tag: 'Array'; value: AlgebraicTypeVariants.Product }
41-
| AlgebraicTypeVariants.Product
42-
>;
46+
export type ViewReturnTypeBuilder =
47+
| TypeBuilder<
48+
readonly object[],
49+
{ tag: 'Array'; value: AlgebraicTypeVariants.Product }
50+
>
51+
| TypeBuilder<
52+
object | undefined,
53+
OptionAlgebraicType<AlgebraicTypeVariants.Product>
54+
>;
4355

4456
export function defineView<
4557
S extends UntypedSchemaDef,
@@ -73,11 +85,23 @@ export function defineView<
7385
},
7486
});
7587

88+
let returnType = ret.algebraicType;
89+
if (returnType.tag == 'Sum') {
90+
const originalFn = fn;
91+
fn = ((ctx: ViewCtx<S>, args: InferTypeOfRow<Params>) => {
92+
const ret = originalFn(ctx, args);
93+
return ret == null ? [] : [ret];
94+
}) as any;
95+
returnType = AlgebraicType.Array(
96+
returnType.value.variants[0].algebraicType
97+
);
98+
}
99+
76100
(anon ? ANON_VIEWS : VIEWS).push({
77101
fn,
78102
params: paramType,
79-
returnType: ret.algebraicType,
80-
returnTypeBaseSize: bsatnBaseSize(MODULE_DEF.typespace, ret.algebraicType),
103+
returnType,
104+
returnTypeBaseSize: bsatnBaseSize(MODULE_DEF.typespace, returnType),
81105
});
82106
}
83107

0 commit comments

Comments
 (0)