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

Cleaned up code and added better error handling and responses for invalid tables #32

Merged
merged 4 commits into from
Mar 12, 2024
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
47 changes: 47 additions & 0 deletions src/client_db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { DatabaseTableClass } from ".";
import { Table } from "./table";

export class ClientDB {
/**
* The tables in the database.
*/
tables: Map<string, Table>;

constructor() {
this.tables = new Map();
}

/**
* Returns the table with the given name.
* @param name The name of the table.
* @returns The table
*/
getTable(name: string): Table {
const table = this.tables.get(name);

// ! This should not happen as the table should be available but an exception is thrown just in case.
if (!table) {
console.error(
"The table has not been registered for this client. Please register the table before using it. If you have registered global tables using the SpacetimeDBClient.registerTables() or `registerTable()` method, please make sure that is executed first!"
);
throw new Error(`Table ${name} does not exist`);
}

return table;
}

getOrCreateTable(
tableName: string,
pkCol: number | undefined,
entityClass: DatabaseTableClass
) {
let table: Table;
if (!this.tables.has(tableName)) {
table = new Table(tableName, pkCol, entityClass);
this.tables.set(tableName, table);
} else {
table = this.tables.get(tableName)!;
}
return table;
}
}
3 changes: 2 additions & 1 deletion src/database_table.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ClientDB } from "./client_db";
import { ReducerEvent } from "./reducer_event";
import { ClientDB, SpacetimeDBClient } from "./spacetimedb";
import { SpacetimeDBClient } from "./spacetimedb";
import { _tableProxy } from "./utils";

export type DatabaseTableClass = {
Expand Down
14 changes: 14 additions & 0 deletions src/global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ClientDB } from "./client_db";
import { SpacetimeDBClient } from "./spacetimedb";

export type SpacetimeDBGlobals = {
clientDB: ClientDB;
spacetimeDBClient: SpacetimeDBClient | undefined;
};

declare global {
interface Window {
__SPACETIMEDB__: SpacetimeDBGlobals;
}
var __SPACETIMEDB__: SpacetimeDBGlobals;
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from "./spacetimedb";
export * from "./identity";
export * from "./address";
export * from "./client_db";
export * from "./message_types";
36 changes: 36 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
type LogLevel = "info" | "warn" | "error" | "debug";

const LogLevelIdentifierIcon = {
component: "📦",
info: "ℹ️",
warn: "⚠️",
error: "❌",
debug: "🐛",
};

const LogStyle = {
component:
"color: #fff; background-color: #8D6FDD; padding: 2px 5px; border-radius: 3px;",
info: "color: #fff; background-color: #007bff; padding: 2px 5px; border-radius: 3px;",
warn: "color: #fff; background-color: #ffc107; padding: 2px 5px; border-radius: 3px;",
error:
"color: #fff; background-color: #dc3545; padding: 2px 5px; border-radius: 3px;",
debug:
"color: #fff; background-color: #28a745; padding: 2px 5px; border-radius: 3px;",
};

const LogTextStyle = {
component: "color: #8D6FDD;",
info: "color: #007bff;",
warn: "color: #ffc107;",
error: "color: #dc3545;",
debug: "color: #28a745;",
};

export const stdbLogger = (level: LogLevel, message: any) => {
console.log(
`%c${LogLevelIdentifierIcon[level]} ${level.toUpperCase()}%c ${message}`,
LogStyle[level],
LogTextStyle[level]
);
};
64 changes: 64 additions & 0 deletions src/message_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Address, Identity } from ".";
import { TableUpdate } from "./table";

export class SubscriptionUpdateMessage {
public tableUpdates: TableUpdate[];

constructor(tableUpdates: TableUpdate[]) {
this.tableUpdates = tableUpdates;
}
}

export class TransactionUpdateEvent {
public identity: Identity;
public address: Address | null;
public originalReducerName: string;
public reducerName: string;
public args: any[] | Uint8Array;
public status: string;
public message: string;

constructor(
identity: Identity,
address: Address | null,
originalReducerName: string,
reducerName: string,
args: any[] | Uint8Array,
status: string,
message: string
) {
this.identity = identity;
this.address = address;
this.originalReducerName = originalReducerName;
this.reducerName = reducerName;
this.args = args;
this.status = status;
this.message = message;
}
}

export class TransactionUpdateMessage {
public tableUpdates: TableUpdate[];
public event: TransactionUpdateEvent;

constructor(tableUpdates: TableUpdate[], event: TransactionUpdateEvent) {
this.tableUpdates = tableUpdates;
this.event = event;
}
}

export class IdentityTokenMessage {
public identity: Identity;
public token: string;
public address: Address;

constructor(identity: Identity, token: string, address: Address) {
this.identity = identity;
this.token = token;
this.address = address;
}
}
export type Message =
| SubscriptionUpdateMessage
| TransactionUpdateMessage
| IdentityTokenMessage;
Loading
Loading