-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
150 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use std::{cell::RefCell, rc::Rc}; | ||
use web_sys::{Document, HtmlElement}; | ||
|
||
use crate::{Composable, Composition, IntoComposable}; | ||
|
||
thread_local! { | ||
static HTML_CONTEXT: RefCell<Option<HtmlContext>> = RefCell::default(); | ||
} | ||
|
||
struct Inner { | ||
document: Document, | ||
body: HtmlElement, | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct HtmlContext { | ||
inner: Rc<RefCell<Inner>>, | ||
} | ||
|
||
impl HtmlContext { | ||
pub fn new() -> Self { | ||
let window = web_sys::window().unwrap(); | ||
let document = window.document().unwrap(); | ||
let body = document.body().unwrap(); | ||
Self { | ||
inner: Rc::new(RefCell::new(Inner { document, body })), | ||
} | ||
} | ||
|
||
pub fn current() -> Self { | ||
HTML_CONTEXT | ||
.try_with(|cx| cx.borrow().as_ref().unwrap().clone()) | ||
.unwrap() | ||
} | ||
|
||
pub fn enter(self) { | ||
HTML_CONTEXT | ||
.try_with(|cx| *cx.borrow_mut() = Some(self)) | ||
.unwrap() | ||
} | ||
} | ||
|
||
#[derive(PartialEq, Eq)] | ||
pub struct Html {} | ||
|
||
impl Composable for Html { | ||
fn compose(&mut self) -> impl IntoComposable { | ||
let cx = HtmlContext::current(); | ||
let inner = cx.inner.borrow_mut(); | ||
let element = inner.document.create_element("div").unwrap(); | ||
inner.body.append_child(&element).unwrap(); | ||
} | ||
} | ||
|
||
pub fn run<C>(content: fn() -> C) | ||
where | ||
C: IntoComposable + 'static, | ||
{ | ||
let cx = HtmlContext::new(); | ||
cx.enter(); | ||
|
||
let mut composition = Composition::new(content); | ||
composition.build() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,24 @@ | ||
use crate::{use_ref, UseRef, TASK_CONTEXT}; | ||
use crate::{use_ref, TASK_CONTEXT}; | ||
use futures::task::LocalSpawnExt; | ||
use futures::Future; | ||
use tokio::task::{self, JoinHandle}; | ||
|
||
/// A hook that spawns a local future on current thread. | ||
pub fn use_future<F>(f: impl FnOnce() -> F) -> UseFuture<F::Output> | ||
pub fn use_future<F>(f: impl FnOnce() -> F) | ||
where | ||
F: Future + 'static, | ||
{ | ||
let handle = use_ref(|| { | ||
let _handle = use_ref(|| { | ||
let future = f(); | ||
TASK_CONTEXT | ||
.try_with(|cx| { | ||
let guard = cx.borrow_mut(); | ||
let cx = guard.as_ref().unwrap(); | ||
let tx = cx.tx.clone(); | ||
task::spawn_local(async move { | ||
let output = future.await; | ||
tx.send(Box::new(())).unwrap(); | ||
output | ||
}) | ||
cx.local_pool.borrow().spawner().spawn_local(async move { | ||
let _output = future.await; | ||
tx.unbounded_send(Box::new(())).unwrap(); | ||
}); | ||
}) | ||
.unwrap() | ||
}); | ||
UseFuture { handle } | ||
} | ||
|
||
pub struct UseFuture<T> { | ||
handle: UseRef<JoinHandle<T>>, | ||
} | ||
|
||
impl<T: 'static> UseFuture<T> { | ||
pub fn abort(&self) { | ||
self.handle.get().abort() | ||
} | ||
} | ||
|
||
impl<T> Clone for UseFuture<T> { | ||
fn clone(&self) -> Self { | ||
*self | ||
} | ||
} | ||
|
||
impl<T> Copy for UseFuture<T> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "concoct-web-examples" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
concoct = { path = "../", features = ["html"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<html> | ||
<body></body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use concoct::{html::Html, IntoComposable}; | ||
|
||
fn app() -> impl IntoComposable { | ||
Html {} | ||
} | ||
|
||
fn main() { | ||
concoct::html::run(app) | ||
} |