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

Bind fetch and stream in JS client #8714

Merged
merged 16 commits into from
Jul 11, 2024
Merged
7 changes: 7 additions & 0 deletions .changeset/shaky-numbers-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@gradio/client": patch
"@gradio/upload": patch
"gradio": patch
---

fix:Bind `fetch` and `stream` in JS client
2 changes: 2 additions & 0 deletions client/js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ export class Client {
this.resolve_config = resolve_config.bind(this);
this.resolve_cookies = resolve_cookies.bind(this);
this.upload = upload.bind(this);
this.fetch = this.fetch.bind(this);
this.handle_space_success = this.handle_space_success.bind(this);
this.stream = this.stream.bind(this);
}

private async init(): Promise<void> {
Expand Down
26 changes: 24 additions & 2 deletions client/js/src/test/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ import {
} from "vitest";

import { Client, client, duplicate } from "..";
import { transformed_api_info, config_response } from "./test_data";
import {
transformed_api_info,
config_response,
response_api_info
} from "./test_data";
import { initialise_server } from "./server";
import { CONFIG_ERROR_MSG, SPACE_METADATA_ERROR_MSG } from "../constants";
import { SPACE_METADATA_ERROR_MSG } from "../constants";

const app_reference = "hmb/hello_world";
const broken_app_reference = "hmb/bye_world";
Expand All @@ -26,6 +30,24 @@ afterAll(() => server.close());

describe("Client class", () => {
describe("initialisation", () => {
test("fetch is bound to the Client instance", async () => {
const test = await Client.connect("hmb/hello_world");
const fetch_method = test.fetch;
const res = await fetch_method(direct_app_reference + "/info");

await expect(res.json()).resolves.toEqual(response_api_info);
});

test("stream is bound to the Client instance", async () => {
const test = await Client.connect("hmb/hello_world");
const stream_method = test.stream;
const url = new URL(`${direct_app_reference}/queue/data`);
const stream = stream_method(url);

expect(stream).toBeDefined();
expect(stream.onmessage).toBeDefined();
});

test("backwards compatibility of client using deprecated syntax", async () => {
const app = await client(app_reference);
expect(app.config).toEqual(config_response);
Expand Down
2 changes: 1 addition & 1 deletion client/js/src/utils/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export function readable_stream(
): EventSource {
const instance: EventSource & { readyState: number } = {
close: () => {
throw new Error("Method not implemented.");
console.warn("Method not implemented.");
},
onerror: null,
onmessage: null,
Expand Down
2 changes: 2 additions & 0 deletions js/upload/src/UploadProgress.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
const _data = JSON.parse(event.data);
if (!progress) progress = true;
if (_data.msg === "done") {
// the stream will close itself but is here for perspicuity; remove .close() in 5.0
stream?.close();
dispatch("done");
} else {
Expand All @@ -59,6 +60,7 @@
};
});
onDestroy(() => {
// the stream will close itself but is here for perspicuity; remove .close() in 5.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the fuck kinda word is this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2024-07-11 at 12 45 56

are ppl just making words up or something

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe i'm just stupider than i thought i was (and i already thought i was pretty stupid)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i did consult a thesaurus and it was exactly the word i needed 😤

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2024-07-11 at 12 48 22

see cambridge dictionarys definition

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i need a new word

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right fine clarity it is

if (stream != null || stream != undefined) stream.close();
});

Expand Down