Skip to content

Commit

Permalink
allow for .subscribe to be overloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
andogq committed May 25, 2024
1 parent 8b27f0d commit 46ea1a9
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changes/overload-subscription.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@qubit-rs/client": minor:feat
---

allow for subscription method to be overloaded if only `on_data` is required.
3 changes: 2 additions & 1 deletion examples/counter/bindings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Stream } from "@qubit-rs/client";
export type Metadata = { param_a: string, param_b: number, param_c: boolean, more_metadata: Metadata | null, };
export type Test = { a: number, b: boolean, };
export type User = { name: string, email: string, age: number, metadata: Metadata, };

export type Server = { version: () => Promise<string>, count: () => Promise<number>, array: () => Promise<Array<string>>, user: { someHandler: (_id: string) => Promise<User>, create: (name: string, email: string, age: number) => Promise<User>, list: () => Promise<Array<Test>>, asdf: () => Promise<null> } };
export type Server = { version: () => Promise<string>, count: () => Promise<number>, countdown: (min: number, max: number) => Stream<number>, array: () => Promise<Array<string>>, user: { someHandler: (_id: string) => Promise<User>, create: (name: string, email: string, age: number) => Promise<User>, list: () => Promise<Array<Test>>, asdf: () => Promise<null> } };
6 changes: 4 additions & 2 deletions examples/counter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import type { Server } from "./bindings.ts";
const client = ws<Server>("ws://localhost:9944/rpc");

client.version().then((version) => console.log({ version })).catch(console.error);
client.user.get("test").then((user) => console.log(user)).catch(console.error);
client.user.someHandler("test").then((user) => console.log(user)).catch(console.error);
client.count().then((value) => console.log({ value })).catch(console.error);

await client.countdown(1, 4).subscribe({
client.countdown(1, 4).subscribe({
on_data: (data) => {
console.log("countdown: ", data);
},
Expand All @@ -16,3 +16,5 @@ await client.countdown(1, 4).subscribe({
}
});

client.countdown(1, 4)
.subscribe((n) => console.log("number is", n))
24 changes: 18 additions & 6 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,28 @@ export function build_client<Server>(client: Client): Server {
}
});

const subscribe: StreamSubscriber<any> = ({ on_data, on_end, on_error }) => {
function error(e: Error) {
if (on_error) {
on_error(e);
const subscribe: StreamSubscriber<any> = (handler) => {
let on_data = (_: unknown) => {};
let on_error = (_: Error) => {};
let on_end = () => {};

if (typeof handler === "function") {
on_data = handler;
} else {
if (handler?.on_data) {
on_data = handler.on_data;
}
if (handler?.on_error) {
on_error = handler.on_error;
}
if (handler?.on_end) {
on_end = handler.on_end;
}
}

// Make sure the client can handle susbcriptions
if (!client.subscribe) {
error(new Error("client does not support subscriptions"));
on_error(new Error("client does not support subscriptions"));
return () => {};
}
const subscribe = client.subscribe;
Expand All @@ -68,7 +80,7 @@ export function build_client<Server>(client: Client): Server {
// Result should be a subscription ID
if (typeof subscription_id !== "string" && typeof subscription_id !== "number") {
// TODO: Throw an error
error(new Error("cannot subscribe to subscription"));
on_error(new Error("cannot subscribe to subscription"));
return () => {};
}

Expand Down
16 changes: 10 additions & 6 deletions packages/client/src/stream.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
export type StreamSubscriber<T> = ({ on_data, on_error, on_end }: {
on_data?: (data: T) => void,
on_error?: (error: Error) => void,
on_end?: () => void,
}) => () => void;
export type StreamSubscriber<T> = (
handler:
| ((data: T) => void)
| {
on_data?: (data: T) => void;
on_error?: (error: Error) => void;
on_end?: () => void;
},
) => () => void;

export type Stream<T> = {
subscribe: StreamSubscriber<T>,
subscribe: StreamSubscriber<T>;
};

0 comments on commit 46ea1a9

Please sign in to comment.