Skip to content

Commit

Permalink
feat: mock value
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey committed Mar 22, 2024
1 parent a4206e3 commit b2617c3
Show file tree
Hide file tree
Showing 19 changed files with 193 additions and 32 deletions.
57 changes: 41 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ members = [
]

[workspace.package]
version = "0.1.44"
version = "0.1.45"
edition = "2021"
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand Down Expand Up @@ -99,6 +99,7 @@ strum_macros = "0.25.0"
extend = "1.1.2"

### io ###
flume = "0.11.0"
clap = { version = "4.2.2", features = ["derive"] }
colorize = "0.1.0"
crossterm = "0.25.0"
Expand Down Expand Up @@ -181,6 +182,7 @@ features = [
'ResizeObserverEntry',
'ResizeObserverOptions',
'ResizeObserverSize',
'DomRect',
'DomRectReadOnly',
# Style
'MediaQueryList',
Expand Down
2 changes: 1 addition & 1 deletion crates/forky/forky_core/src/extensions/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub impl<T> Option<T> {
fn ok(self) -> Result<T> { option_to_result(self) }
}

pub fn option_to_result<T>(option: Option<T>) -> Result<T> {
fn option_to_result<T>(option: Option<T>) -> Result<T> {
match option {
Some(value) => Ok(value),
None => Err(anyhow!("Expected Some")),
Expand Down
8 changes: 0 additions & 8 deletions crates/forky/forky_core/test/extensions/num_x.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
use forky_core::*;
use sweet::*;


#[sweet_test]
pub fn f32() -> Result<()> {
expect(f32::lerp(1., 2., 0.5)).to_be(1.5)?;
Ok(())
}
1 change: 1 addition & 0 deletions crates/forky/forky_web/src/dom_utils/animation_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl AnimationFrame {
request_animation_frame(f2.borrow().as_ref().unwrap());
Self(handle)
}
pub fn forget(self) { *self.0.borrow_mut() = i32::MAX; }
}

impl Drop for AnimationFrame {
Expand Down
5 changes: 3 additions & 2 deletions crates/forky/forky_web/src/dom_utils/html_event_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,20 @@ impl<T> HtmlEventListener<T> {
F: FnMut(T) + 'static,
T: FromWasmAbi + 'static,
{
Self::new_with_target(name, f, window().unwrap().unchecked_into())
Self::new_with_target(name, f, window().unwrap())
}
#[must_use]
pub fn new_with_target<F>(
name: &'static str,
f: F,
target: EventTarget,
target: impl Into<EventTarget>,
) -> Self
where
F: FnMut(T) + 'static,
T: FromWasmAbi + 'static,
{
let closure = Closure::from_func(f);
let target = target.into();
// let closure = Closure::wrap(Box::new(f) as Box<dyn FnMut(_)>);
target
.add_event_listener_with_callback(
Expand Down
40 changes: 40 additions & 0 deletions crates/forky/forky_web/src/dom_utils/resize_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ impl ResizeListener {
observer.observe(el);
Self { cb, observer }
}

pub fn forget(self) { std::mem::forget(self); }
/// utility function for parsing the entry, usually this is what you want
/// https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry/contentBoxSize
pub fn parse_entry(entry: &ResizeObserverEntry) -> (u32, u32) {
Expand All @@ -55,6 +57,44 @@ impl Drop for ResizeListener {
fn drop(&mut self) { self.observer.disconnect(); }
}


#[cfg(feature = "leptos")]
#[allow(unused)]
pub use leptos_resize::*;
#[cfg(feature = "leptos")]
pub mod leptos_resize {
use crate::ResizeListener;
use leptos::html::Div;
use leptos::*;
use std::ops::Deref;
use web_sys::ResizeObserverEntry;

pub fn create_resize_listener(
el: NodeRef<Div>,
) -> ReadSignal<Option<ResizeObserverEntry>> {
let signal = create_rw_signal(None);
let resize_listener = create_effect(move |_| {
if let Some(container) = el.get() {
let el = container.deref();
let el: &web_sys::Element = el.as_ref();
let listener = ResizeListener::new(el, move |entry| {
signal.set(Some(entry.clone()));
});
Some(listener)
} else {
None
}
});

on_cleanup(move || drop(resize_listener));

signal.read_only()
}
}




// pub fn sync_canvas_size(
// canvas: HtmlCanvasElement,
// ) -> FnClosure2<Array, ResizeObserver> {
Expand Down
4 changes: 2 additions & 2 deletions crates/forky/forky_web/src/dom_utils_leptos/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use web_sys::Document;

pub fn mount<F, N>(f: F)
where
F: Fn() -> N + 'static,
F: FnOnce() -> N + 'static,
N: IntoView,
{
set_panic_hook();
Expand All @@ -14,7 +14,7 @@ where

pub fn mount_to_head<F, N>(f: F)
where
F: Fn() -> N + 'static,
F: FnOnce() -> N + 'static,
N: IntoView,
{
set_panic_hook();
Expand Down
Empty file.
1 change: 1 addition & 0 deletions crates/sweet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ anyhow = { workspace = true }
getrandom = { workspace = true }
inventory = { workspace = true }
extend = { workspace = true }
flume = { workspace = true }

### console ###
log = { workspace = true }
Expand Down
5 changes: 4 additions & 1 deletion crates/sweet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@
fn true_is_true() -> Result<()> {
expect(true).to_be_true()
}
```
```

## TODO
- make the crate rustier like [cargo-pretty-test](https://github.com/josecelano/cargo-pretty-test)
39 changes: 39 additions & 0 deletions crates/sweet/src/common/mock_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use flume::Receiver;
use flume::Sender;
use flume::TryRecvError;



pub fn mock_value<T>() -> MockValue<T> { MockValue::new() }

#[derive(Debug, Clone)]
pub struct MockValue<T> {
pub send: Sender<T>,
pub recv: Receiver<T>,
}

impl<T> Default for MockValue<T> {
fn default() -> Self { Self::new() }
}

impl<T> MockValue<T> {
pub fn new() -> Self {
let (send, recv) = flume::unbounded();
Self { send, recv }
}
pub fn push(&self, value: T) {
self.send
.send(value)
.expect("the channel has been disconnected");
}

pub fn pop(&self) -> Option<T> {
match self.recv.try_recv() {
Ok(value) => Some(value),
Err(TryRecvError::Empty) => None,
Err(TryRecvError::Disconnected) => {
panic!("the channel has been disconnected")
}
}
}
}
Loading

0 comments on commit b2617c3

Please sign in to comment.