Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shared ScheduleAt type #88

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/algebraic_type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import ScheduleAt from "./schedule_at";

/**
* A variant of a sum type.
*
Expand Down Expand Up @@ -258,6 +260,9 @@ export class AlgebraicType {
public static createBytesType(): AlgebraicType {
return this.createArrayType(this.createU8Type());
}
public static createScheduleAtType(): AlgebraicType {
return ScheduleAt.getAlgebraicType();
}

public isProductType(): boolean {
return this.type === Type.ProductType;
Expand Down Expand Up @@ -295,6 +300,17 @@ export class AlgebraicType {
public isAddress(): boolean {
return this.isBytesNewtype("__address_bytes");
}

public 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
);
}
}

export namespace AlgebraicType {
Expand Down
5 changes: 5 additions & 0 deletions src/algebraic_value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} 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 @@ -331,6 +332,10 @@ export class AlgebraicValue {
public asAddress(): Address {
return new Address(this.asField(0).asBytes());
}

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

export interface ParseableType<ParsedType> {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./spacetimedb";
export * from "./identity";
export * from "./address";
export * from "./schedule_at";
PuruVJ marked this conversation as resolved.
Show resolved Hide resolved
export * from "./client_db";
export * from "./message_types";
45 changes: 45 additions & 0 deletions 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 { AlgebraicValue } from "./algebraic_value";

PuruVJ marked this conversation as resolved.
Show resolved Hide resolved
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;
Loading