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

Inactive (path_find) subscriptions #14

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
ConnectReinstateOptions,
serverInfoAndState,
} from "./types";
import addSubscription from "./util";

export * from "./types";

Expand Down Expand Up @@ -1030,7 +1031,7 @@ export class XrplClient extends EventEmitter {
if (
String(_call?.id || "").split("@")[0] !== "_WsClient_Internal_ServerInfo"
) {
this[isSubscription ? "subscriptions" : "pendingCalls"].push(pendingCall);
(isSubscription) ? addSubscription(this.subscriptions, pendingCall) : this.pendingCalls.push(pendingCall);
}

this.eventBus.emit("__WsClient_call", pendingCall);
Expand Down
15 changes: 15 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { PendingCall } from "./types";

export default function addSubscription(subscriptions: PendingCall[], pendingCall: PendingCall): PendingCall[] {
// https://xrpl.org/path_find.html
// A client can only have one pathfinding request open at a time.
// If another pathfinding request is already open on the same connection, the old request is automatically closed and replaced with the new request.
if ((pendingCall.request.command === "path_find" && pendingCall.request.subcommand !== "status")) {
const find = subscriptions.findIndex(({ request }) => request.command == "path_find");
if (find > -1) subscriptions.splice(find, 1);
};
if (pendingCall.request.command !== "path_find" || (pendingCall.request.command === "path_find" && pendingCall.request.subcommand === "create"))
subscriptions.push(pendingCall)
return subscriptions;
}

175 changes: 173 additions & 2 deletions test/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { XrplClient } from "../dist/src/index.js";
import { AnyJson, PendingCall, XrplClient } from "../dist/src/index.js";
import addSubscription from "../dist/src/util.js";

jest.setTimeout(30 * 1000);

Expand All @@ -13,7 +14,7 @@ describe("Common", () => {
new Promise(async (resolve) => {
client = new XrplClient();
await client.ready();
await client.close();
client.close();
clearTimeout(timeoutPromise);
resolve(true);
}),
Expand All @@ -28,4 +29,174 @@ describe("Common", () => {
return expect(response).toEqual(true);
});
});

});

describe("Util", () => {
describe("Add Subscription", () => {
it("should add a path find subscription", async () => {
let subscriptions = addSubscription(
[],
createFakePendingCall(
"path_finding_subscribe_1",
{
command: "path_find",
subcommand: "create",
},
),
);
return expect(subscriptions.length).toEqual(1);
});
it("should add a path find subscription, then add a new one without closing removing the former", async () => {
let subs = addSubscription(
[],
createFakePendingCall(
"path_finding_subscribe_1",
{
command: "path_find",
subcommand: "create",
},
),
);
subs = addSubscription(
subs,
createFakePendingCall(
"path_finding_subscribe_2",
{
command: "path_find",
subcommand: "create",
},
),
);
expect(subs.length).toEqual(1);
return expect(subs[0].request.id._Request).toEqual("path_finding_subscribe_2");
});
it("should add a path find subscription, then close it, then add a new one, keeping just the new one", async () => {
let subs = addSubscription(
[],
createFakePendingCall(
"path_finding_subscribe_1",
{
command: "path_find",
subcommand: "create",
},
),
);
subs = addSubscription(
subs,
createFakePendingCall(
"path_finding_subscribe_2",
{
command: "path_find",
subcommand: "close",
},
),
);
subs = addSubscription(
subs,
createFakePendingCall(
"path_finding_subscribe_3",
{
command: "path_find",
subcommand: "create",
},
),
);
expect(subs.length).toEqual(1);
return expect(subs[0].request.id._Request).toEqual("path_finding_subscribe_3");
});
it("should add a path find subscription, not add the path find status request and keep the former", async () => {
let subs = addSubscription(
[],
createFakePendingCall(
"path_finding_subscribe_1",
{
command: "path_find",
subcommand: "create",
},
),
);
subs = addSubscription(
subs,
createFakePendingCall(
"path_finding_status_2",
{
command: "path_find",
subcommand: "status",
},
),
);
expect(subs.length).toEqual(1);
return expect(subs[0].request.id._Request).toEqual("path_finding_subscribe_1");
});
it("should add a non path find subscription", async () => {
let subs = addSubscription(
[],
createFakePendingCall(
"ledger_subscribe",
{
command: "subscribe",
streams: ["ledger"],
},
),
);
return expect(subs.length).toEqual(1);
});
it("should add a non path find subscription, then add a path find one, then a non path find", async () => {
let subs = addSubscription(
[],
createFakePendingCall(
"account_subscribe",
{
command: "subscribe",
streams: ["ledger"],
},
),
);
subs = addSubscription(
subs,
createFakePendingCall(
"account_subscribe",
{
command: "path_find",
subcommand: "create",
},
),
);
subs = addSubscription(
subs,
createFakePendingCall(
"account_subscribe",
{
command: "subscribe",
accounts: ["xxx"],
},
),
);
return expect(subs.length).toEqual(3);
});
});

});

const createFakePendingCall = (id: string, request: AnyJson): PendingCall => {
const base = Object.assign({}, basicFakePendingCall);
const deepReq = {...base.request,...request};
const body = Object.assign({}, base);
body.request = deepReq;
body.request.id._Request = id;
return JSON.parse(JSON.stringify(body)); // deep copy
};

const basicFakePendingCall = {
id: 4,
request: {
id: {
_WsClient: 4,
_Request: "",
},
},
promise: {} as any,
promiseCallables: {} as any,
sendOptions: {},
};
4 changes: 2 additions & 2 deletions test/network-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("Network specific", () => {
const client = new XrplClient(networkWithDefinitions);
await client.ready();
const definitions = await client.definitions();
await client.close();
client.close();
return expect(typeof definitions?.FIELDS).toEqual("object");
}
);
Expand All @@ -24,7 +24,7 @@ describe("Network specific", () => {
const client = new XrplClient(networkWithoutDefinitions);
await client.ready();
const definitions = await client.definitions();
await client.close();
client.close();
return expect(definitions).toEqual(null);
}
);
Expand Down
Loading