Skip to content

Commit 797c56e

Browse files
prompt debounce (#150)
1 parent 80eb6c8 commit 797c56e

File tree

2 files changed

+78
-9
lines changed

2 files changed

+78
-9
lines changed

demo/realtime-img2img/frontend/src/lib/components/BlendingControl.svelte

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import { createEventDispatcher } from 'svelte';
2+
import { createEventDispatcher, onDestroy } from 'svelte';
33
import Button from './Button.svelte';
44
55
export let promptBlendingConfig: any = null;
@@ -27,6 +27,21 @@
2727
// UI state
2828
let activeTab: 'prompts' | 'seeds' = 'prompts';
2929
30+
// Debounce timers
31+
let promptDebounceTimer: number | null = null;
32+
let seedDebounceTimer: number | null = null;
33+
const DEBOUNCE_DELAY = 300; // milliseconds
34+
35+
// Cleanup timers on component destroy
36+
onDestroy(() => {
37+
if (promptDebounceTimer !== null) {
38+
clearTimeout(promptDebounceTimer);
39+
}
40+
if (seedDebounceTimer !== null) {
41+
clearTimeout(seedDebounceTimer);
42+
}
43+
});
44+
3045
// Helper functions for config hashing
3146
function getPromptConfigHash(config: any, current: string): string {
3247
return JSON.stringify(config) + '|' + current;
@@ -146,8 +161,18 @@
146161
147162
function updatePromptText(index: number, value: string) {
148163
promptList[index][0] = value;
149-
promptList = [...promptList];
150-
updatePromptBlendingWithoutRefresh();
164+
hasPendingPromptChanges = true;
165+
166+
// Clear existing timer
167+
if (promptDebounceTimer !== null) {
168+
clearTimeout(promptDebounceTimer);
169+
}
170+
171+
// Set new debounced timer
172+
promptDebounceTimer = setTimeout(() => {
173+
updatePromptBlendingWithoutRefresh();
174+
promptDebounceTimer = null;
175+
}, DEBOUNCE_DELAY) as unknown as number;
151176
}
152177
153178
function updatePromptWeight(index: number, value: number) {
@@ -225,8 +250,18 @@
225250
226251
function updateSeedValue(index: number, value: number) {
227252
seedList[index][0] = value;
228-
seedList = [...seedList];
229-
updateSeedBlendingWithoutRefresh();
253+
hasPendingSeedChanges = true;
254+
255+
// Clear existing timer
256+
if (seedDebounceTimer !== null) {
257+
clearTimeout(seedDebounceTimer);
258+
}
259+
260+
// Set new debounced timer
261+
seedDebounceTimer = setTimeout(() => {
262+
updateSeedBlendingWithoutRefresh();
263+
seedDebounceTimer = null;
264+
}, DEBOUNCE_DELAY) as unknown as number;
230265
}
231266
232267
function updateSeedWeight(index: number, value: number) {
@@ -370,7 +405,7 @@
370405

371406
<textarea
372407
bind:value={promptList[index][0]}
373-
on:input={() => updatePromptText(index, promptList[index][0])}
408+
on:input={(e) => updatePromptText(index, e.currentTarget.value)}
374409
placeholder="Enter prompt..."
375410
rows="2"
376411
class="w-full p-2 border rounded resize-none dark:bg-gray-600 dark:border-gray-500"
@@ -463,7 +498,7 @@
463498
<input
464499
type="number"
465500
bind:value={seedList[index][0]}
466-
on:input={() => updateSeedValue(index, seedList[index][0])}
501+
on:input={(e) => updateSeedValue(index, Number(e.currentTarget.value))}
467502
min="1"
468503
max="999999"
469504
class="flex-1 p-2 border rounded dark:bg-gray-600 dark:border-gray-500"

demo/realtime-img2img/frontend/src/lib/components/TextArea.svelte

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,46 @@
11
<script lang="ts">
22
import type { FieldProps } from '$lib/types';
3-
import { onMount } from 'svelte';
3+
import { onMount, onDestroy } from 'svelte';
44
export let value: string;
55
export let params: FieldProps;
6+
7+
let localValue: string = value;
8+
let debounceTimer: number | null = null;
9+
const DEBOUNCE_DELAY = 300;
10+
611
onMount(() => {
712
if (!value || value === '') {
813
value = String(params?.default ?? '');
14+
localValue = value;
915
}
1016
});
17+
18+
onDestroy(() => {
19+
if (debounceTimer !== null) {
20+
clearTimeout(debounceTimer);
21+
}
22+
});
23+
24+
// Update localValue when external value changes (but not during typing)
25+
$: if (value !== localValue && debounceTimer === null) {
26+
localValue = value;
27+
}
28+
29+
function handleInput(event: Event) {
30+
const target = event.currentTarget as HTMLTextAreaElement;
31+
localValue = target.value;
32+
33+
// Clear existing timer
34+
if (debounceTimer !== null) {
35+
clearTimeout(debounceTimer);
36+
}
37+
38+
// Set new debounced timer to update parent value
39+
debounceTimer = setTimeout(() => {
40+
value = localValue;
41+
debounceTimer = null;
42+
}, DEBOUNCE_DELAY) as unknown as number;
43+
}
1144
</script>
1245

1346
<div class="">
@@ -19,7 +52,8 @@
1952
class="mx-1 w-full px-3 py-2 outline-none"
2053
title={params?.title}
2154
placeholder="Add your prompt here..."
22-
bind:value
55+
value={localValue}
56+
on:input={handleInput}
2357
></textarea>
2458
</div>
2559
</div>

0 commit comments

Comments
 (0)