Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract reactive primitives into new crate #204

Merged
merged 8 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions .github/workflows/build_website.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: Build Website

on: [push, pull_request]
on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
deploy:
Expand All @@ -23,8 +27,9 @@ jobs:

- name: Install trunk
run: >
wget -qO- https://github.com/thedodd/trunk/releases/download/v0.12.1/trunk-x86_64-unknown-linux-gnu.tar.gz | tar -xzf- &&
sudo mv trunk /usr/bin/
wget -qO-
https://github.com/thedodd/trunk/releases/download/v0.12.1/trunk-x86_64-unknown-linux-gnu.tar.gz
| tar -xzf- && sudo mv trunk /usr/bin/

- name: Cargo generate-lockfile
run: cargo generate-lockfile
Expand All @@ -42,9 +47,7 @@ jobs:

- name: Build website
run: >
cd website/ &&
npm ci &&
npm run prod
cd website/ && npm ci && npm run prod

- name: Build examples
run: |
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: Test

on: [push, pull_request]
on:
push:
branches: [master]
pull_request:
branches: [master]

env:
CARGO_TERM_COLOR: always
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"packages/sycamore",
"packages/sycamore-macro",
"packages/sycamore-reactive",
"packages/sycamore-router",
"packages/sycamore-router-macro",
"examples/components",
Expand Down
3 changes: 2 additions & 1 deletion examples/context/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use sycamore::context::{ContextProvider, ContextProviderProps};
use sycamore::prelude::*;
use sycamore::rx::{use_context, ContextProvider, ContextProviderProps};
use sycamore::rx::use_context;

#[component(Counter<G>)]
fn counter() -> Template<G> {
Expand Down
2 changes: 1 addition & 1 deletion examples/tweened/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Duration;

use sycamore::rx::Tweened;
use sycamore::motion::Tweened;
use sycamore::{easing, prelude::*};

#[component(App<G>)]
Expand Down
2 changes: 1 addition & 1 deletion packages/sycamore-macro/tests/component/component-pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn comp1() -> Template<G> {
}

#[component(Comp2<G>)]
fn comp2(props: ()) -> Template<G> {
fn comp2(_props: ()) -> Template<G> {
todo!();
}

Expand Down
24 changes: 24 additions & 0 deletions packages/sycamore-reactive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
authors = ["Luke Chu <37006668+lukechu10@users.noreply.github.com>"]
categories = ["gui", "wasm", "web-programming"]
description = "Reactive primitives for Sycamore"
edition = "2018"
homepage = "https://github.com/sycamore-rs/sycamore"
keywords = ["reactive"]
license = "MIT"
name = "sycamore-reactive"
readme = "../../README.md"
repository = "https://github.com/sycamore-rs/sycamore"
version = "0.5.2"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ahash = "0.7.4"
indexmap = { version = "1.7.0", features = ["std"] }
smallvec = "1.6.1"
serde = { version = "1.0.127", optional = true }

[dependencies.web-sys]
features = ["console"]
version = "0.3.52"
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::any::{Any, TypeId};

use crate::prelude::*;

use super::*;
use crate::*;

/// Trait for any type of context.
///
Expand Down Expand Up @@ -32,38 +30,6 @@ impl<T: 'static> ContextAny for Context<T> {
}
}

/// Props for [`ContextProvider`].
pub struct ContextProviderProps<T, F, G>
where
T: 'static,
F: FnOnce() -> Template<G>,
G: GenericNode,
{
pub value: T,
pub children: F,
}

/// Creates a new [`ReactiveScope`] with a context.
#[component(ContextProvider<G>)]
pub fn context_provider<T, F>(props: ContextProviderProps<T, F, G>) -> Template<G>
where
T: 'static,
F: FnOnce() -> Template<G>,
{
let ContextProviderProps { value, children } = props;

SCOPES.with(|scopes| {
// Create a new ReactiveScope with a context.
let mut scope = ReactiveScope::default();
scope.context = Some(Box::new(Context { value }));
scopes.borrow_mut().push(scope);
let template = children();
let scope = scopes.borrow_mut().pop().unwrap();
on_cleanup(move || drop(scope));
template
})
}

/// Get the value of a context in the current [`ReactiveScope`].
///
/// # Panics
Expand All @@ -83,23 +49,16 @@ pub fn use_context<T: Clone + 'static>() -> T {
})
}

#[cfg(all(test, feature = "ssr"))]
mod tests {
use super::*;

#[test]
fn basic_context() {
sycamore::render_to_string(|| {
template! {
ContextProvider(ContextProviderProps {
value: 1i32,
children: || {
let ctx = use_context::<i32>();
assert_eq!(ctx, 1);
template! {}
}
})
}
});
}
/// Creates a new [`ReactiveScope`] with a context and runs the supplied callback function.
pub fn create_context_scope<T: 'static, Out>(value: T, f: impl FnOnce() -> Out) -> Out {
SCOPES.with(|scopes| {
// Create a new ReactiveScope with a context.
let mut scope = ReactiveScope::default();
scope.context = Some(Box::new(Context { value }));
scopes.borrow_mut().push(scope);
let out = f();
let scope = scopes.borrow_mut().pop().unwrap();
on_cleanup(move || drop(scope));
out
})
}
Loading