Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debounce wildcard search for large hierarchies #737

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions javascript/dynamic_prompting.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class SDDP_UI {
this.lastMessage = null;
this.treeView = null;
this.treeContent = null;
this.collectionCount = 0;
this.treeFilter = null;
}

Expand Down Expand Up @@ -192,6 +193,7 @@ class SDDP_UI {
const { action, success } = message;
if (action === "load tree" && success) {
this.treeContent = message.tree;
this.collectionCount = message.collection_count ?? 0;
this.setupTree();
} else if (action === "load file" && success) {
this.loadFileIntoEditor(message);
Expand Down Expand Up @@ -256,12 +258,17 @@ class SDDP_UI {
this.messageReadTimer = setInterval(this.doReadMessage.bind(this), 120);
}
if (!this.searchKeyConfigured) {
gradioApp()
.querySelector("#sddp-wildcard-search textarea")
?.addEventListener("input", (event) => {
const debouncedSearch = this.debounce(
(event) => {
this.treeFilter = event.target.value?.trim() || null;
this.setupTree();
});
},
() => (this.collectionCount > 5000 ? 500 : 50),
this,
);
gradioApp()
.querySelector("#sddp-wildcard-search textarea")
?.addEventListener("input", debouncedSearch);
this.searchKeyConfigured = true;
}
}
Expand Down Expand Up @@ -301,6 +308,14 @@ class SDDP_UI {
content.forEach(walk);
return filteredContent;
};

debounce(handler, timeFunction, context) {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => handler.apply(context, args), timeFunction());
};
}
}

const SDDP = new SDDP_UI();
Expand Down
10 changes: 5 additions & 5 deletions sd_dynamic_prompts/wildcards_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ def _format_node_for_json(
return [*collections, *child_items]


def get_wildcard_hierarchy_for_json():
return _format_node_for_json(wildcard_manager, wildcard_manager.tree.root)


def on_ui_tabs():
help_html = f"""
<ol>
Expand Down Expand Up @@ -224,10 +220,14 @@ def copy_collection_callback(overwrite_checkbox, collection):

def refresh_wildcards_callback():
wildcard_manager.clear_cache()
root = wildcard_manager.tree.root
tree = _format_node_for_json(wildcard_manager, root)
collection_count = len(list(root.walk_full_names()))
return create_payload(
action=LOAD_TREE_ACTION,
success=True,
tree=get_wildcard_hierarchy_for_json(),
tree=tree,
collection_count=collection_count,
)


Expand Down
Loading