mirror of
https://github.com/gradio-app/gradio.git
synced 2024-11-21 01:01:05 +08:00
Improve rapid generation performance via UI throttling (#7084)
* changes * add changeset --------- Co-authored-by: Ali Abid <aliabid@Alis-MacBook-Pro.local> Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
parent
e8b2d8b2f8
commit
94aa271ab1
6
.changeset/flat-suns-cry.md
Normal file
6
.changeset/flat-suns-cry.md
Normal file
@ -0,0 +1,6 @@
|
||||
---
|
||||
"@gradio/app": minor
|
||||
"gradio": minor
|
||||
---
|
||||
|
||||
feat:Improve rapid generation performance via UI throttling
|
@ -305,6 +305,43 @@
|
||||
});
|
||||
}
|
||||
|
||||
function throttle<T extends (...args: any[]) => any>(
|
||||
func: T,
|
||||
limit: number
|
||||
): (...funcArgs: Parameters<T>) => void {
|
||||
let lastFunc: ReturnType<typeof setTimeout>;
|
||||
let lastRan: number;
|
||||
let lastThis: any;
|
||||
let lastArgs: IArguments | null;
|
||||
|
||||
return function (this: any, ...args: Parameters<T>) {
|
||||
if (!lastRan) {
|
||||
func.apply(this, args);
|
||||
lastRan = Date.now();
|
||||
} else {
|
||||
clearTimeout(lastFunc);
|
||||
lastThis = this;
|
||||
lastArgs = arguments;
|
||||
|
||||
lastFunc = setTimeout(
|
||||
() => {
|
||||
if (Date.now() - lastRan >= limit) {
|
||||
if (lastArgs) {
|
||||
func.apply(lastThis, Array.prototype.slice.call(lastArgs));
|
||||
}
|
||||
lastRan = Date.now();
|
||||
}
|
||||
},
|
||||
Math.max(limit - (Date.now() - lastRan), 0)
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const refresh = throttle(() => {
|
||||
rootNode = rootNode;
|
||||
}, 50);
|
||||
|
||||
async function handle_update(
|
||||
data: any,
|
||||
fn_index: number,
|
||||
@ -317,7 +354,7 @@
|
||||
output.props.value_is_output = true;
|
||||
});
|
||||
|
||||
rootNode = rootNode;
|
||||
refresh();
|
||||
await tick();
|
||||
data?.forEach((value: any, i: number) => {
|
||||
const output = instance_map[outputs[i]];
|
||||
@ -340,7 +377,7 @@
|
||||
output.props.value = value;
|
||||
}
|
||||
});
|
||||
rootNode = rootNode;
|
||||
refresh();
|
||||
}
|
||||
|
||||
let submit_map: Map<number, ReturnType<typeof app.submit>> = new Map();
|
||||
@ -355,7 +392,7 @@
|
||||
obj.props = {};
|
||||
}
|
||||
obj.props[prop] = val;
|
||||
rootNode = rootNode;
|
||||
refresh();
|
||||
}
|
||||
let handled_dependencies: number[][] = [];
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user