Skip to content

Commit

Permalink
Merge branch 'phoebe/protobufectomy' into puru/modernize
Browse files Browse the repository at this point in the history
  • Loading branch information
PuruVJ committed Jul 1, 2024
2 parents d9f394a + 6eaba28 commit f4e8423
Showing 1 changed file with 46 additions and 40 deletions.
86 changes: 46 additions & 40 deletions src/spacetimedb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,49 +251,55 @@ export class SpacetimeDBClient {
}
} else if (message instanceof TransactionUpdateMessage) {
const reducerName = message.event.reducerName;
const reducer: any | undefined = reducerName
? SpacetimeDBClient.getReducerClass(reducerName)
: undefined;

let reducerEvent: ReducerEvent | undefined;
let reducerArgs: any;
if (reducer && message.event.status === "committed") {
let adapter: ReducerArgsAdapter = new BinaryReducerArgsAdapter(
new BinaryAdapter(
new BinaryReader(message.event.args as Uint8Array)
)
);

reducerArgs = reducer.deserializeArgs(adapter);
}

reducerEvent = new ReducerEvent(
message.event.identity,
message.event.address,
message.event.originalReducerName,
message.event.status,
message.event.message,
reducerArgs
);

for (let tableUpdate of message.tableUpdates) {
const tableName = tableUpdate.tableName;
const entityClass = SpacetimeDBClient.getTableClass(tableName);
const table = this.db.getOrCreateTable(
tableUpdate.tableName,
undefined,
entityClass
if (reducerName == "<none>") {
let errorMessage = message.event.message;
console.error(`Received an error from the database: ${errorMessage}`);
} else {
const reducer: any | undefined = reducerName
? SpacetimeDBClient.getReducerClass(reducerName)
: undefined;

let reducerEvent: ReducerEvent | undefined;
let reducerArgs: any;
if (reducer && message.event.status === "committed") {
let adapter: ReducerArgsAdapter = new BinaryReducerArgsAdapter(
new BinaryAdapter(
new BinaryReader(message.event.args as Uint8Array)
)
);

reducerArgs = reducer.deserializeArgs(adapter);
}

reducerEvent = new ReducerEvent(
message.event.identity,
message.event.address,
message.event.originalReducerName,
message.event.status,
message.event.message,
reducerArgs
);

table.applyOperations(tableUpdate.operations, reducerEvent);
}

if (reducer) {
this.emitter.emit(
"reducer:" + reducerName,
reducerEvent,
...(reducerArgs || [])
);
for (let tableUpdate of message.tableUpdates) {
const tableName = tableUpdate.tableName;
const entityClass = SpacetimeDBClient.getTableClass(tableName);
const table = this.db.getOrCreateTable(
tableUpdate.tableName,
undefined,
entityClass
);

table.applyOperations(tableUpdate.operations, reducerEvent);
}

if (reducer) {
this.emitter.emit(
"reducer:" + reducerName,
reducerEvent,
...(reducerArgs || [])
);
}
}
} else if (message instanceof IdentityTokenMessage) {
this.identity = message.identity;
Expand Down

0 comments on commit f4e8423

Please sign in to comment.