Skip to content

Commit

Permalink
Push creds
Browse files Browse the repository at this point in the history
  • Loading branch information
PuruVJ committed Oct 22, 2024
1 parent b62ca68 commit 038059b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 39 deletions.
4 changes: 2 additions & 2 deletions packages/test-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ function App() {
})
.withCredentials([
Identity.fromString(
'93dda09db9a56d8fa6c024d843e805d8262191db3b4ba84c5efcd1ad451fed4e'
'c200c4018ee1d21bb54b0379bedb68643ca1dd209710e504fac2a41d43567fb2'
),
'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJoZXhfaWRlbnRpdHkiOiI5M2RkYTA5ZGI5YTU2ZDhmYTZjMDI0ZDg0M2U4MDVkODI2MjE5MWRiM2I0YmE4NGM1ZWZjZDFhZDQ1MWZlZDRlIiwiaWF0IjoxNzI4MzY3NDk0LCJleHAiOm51bGx9.ua3DQaiXo5i3Ye0okughs84PV9uZLCnov26zvCMG-3ibqGg6vH0uDDY6L-zDf8XiFgUqEIcvnJvuKLThPDipjQ',
'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJoZXhfaWRlbnRpdHkiOiJjMjAwYzQwMThlZTFkMjFiYjU0YjAzNzliZWRiNjg2NDNjYTFkZDIwOTcxMGU1MDRmYWMyYTQxZDQzNTY3ZmIyIiwic3ViIjoiYjNhZWRmNDgtZDExNC00MmVjLWI0Y2EtNmY5MjAwNjk2OGJmIiwiaXNzIjoibG9jYWxob3N0IiwiYXVkIjpbInNwYWNldGltZWRiIl0sImlhdCI6MTcyOTYyMTI2NiwiZXhwIjpudWxsfQ.TIJGisLeX3SuN8_9horfk2NifEGjCutq4wh7Fm5Qfp1-vhpKWmFCtAY0MRuYdalQtaWXT0bQ2seHlXA4PkOGbg',
])
.build()
);
Expand Down
104 changes: 67 additions & 37 deletions packages/test-app/src/module_bindings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,42 +38,42 @@ import {
TableCache,
// @ts-ignore
deepEqual,
} from "@clockworklabs/spacetimedb-sdk";
} from '@clockworklabs/spacetimedb-sdk';

// Import and reexport all reducer arg types
import { CreatePlayer } from "./create_player_reducer.ts";
import { CreatePlayer } from './create_player_reducer.ts';
export { CreatePlayer };

// Import and reexport all table handle types
import { PlayerTableHandle } from "./player_table.ts";
import { PlayerTableHandle } from './player_table.ts';
export { PlayerTableHandle };
import { UserTableHandle } from "./user_table.ts";
import { UserTableHandle } from './user_table.ts';
export { UserTableHandle };

// Import and reexport all types
import { Player } from "./player_type.ts";
import { Player } from './player_type.ts';
export { Player };
import { Point } from "./point_type.ts";
import { Point } from './point_type.ts';
export { Point };
import { User } from "./user_type.ts";
import { User } from './user_type.ts';
export { User };

const REMOTE_MODULE = {
tables: {
player: {
tableName: "player",
tableName: 'player',
rowType: Player.getTypeScriptAlgebraicType(),
primaryKey: "owner_id",
primaryKey: 'owner_id',
},
user: {
tableName: "user",
tableName: 'user',
rowType: User.getTypeScriptAlgebraicType(),
primaryKey: "identity",
primaryKey: 'identity',
},
},
reducers: {
create_player: {
reducerName: "create_player",
reducerName: 'create_player',
argsType: CreatePlayer.getTypeScriptAlgebraicType(),
},
},
Expand All @@ -82,70 +82,100 @@ const REMOTE_MODULE = {
eventContextConstructor: (imp: DBConnectionImpl, event: Event<Reducer>) => {
return {
...(imp as DBConnection),
event
}
event,
};
},
dbViewConstructor: (imp: DBConnectionImpl) => {
return new RemoteTables(imp);
},
reducersConstructor: (imp: DBConnectionImpl, setReducerFlags: SetReducerFlags) => {
reducersConstructor: (
imp: DBConnectionImpl,
setReducerFlags: SetReducerFlags
) => {
return new RemoteReducers(imp, setReducerFlags);
}
},
setReducerFlagsConstructor: () => {
return new SetReducerFlags();
}
}
},
};

