Skip to content

Commit

Permalink
Debounce wildcard search for large hierarchies (#737)
Browse files Browse the repository at this point in the history
* Publish collection count to frontend

* Debounce wildcard search when there are more than 5000 collections in the hierarchy

Fixes #736
  • Loading branch information
akx authored Mar 7, 2024
1 parent 7499182 commit 34f6523
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
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

0 comments on commit 34f6523

Please sign in to comment.