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

Changes required for SpacetimeDB#603 + improvements #29

Merged
merged 17 commits into from
Dec 11, 2023
Merged
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
6 changes: 4 additions & 2 deletions examples/quickstart/client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import Message from "./module_bindings/message";
import User from "./module_bindings/user";
import SendMessageReducer from "./module_bindings/send_message_reducer";
import SetNameReducer from "./module_bindings/set_name_reducer";
console.log(Message, User, SendMessageReducer, SetNameReducer);

SpacetimeDBClient.registerTables(Message, User);
SpacetimeDBClient.registerReducers(SendMessageReducer, SetNameReducer);

export type MessageType = {
name: string;
Expand Down Expand Up @@ -42,7 +44,7 @@ function App() {
console.log("client_error");
});

client.current.onConnect((token, identity) => {
client.current.onConnect((token: string, identity: Identity) => {
console.log("Connected to SpacetimeDB");

local_identity.current = identity;
Expand Down
83 changes: 12 additions & 71 deletions examples/quickstart/client/src/module_bindings/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@ import {
ProductTypeElement,
SumType,
SumTypeVariant,
IDatabaseTable,
DatabaseTable,
AlgebraicValue,
ReducerEvent,
Identity,
Address,
ClientDB,
SpacetimeDBClient,
} from "@clockworklabs/spacetimedb-sdk";

export class Message extends IDatabaseTable {
export class Message extends DatabaseTable {
public static db: ClientDB = __SPACETIMEDB__.clientDB;
public static tableName = "Message";
public sender: Identity;
public sent: number;
public sent: BigInt;
public text: string;

constructor(sender: Identity, sent: number, text: string) {
constructor(sender: Identity, sent: BigInt, text: string) {
super();
this.sender = sender;
this.sent = sent;
Expand Down Expand Up @@ -63,38 +66,24 @@ export class Message extends IDatabaseTable {
let __sender = new Identity(
productValue.elements[0].asProductValue().elements[0].asBytes()
);
let __sent = productValue.elements[1].asNumber();
let __sent = productValue.elements[1].asBigInt();
let __text = productValue.elements[2].asString();
return new this(__sender, __sent, __text);
}

public static count(): number {
return __SPACETIMEDB__.clientDB.getTable("Message").count();
}

public static all(): Message[] {
return __SPACETIMEDB__.clientDB
.getTable("Message")
.getInstances() as unknown as Message[];
}

public static filterBySender(value: Identity): Message[] {
let result: Message[] = [];
for (let instance of __SPACETIMEDB__.clientDB
.getTable("Message")
.getInstances()) {
for (let instance of this.db.getTable("Message").getInstances()) {
if (instance.sender.isEqual(value)) {
result.push(instance);
}
}
return result;
}

public static filterBySent(value: number): Message[] {
public static filterBySent(value: BigInt): Message[] {
let result: Message[] = [];
for (let instance of __SPACETIMEDB__.clientDB
.getTable("Message")
.getInstances()) {
for (let instance of this.db.getTable("Message").getInstances()) {
if (instance.sent === value) {
result.push(instance);
}
Expand All @@ -104,61 +93,13 @@ export class Message extends IDatabaseTable {

public static filterByText(value: string): Message[] {
let result: Message[] = [];
for (let instance of __SPACETIMEDB__.clientDB
.getTable("Message")
.getInstances()) {
for (let instance of this.db.getTable("Message").getInstances()) {
if (instance.text === value) {
result.push(instance);
}
}
return result;
}

public static onInsert(
callback: (value: Message, reducerEvent: ReducerEvent | undefined) => void
) {
__SPACETIMEDB__.clientDB.getTable("Message").onInsert(callback);
}

public static onUpdate(
callback: (
oldValue: Message,
newValue: Message,
reducerEvent: ReducerEvent | undefined
) => void
) {
__SPACETIMEDB__.clientDB.getTable("Message").onUpdate(callback);
}

public static onDelete(
callback: (value: Message, reducerEvent: ReducerEvent | undefined) => void
) {
__SPACETIMEDB__.clientDB.getTable("Message").onDelete(callback);
}

public static removeOnInsert(
callback: (value: Message, reducerEvent: ReducerEvent | undefined) => void
) {
__SPACETIMEDB__.clientDB.getTable("Message").removeOnInsert(callback);
}

public static removeOnUpdate(
callback: (
oldValue: Message,
newValue: Message,
reducerEvent: ReducerEvent | undefined
) => void
) {
__SPACETIMEDB__.clientDB.getTable("Message").removeOnUpdate(callback);
}

public static removeOnDelete(
callback: (value: Message, reducerEvent: ReducerEvent | undefined) => void
) {
__SPACETIMEDB__.clientDB.getTable("Message").removeOnDelete(callback);
}
}

export default Message;

__SPACETIMEDB__.registerComponent("Message", Message);
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,29 @@ import {
ProductType,
BuiltinType,
ProductTypeElement,
IDatabaseTable,
DatabaseTable,
AlgebraicValue,
ReducerArgsAdapter,
SumTypeVariant,
Serializer,
Identity,
Address,
ReducerEvent,
Reducer,
SpacetimeDBClient,
} from "@clockworklabs/spacetimedb-sdk";

export class SendMessageReducer {
export class SendMessageReducer extends Reducer {
public static reducerName: string = "SendMessage";
public static call(_text: string) {
if (__SPACETIMEDB__.spacetimeDBClient) {
const serializer = __SPACETIMEDB__.spacetimeDBClient.getSerializer();
let _textType = AlgebraicType.createPrimitiveType(
BuiltinType.Type.String
);
serializer.write(_textType, _text);
__SPACETIMEDB__.spacetimeDBClient.call("send_message", serializer);
}
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 static deserializeArgs(adapter: ReducerArgsAdapter): any[] {
Expand All @@ -38,20 +41,13 @@ export class SendMessageReducer {
}

public static on(
callback: (reducerEvent: ReducerEvent, reducerArgs: any[]) => void
callback: (reducerEvent: ReducerEvent, _text: string) => void
) {
if (__SPACETIMEDB__.spacetimeDBClient) {
__SPACETIMEDB__.spacetimeDBClient.on("reducer:SendMessage", callback);
}
this.getReducer().on(callback);
}
public on(callback: (reducerEvent: ReducerEvent, _text: string) => void) {
this.client.on("reducer:SendMessage", callback);
}
}

__SPACETIMEDB__.reducers.set("SendMessage", SendMessageReducer);
if (__SPACETIMEDB__.spacetimeDBClient) {
__SPACETIMEDB__.spacetimeDBClient.registerReducer(
"SendMessage",
SendMessageReducer
);
}

export default SendMessageReducer;
37 changes: 18 additions & 19 deletions examples/quickstart/client/src/module_bindings/set_name_reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,29 @@ import {
ProductType,
BuiltinType,
ProductTypeElement,
IDatabaseTable,
DatabaseTable,
AlgebraicValue,
ReducerArgsAdapter,
SumTypeVariant,
Serializer,
Identity,
Address,
ReducerEvent,
Reducer,
SpacetimeDBClient,
} from "@clockworklabs/spacetimedb-sdk";

export class SetNameReducer {
export class SetNameReducer extends Reducer {
public static reducerName: string = "SetName";
public static call(_name: string) {
if (__SPACETIMEDB__.spacetimeDBClient) {
const serializer = __SPACETIMEDB__.spacetimeDBClient.getSerializer();
let _nameType = AlgebraicType.createPrimitiveType(
BuiltinType.Type.String
);
serializer.write(_nameType, _name);
__SPACETIMEDB__.spacetimeDBClient.call("set_name", serializer);
}
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 static deserializeArgs(adapter: ReducerArgsAdapter): any[] {
Expand All @@ -38,17 +41,13 @@ export class SetNameReducer {
}

public static on(
callback: (reducerEvent: ReducerEvent, reducerArgs: any[]) => void
callback: (reducerEvent: ReducerEvent, _name: string) => void
) {
if (__SPACETIMEDB__.spacetimeDBClient) {
__SPACETIMEDB__.spacetimeDBClient.on("reducer:SetName", callback);
}
this.getReducer().on(callback);
}
public on(callback: (reducerEvent: ReducerEvent, _name: string) => void) {
this.client.on("reducer:SetName", callback);
}
}

__SPACETIMEDB__.reducers.set("SetName", SetNameReducer);
if (__SPACETIMEDB__.spacetimeDBClient) {
__SPACETIMEDB__.spacetimeDBClient.registerReducer("SetName", SetNameReducer);
}

export default SetNameReducer;
71 changes: 7 additions & 64 deletions examples/quickstart/client/src/module_bindings/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ import {
ProductTypeElement,
SumType,
SumTypeVariant,
IDatabaseTable,
DatabaseTable,
AlgebraicValue,
ReducerEvent,
Identity,
Address,
ClientDB,
SpacetimeDBClient,
} from "@clockworklabs/spacetimedb-sdk";

export class User extends IDatabaseTable {
export class User extends DatabaseTable {
public static db: ClientDB = __SPACETIMEDB__.clientDB;
public static tableName = "User";
public identity: Identity;
public name: string | null;
Expand Down Expand Up @@ -83,20 +86,8 @@ export class User extends IDatabaseTable {
return new this(__identity, __name, __online);
}

public static count(): number {
return __SPACETIMEDB__.clientDB.getTable("User").count();
}

public static all(): User[] {
return __SPACETIMEDB__.clientDB
.getTable("User")
.getInstances() as unknown as User[];
}

public static filterByIdentity(value: Identity): User | null {
for (let instance of __SPACETIMEDB__.clientDB
.getTable("User")
.getInstances()) {
for (let instance of this.db.getTable("User").getInstances()) {
if (instance.identity.isEqual(value)) {
return instance;
}
Expand All @@ -106,61 +97,13 @@ export class User extends IDatabaseTable {

public static filterByOnline(value: boolean): User[] {
let result: User[] = [];
for (let instance of __SPACETIMEDB__.clientDB
.getTable("User")
.getInstances()) {
for (let instance of this.db.getTable("User").getInstances()) {
if (instance.online === value) {
result.push(instance);
}
}
return result;
}

public static onInsert(
callback: (value: User, reducerEvent: ReducerEvent | undefined) => void
) {
__SPACETIMEDB__.clientDB.getTable("User").onInsert(callback);
}

public static onUpdate(
callback: (
oldValue: User,
newValue: User,
reducerEvent: ReducerEvent | undefined
) => void
) {
__SPACETIMEDB__.clientDB.getTable("User").onUpdate(callback);
}

public static onDelete(
callback: (value: User, reducerEvent: ReducerEvent | undefined) => void
) {
__SPACETIMEDB__.clientDB.getTable("User").onDelete(callback);
}

public static removeOnInsert(
callback: (value: User, reducerEvent: ReducerEvent | undefined) => void
) {
__SPACETIMEDB__.clientDB.getTable("User").removeOnInsert(callback);
}

public static removeOnUpdate(
callback: (
oldValue: User,
newValue: User,
reducerEvent: ReducerEvent | undefined
) => void
) {
__SPACETIMEDB__.clientDB.getTable("User").removeOnUpdate(callback);
}

public static removeOnDelete(
callback: (value: User, reducerEvent: ReducerEvent | undefined) => void
) {
__SPACETIMEDB__.clientDB.getTable("User").removeOnDelete(callback);
}
}

export default User;

__SPACETIMEDB__.registerComponent("User", User);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@clockworklabs/spacetimedb-sdk",
"version": "0.7.2",
"version": "0.8.0-alpha.5",
"description": "SDK for SpacetimeDB",
"author": {
"name": "Clockwork Labs",
Expand Down
Loading
Loading