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

Change Identity from UInt8Array to Identity to match other SDKs #14

Merged
merged 2 commits into from
Aug 1, 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
40 changes: 40 additions & 0 deletions src/identity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export class Identity {
private data: Uint8Array;

constructor(data: Uint8Array) {
this.data = data;
}

// Method to compare two identities
isEqual(other: Identity): boolean {
if (this.data.length !== other.data.length) {
return false;
}
for (let i = 0; i < this.data.length; i++) {
if (this.data[i] !== other.data[i]) {
return false;
}
}
return true;
}

// Method to convert the Uint8Array to a hex string
toHexString(): string {
return Array.prototype.map
.call(this.data, (x) => ("00" + x.toString(16)).slice(-2))
.join("");
}

toUint8Array(): Uint8Array {
return this.data;
}

// Static method to create an Identity from a hex string
static fromString(str: string): Identity {
let matches = str.match(/.{1,2}/g) || [];
let data = Uint8Array.from(
matches.map((byte: string) => parseInt(byte, 16))
);
return new Identity(data);
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./spacetimedb";
export * from "./identity";
30 changes: 14 additions & 16 deletions src/spacetimedb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
BuiltinType,
} from "./algebraic_type";
import { EventType } from "./types";
import { Identity } from "./identity";
import {
Message as ProtobufMessage,
event_StatusToJSON,
Expand Down Expand Up @@ -73,14 +74,14 @@ export class Reducer {}
export class IDatabaseTable {}

export class ReducerEvent {
public callerIdentity: Uint8Array;
public callerIdentity: Identity;
public reducerName: string;
public status: string;
public message: string;
public args: any;

constructor(
callerIdentity: Uint8Array,
callerIdentity: Identity,
reducerName: string,
status: string,
message: string,
Expand Down Expand Up @@ -357,15 +358,15 @@ class SubscriptionUpdateMessage {
}

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

constructor(
identity: Uint8Array,
identity: Identity,
originalReducerName: string,
reducerName: string,
args: any[] | Uint8Array,
Expand All @@ -392,10 +393,10 @@ class TransactionUpdateMessage {
}

class IdentityTokenMessage {
public identity: Uint8Array;
public identity: Identity;
public token: string;

constructor(identity: Uint8Array, token: string) {
constructor(identity: Identity, token: string) {
this.identity = identity;
this.token = token;
}
Expand All @@ -422,7 +423,7 @@ export class SpacetimeDBClient {
/**
* The identity of the user.
*/
identity?: Uint8Array = undefined;
identity?: Identity = undefined;
/**
* The token of the user.
*/
Expand Down Expand Up @@ -630,8 +631,7 @@ export class SpacetimeDBClient {
if (reducer) {
this.emitter.emit(
"reducer:" + reducerName,
message.event.status,
message.event.identity,
reducerEvent,
reducerArgs
);
}
Expand Down Expand Up @@ -813,7 +813,7 @@ export class SpacetimeDBClient {

const event = message["transactionUpdate"]["event"] as any;
const functionCall = event["functionCall"] as any;
const identity: Uint8Array = event["callerIdentity"];
const identity: Identity = new Identity(event["callerIdentity"]);
const originalReducerName: string = functionCall["reducer"];
const reducerName: string = toPascalCase(originalReducerName);
const args = functionCall["argBytes"];
Expand All @@ -837,7 +837,7 @@ export class SpacetimeDBClient {
callback(transactionUpdate);
} else if (message["identityToken"]) {
const identityToken = message["identityToken"] as any;
const identity = identityToken["identity"];
const identity = new Identity(identityToken["identity"]);
const token = identityToken["token"];
const identityTokenMessage: IdentityTokenMessage =
new IdentityTokenMessage(identity, token);
Expand Down Expand Up @@ -890,10 +890,8 @@ export class SpacetimeDBClient {

const event = txUpdate["event"] as any;
const functionCall = event["function_call"] as any;
const identity: Uint8Array = Uint8Array.from(
const identity: Identity = Identity.fromString(
event["caller_identity"]
.match(/.{1,2}/g)
.map((byte: string) => parseInt(byte, 16))
);
const originalReducerName: string = functionCall["reducer"];
const reducerName: string = toPascalCase(originalReducerName);
Expand All @@ -918,7 +916,7 @@ export class SpacetimeDBClient {
callback(transactionUpdate);
} else if (data["IdentityToken"]) {
const identityToken = data["IdentityToken"];
const identity = identityToken["identity"];
const identity = new Identity(identityToken["identity"]);
const token = identityToken["token"];
const identityTokenMessage: IdentityTokenMessage =
new IdentityTokenMessage(identity, token);
Expand Down Expand Up @@ -1014,7 +1012,7 @@ export class SpacetimeDBClient {
this.emitter.off(eventName, callback);
}

onConnect(callback: (token: string, identity: Uint8Array) => void) {
onConnect(callback: (token: string, identity: Identity) => void) {
this.on("connected", callback);
}

Expand Down
19 changes: 9 additions & 10 deletions tests/spacetimedb_client.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SpacetimeDBClient, ReducerEvent } from "../src/spacetimedb";
import { Identity } from "../src/identity";
import WebsocketTestAdapter from "../src/websocket_test_adapter";
import Player from "./types/player";
import Point from "./types/point";
Expand Down Expand Up @@ -112,15 +113,12 @@ describe("SpacetimeDBClient", () => {
);

let reducerCallbackLog: {
status: string;
identity: Uint8Array;
reducerEvent: ReducerEvent;
reducerArgs: any[];
}[] = [];
CreatePlayerReducer.on(
(status: string, identity: Uint8Array, reducerArgs: any[]) => {
reducerCallbackLog.push({ status, identity, reducerArgs });
}
);
CreatePlayerReducer.on((reducerEvent: ReducerEvent, reducerArgs: any[]) => {
reducerCallbackLog.push({ reducerEvent, reducerArgs });
});

const subscriptionMessage = {
SubscriptionUpdate: {
Expand Down Expand Up @@ -183,16 +181,17 @@ describe("SpacetimeDBClient", () => {
expect(inserts[1].reducerEvent?.status).toBe("committed");
expect(inserts[1].reducerEvent?.message).toBe("a message");
expect(inserts[1].reducerEvent?.callerIdentity).toEqual(
Uint8Array.from([0, 255, 1])
Identity.fromString("00FF01")
);
expect(inserts[1].reducerEvent?.args).toEqual([
"A Player",
new Point(0.2, 0.3),
]);

expect(reducerCallbackLog).toHaveLength(1);
expect(reducerCallbackLog[0]["identity"]).toEqual(
Uint8Array.from([0, 255, 1])

expect(reducerCallbackLog[0]["reducerEvent"]["callerIdentity"]).toEqual(
Identity.fromString("00FF01")
);
});

Expand Down
4 changes: 3 additions & 1 deletion tests/types/create_player_reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
IDatabaseTable,
AlgebraicValue,
ReducerArgsAdapter,
ReducerEvent,
Identity,
} from "../../src/index";
// @ts-ignore
import { Point } from "./point";
Expand All @@ -32,7 +34,7 @@ export class CreatePlayerReducer {
}

public static on(
callback: (status: string, identity: Uint8Array, reducerArgs: any[]) => void
callback: (reducerEvent: ReducerEvent, reducerArgs: any[]) => void
) {
if (__SPACETIMEDB__.spacetimeDBClient) {
__SPACETIMEDB__.spacetimeDBClient.on("reducer:CreatePlayer", callback);
Expand Down
Loading