use ipc_channel::ipc;
use ipc_channel::ipc::IpcSender;
use ipc_channel::router::ROUTER;
use net_traits::image_cache::{ImageResponse, PendingImageResponse};
use crate::dom::bindings::conversions::DerivedFrom;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::reflector::DomObject;
use crate::dom::node::{window_from_node, Node};
use crate::script_runtime::CanGc;
use crate::task_source::TaskSource;
pub trait ImageCacheListener {
fn generation_id(&self) -> u32;
fn process_image_response(&self, response: ImageResponse, can_gc: CanGc);
}
pub fn generate_cache_listener_for_element<
T: ImageCacheListener + DerivedFrom<Node> + DomObject,
>(
elem: &T,
) -> IpcSender<PendingImageResponse> {
let trusted_node = Trusted::new(elem);
let (responder_sender, responder_receiver) = ipc::channel().unwrap();
let window = window_from_node(elem);
let (task_source, canceller) = window
.task_manager()
.networking_task_source_with_canceller();
let generation = elem.generation_id();
ROUTER.add_typed_route(
responder_receiver,
Box::new(move |message| {
let element = trusted_node.clone();
let image: PendingImageResponse = message.unwrap();
debug!("Got image {:?}", image);
let _ = task_source.queue_with_canceller(
task!(process_image_response: move || {
let element = element.root();
if generation == element.generation_id() {
element.process_image_response(image.response, CanGc::note());
}
}),
&canceller,
);
}),
);
responder_sender
}