Skip to content

Commit

Permalink
Add shared ScheduleAt type (#88)
Browse files Browse the repository at this point in the history
* Add shared ScheduleAt type

* Lint

---------

Co-authored-by: Tyler Cloutier <[email protected]>
Co-authored-by: Tyler Cloutier <[email protected]>
  • Loading branch information
3 people authored Feb 8, 2025
1 parent 9884068 commit 2e253a1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
18 changes: 13 additions & 5 deletions packages/sdk/src/algebraic_type.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Address } from './address';
import { ProductValue, SumValue, type ValueAdapter } from './algebraic_value';
import type BinaryReader from './binary_reader';
import type BinaryWriter from './binary_writer';
import { Identity } from './identity';
import ScheduleAt from './schedule_at';

/**
* A variant of a sum type.
Expand Down Expand Up @@ -353,10 +353,7 @@ export class AlgebraicType {
]);
}
static createScheduleAtType(): AlgebraicType {
return AlgebraicType.createSumType([
new SumTypeVariant('Interval', AlgebraicType.createTimeDurationType()),
new SumTypeVariant('Time', AlgebraicType.createTimestampType()),
]);
return ScheduleAt.getAlgebraicType();
}
static createTimestampType(): AlgebraicType {
return AlgebraicType.createProductType([
Expand Down Expand Up @@ -422,6 +419,17 @@ export class AlgebraicType {
return this.#isBytesNewtype('__address__');
}

isScheduleAt(): boolean {
return (
this.isSumType() &&
this.sum.variants.length === 2 &&
this.sum.variants[0].name === 'Interval' &&
this.sum.variants[0].algebraicType.type === Type.U64 &&
this.sum.variants[1].name === 'Time' &&
this.sum.variants[1].algebraicType.type === Type.U64
);
}

isTimestamp(): boolean {
return this.#isI64Newtype('__timestamp_micros_since_unix_epoch__');
}
Expand Down
5 changes: 5 additions & 0 deletions packages/sdk/src/algebraic_value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Address } from './address';
import { AlgebraicType, ProductType, SumType } from './algebraic_type';
import BinaryReader from './binary_reader';
import { Identity } from './identity';
import { ScheduleAt } from './schedule_at';

export interface ReducerArgsAdapter {
next: () => ValueAdapter;
Expand Down Expand Up @@ -314,6 +315,10 @@ export class AlgebraicValue {
asAddress(): Address {
return new Address(this.asField(0).asBigInt());
}

asScheduleAt(): ScheduleAt {
return ScheduleAt.fromValue(this);
}
}

export interface ParseableType<T> {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Should be at the top as other modules depend on it
export * from './db_connection_impl.ts';

export * from './schedule_at';
export * from './address.ts';
export * from './client_cache.ts';
export * from './identity.ts';
Expand Down
45 changes: 45 additions & 0 deletions packages/sdk/src/schedule_at.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { AlgebraicType, SumTypeVariant } from './algebraic_type';
import type { AlgebraicValue } from './algebraic_value';

export namespace ScheduleAt {
export function getAlgebraicType(): AlgebraicType {
return AlgebraicType.createSumType([
new SumTypeVariant('Interval', AlgebraicType.createU64Type()),
new SumTypeVariant('Time', AlgebraicType.createU64Type()),
]);
}

export function serialize(value: ScheduleAt): object {
switch (value.tag) {
case 'Interval':
return { Interval: value.value };
case 'Time':
return { Time: value.value };
default:
throw 'unreachable';
}
}

export type Interval = { tag: 'Interval'; value: BigInt };
export const Interval = (value: BigInt): Interval => ({
tag: 'Interval',
value,
});
export type Time = { tag: 'Time'; value: BigInt };
export const Time = (value: BigInt): Time => ({ tag: 'Time', value });

export function fromValue(value: AlgebraicValue): ScheduleAt {
let sumValue = value.asSumValue();
switch (sumValue.tag) {
case 0:
return { tag: 'Interval', value: sumValue.value.asBigInt() };
case 1:
return { tag: 'Time', value: sumValue.value.asBigInt() };
default:
throw 'unreachable';
}
}
}

export type ScheduleAt = ScheduleAt.Interval | ScheduleAt.Time;
export default ScheduleAt;

0 comments on commit 2e253a1

Please sign in to comment.