// A type representing all the possible variants of a reducer.
export type Reducer = never
| { name: "CreatePlayer", args: CreatePlayer }
;
export type Reducer = never | { name: 'CreatePlayer'; args: CreatePlayer };

export class RemoteReducers {
constructor(private connection: DBConnectionImpl, private setCallReducerFlags: SetReducerFlags) {}
constructor(
private connection: DBConnectionImpl,
private setCallReducerFlags: SetReducerFlags
) {}

createPlayer(name: string, location: Point) {
const __args = { name, location };
let __writer = new BinaryWriter(1024);
CreatePlayer.getTypeScriptAlgebraicType().serialize(__writer, __args);
let __argsBuffer = __writer.getBuffer();
this.connection.callReducer("create_player", __argsBuffer, this.setCallReducerFlags.createPlayerFlags);
this.connection.callReducer(
'create_player',
__argsBuffer,
this.setCallReducerFlags.createPlayerFlags
);
}

onCreatePlayer(callback: (ctx: EventContext, name: string, location: Point) => void) {
this.connection.onReducer("create_player", callback);
onCreatePlayer(
callback: (ctx: EventContext, name: string, location: Point) => void
) {
this.connection.onReducer('create_player', callback);
}

removeOnCreatePlayer(callback: (ctx: EventContext, name: string, location: Point) => void) {
this.connection.offReducer("create_player", callback);
removeOnCreatePlayer(
callback: (ctx: EventContext, name: string, location: Point) => void
) {
this.connection.offReducer('create_player', callback);
}

}

export class SetReducerFlags {
createPlayerFlags: CallReducerFlags;
createPlayerFlags: CallReducerFlags = 'FullUpdate';
createPlayer(flags: CallReducerFlags) {
this.createPlayerFlags = flags;
}

}

export class RemoteTables {
constructor(private connection: DBConnectionImpl) {}

get player(): PlayerTableHandle {
return new PlayerTableHandle(this.connection.clientCache.getOrCreateTable<Player>(REMOTE_MODULE.tables.player));
return new PlayerTableHandle(
this.connection.clientCache.getOrCreateTable<Player>(
REMOTE_MODULE.tables.player
)
);
}

get user(): UserTableHandle {
return new UserTableHandle(this.connection.clientCache.getOrCreateTable<User>(REMOTE_MODULE.tables.user));
return new UserTableHandle(
this.connection.clientCache.getOrCreateTable<User>(
REMOTE_MODULE.tables.user
)
);
}
}

export class DBConnection extends DBConnectionImpl<RemoteTables, RemoteReducers, SetReducerFlags> {
static builder = (): DBConnectionBuilder<DBConnection> => {
return new DBConnectionBuilder<DBConnection>(REMOTE_MODULE, (imp: DBConnectionImpl) => imp as DBConnection);
}
export class DBConnection extends DBConnectionImpl<
RemoteTables,
RemoteReducers,
SetReducerFlags
> {
static builder = (): DBConnectionBuilder<DBConnection> => {
return new DBConnectionBuilder<DBConnection>(
REMOTE_MODULE,
(imp: DBConnectionImpl) => imp as DBConnection
);
};
}

export type EventContext = EventContextInterface<RemoteTables, RemoteReducers, SetReducerFlags, Reducer>;
export type EventContext = EventContextInterface<
RemoteTables,
RemoteReducers,
SetReducerFlags,
Reducer
>;

0 comments on commit 038059b

Please sign in to comment.