diff --git a/examples/quickstart/client/src/module_bindings/message.ts b/examples/quickstart/client/src/module_bindings/message.ts index e811c7c..6ed4f5b 100644 --- a/examples/quickstart/client/src/module_bindings/message.ts +++ b/examples/quickstart/client/src/module_bindings/message.ts @@ -2,80 +2,98 @@ // WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. // @ts-ignore -import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, SumType, SumTypeVariant, DatabaseTable, AlgebraicValue, ReducerEvent, Identity, Address, ClientDB, SpacetimeDBClient } from "@clockworklabs/spacetimedb-sdk"; +import { + __SPACETIMEDB__, + AlgebraicType, + ProductType, + BuiltinType, + ProductTypeElement, + SumType, + SumTypeVariant, + DatabaseTable, + AlgebraicValue, + ReducerEvent, + Identity, + Address, + ClientDB, + SpacetimeDBClient, +} from "@clockworklabs/spacetimedb-sdk"; -export class Message extends DatabaseTable -{ - public static db: ClientDB = __SPACETIMEDB__.clientDB; - public static tableName = "Message"; - public sender: Identity; - public sent: BigInt; - public text: string; +export class Message extends DatabaseTable { + public static db: ClientDB = __SPACETIMEDB__.clientDB; + public static tableName = "Message"; + public sender: Identity; + public sent: BigInt; + public text: string; - constructor(sender: Identity, sent: BigInt, text: string) { - super(); - this.sender = sender; - this.sent = sent; - this.text = text; - } + constructor(sender: Identity, sent: BigInt, text: string) { + super(); + this.sender = sender; + this.sent = sent; + this.text = text; + } - public static serialize(value: Message): object { - return [ - Array.from(value.sender.toUint8Array()), value.sent, value.text - ]; - } + public static serialize(value: Message): object { + return [Array.from(value.sender.toUint8Array()), value.sent, value.text]; + } - public static getAlgebraicType(): AlgebraicType - { - return AlgebraicType.createProductType([ - new ProductTypeElement("sender", AlgebraicType.createProductType([ - new ProductTypeElement("__identity_bytes", AlgebraicType.createArrayType(AlgebraicType.createPrimitiveType(BuiltinType.Type.U8))), - ])), - new ProductTypeElement("sent", AlgebraicType.createPrimitiveType(BuiltinType.Type.U64)), - new ProductTypeElement("text", AlgebraicType.createPrimitiveType(BuiltinType.Type.String)), - ]); - } + public static getAlgebraicType(): AlgebraicType { + return AlgebraicType.createProductType([ + new ProductTypeElement( + "sender", + AlgebraicType.createProductType([ + new ProductTypeElement( + "__identity_bytes", + AlgebraicType.createArrayType( + AlgebraicType.createPrimitiveType(BuiltinType.Type.U8) + ) + ), + ]) + ), + new ProductTypeElement( + "sent", + AlgebraicType.createPrimitiveType(BuiltinType.Type.U64) + ), + new ProductTypeElement( + "text", + AlgebraicType.createPrimitiveType(BuiltinType.Type.String) + ), + ]); + } - public static fromValue(value: AlgebraicValue): Message - { - let productValue = value.asProductValue(); - let __sender = new Identity(productValue.elements[0].asProductValue().elements[0].asBytes()); - let __sent = productValue.elements[1].asBigInt(); - let __text = productValue.elements[2].asString(); - return new this(__sender, __sent, __text); - } + public static fromValue(value: AlgebraicValue): Message { + let productValue = value.asProductValue(); + let __sender = new Identity( + productValue.elements[0].asProductValue().elements[0].asBytes() + ); + let __sent = productValue.elements[1].asBigInt(); + let __text = productValue.elements[2].asString(); + return new this(__sender, __sent, __text); + } - public static *filterBySender(value: Identity): IterableIterator - { - for (let instance of this.db.getTable("Message").getInstances()) - { - if (instance.sender.isEqual(value)) { - yield instance; - } - } - } - - public static *filterBySent(value: BigInt): IterableIterator - { - for (let instance of this.db.getTable("Message").getInstances()) - { - if (instance.sent === value) { - yield instance; - } - } - } - - public static *filterByText(value: string): IterableIterator - { - for (let instance of this.db.getTable("Message").getInstances()) - { - if (instance.text === value) { - yield instance; - } - } - } + public static *filterBySender(value: Identity): IterableIterator { + for (let instance of this.db.getTable("Message").getInstances()) { + if (instance.sender.isEqual(value)) { + yield instance; + } + } + } + public static *filterBySent(value: BigInt): IterableIterator { + for (let instance of this.db.getTable("Message").getInstances()) { + if (instance.sent === value) { + yield instance; + } + } + } + public static *filterByText(value: string): IterableIterator { + for (let instance of this.db.getTable("Message").getInstances()) { + if (instance.text === value) { + yield instance; + } + } + } } export default Message; diff --git a/examples/quickstart/client/src/module_bindings/user.ts b/examples/quickstart/client/src/module_bindings/user.ts index 8489cff..2e076dd 100644 --- a/examples/quickstart/client/src/module_bindings/user.ts +++ b/examples/quickstart/client/src/module_bindings/user.ts @@ -2,81 +2,109 @@ // WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. // @ts-ignore -import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, SumType, SumTypeVariant, DatabaseTable, AlgebraicValue, ReducerEvent, Identity, Address, ClientDB, SpacetimeDBClient } from "@clockworklabs/spacetimedb-sdk"; +import { + __SPACETIMEDB__, + AlgebraicType, + ProductType, + BuiltinType, + ProductTypeElement, + SumType, + SumTypeVariant, + DatabaseTable, + AlgebraicValue, + ReducerEvent, + Identity, + Address, + ClientDB, + SpacetimeDBClient, +} from "@clockworklabs/spacetimedb-sdk"; -export class User extends DatabaseTable -{ - public static db: ClientDB = __SPACETIMEDB__.clientDB; - public static tableName = "User"; - public identity: Identity; - public name: string | null; - public online: boolean; +export class User extends DatabaseTable { + public static db: ClientDB = __SPACETIMEDB__.clientDB; + public static tableName = "User"; + public identity: Identity; + public name: string | null; + public online: boolean; - public static primaryKey: string | undefined = "identity"; + public static primaryKey: string | undefined = "identity"; - constructor(identity: Identity, name: string | null, online: boolean) { - super(); - this.identity = identity; - this.name = name; - this.online = online; - } + constructor(identity: Identity, name: string | null, online: boolean) { + super(); + this.identity = identity; + this.name = name; + this.online = online; + } - public static serialize(value: User): object { - return [ - Array.from(value.identity.toUint8Array()), value.name ? { "some": value.name } : { "none": [] }, value.online - ]; - } + public static serialize(value: User): object { + return [ + Array.from(value.identity.toUint8Array()), + value.name ? { some: value.name } : { none: [] }, + value.online, + ]; + } - public static getAlgebraicType(): AlgebraicType - { - return AlgebraicType.createProductType([ - new ProductTypeElement("identity", AlgebraicType.createProductType([ - new ProductTypeElement("__identity_bytes", AlgebraicType.createArrayType(AlgebraicType.createPrimitiveType(BuiltinType.Type.U8))), - ])), - new ProductTypeElement("name", AlgebraicType.createSumType([ - new SumTypeVariant("some", AlgebraicType.createPrimitiveType(BuiltinType.Type.String)), - new SumTypeVariant("none", AlgebraicType.createProductType([ - ])), - ])), - new ProductTypeElement("online", AlgebraicType.createPrimitiveType(BuiltinType.Type.Bool)), - ]); - } + public static getAlgebraicType(): AlgebraicType { + return AlgebraicType.createProductType([ + new ProductTypeElement( + "identity", + AlgebraicType.createProductType([ + new ProductTypeElement( + "__identity_bytes", + AlgebraicType.createArrayType( + AlgebraicType.createPrimitiveType(BuiltinType.Type.U8) + ) + ), + ]) + ), + new ProductTypeElement( + "name", + AlgebraicType.createSumType([ + new SumTypeVariant( + "some", + AlgebraicType.createPrimitiveType(BuiltinType.Type.String) + ), + new SumTypeVariant("none", AlgebraicType.createProductType([])), + ]) + ), + new ProductTypeElement( + "online", + AlgebraicType.createPrimitiveType(BuiltinType.Type.Bool) + ), + ]); + } - public static fromValue(value: AlgebraicValue): User - { - let productValue = value.asProductValue(); - let __identity = new Identity(productValue.elements[0].asProductValue().elements[0].asBytes()); - let __name = productValue.elements[1].asSumValue().tag == 1 ? null : productValue.elements[1].asSumValue().value.asString(); - let __online = productValue.elements[2].asBoolean(); - return new this(__identity, __name, __online); - } + public static fromValue(value: AlgebraicValue): User { + let productValue = value.asProductValue(); + let __identity = new Identity( + productValue.elements[0].asProductValue().elements[0].asBytes() + ); + let __name = + productValue.elements[1].asSumValue().tag == 1 + ? null + : productValue.elements[1].asSumValue().value.asString(); + let __online = productValue.elements[2].asBoolean(); + return new this(__identity, __name, __online); + } - public static *filterByIdentity(value: Identity): IterableIterator - { - for (let instance of this.db.getTable("User").getInstances()) - { - if (instance.identity.isEqual(value)) { - yield instance; - } - } - } - - public static findByIdentity(value: Identity): User | undefined - { - return this.filterByIdentity(value).next().value; - } - - public static *filterByOnline(value: boolean): IterableIterator - { - for (let instance of this.db.getTable("User").getInstances()) - { - if (instance.online === value) { - yield instance; - } - } - } + public static *filterByIdentity(value: Identity): IterableIterator { + for (let instance of this.db.getTable("User").getInstances()) { + if (instance.identity.isEqual(value)) { + yield instance; + } + } + } + public static findByIdentity(value: Identity): User | undefined { + return this.filterByIdentity(value).next().value; + } + public static *filterByOnline(value: boolean): IterableIterator { + for (let instance of this.db.getTable("User").getInstances()) { + if (instance.online === value) { + yield instance; + } + } + } } export default User; diff --git a/examples/quickstart/server/Cargo.lock b/examples/quickstart/server/Cargo.lock index 4f19843..08c1666 100644 --- a/examples/quickstart/server/Cargo.lock +++ b/examples/quickstart/server/Cargo.lock @@ -62,6 +62,12 @@ dependencies = [ "generic-array", ] +[[package]] +name = "bytes" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" + [[package]] name = "cfg-if" version = "1.0.0" @@ -424,6 +430,7 @@ version = "0.10.0" dependencies = [ "arrayvec", "bitflags", + "bytes", "decorum", "derive_more", "enum-as-inner", diff --git a/src/client_api/server_message.ts b/src/client_api/server_message.ts index 5d54f30..ac4da8e 100644 --- a/src/client_api/server_message.ts +++ b/src/client_api/server_message.ts @@ -64,20 +64,14 @@ export namespace ServerMessage { }; export const InitialSubscription = ( value: __InitialSubscription - ): InitialSubscription => ({ - tag: "InitialSubscription", - value, - }); + ): InitialSubscription => ({ tag: "InitialSubscription", value }); export type TransactionUpdate = { tag: "TransactionUpdate"; value: __TransactionUpdate; }; export const TransactionUpdate = ( value: __TransactionUpdate - ): TransactionUpdate => ({ - tag: "TransactionUpdate", - value, - }); + ): TransactionUpdate => ({ tag: "TransactionUpdate", value }); export type IdentityToken = { tag: "IdentityToken"; value: __IdentityToken }; export const IdentityToken = (value: __IdentityToken): IdentityToken => ({ tag: "IdentityToken", @@ -89,10 +83,7 @@ export namespace ServerMessage { }; export const OneOffQueryResponse = ( value: __OneOffQueryResponse - ): OneOffQueryResponse => ({ - tag: "OneOffQueryResponse", - value, - }); + ): OneOffQueryResponse => ({ tag: "OneOffQueryResponse", value }); export function fromValue(value: AlgebraicValue): ServerMessage { let sumValue = value.asSumValue(); diff --git a/src/spacetimedb.ts b/src/spacetimedb.ts index 0e9c65e..d1b9162 100644 --- a/src/spacetimedb.ts +++ b/src/spacetimedb.ts @@ -1,4 +1,4 @@ -import { EventEmitter } from "eventemitter3"; +import { EventEmitter } from "events"; import { WebsocketDecompressAdapter } from "./websocket_decompress_adapter"; import type { WebsocketTestAdapter } from "./websocket_test_adapter"; @@ -16,10 +16,10 @@ import { AlgebraicValue, BinaryAdapter, BinaryReducerArgsAdapter, - ProductValue, parseValue, - type ReducerArgsAdapter, - type ValueAdapter, + ProductValue, + ReducerArgsAdapter, + ValueAdapter, } from "./algebraic_value"; import BinaryReader from "./binary_reader"; import * as ws from "./client_api"; @@ -35,12 +35,9 @@ import { TransactionUpdateMessage, type Message, } from "./message_types"; -import { Reducer, type ReducerClass } from "./reducer"; import { ReducerEvent } from "./reducer_event"; -import { BinarySerializer, type Serializer } from "./serializer"; -import { TableOperation, TableUpdate } from "./table"; -import { type EventType } from "./types"; -import { toPascalCase } from "./utils"; +import { BinarySerializer, Serializer } from "./serializer"; +import { EventType } from "./types"; export { AlgebraicType, diff --git a/src/table.ts b/src/table.ts index 89bd602..0627e3e 100644 --- a/src/table.ts +++ b/src/table.ts @@ -1,9 +1,9 @@ import { EventEmitter } from "eventemitter3"; -import { BinaryAdapter } from "./algebraic_value"; +import { AlgebraicValue, BinaryAdapter } from "./algebraic_value"; import BinaryReader from "./binary_reader"; +import type { DatabaseTable } from "./database_table"; import OperationsMap from "./operations_map"; import { ReducerEvent } from "./reducer_event"; -import { AlgebraicValue, DatabaseTable } from "./spacetimedb"; class DBOp { public type: "insert" | "delete"; diff --git a/src/websocket_decompress_adapter.ts b/src/websocket_decompress_adapter.ts index e8fa7aa..f67d5ce 100644 --- a/src/websocket_decompress_adapter.ts +++ b/src/websocket_decompress_adapter.ts @@ -1,6 +1,6 @@ +import WebSocket from "isomorphic-ws"; import decompress from "brotli/decompress"; import { Buffer } from "buffer"; -import WebSocket from "isomorphic-ws"; export class WebsocketDecompressAdapter { public onclose: Function | undefined; diff --git a/tests/spacetimedb_client.test.ts b/tests/spacetimedb_client.test.ts index 5053ad2..ed0e4ae 100644 --- a/tests/spacetimedb_client.test.ts +++ b/tests/spacetimedb_client.test.ts @@ -1,4 +1,3 @@ -import { beforeEach, describe, expect, test } from "vitest"; import { Address } from "../src/address"; import { AlgebraicType, BuiltinType } from "../src/algebraic_type"; import { parseValue } from "../src/algebraic_value";