Skip to content

Commit 5235082

Browse files
cloutiertylerjsdt
andauthored
Addresses #2969 (#2980)
# Description of Changes The `ScheduleAt` type appears to have an outdated structure. I've updated the structure of the `ScheduleAt` type represented in TypeScript to be in line with the Rust type: https://docs.rs/spacetimedb/latest/spacetimedb/enum.ScheduleAt.html Namely, we were missing the inner Spacetime library types of `TimeDuration` and `Timestamp`. This is to address #2969. # API and ABI breaking changes This is an API breaking change in that it changes the API, but it's breaking in that it fixes a bug. # Expected complexity level and risk 2 # Testing I have not done additional testing, but I thought I would get this PR started to make it easier for the next person who comes along. The ideal automated test to add would be one which connects a TypeScript client to a SpacetimeDB module with a scheduled table, and verifies that the type is correctly deserialized and represented in TypeScript when the rows are sent down. --------- Co-authored-by: Jeffrey Dallatezza <[email protected]>
1 parent 84edd87 commit 5235082

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

sdks/typescript/packages/sdk/src/schedule_at.ts

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,47 @@ import type { AlgebraicValue } from './algebraic_value';
44
export namespace ScheduleAt {
55
export function getAlgebraicType(): AlgebraicType {
66
return AlgebraicType.createSumType([
7-
new SumTypeVariant('Interval', AlgebraicType.createU64Type()),
8-
new SumTypeVariant('Time', AlgebraicType.createU64Type()),
7+
new SumTypeVariant('Interval', AlgebraicType.createTimeDurationType()),
8+
new SumTypeVariant('Time', AlgebraicType.createTimestampType()),
99
]);
1010
}
1111

12-
export function serialize(value: ScheduleAt): object {
13-
switch (value.tag) {
14-
case 'Interval':
15-
return { Interval: value.value };
16-
case 'Time':
17-
return { Time: value.value };
18-
default:
19-
throw 'unreachable';
20-
}
21-
}
22-
23-
export type Interval = { tag: 'Interval'; value: BigInt };
12+
export type Interval = {
13+
tag: 'Interval';
14+
value: { __time_duration_micros__: BigInt };
15+
};
2416
export const Interval = (value: BigInt): Interval => ({
2517
tag: 'Interval',
26-
value,
18+
value: { __time_duration_micros__: value },
19+
});
20+
export type Time = {
21+
tag: 'Time';
22+
value: { __timestamp_micros_since_unix_epoch__: BigInt };
23+
};
24+
export const Time = (value: BigInt): Time => ({
25+
tag: 'Time',
26+
value: { __timestamp_micros_since_unix_epoch__: value },
2727
});
28-
export type Time = { tag: 'Time'; value: BigInt };
29-
export const Time = (value: BigInt): Time => ({ tag: 'Time', value });
3028

3129
export function fromValue(value: AlgebraicValue): ScheduleAt {
3230
let sumValue = value.asSumValue();
3331
switch (sumValue.tag) {
3432
case 0:
35-
return { tag: 'Interval', value: sumValue.value.asBigInt() };
33+
return {
34+
tag: 'Interval',
35+
value: {
36+
__time_duration_micros__: sumValue.value
37+
.asProductValue()
38+
.elements[0].asBigInt(),
39+
},
40+
};
3641
case 1:
37-
return { tag: 'Time', value: sumValue.value.asBigInt() };
42+
return {
43+
tag: 'Time',
44+
value: {
45+
__timestamp_micros_since_unix_epoch__: sumValue.value.asBigInt(),
46+
},
47+
};
3848
default:
3949
throw 'unreachable';
4050
}

0 commit comments

Comments
 (0)