Skip to content

Commit 6892602

Browse files
committed
chore: rename FromJSON to fromJSON
1 parent a9b20f2 commit 6892602

File tree

5 files changed

+23
-19
lines changed

5 files changed

+23
-19
lines changed

js/src/core/MessageUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function getMessageClass(
2121
return null;
2222
}
2323

24-
export function FromJSON(str): Messages.ButtplugMessage[] {
24+
export function fromJSON(str): Messages.ButtplugMessage[] {
2525
const msgarray: object[] = JSON.parse(str);
2626
const msgs: Messages.ButtplugMessage[] = [];
2727
for (const x of Array.from(msgarray)) {

js/src/utils/ButtplugBrowserWebsocketConnector.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import { EventEmitter } from 'eventemitter3';
1212
import { ButtplugMessage } from '../core/Messages';
13-
import { FromJSON } from '../core/MessageUtils';
13+
import { fromJSON } from '../core/MessageUtils';
1414

1515
export class ButtplugBrowserWebsocketConnector extends EventEmitter {
1616
protected _ws: WebSocket | undefined;
@@ -78,15 +78,15 @@ export class ButtplugBrowserWebsocketConnector extends EventEmitter {
7878

7979
protected parseIncomingMessage(event: MessageEvent) {
8080
if (typeof event.data === 'string') {
81-
const msgs = FromJSON(event.data);
81+
const msgs = fromJSON(event.data);
8282
this.emit('message', msgs);
8383
} else if (event.data instanceof Blob) {
8484
// No-op, we only use text message types.
8585
}
8686
}
8787

8888
protected onReaderLoad(event: Event) {
89-
const msgs = FromJSON((event.target as FileReader).result);
89+
const msgs = fromJSON((event.target as FileReader).result);
9090
this.emit('message', msgs);
9191
}
9292
}

js/tests/test-messages.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as Messages from "../src/core/Messages";
22
import { ButtplugClient } from "../src/client/Client";
3-
import { FromJSON } from "../src/core/MessageUtils";
3+
import { fromJSON } from "../src/core/MessageUtils";
44
import { SetupTestSuite } from "./utils";
55
import { ScalarSubcommand, VectorSubcommand, RotateSubcommand } from "../src/core/Messages";
66

@@ -15,7 +15,7 @@ describe("Message", () => {
1515
it("Converts ok message from json correctly",
1616
() => {
1717
const jsonStr = '[{"Ok":{"Id":2}}]';
18-
expect(FromJSON(jsonStr)).toEqual([new Messages.Ok(2)]);
18+
expect(fromJSON(jsonStr)).toEqual([new Messages.Ok(2)]);
1919
});
2020
it("Converts DeviceList message from json correctly",
2121
() => {
@@ -64,7 +64,7 @@ describe("Message", () => {
6464
}
6565
]
6666
`;
67-
expect(FromJSON(jsonStr))
67+
expect(fromJSON(jsonStr))
6868
.toEqual(
6969
[
7070
new Messages.DeviceList(
@@ -121,7 +121,7 @@ describe("Message", () => {
121121
}
122122
}
123123
]`;
124-
expect(FromJSON(jsonStr)[0] as Messages.DeviceAdded)
124+
expect(fromJSON(jsonStr)[0] as Messages.DeviceAdded)
125125
.toEqual(
126126
new Messages.DeviceAdded({
127127
DeviceIndex: 0,
@@ -139,15 +139,15 @@ describe("Message", () => {
139139
it("Converts Error message from json correctly",
140140
() => {
141141
const jsonStr = '[{"Error":{"Id":2,"ErrorCode":3,"ErrorMessage":"TestError"}}]';
142-
expect(FromJSON(jsonStr)).toEqual([new Messages.Error("TestError",
142+
expect(fromJSON(jsonStr)).toEqual([new Messages.Error("TestError",
143143
Messages.ErrorClass.ERROR_MSG,
144144
2)]);
145145
});
146146
/*
147147
it("Handles Device Commands with Subcommand arrays correctly",
148148
() => {
149149
const jsonStr = '[{"VibrateCmd":{"Id":2, "DeviceIndex": 3, "Speeds": [{ "Index": 0, "Speed": 1.0}, {"Index": 1, "Speed": 0.5}]}}]';
150-
expect(FromJSON(jsonStr)).toEqual([new Messages.VibrateCmd([{Index: 0, Speed: 1.0}, {Index: 1, Speed: 0.5}], 3, 2)]);
150+
expect(fromJSON(jsonStr)).toEqual([new Messages.VibrateCmd([{Index: 0, Speed: 1.0}, {Index: 1, Speed: 0.5}], 3, 2)]);
151151
});
152152
*/
153153
});

js/tests/test-websocketclient.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Server, WebSocket, Client } from "mock-socket";
22
import { ButtplugClient } from "../src/client/Client";
33
import * as Messages from "../src/core/Messages";
44
import { ButtplugLogLevel } from "../src/core/Logging";
5-
import { FromJSON } from "../src/core/MessageUtils";
5+
import { fromJSON } from "../src/core/MessageUtils";
66
import { SetupTestSuite } from "./utils";
77
import { ButtplugMessageError, ButtplugBrowserWebsocketClientConnector } from "../src";
88

@@ -27,7 +27,7 @@ describe("Websocket Client Tests", () => {
2727
mockServer = new Server("ws://localhost:6868");
2828
p = new Promise((resolve, reject) => { res = resolve; rej = reject; });
2929
const serverInfo = (jsonmsg: string) => {
30-
const msg: Messages.ButtplugMessage = FromJSON(jsonmsg)[0] as Messages.ButtplugMessage;
30+
const msg: Messages.ButtplugMessage = fromJSON(jsonmsg)[0] as Messages.ButtplugMessage;
3131
if (msg.Type === Messages.RequestServerInfo) {
3232
delaySend(new Messages.ServerInfo(3, 0, "Test Server", msg.Id));
3333
}
@@ -57,7 +57,7 @@ describe("Websocket Client Tests", () => {
5757

5858
it("Should deal with request/reply correctly", async () => {
5959
(socket as any).on("message", (jsonmsg: string) => {
60-
const msg: Messages.ButtplugMessage = FromJSON(jsonmsg)[0] as Messages.ButtplugMessage;
60+
const msg: Messages.ButtplugMessage = fromJSON(jsonmsg)[0] as Messages.ButtplugMessage;
6161
delaySend(new Messages.Ok(msg.Id));
6262
});
6363
await bp.startScanning();
@@ -72,7 +72,7 @@ describe("Websocket Client Tests", () => {
7272

7373
it("Should throw Error on return of error message", async () => {
7474
(socket as any).on("message", (jsonmsg: string) => {
75-
const msg: Messages.ButtplugMessage = FromJSON(jsonmsg)[0] as Messages.ButtplugMessage;
75+
const msg: Messages.ButtplugMessage = fromJSON(jsonmsg)[0] as Messages.ButtplugMessage;
7676
if (msg.Type === Messages.StopAllDevices) {
7777
delaySend(new Messages.Error("Error", Messages.ErrorClass.ERROR_MSG, msg.Id));
7878
}

wasm/src/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ButtplugMessage, IButtplugClientConnector, FromJSON } from 'buttplug';
1+
import { ButtplugMessage, IButtplugClientConnector, fromJSON } from 'buttplug';
22
import { EventEmitter } from 'eventemitter3';
33

44
export class ButtplugWasmClientConnector extends EventEmitter implements IButtplugClientConnector {
@@ -37,8 +37,7 @@ export class ButtplugWasmClientConnector extends EventEmitter implements IButtpl
3737
//ButtplugWasmClientConnector.wasmInstance.buttplug_activate_env_logger('debug');
3838
console.log(ButtplugWasmClientConnector.wasmInstance.buttplug_create_embedded_wasm_server);
3939
this.client = ButtplugWasmClientConnector.wasmInstance.buttplug_create_embedded_wasm_server((msgs) => {
40-
let str = new TextDecoder().decode(msgs);
41-
this.emit('message', FromJSON(str));
40+
this.emitMessage(msgs);
4241
}, this.serverPtr);
4342
this._connected = true;
4443
};
@@ -48,8 +47,13 @@ export class ButtplugWasmClientConnector extends EventEmitter implements IButtpl
4847
public send = (msg: ButtplugMessage): void => {
4948
console.log(msg);
5049
ButtplugWasmClientConnector.wasmInstance.buttplug_client_send_json_message(this.client, new TextEncoder().encode('[' + msg.toJSON() + ']'), (output) => {
51-
let str = new TextDecoder().decode(output);
52-
this.emit('message', FromJSON(str));
50+
this.emitMessage(output);
5351
});
5452
};
53+
54+
private emitMessage = (msg: Uint8Array) => {
55+
let str = new TextDecoder().decode(output);
56+
// This needs to use buttplug-js's fromJSON, otherwise we won't resolve the message name correctly.
57+
this.emit('message', fromJSON(str));
58+
}
5559
}

0 commit comments

Comments
 (0)