use std::pin::Pin;
use std::borrow::BorrowMut;
use std::convert::AsRef;
use std::future::Future;
use std::task::{Context, Poll};
use once_cell::sync::Lazy;
use futures_signals::signal::{Signal, not};
use futures_signals::signal_vec::SignalVec;
use futures_util::FutureExt;
use futures_channel::oneshot;
use discard::{Discard, DiscardOnDrop};
use wasm_bindgen::{JsValue, UnwrapThrowExt, JsCast, intern};
use web_sys::{HtmlElement, Node, EventTarget, Element, CssStyleSheet, CssStyleDeclaration, ShadowRoot, ShadowRootMode, ShadowRootInit, Text};
use crate::bindings;
use crate::callbacks::Callbacks;
use crate::traits::*;
use crate::operations;
use crate::operations::{for_each, spawn_future};
use crate::utils::{EventListener, on, ValueDiscard, FnDiscard};
pub struct RefFn where B: ?Sized, C: Fn(&A) -> &B {
value: A,
callback: C,
}
impl RefFn where B: ?Sized, C: Fn(&A) -> &B {
#[inline]
pub fn new(value: A, callback: C) -> Self {
Self {
value,
callback,
}
}
#[inline]
pub fn call_ref(&self) -> &B {
(self.callback)(&self.value)
}
/*pub fn map(self, callback: E) -> RefFn &D>
where D: ?Sized,
E: Fn(&B) -> &D {
let old_callback = self.callback;
RefFn {
value: self.value,
callback: move |value| callback(old_callback(value)),
}
}*/
}
/*impl Deref for RefFn where B: ?Sized, C: Fn(&A) -> &B {
type Target = B;
#[inline]
fn deref(&self) -> &Self::Target {
self.call_ref()
}
}*/
/*impl AsRef for RefFn where B: ?Sized, C: Fn(&A) -> &B {
#[inline]
fn as_ref(&self) -> &B {
self.call_ref()
}
}*/
// https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS#Valid%20Namespace%20URIs
const SVG_NAMESPACE: &str = "http://www.w3.org/2000/svg";
// 32-bit signed int
pub const HIGHEST_ZINDEX: &str = "2147483647";
static HIDDEN_CLASS: Lazy = Lazy::new(|| class! {
.style_important("display", "none")
});
// TODO should return HtmlBodyElement ?
pub fn body() -> HtmlElement {
bindings::body()
}
pub fn get_id(id: &str) -> Element {
// TODO intern ?
bindings::get_element_by_id(id)
}
pub struct DomHandle {
parent: Node,
dom: Dom,
}
impl Discard for DomHandle {
#[inline]
fn discard(self) {
bindings::remove_child(&self.parent, &self.dom.element);
self.dom.callbacks.discard();
}
}
#[inline]
pub fn append_dom(parent: &Node, mut dom: Dom) -> DomHandle {
bindings::append_child(&parent, &dom.element);
dom.callbacks.trigger_after_insert();
// This prevents it from triggering after_remove
dom.callbacks.leak();
DomHandle {
parent: parent.clone(),
dom,
}
}
// TODO use must_use ?
enum IsWindowLoaded {
Initial {},
Pending {
receiver: oneshot::Receiver