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

modernize: Standardize UpdateStatus #82

Closed
wants to merge 1 commit into from
Closed
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
65 changes: 33 additions & 32 deletions packages/sdk/src/client_api/update_status.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD.

import {
AlgebraicType,
BuiltinType,
SumTypeVariant,
} from '../algebraic_type.ts';
import type { AlgebraicValue } from '../algebraic_value.ts';
import { DatabaseUpdate as __DatabaseUpdate } from './database_update.ts';
import { DatabaseUpdate } from './database_update.ts';

export class UpdateStatus {
constructor(
public tag: 'Committed' | 'Failed' | 'OutOfEnergy',
public value: DatabaseUpdate | string | undefined
) {
this.tag = tag;
this.value = value;
}

export namespace UpdateStatus {
export function getAlgebraicType(): AlgebraicType {
static getAlgebraicType(): AlgebraicType {
return AlgebraicType.createSumType([
new SumTypeVariant('Committed', __DatabaseUpdate.getAlgebraicType()),
new SumTypeVariant('Committed', DatabaseUpdate.getAlgebraicType()),
new SumTypeVariant(
'Failed',
AlgebraicType.createPrimitiveType(BuiltinType.Type.String)
Expand All @@ -21,10 +26,12 @@ export namespace UpdateStatus {
]);
}

export function serialize(value: UpdateStatus): object {
static serialize(value: UpdateStatus): object {
switch (value.tag) {
case 'Committed':
return { Committed: __DatabaseUpdate.serialize(value.value) };
return {
Committed: DatabaseUpdate.serialize(value.value as DatabaseUpdate),
};
case 'Failed':
return { Failed: value.value };
case 'OutOfEnergy':
Expand All @@ -34,36 +41,30 @@ export namespace UpdateStatus {
}
}

export type Committed = { tag: 'Committed'; value: __DatabaseUpdate };
export const Committed = (value: __DatabaseUpdate): Committed => ({
tag: 'Committed',
value,
});
export type Failed = { tag: 'Failed'; value: string };
export const Failed = (value: string): Failed => ({ tag: 'Failed', value });
export type OutOfEnergy = { tag: 'OutOfEnergy'; value: undefined };
export const OutOfEnergy = { tag: 'OutOfEnergy', value: undefined };

export function fromValue(value: AlgebraicValue): UpdateStatus {
static fromValue(value: AlgebraicValue): UpdateStatus {
let sumValue = value.asSumValue();
switch (sumValue.tag) {
case 0:
return {
tag: 'Committed',
value: __DatabaseUpdate.fromValue(sumValue.value),
};
return new UpdateStatus(
'Committed',
DatabaseUpdate.fromValue(sumValue.value)
);
case 1:
return { tag: 'Failed', value: sumValue.value.asString() };
return new UpdateStatus('Failed', sumValue.value.asString());
case 2:
return { tag: 'OutOfEnergy', value: undefined };
return new UpdateStatus('OutOfEnergy', undefined);
default:
throw 'unreachable';
}
}
}

export type UpdateStatus =
| UpdateStatus.Committed
| UpdateStatus.Failed
| UpdateStatus.OutOfEnergy;
export default UpdateStatus;
static Committed(value: DatabaseUpdate): UpdateStatus {
return new UpdateStatus('Committed', value);
}
static Failed(value: string): UpdateStatus {
return new UpdateStatus('Failed', value);
}
static OutOfEnergy(): UpdateStatus {
return new UpdateStatus('OutOfEnergy', undefined);
}
}
6 changes: 4 additions & 2 deletions packages/sdk/src/spacetimedb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,11 +516,13 @@ export class SpacetimeDBClient {
let errMessage = '';
switch (txUpdate.status.tag) {
case 'Committed':
subscriptionUpdate = parseDatabaseUpdate(txUpdate.status.value);
subscriptionUpdate = parseDatabaseUpdate(
txUpdate.status.value as ws.DatabaseUpdate
);
break;
case 'Failed':
subscriptionUpdate = new SubscriptionUpdateMessage([]);
errMessage = txUpdate.status.value;
errMessage = txUpdate.status.value as string;
break;
case 'OutOfEnergy':
subscriptionUpdate = new SubscriptionUpdateMessage([]);
Expand Down