Fix the Lite custom element to initialize the app in the connected callback and dispose the app in the disconnected callback (#7577)

* Fix the Lite custom element to initialize the app in the connected callback and dispose the app in the disconnected callback

* add changeset

* Add type hints

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
Yuichiro Tachibana (Tsuchiya) 2024-03-08 20:04:39 +00:00 committed by GitHub
parent 2edba133e2
commit 7c66a29dea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 14 deletions

View File

@ -0,0 +1,6 @@
---
"@gradio/app": minor
"gradio": minor
---
feat:Fix the Lite custom element to initialize the app in the connected callback and dispose the app in the disconnected callback

View File

@ -1,6 +1,10 @@
// NOTE: We should only import the types from ".." to avoid the circular dependency of implementations,
// which causes repeated executions of the ".." module in †he dev mode and can lead to multiple instances of the dev app.
import type { create as createLiteAppFunc, Options } from "..";
import type {
create as createLiteAppFunc,
Options,
GradioAppController
} from "..";
interface GradioComponentOptions {
info: Options["info"];
@ -40,24 +44,33 @@ export function bootstrap_custom_element(
}
class GradioLiteAppElement extends HTMLElement {
constructor() {
super();
controller: GradioAppController | null = null;
const gradioComponentOptions = this.parseGradioComponentOptions();
const gradioLiteAppOptions = this.parseGradioLiteAppOptions();
connectedCallback(): void {
// At the time of connectedCallback, the child elements of the custom element are not yet parsed,
// so we need to defer the initialization to the next frame.
// Ref: https://stackoverflow.com/q/70949141/13103190
window.requestAnimationFrame(() => {
const gradioComponentOptions = this.parseGradioComponentOptions();
const gradioLiteAppOptions = this.parseGradioLiteAppOptions();
this.innerHTML = "";
this.innerHTML = "";
create({
target: this, // Same as `js/app/src/main.ts`
code: gradioLiteAppOptions.code,
requirements: gradioLiteAppOptions.requirements,
files: gradioLiteAppOptions.files,
entrypoint: gradioLiteAppOptions.entrypoint,
...gradioComponentOptions
this.controller = create({
target: this, // Same as `js/app/src/main.ts`
code: gradioLiteAppOptions.code,
requirements: gradioLiteAppOptions.requirements,
files: gradioLiteAppOptions.files,
entrypoint: gradioLiteAppOptions.entrypoint,
...gradioComponentOptions
});
});
}
disconnectedCallback(): void {
this.controller?.unmount();
}
parseGradioComponentOptions(): GradioComponentOptions {
// Parse the options from the attributes of the <gradio-lite> element.
// The following attributes are supported:

View File

@ -35,7 +35,7 @@ declare let GRADIO_VERSION: string;
// As a result, the users of the Wasm app will have to load the CSS file manually.
// const ENTRY_CSS = "__ENTRY_CSS__";
interface GradioAppController {
export interface GradioAppController {
run_code: (code: string) => Promise<void>;
run_file: (path: string) => Promise<void>;
write: (