1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
use std::{fmt, rc::Rc};
use crate::engine::d2::platform::Dynamic;
use super::SignalConnection;
struct Generate<T>(fn() -> T);
impl<T> Copy for Generate<T> {}
impl<T> Clone for Generate<T> {
fn clone(&self) -> Self {
*self
}
}
#[derive(Default, Clone, Debug)]
pub struct SignalBase {
head: Box<Option<SignalConnection>>,
deferred_tasks: Option<Rc<Task>>,
}
impl SignalBase {
// pub const DISPATCHING_SENTINEL: SignalConnection = SignalConnection {
// listener: None,
// next: None,
// signal: None,
// stayInList: false,
// };
pub fn new(listener: Option<Dynamic>) -> Self {
// let instance = Self {
// head: None,
// deferred_tasks: None,
// };
// instance.head = if listener.is_some() {
// SignalConnection::new(Some(instance), listener)
// } else {
// None
// };
// instance
todo!("should deal with it");
}
/// Whether this signal has at least one listener.
#[inline]
pub fn has_listeners(&self) -> bool {
self.head.is_some()
}
pub(crate) fn connect_impl(&self, listener: Dynamic, prioritize: bool) -> SignalConnection {
todo!("should deal with it");
// let conn = SignalConnection::new(Some(self), listener);
// if self.dispatching() {
// todo!("should deal with it");
// // self.defer(Box::new(|| {
// // self.listAdd(conn, prioritize);
// // }));
// } else {
// self.listAdd(conn, prioritize);
// }
// conn
}
pub fn disconnect(&self, conn: &SignalConnection) {
if self.dispatching() {
todo!("should deal with it");
// self.defer(Box::new(|| {
// self.listRemove(conn);
// }));
} else {
self.list_remove(conn);
}
}
pub fn defer(&self, function: Box<dyn Fn()>) {
todo!("should deal with it");
// let tail = None;
// let p = self.deferred_tasks;
// while let Some(ref val) = p {
// tail = p;
// p = val.next;
// }
// let task = Task::new(function);
// if let Some(ref tail) = tail {
// tail.next = Some(task);
// } else {
// self.deferred_tasks = Some(task);
// }
}
pub fn will_emit(&self) -> Option<SignalConnection> {
// // Should never happen, since the pub emit methods will defer, but just in case..
// assert!(!self.dispatching());
// let snapshot = self.head;
// self.head = Self::DISPATCHING_SENTINEL;
// snapshot
todo!("should deal with it");
}
pub fn did_emit(&self, head: SignalConnection) {
// self.head = Some(head);
// let snapshot = self.deferred_tasks;
// self.deferred_tasks = None;
// while let Some(ref val) = snapshot {
// (val.func)();
// snapshot = val.next;
// }
todo!("should deal with it");
}
pub fn list_add(&self, conn: SignalConnection, prioritize: bool) {
todo!("should deal with it");
// if prioritize {
// // Prepend it to the beginning of the list
// conn.next = self.head;
// self.head = Some(conn);
// } else {
// // Append it to the end of the list
// let tail = None;
// let p = self.head;
// while let Some(ref val) = p {
// tail = p;
// p = val._next;
// }
// if let Some(ref tail) = tail {
// tail._next = Some(conn);
// } else {
// self.head = Some(conn);
// }
// }
}
pub fn list_remove(&self, conn: &SignalConnection) {
let prev: Option<SignalConnection> = None;
let p = &self.head;
todo!("should deal with it");
// while let Some(val) = p {
// if val == conn {
// // Splice out p
// let next = val._next;
// match prev {
// None => self.head = next,
// Some(ref prev) => prev.next = next,
// }
// return;
// }
// prev = *p;
// p = &val._next;
// }
}
#[inline]
pub fn dispatching(&self) -> bool {
todo!("should deal with it");
// self.head == Self::DISPATCHING_SENTINEL
}
}
#[derive(Default, Clone)]
struct Task {
pub func: Option<Rc<dyn Fn() + 'static>>,
pub next: Option<Box<Task>>,
}
// impl Copy for Task { }
impl Task {
pub fn new(func: Box<dyn Fn()>) -> Self {
// Self {
// func: Some(func),
// next: None,
// }
todo!("should deal with it");
}
}
impl fmt::Debug for Task {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Task")
// .field("x", &self.x)
// .field("y", &self.y)
.finish()
}
}