From 79d3cf3bfe7a274bbf2b127fa454f0d852dcd0d2 Mon Sep 17 00:00:00 2001 From: Puru Vijay Date: Tue, 16 Jul 2024 18:00:33 +0530 Subject: [PATCH] Format --- examples/quickstart/client/index.html | 2 +- examples/quickstart/client/src/main.tsx | 14 +- .../client/src/module_bindings/message.ts | 150 ++++++++-------- .../module_bindings/send_message_reducer.ts | 71 +++++--- .../src/module_bindings/set_name_reducer.ts | 71 +++++--- .../client/src/module_bindings/user.ts | 160 ++++++++++-------- examples/quickstart/client/vite.config.ts | 6 +- packages/sdk/test-app/README.md | 8 +- packages/sdk/test-app/index.html | 2 +- packages/sdk/test-app/src/main.tsx | 14 +- packages/sdk/test-app/vite.config.ts | 6 +- 11 files changed, 290 insertions(+), 214 deletions(-) diff --git a/examples/quickstart/client/index.html b/examples/quickstart/client/index.html index e4b78ea..e0d1c84 100644 --- a/examples/quickstart/client/index.html +++ b/examples/quickstart/client/index.html @@ -1,4 +1,4 @@ - + diff --git a/examples/quickstart/client/src/main.tsx b/examples/quickstart/client/src/main.tsx index 3d7150d..966f17a 100644 --- a/examples/quickstart/client/src/main.tsx +++ b/examples/quickstart/client/src/main.tsx @@ -1,10 +1,10 @@ -import React from 'react' -import ReactDOM from 'react-dom/client' -import App from './App.tsx' -import './index.css' +import React from "react"; +import ReactDOM from "react-dom/client"; +import App from "./App.tsx"; +import "./index.css"; -ReactDOM.createRoot(document.getElementById('root')!).render( +ReactDOM.createRoot(document.getElementById("root")!).render( - , -) + +); 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/send_message_reducer.ts b/examples/quickstart/client/src/module_bindings/send_message_reducer.ts index dc3c224..3e4ca1d 100644 --- a/examples/quickstart/client/src/module_bindings/send_message_reducer.ts +++ b/examples/quickstart/client/src/module_bindings/send_message_reducer.ts @@ -2,37 +2,52 @@ // WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. // @ts-ignore -import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, DatabaseTable, AlgebraicValue, ReducerArgsAdapter, SumTypeVariant, Serializer, Identity, Address, ReducerEvent, Reducer, SpacetimeDBClient } from "@clockworklabs/spacetimedb-sdk"; +import { + __SPACETIMEDB__, + AlgebraicType, + ProductType, + BuiltinType, + ProductTypeElement, + DatabaseTable, + AlgebraicValue, + ReducerArgsAdapter, + SumTypeVariant, + Serializer, + Identity, + Address, + ReducerEvent, + Reducer, + SpacetimeDBClient, +} from "@clockworklabs/spacetimedb-sdk"; -export class SendMessageReducer extends Reducer -{ - public static reducerName: string = "SendMessage"; - public static call(_text: string) { - this.getReducer().call(_text); - } +export class SendMessageReducer extends Reducer { + public static reducerName: string = "SendMessage"; + public static call(_text: string) { + this.getReducer().call(_text); + } - public call(_text: string) { - const serializer = this.client.getSerializer(); - let _textType = AlgebraicType.createPrimitiveType(BuiltinType.Type.String); - serializer.write(_textType, _text); - this.client.call("send_message", serializer); - } + public call(_text: string) { + const serializer = this.client.getSerializer(); + let _textType = AlgebraicType.createPrimitiveType(BuiltinType.Type.String); + serializer.write(_textType, _text); + this.client.call("send_message", serializer); + } - public static deserializeArgs(adapter: ReducerArgsAdapter): any[] { - let textType = AlgebraicType.createPrimitiveType(BuiltinType.Type.String); - let textValue = AlgebraicValue.deserialize(textType, adapter.next()) - let text = textValue.asString(); - return [text]; - } + public static deserializeArgs(adapter: ReducerArgsAdapter): any[] { + let textType = AlgebraicType.createPrimitiveType(BuiltinType.Type.String); + let textValue = AlgebraicValue.deserialize(textType, adapter.next()); + let text = textValue.asString(); + return [text]; + } - public static on(callback: (reducerEvent: ReducerEvent, _text: string) => void) { - this.getReducer().on(callback); - } - public on(callback: (reducerEvent: ReducerEvent, _text: string) => void) - { - this.client.on("reducer:SendMessage", callback); - } + public static on( + callback: (reducerEvent: ReducerEvent, _text: string) => void + ) { + this.getReducer().on(callback); + } + public on(callback: (reducerEvent: ReducerEvent, _text: string) => void) { + this.client.on("reducer:SendMessage", callback); + } } - -export default SendMessageReducer +export default SendMessageReducer; diff --git a/examples/quickstart/client/src/module_bindings/set_name_reducer.ts b/examples/quickstart/client/src/module_bindings/set_name_reducer.ts index d962289..b1eb683 100644 --- a/examples/quickstart/client/src/module_bindings/set_name_reducer.ts +++ b/examples/quickstart/client/src/module_bindings/set_name_reducer.ts @@ -2,37 +2,52 @@ // WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. // @ts-ignore -import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, DatabaseTable, AlgebraicValue, ReducerArgsAdapter, SumTypeVariant, Serializer, Identity, Address, ReducerEvent, Reducer, SpacetimeDBClient } from "@clockworklabs/spacetimedb-sdk"; +import { + __SPACETIMEDB__, + AlgebraicType, + ProductType, + BuiltinType, + ProductTypeElement, + DatabaseTable, + AlgebraicValue, + ReducerArgsAdapter, + SumTypeVariant, + Serializer, + Identity, + Address, + ReducerEvent, + Reducer, + SpacetimeDBClient, +} from "@clockworklabs/spacetimedb-sdk"; -export class SetNameReducer extends Reducer -{ - public static reducerName: string = "SetName"; - public static call(_name: string) { - this.getReducer().call(_name); - } +export class SetNameReducer extends Reducer { + public static reducerName: string = "SetName"; + public static call(_name: string) { + this.getReducer().call(_name); + } - public call(_name: string) { - const serializer = this.client.getSerializer(); - let _nameType = AlgebraicType.createPrimitiveType(BuiltinType.Type.String); - serializer.write(_nameType, _name); - this.client.call("set_name", serializer); - } + public call(_name: string) { + const serializer = this.client.getSerializer(); + let _nameType = AlgebraicType.createPrimitiveType(BuiltinType.Type.String); + serializer.write(_nameType, _name); + this.client.call("set_name", serializer); + } - public static deserializeArgs(adapter: ReducerArgsAdapter): any[] { - let nameType = AlgebraicType.createPrimitiveType(BuiltinType.Type.String); - let nameValue = AlgebraicValue.deserialize(nameType, adapter.next()) - let name = nameValue.asString(); - return [name]; - } + public static deserializeArgs(adapter: ReducerArgsAdapter): any[] { + let nameType = AlgebraicType.createPrimitiveType(BuiltinType.Type.String); + let nameValue = AlgebraicValue.deserialize(nameType, adapter.next()); + let name = nameValue.asString(); + return [name]; + } - public static on(callback: (reducerEvent: ReducerEvent, _name: string) => void) { - this.getReducer().on(callback); - } - public on(callback: (reducerEvent: ReducerEvent, _name: string) => void) - { - this.client.on("reducer:SetName", callback); - } + public static on( + callback: (reducerEvent: ReducerEvent, _name: string) => void + ) { + this.getReducer().on(callback); + } + public on(callback: (reducerEvent: ReducerEvent, _name: string) => void) { + this.client.on("reducer:SetName", callback); + } } - -export default SetNameReducer +export default SetNameReducer; 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/client/vite.config.ts b/examples/quickstart/client/vite.config.ts index 5a33944..9cc50ea 100644 --- a/examples/quickstart/client/vite.config.ts +++ b/examples/quickstart/client/vite.config.ts @@ -1,7 +1,7 @@ -import { defineConfig } from 'vite' -import react from '@vitejs/plugin-react' +import { defineConfig } from "vite"; +import react from "@vitejs/plugin-react"; // https://vitejs.dev/config/ export default defineConfig({ plugins: [react()], -}) +}); diff --git a/packages/sdk/test-app/README.md b/packages/sdk/test-app/README.md index e1cdc89..85a6989 100644 --- a/packages/sdk/test-app/README.md +++ b/packages/sdk/test-app/README.md @@ -17,12 +17,12 @@ If you are developing a production application, we recommend updating the config export default { // other rules... parserOptions: { - ecmaVersion: 'latest', - sourceType: 'module', - project: ['./tsconfig.json', './tsconfig.node.json', './tsconfig.app.json'], + ecmaVersion: "latest", + sourceType: "module", + project: ["./tsconfig.json", "./tsconfig.node.json", "./tsconfig.app.json"], tsconfigRootDir: __dirname, }, -} +}; ``` - Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked` diff --git a/packages/sdk/test-app/index.html b/packages/sdk/test-app/index.html index e4b78ea..e0d1c84 100644 --- a/packages/sdk/test-app/index.html +++ b/packages/sdk/test-app/index.html @@ -1,4 +1,4 @@ - + diff --git a/packages/sdk/test-app/src/main.tsx b/packages/sdk/test-app/src/main.tsx index 3d7150d..966f17a 100644 --- a/packages/sdk/test-app/src/main.tsx +++ b/packages/sdk/test-app/src/main.tsx @@ -1,10 +1,10 @@ -import React from 'react' -import ReactDOM from 'react-dom/client' -import App from './App.tsx' -import './index.css' +import React from "react"; +import ReactDOM from "react-dom/client"; +import App from "./App.tsx"; +import "./index.css"; -ReactDOM.createRoot(document.getElementById('root')!).render( +ReactDOM.createRoot(document.getElementById("root")!).render( - , -) + +); diff --git a/packages/sdk/test-app/vite.config.ts b/packages/sdk/test-app/vite.config.ts index 5a33944..9cc50ea 100644 --- a/packages/sdk/test-app/vite.config.ts +++ b/packages/sdk/test-app/vite.config.ts @@ -1,7 +1,7 @@ -import { defineConfig } from 'vite' -import react from '@vitejs/plugin-react' +import { defineConfig } from "vite"; +import react from "@vitejs/plugin-react"; // https://vitejs.dev/config/ export default defineConfig({ plugins: [react()], -}) +});