2021-07-23 14:27:43 +00:00
|
|
|
use std::cell::{RefCell, RefMut};
|
2021-07-29 01:46:53 +00:00
|
|
|
use std::fmt::Display;
|
2021-07-15 08:09:28 +00:00
|
|
|
use std::{cell::UnsafeCell, rc::Rc};
|
2021-05-16 06:55:16 +00:00
|
|
|
|
2021-07-23 14:27:43 +00:00
|
|
|
use crate::heuristics::*;
|
2021-05-16 06:55:16 +00:00
|
|
|
use crate::innerlude::*;
|
2021-07-23 14:27:43 +00:00
|
|
|
use futures_util::stream::FuturesUnordered;
|
2021-07-29 22:04:09 +00:00
|
|
|
use fxhash::{FxHashMap, FxHashSet};
|
2021-07-23 21:03:51 +00:00
|
|
|
use slab::Slab;
|
2021-07-29 22:04:09 +00:00
|
|
|
use smallvec::SmallVec;
|
2021-05-16 06:55:16 +00:00
|
|
|
|
2021-07-23 21:03:51 +00:00
|
|
|
// slotmap::new_key_type! {
|
|
|
|
// // A dedicated key type for the all the scopes
|
|
|
|
// pub struct ScopeId;
|
|
|
|
// }
|
|
|
|
// #[cfg(feature = "serialize", serde::Serialize)]
|
|
|
|
// #[cfg(feature = "serialize", serde::Serialize)]
|
|
|
|
#[derive(serde::Serialize, serde::Deserialize, Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
2021-07-24 04:29:23 +00:00
|
|
|
pub struct ScopeId(pub usize);
|
2021-05-16 06:55:16 +00:00
|
|
|
|
2021-07-23 21:03:51 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
2021-07-24 04:29:23 +00:00
|
|
|
pub struct ElementId(pub usize);
|
2021-07-29 01:46:53 +00:00
|
|
|
impl Display for ElementId {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{}", self.0)
|
|
|
|
}
|
|
|
|
}
|
2021-05-16 06:55:16 +00:00
|
|
|
|
2021-07-23 14:27:43 +00:00
|
|
|
impl ElementId {
|
|
|
|
pub fn as_u64(self) -> u64 {
|
2021-07-24 06:52:05 +00:00
|
|
|
self.0 as u64
|
2021-05-16 06:55:16 +00:00
|
|
|
}
|
2021-07-23 14:27:43 +00:00
|
|
|
}
|
2021-05-16 06:55:16 +00:00
|
|
|
|
2021-07-23 14:27:43 +00:00
|
|
|
type Shared<T> = Rc<RefCell<T>>;
|
2021-08-06 02:23:41 +00:00
|
|
|
type TaskReceiver = futures_channel::mpsc::UnboundedReceiver<EventTrigger>;
|
|
|
|
type TaskSender = futures_channel::mpsc::UnboundedSender<EventTrigger>;
|
2021-05-16 06:55:16 +00:00
|
|
|
|
2021-07-23 14:27:43 +00:00
|
|
|
/// These are resources shared among all the components and the virtualdom itself
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct SharedResources {
|
2021-07-23 21:03:51 +00:00
|
|
|
pub components: Rc<UnsafeCell<Slab<Scope>>>,
|
2021-05-16 06:55:16 +00:00
|
|
|
|
2021-08-06 02:23:41 +00:00
|
|
|
pub(crate) heuristics: Shared<HeuristicsEngine>,
|
2021-07-23 14:27:43 +00:00
|
|
|
|
2021-08-06 02:23:41 +00:00
|
|
|
///
|
|
|
|
pub task_sender: TaskSender,
|
2021-07-23 14:27:43 +00:00
|
|
|
|
2021-08-06 02:23:41 +00:00
|
|
|
pub task_receiver: Shared<TaskReceiver>,
|
2021-07-23 14:27:43 +00:00
|
|
|
|
2021-08-06 02:23:41 +00:00
|
|
|
pub async_tasks: Shared<FuturesUnordered<FiberTask>>,
|
2021-07-23 14:27:43 +00:00
|
|
|
|
|
|
|
/// We use a SlotSet to keep track of the keys that are currently being used.
|
|
|
|
/// However, we don't store any specific data since the "mirror"
|
2021-07-29 22:04:09 +00:00
|
|
|
pub raw_elements: Rc<RefCell<Slab<()>>>,
|
2021-07-23 14:27:43 +00:00
|
|
|
|
|
|
|
pub task_setter: Rc<dyn Fn(ScopeId)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SharedResources {
|
|
|
|
pub fn new() -> Self {
|
2021-07-23 21:03:51 +00:00
|
|
|
// preallocate 2000 elements and 20 scopes to avoid dynamic allocation
|
|
|
|
let components: Rc<UnsafeCell<Slab<Scope>>> =
|
|
|
|
Rc::new(UnsafeCell::new(Slab::with_capacity(100)));
|
|
|
|
|
|
|
|
// elements are super cheap - the value takes no space
|
|
|
|
let raw_elements = Slab::with_capacity(2000);
|
2021-07-23 14:27:43 +00:00
|
|
|
|
2021-08-06 02:23:41 +00:00
|
|
|
// let pending_events = Rc::new(RefCell::new(Vec::new()));
|
|
|
|
|
|
|
|
let (sender, receiver) = futures_channel::mpsc::unbounded();
|
|
|
|
|
2021-07-23 14:27:43 +00:00
|
|
|
let heuristics = HeuristicsEngine::new();
|
|
|
|
|
2021-08-06 02:23:41 +00:00
|
|
|
// we allocate this task setter once to save us from having to allocate later
|
2021-07-23 21:03:51 +00:00
|
|
|
let task_setter = {
|
2021-08-06 02:23:41 +00:00
|
|
|
let queue = sender.clone();
|
2021-07-23 21:03:51 +00:00
|
|
|
let components = components.clone();
|
|
|
|
Rc::new(move |idx: ScopeId| {
|
|
|
|
let comps = unsafe { &*components.get() };
|
|
|
|
|
|
|
|
if let Some(scope) = comps.get(idx.0) {
|
2021-08-06 02:23:41 +00:00
|
|
|
queue
|
|
|
|
.unbounded_send(EventTrigger::new(
|
|
|
|
VirtualEvent::ScheduledUpdate {
|
|
|
|
height: scope.height,
|
|
|
|
},
|
|
|
|
idx,
|
|
|
|
None,
|
|
|
|
EventPriority::High,
|
|
|
|
))
|
|
|
|
.expect("The event queu receiver should *never* be dropped");
|
2021-07-23 21:03:51 +00:00
|
|
|
}
|
2021-08-06 02:23:41 +00:00
|
|
|
}) as Rc<dyn Fn(ScopeId)>
|
2021-07-23 21:03:51 +00:00
|
|
|
};
|
2021-07-23 14:27:43 +00:00
|
|
|
|
|
|
|
Self {
|
|
|
|
components,
|
2021-08-06 02:23:41 +00:00
|
|
|
async_tasks: Rc::new(RefCell::new(FuturesUnordered::new())),
|
|
|
|
task_receiver: Rc::new(RefCell::new(receiver)),
|
|
|
|
task_sender: sender,
|
2021-07-23 14:27:43 +00:00
|
|
|
heuristics: Rc::new(RefCell::new(heuristics)),
|
|
|
|
raw_elements: Rc::new(RefCell::new(raw_elements)),
|
|
|
|
task_setter,
|
|
|
|
}
|
2021-05-16 06:55:16 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 14:27:43 +00:00
|
|
|
/// this is unsafe because the caller needs to track which other scopes it's already using
|
|
|
|
pub unsafe fn get_scope(&self, idx: ScopeId) -> Option<&Scope> {
|
|
|
|
let inner = &*self.components.get();
|
2021-07-23 21:03:51 +00:00
|
|
|
inner.get(idx.0)
|
2021-05-16 06:55:16 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 14:27:43 +00:00
|
|
|
/// this is unsafe because the caller needs to track which other scopes it's already using
|
2021-07-23 21:03:51 +00:00
|
|
|
pub unsafe fn get_scope_mut(&self, idx: ScopeId) -> Option<&mut Scope> {
|
2021-07-23 14:27:43 +00:00
|
|
|
let inner = &mut *self.components.get();
|
2021-07-23 21:03:51 +00:00
|
|
|
inner.get_mut(idx.0)
|
2021-05-16 06:55:16 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 18:14:49 +00:00
|
|
|
pub fn with_scope<'b, O: 'static>(
|
|
|
|
&'b self,
|
2021-07-15 07:38:09 +00:00
|
|
|
_id: ScopeId,
|
2021-07-13 20:48:47 +00:00
|
|
|
_f: impl FnOnce(&'b mut Scope) -> O,
|
2021-06-07 18:14:49 +00:00
|
|
|
) -> Result<O> {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
// return a bumpframe with a lifetime attached to the arena borrow
|
|
|
|
// this is useful for merging lifetimes
|
|
|
|
pub fn with_scope_vnode<'b>(
|
|
|
|
&self,
|
2021-07-15 07:38:09 +00:00
|
|
|
_id: ScopeId,
|
2021-07-13 20:48:47 +00:00
|
|
|
_f: impl FnOnce(&mut Scope) -> &VNode<'b>,
|
2021-06-07 18:14:49 +00:00
|
|
|
) -> Result<&VNode<'b>> {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
2021-07-15 07:38:09 +00:00
|
|
|
pub fn try_remove(&self, id: ScopeId) -> Result<Scope> {
|
2021-07-09 15:54:07 +00:00
|
|
|
let inner = unsafe { &mut *self.components.get() };
|
2021-07-23 21:03:51 +00:00
|
|
|
Ok(inner.remove(id.0))
|
|
|
|
// .try_remove(id.0)
|
|
|
|
// .ok_or_else(|| Error::FatalInternal("Scope not found"))
|
2021-06-06 03:47:54 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 14:27:43 +00:00
|
|
|
pub fn reserve_node(&self) -> ElementId {
|
2021-07-23 21:03:51 +00:00
|
|
|
ElementId(self.raw_elements.borrow_mut().insert(()))
|
2021-07-23 14:27:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// return the id, freeing the space of the original node
|
2021-07-29 22:04:09 +00:00
|
|
|
pub fn collect_garbage(&self, id: ElementId) {
|
2021-08-06 02:23:41 +00:00
|
|
|
self.raw_elements.borrow_mut().remove(id.0);
|
2021-05-16 06:55:16 +00:00
|
|
|
}
|
2021-07-23 14:27:43 +00:00
|
|
|
|
|
|
|
pub fn insert_scope_with_key(&self, f: impl FnOnce(ScopeId) -> Scope) -> ScopeId {
|
|
|
|
let g = unsafe { &mut *self.components.get() };
|
2021-07-23 21:03:51 +00:00
|
|
|
let entry = g.vacant_entry();
|
|
|
|
let id = ScopeId(entry.key());
|
|
|
|
entry.insert(f(id));
|
|
|
|
id
|
2021-07-23 14:27:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn schedule_update(&self) -> Rc<dyn Fn(ScopeId)> {
|
|
|
|
self.task_setter.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn submit_task(&self, task: FiberTask) -> TaskHandle {
|
2021-08-06 02:23:41 +00:00
|
|
|
self.async_tasks.borrow_mut().push(task);
|
2021-07-23 14:27:43 +00:00
|
|
|
TaskHandle {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TaskHandle {}
|
|
|
|
|
|
|
|
impl TaskHandle {
|
|
|
|
pub fn toggle(&self) {}
|
|
|
|
pub fn start(&self) {}
|
|
|
|
pub fn stop(&self) {}
|
|
|
|
pub fn restart(&self) {}
|
2021-05-16 06:55:16 +00:00
|
|
|
}
|