From 363dc3def6c8ea28ed56a3765321b0271796859b Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 3 Feb 2025 09:31:02 +1100 Subject: [PATCH] #12 --- image_filter.py | 22 +++++++++--------- js/popup.js | 60 ++++++++++++++++++++++++++++++++----------------- 2 files changed, 51 insertions(+), 31 deletions(-) diff --git a/image_filter.py b/image_filter.py index ded6892..ba7bae3 100644 --- a/image_filter.py +++ b/image_filter.py @@ -16,27 +16,29 @@ import torch @PromptServer.instance.routes.post('/cg-image-filter-message') async def cg_image_filter_message(request): post = await request.post() - Message.data = post.get("response") + response = post.get("response") + if (Message.data is None or response!="-1"): + Message.data = response return web.json_response({}) -def wait(secs): +def wait(secs, uid): Message.data = None end_time = time.monotonic() + secs while(time.monotonic() < end_time and Message.data is None): throw_exception_if_processing_interrupted() - PromptServer.instance.send_sync("cg-image-filter-images", {"tick": int(end_time - time.monotonic())}) + PromptServer.instance.send_sync("cg-image-filter-images", {"tick": int(end_time - time.monotonic()), "uid": uid}) time.sleep(0.2) response = Message.data if response is None: - PromptServer.instance.send_sync("cg-image-filter-images", {"timeout": True}) + PromptServer.instance.send_sync("cg-image-filter-images", {"timeout": True, "uid": uid}) Message.data = None return response -def send_with_resend(payload, timeout): +def send_with_resend(payload, timeout, uid): response = "-1" while response == "-1": PromptServer.instance.send_sync("cg-image-filter-images", payload) - response = wait(timeout) + response = wait(timeout, uid) return response def mask_to_image(mask:torch.Tensor): @@ -83,7 +85,7 @@ class ImageFilter(PreviewImage): urls:list[str] = self.save_images(images=images, **kwargs)['ui']['images'] payload = {"uid": uid, "urls":urls, "allsame":all_the_same} - response = send_with_resend(payload, timeout) + response = send_with_resend(payload, timeout, uid) if not response: if ontimeout=='send none': response = "" @@ -132,7 +134,7 @@ class TextImageFilter(PreviewImage): payload = {"uid": uid, "urls":urls, "text":text} if mask is not None: payload['mask_urls'] = self.save_images(images=mask_to_image(mask), **kwargs)['ui']['images'] - response = send_with_resend(payload, timeout) + response = send_with_resend(payload, timeout, uid) return (image, response) @@ -169,7 +171,7 @@ class TextImageFilterWithExtras(PreviewImage): payload = {"uid": uid, "urls":urls, "text":text, "extras":[extra1, extra2, extra3]} if mask is not None: payload['mask_urls'] = self.save_images(images=mask_to_image(mask), **kwargs)['ui']['images'] - response = send_with_resend(payload, timeout) + response = send_with_resend(payload, timeout, uid) response = response.split("|||") if response else [text, extra1, extra2, extra3] return (image, *response) @@ -203,7 +205,7 @@ class MaskImageFilter(PreviewImage, LoadImage): def func(self, image, timeout, uid, if_no_mask, **kwargs): urls:list[str] = self.save_images(images=image, **kwargs)['ui']['images'] payload = {"uid": uid, "urls":urls, "maskedit":True} - response = send_with_resend(payload, timeout) + response = send_with_resend(payload, timeout, uid) if (response): try: diff --git a/js/popup.js b/js/popup.js index ef87b2f..83e887b 100644 --- a/js/popup.js +++ b/js/popup.js @@ -3,7 +3,14 @@ import { api } from "../../scripts/api.js"; import { create } from "./utils.js"; -EXTENSION_NODES = ["ImageFilter", "TextImageFilter", "MaskImageFilter", "TextImageFilterWithExtras"] +const EXTENSION_NODES = ["Image Filter", "Text Image Filter", "Mask Image Filter", "Text Image Filter with Extras"] +const POPUP_NODES = ["Image Filter", "Text Image Filter", "Text Image Filter with Extras"] + +class Log { + static log(s) { + console.log(s) + } +} class Popup extends HTMLSpanElement { constructor() { @@ -29,22 +36,22 @@ class Popup extends HTMLSpanElement { this.cancel_button = create('button', 'control', this.buttons, {innerText:"Cancel (X)"} ) this.extras = create('span', 'extras', this.buttons) - this.grid.addEventListener('click', this.on_click) - + this.grid.addEventListener('click', this.on_click.bind(this)) this.send_button.addEventListener( 'click', this.send_current_state.bind(this) ) this.cancel_button.addEventListener('click', this.send_cancel.bind(this) ) - - document.addEventListener('keypress', (e)=>{ this.on_keypress(e) }) + document.addEventListener('keypress', this.on_keypress.bind(this) ) + document.body.appendChild(this) this.close() } - _send_response(msg) { + _send_response(msg, no_extras) { const body = new FormData(); var response = msg - Array.from(this.extras.children).forEach((e)=>{ response = response + "|||" + e.value }) + if (!no_extras) Array.from(this.extras.children).forEach((e)=>{ response = response + "|||" + e.value }) body.append('response', response); api.fetchApi("/cg-image-filter-message", { method: "POST", body, }); + Log.log(`"Sent ${JSON.stringify(response)}`) this.close() } @@ -68,31 +75,42 @@ class Popup extends HTMLSpanElement { } handle_message(message) { - const detail = message.detail - this.allsame = detail.allsame || false - - if (detail.uid) { - const node = app.graph._nodes_by_id[detail.uid] - if (!node) return - const type = nodeType.type ?? nodeType.comfyClass; - if (!EXTENSION_NODES.includes(type)) return + if (!message.detail?.tick) Log.log(`Got ${JSON.stringify(message.detail)}`) + if (this.handling_message && !detail.timeout) { + Log.log(`Already handling a message, so dropped message`) + return } + this.handling_message = true + try { + const detail = message.detail + this.allsame = detail.allsame || false - if (detail.timeout) this.handle_timeout(detail) - else if (detail.tick) this.handle_tick(detail) - else if (detail.maskedit) this.handle_maskedit(detail) - else if (detail.urls) this.handle_urls(detail) + if (detail.uid) { + const node = app.graph._nodes_by_id[detail.uid] + if (!node) return + if (!EXTENSION_NODES.includes(node.type)) return + } else { + Log.log("Alien message") + } + + if (detail.timeout) this.handle_timeout(detail) + else if (detail.tick) this.handle_tick(detail) + else if (detail.maskedit) this.handle_maskedit(detail) + else if (detail.urls) this.handle_urls(detail) + } finally { this.handling_message = false } } reshow_window() { - this._send_response("-1") + Log.log('requesting reshow') + this._send_response("-1", true) } handle_timeout(detail) { this.close() } handle_tick(detail) { this.counter.innerText = `${detail.tick} s` - if (this.n_images && this.classList.contains('hidden')) this.reshow_window() + const node = app.graph._nodes_by_id[detail.uid] + if (POPUP_NODES.includes(node.type) && this.classList.contains('hidden')) this.reshow_window() } handle_maskedit(detail) {