Skip to content

Commit 6505763

Browse files
Fix Reload Mode in 6.0 (#12383)
* Fix but ugly * add changeset * Remove console.log --------- Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
1 parent 2043967 commit 6505763

File tree

7 files changed

+219
-135
lines changed

7 files changed

+219
-135
lines changed

.changeset/heavy-lights-shop.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@gradio/core": minor
3+
"@gradio/dataset": minor
4+
"@self/app": minor
5+
"@self/spa": minor
6+
"gradio": minor
7+
---
8+
9+
feat:Fix Reload Mode in 6.0

js/app/src/routes/[...catchall]/+page.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@
212212
let wrapper: HTMLDivElement;
213213
let ready = false;
214214
let render_complete = false;
215+
let reload_count = 0;
215216
$: config = data.config;
216217
217218
let intersecting: ReturnType<typeof create_intersection_store> = {
@@ -313,7 +314,7 @@
313314
if (!app.config) {
314315
throw new Error("Could not resolve app config");
315316
}
316-
317+
reload_count += 1;
317318
config = app.config;
318319
window.__gradio_space__ = config.space_id;
319320
});

js/core/src/Blocks.svelte

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import { tick, onMount, setContext, settled } from "svelte";
2+
import { tick, onMount, setContext, settled, untrack } from "svelte";
33
import { _ } from "svelte-i18n";
44
import { Client } from "@gradio/client";
55
import { writable } from "svelte/store";
@@ -66,7 +66,8 @@
6666
vibe_mode,
6767
search_params,
6868
render_complete = false,
69-
ready = $bindable(false)
69+
ready = $bindable(false),
70+
reload_count = $bindable(0)
7071
}: {
7172
root: string;
7273
components: ComponentMeta[];
@@ -93,6 +94,7 @@
9394
search_params: URLSearchParams;
9495
render_complete: boolean;
9596
ready: boolean;
97+
reload_count: number;
9698
} = $props();
9799
98100
components.forEach((comp) => {
@@ -178,7 +180,7 @@
178180
api_calls = [...api_calls, payload];
179181
};
180182
181-
const dep_manager = new DependencyManager(
183+
let dep_manager = new DependencyManager(
182184
dependencies,
183185
app,
184186
app_tree.update_state.bind(app_tree),
@@ -188,16 +190,26 @@
188190
add_to_api_calls
189191
);
190192
191-
let old_dependencies = dependencies;
192-
// $: if (
193-
// dependencies !== old_dependencies &&
194-
// render_complete &&
195-
// !layout_creating
196-
// ) {
197-
// // re-run load triggers in SSR mode when page changes
198-
// // handle_load_triggers();
199-
// old_dependencies = dependencies;
200-
// }
193+
$effect(() => {
194+
reload_count;
195+
untrack(() => {
196+
app_tree.reload(components, layout, dependencies, {
197+
root,
198+
theme: theme_mode,
199+
version,
200+
api_prefix,
201+
max_file_size,
202+
autoscroll
203+
});
204+
dep_manager.reload(
205+
dependencies,
206+
app_tree.update_state.bind(app_tree),
207+
app_tree.get_state.bind(app_tree),
208+
app_tree.rerender.bind(app_tree),
209+
app
210+
);
211+
});
212+
});
201213
202214
let vibe_editor_width = 350;
203215

js/core/src/dependency.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,25 +204,36 @@ export class DependencyManager {
204204
) => void,
205205
add_to_api_calls: (payload: Payload) => void
206206
) {
207-
const { by_id, by_event } = this.create(dependencies);
208-
209-
this.dependencies_by_event = by_event;
210-
this.dependencies_by_fn = by_id;
207+
this.add_to_api_calls = add_to_api_calls;
211208
this.client = client;
212-
this.update_state_cb = update_state_cb;
213-
this.get_state_cb = get_state_cb;
214-
this.rerender_cb = rerender_cb;
215209
this.log_cb = log_cb;
216-
this.add_to_api_calls = add_to_api_calls;
210+
// this.update_state_cb = update_state_cb;
211+
// this.get_state_cb = get_state_cb;
212+
// this.rerender_cb = rerender_cb;
213+
this.reload(dependencies, update_state_cb, get_state_cb, rerender_cb);
214+
}
217215

216+
reload(
217+
dependencies: IDependency[],
218+
update_state,
219+
get_state,
220+
rerender,
221+
client
222+
) {
223+
const { by_id, by_event } = this.create(dependencies);
224+
this.dependencies_by_event = by_event;
225+
this.dependencies_by_fn = by_id;
218226
for (const [dep_id, dep] of this.dependencies_by_fn) {
219227
for (const [output_id] of dep.targets) {
220228
this.set_event_args(output_id, dep.event_args);
221229
}
222230
}
231+
this.client = client;
232+
this.update_state_cb = update_state;
233+
this.get_state_cb = get_state;
234+
this.rerender_cb = rerender;
223235
this.register_loading_stati(by_id);
224236
}
225-
226237
register_loading_stati(deps: Map<number, Dependency>): void {
227238
for (const [_, dep] of deps) {
228239
this.loading_stati.register(

js/core/src/init.svelte.ts

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,56 @@ export class AppTree {
110110

111111
this.root!.children = this.#layout_payload.children.map((node) =>
112112
this.traverse(node, (node) => {
113-
const new_node = this.create_node(node, component_map);
113+
const new_node = this.create_node(
114+
node,
115+
component_map,
116+
false,
117+
this.reactive_formatter
118+
);
119+
return new_node;
120+
})
121+
);
122+
this.component_ids = components.map((c) => c.id);
123+
this.initial_tabs = {};
124+
gather_initial_tabs(this.root!, this.initial_tabs);
125+
this.postprocess(this.root!);
126+
}
127+
128+
reload(
129+
components: ComponentMeta[],
130+
layout: LayoutNode,
131+
dependencies: Dependency[],
132+
config: AppConfig
133+
) {
134+
this.#layout_payload = layout;
135+
this.#component_payload = components;
136+
this.#config = config;
137+
this.#dependency_payload = dependencies;
138+
139+
this.root = this.create_node(
140+
{ id: layout.id, children: [] },
141+
new Map(),
142+
true
143+
);
144+
for (const comp of components) {
145+
if (comp.props.visible != false) this.components_to_register.add(comp.id);
146+
}
147+
148+
this.prepare();
149+
150+
const component_map = components.reduce((map, comp) => {
151+
map.set(comp.id, comp);
152+
return map;
153+
}, new Map<number, ComponentMeta>());
154+
155+
this.root!.children = this.#layout_payload.children.map((node) =>
156+
this.traverse(node, (node) => {
157+
const new_node = this.create_node(
158+
node,
159+
component_map,
160+
false,
161+
this.reactive_formatter
162+
);
114163
return new_node;
115164
})
116165
);
@@ -426,10 +475,6 @@ function gather_props(
426475
// For Tabs (or any component that already has an id prop)
427476
// Set the id to the props so that it doesn't get overwritten
428477
if (key === "id" || key === "autoscroll") {
429-
console.log("gather_props setting id/autoscroll", {
430-
key,
431-
value: props[key]
432-
});
433478
_props[key] = props[key];
434479
} else if (allowed_shared_props.includes(key as keyof SharedProps)) {
435480
const _key = key as keyof SharedProps;

0 commit comments

Comments
 (0)