gradio/js/app/component_loader.js
pngwn 5d1e8dae5a
batch UI updates on a per frame basis (#7564)
* changes

* process fe fn tests

* cleanup

* cleanup

* create_target_meta tests and abstraction

* add interactivity detection and tests

* more functions more tests

* add tests for component loader, fix errors

* fix everything

* add changeset

* add changeset

* ci

* cleanup

* cleanup

* test

* fix again

* tweaks

* cleanup

* add changeset

* fix loading_status

* cleanup

* ensure updates have been flushed before making API requests

* add changeset

* df fix

* fixes

* fix dataframe updates

* fix dataframe updates

* remove $open var

* add changeset

* fix tests

* extend timeout for lite

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
Co-authored-by: Hannah <hannahblair@users.noreply.github.com>
2024-03-13 13:46:40 +00:00

79 lines
1.7 KiB
JavaScript

// @ts-nocheck
const request_map = {};
export function load_component({ api_url, name, id, variant }) {
const comps = window.__GRADIO__CC__;
const _component_map = {
// eslint-disable-next-line no-undef
...component_map,
...(!comps ? {} : comps)
};
if (request_map[`${id}-${variant}`]) {
return { component: request_map[`${id}-${variant}`], name };
}
try {
if (!_component_map?.[id]?.[variant] && !_component_map?.[name]?.[variant])
throw new Error();
request_map[`${id}-${variant}`] = (
_component_map?.[id]?.[variant] || // for dev mode custom components
_component_map?.[name]?.[variant]
)();
return {
name,
component: request_map[`${id}-${variant}`]
};
} catch (e) {
try {
request_map[`${id}-${variant}`] = get_component_with_css(
api_url,
id,
variant
);
return {
name,
component: request_map[`${id}-${variant}`]
};
} catch (e) {
if (variant === "example") {
request_map[`${id}-${variant}`] = import("@gradio/fallback/example");
return {
name,
component: request_map[`${id}-${variant}`]
};
}
console.error(`failed to load: ${name}`);
console.error(e);
throw e;
}
}
}
function load_css(url) {
return new Promise((resolve, reject) => {
const link = document.createElement("link");
link.rel = "stylesheet";
link.href = url;
document.head.appendChild(link);
link.onload = () => resolve();
link.onerror = () => reject();
});
}
function get_component_with_css(api_url, id, variant) {
return Promise.all([
load_css(`${api_url}/custom_component/${id}/${variant}/style.css`),
import(
/* @vite-ignore */ `${api_url}/custom_component/${id}/${variant}/index.js`
)
]).then(([_, module]) => {
return module;
});
}