Compare commits

...

503 Commits

Author SHA1 Message Date
Greg Johnston
11f8453f7c fix: use shell for SSR, and do not create a second <head> 2024-07-12 11:32:10 -04:00
Saber Haj Rabiee
d0f2069f1c chore: leptosfmt 2024-07-11 06:56:25 -07:00
Saber Haj Rabiee
54481223bf fix: hackernews_js_fetch example 2024-07-11 00:48:09 -07:00
Greg Johnston
6ac6fc6a12 feat: iteration over reactive store list 2024-07-10 22:04:45 -04:00
Greg Johnston
a09cb02b31 feat: add arena-allocated/Copy Store 2024-07-10 21:03:48 -04:00
Greg Johnston
1f9ec1b979 docs: initial work on porting docs from 0.6 to 0.7 2024-07-10 20:47:24 -04:00
Greg Johnston
6c72aaf444 feat: initial work on reactive stores 2024-07-10 20:34:50 -04:00
Greg Johnston
899feb0575 fix: improved API for unsync actions that doesn't require SendWrapper on input 2024-07-10 19:33:40 -04:00
Greg Johnston
b0c1bf46af chore: leptosfmt 2024-07-10 19:21:59 -04:00
Greg Johnston
ec4958959c fix: properly handle errors in streaming body responses 2024-07-10 09:11:39 -04:00
Greg Johnston
174a5a1769 fix: invalid p/ul relationship causing hydration issue 2024-07-10 09:11:39 -04:00
Saber Haj Rabiee
ae68435fb6 chore: cargo fmt (#2672) 2024-07-09 08:51:01 -04:00
Greg Johnston
e507945cd1 feat: use codee for shared ser-de codexes with leptos-use (and more possibilities in future) 2024-07-08 21:31:51 -04:00
Greg Johnston
8f6620d06a chore: remove unused dependencies 2024-07-08 19:53:29 -04:00
Greg Johnston
23f433efa4 fix: was disposing of Suspense Owner too early 2024-07-08 19:47:09 -04:00
Greg Johnston
d9502a94c1 fix: rebuilding NodeRef 2024-07-08 19:13:53 -04:00
Greg Johnston
ae0dc13ce7 feat: top-level Suspend without Suspense 2024-07-06 17:46:26 -04:00
Greg Johnston
22b1132340 chore: clippy and clean up unused functions 2024-07-06 12:56:29 -04:00
Greg Johnston
cb7656599e docs: update syntax for Html/Body 2024-07-06 12:51:39 -04:00
Greg Johnston
f40d0c1334 feat: correct HTML rendering for spread attributes on <Body/> and <Html/> 2024-07-06 12:49:16 -04:00
Greg Johnston
8a878eaaa4 feat: attributes on body and html 2024-07-06 09:39:54 -04:00
Greg Johnston
f70702c6c4 feat: move to a channel-based implementation for meta 2024-07-06 08:16:48 -04:00
Greg Johnston
e2d41f91fa feat: support Suspend in attributes 2024-07-06 07:41:07 -04:00
Greg Johnston
dc21e4ff53 feat: local resources with .await 2024-07-04 15:39:42 -04:00
Greg Johnston
d5aecbe705 chore: cfg warnings 2024-07-04 11:42:06 -04:00
Greg Johnston
032faec5bc fix: MaybeProp None case 2024-07-03 21:01:04 -04:00
Greg Johnston
443fca495d reexport SVG and MathML element types 2024-07-03 21:01:04 -04:00
Greg Johnston
c5f0f4a6ed router alpha 3 2024-07-03 21:01:04 -04:00
Greg Johnston
64cdfbd2a8 Form component in 0.7 2024-07-03 21:01:04 -04:00
Greg Johnston
698d7f6f1b fix: don't break Routes SSR now that it uses Suspend 2024-07-03 21:01:04 -04:00
Alicia Garcia-Raboso
8789ed0882 Implement Default trait for signals with a parameter that also implements Default (#2662)
Co-authored-by: alicia.garcia.raboso <alicia.garcia.raboso@bbva.com>
2024-07-03 21:01:04 -04:00
Greg Johnston
fd5a6bebd3 feat: automatically replace Suspense blocks if they are still waiting to be flushed, without JS (replaces PartiallyBlocked) 2024-07-03 21:01:04 -04:00
Greg Johnston
0a081fafe3 blocking resources 2024-07-03 21:01:04 -04:00
Greg Johnston
f8711611b0 macro alpha 2 2024-07-03 21:01:04 -04:00
Greg Johnston
f508f30be9 router alpha 2 2024-07-03 21:01:04 -04:00
Luke Naylor
52261415cd Add missing 'form' attribute for <mo> (#2660)
This attribute seems to be missing in the attribute
table on Mozilla Docs, however does appear in the
compatibility table lower down.
This attribute is also frequently used by temml,
a common generator for mathml content.
2024-07-03 21:01:04 -04:00
Greg Johnston
65b270307d allow Outlet to be called multiple times 2024-07-03 21:01:04 -04:00
Greg Johnston
f2db13da10 fix: prevent panicking if conditionally rendering Outlet 2024-07-03 21:01:03 -04:00
Greg Johnston
1d90b11e81 remove log 2024-07-03 21:01:03 -04:00
Greg Johnston
f0048e4d45 fix: ensure correct ownership chain when passing views through Outlet 2024-07-03 21:01:03 -04:00
Greg Johnston
5c211cf1ce chore: clippy 2024-07-03 21:01:03 -04:00
Greg Johnston
aed4d49537 support MaybeSignal in view 2024-07-03 21:01:03 -04:00
Kajetan Welc
fa15a00288 fix: impl Copy for Callback (#2658) 2024-07-03 21:01:03 -04:00
Greg Johnston
40b383a0f7 fix: update imports and methods 2024-07-03 21:01:03 -04:00
Greg Johnston
cd289ef811 fix: trait import 2024-07-03 21:01:03 -04:00
Greg Johnston
76dcb823d8 impl From<T> for (Arc)Signal<T> 2024-07-03 21:01:03 -04:00
Greg Johnston
88c9742e6c Revert "lazy Future construction for AsyncDerived"
This reverts commit 9e84e1f57c.
2024-07-03 21:01:03 -04:00
Greg Johnston
edc86bbb43 fix: <option> struct generation 2024-07-03 21:01:03 -04:00
Greg Johnston
3fc0a6ff32 fix: <option> struct generation 2024-07-03 21:01:03 -04:00
Lucas Åström
3797a4167a Destructuring let (0.7) (#2655)
* Use `let()` syntax for bindings

This lets users use destructuring when binding more complex values, and we also get better IDE support.

* Update rstml
2024-07-03 21:01:03 -04:00
Greg Johnston
5ffcfc4079 fix: nonexistent feature 2024-07-03 21:01:03 -04:00
Greg Johnston
87f5d95bc3 fix: rebuilding AnyView 2024-07-03 21:01:03 -04:00
Greg Johnston
0597cda70a support wasm-only 2024-07-03 21:01:02 -04:00
Greg Johnston
a2f6ebf3a6 support wasm-only 2024-07-03 21:00:50 -04:00
Greg Johnston
0be5628038 support wasm-only 2024-07-03 21:00:50 -04:00
Greg Johnston
52434ada9d any_spawner tick version 2024-07-03 21:00:50 -04:00
Greg Johnston
ae0b09419d un-break circular dependency 2024-07-03 21:00:50 -04:00
Greg Johnston
2f93b6d147 chore: update test output 2024-07-03 21:00:50 -04:00
Greg Johnston
4cf7dd0e0c chore: unused import 2024-07-03 21:00:50 -04:00
Greg Johnston
2402688493 restore fallback for compressed version 2024-07-03 21:00:50 -04:00
Greg Johnston
36726e9dfc docs: working on memo docs 2024-07-03 21:00:50 -04:00
Greg Johnston
ab886a1258 0.7.0-alpha 2024-07-03 21:00:50 -04:00
Greg Johnston
2d51311362 feat: support reactive and asynchronous ProtectedRoute conditions 2024-07-03 21:00:50 -04:00
Greg Johnston
9262b512e0 fix: correct For behavior when mounting with siblings, and when clearing 2024-07-03 21:00:50 -04:00
Ben Wishovich
28b38329a3 Make get_configuration sync (#2647)
* Made get_configuraiton sync

* Update examples
2024-07-03 21:00:50 -04:00
Greg Johnston
0567aeb5b8 examples: update directives tests 2024-07-03 21:00:50 -04:00
Greg Johnston
f9cd5ca159 chore: cargo fmt 2024-07-03 21:00:50 -04:00
Greg Johnston
9b72efb9c4 make RemoveEventHandler a concrete type 2024-07-03 21:00:50 -04:00
Greg Johnston
61ee451e19 feat: add ElementExt to give access to the same view APIs at runtime that we do at compile time 2024-07-03 21:00:50 -04:00
Greg Johnston
50d4eb2f50 feat: add ElementExt to give access to the same view APIs at runtime that we do at compile time 2024-07-03 21:00:50 -04:00
Greg Johnston
72aa8a6598 chore: cargo fmt 2024-07-03 21:00:50 -04:00
Greg Johnston
06fe32e90b chore: cargo fmt 2024-07-03 21:00:50 -04:00
Greg Johnston
75184a4638 docs: warn on unused RenderEffect 2024-07-03 21:00:50 -04:00
Greg Johnston
29e5396912 chore: missing Debug implementations 2024-07-03 21:00:50 -04:00
Greg Johnston
b8df56afa1 chore: suppress unnecessary .into() warning 2024-07-03 21:00:50 -04:00
Greg Johnston
7d4b7d2ce8 cargo fmt 2024-07-03 21:00:49 -04:00
Greg Johnston
d132c8e338 updated directives example 2024-07-03 21:00:49 -04:00
brofrain
6bf6fb6923 fix: update HtmlViewState & BodyViewState as well 2024-07-03 21:00:49 -04:00
Kajetan Welc
1bda454441 fix: do not accidentally mount things before meta tags in the <head> when updating the DOM v2 2024-07-03 21:00:49 -04:00
Greg Johnston
9aca55f14f remove log 2024-07-03 21:00:49 -04:00
Greg Johnston
f1a007bf15 preliminary work on directives (not useful yet until we have an ElementExt that allows you to do things declaratively from an Element 2024-07-03 21:00:49 -04:00
Greg Johnston
d83c2acded omit () entirely if it is the only child of an HTML element 2024-07-03 21:00:49 -04:00
Greg Johnston
f984197070 fix: don't override a text node's 'next sibling after text' position if it's in Either, now that they don't have separate marker nodes 2024-07-03 21:00:49 -04:00
Greg Johnston
c8491cc709 fix: do not accidentally mount things before meta tags in the <head> when updating the DOM 2024-07-03 21:00:49 -04:00
Greg Johnston
222aed6d11 fix docs for hydrate_islands 2024-07-03 21:00:49 -04:00
Greg Johnston
7d37c60bae hackernews islands example 2024-07-03 21:00:49 -04:00
Greg Johnston
fea3fdb387 update static file serving in Axum examples 2024-07-03 21:00:49 -04:00
Greg Johnston
d57f484502 fix islands ci setup 2024-07-03 21:00:49 -04:00
Greg Johnston
62bd6a244e fix: remove extra comment at end of Suspense now that Either no longer requires it 2024-07-03 21:00:49 -04:00
Greg Johnston
fe147f0c4d feat: provide static file handling/fallback directly in integration 2024-07-03 21:00:49 -04:00
Greg Johnston
3e0d4e4876 islands example 2024-07-03 21:00:49 -04:00
Greg Johnston
9eda75525a remove unused tests and dependencies 2024-07-03 21:00:49 -04:00
Greg Johnston
de1864867b fix: () in templates 2024-07-03 21:00:49 -04:00
Greg Johnston
116d8f072b chore: clippy 2024-07-03 21:00:49 -04:00
Greg Johnston
6097e3cc57 fix js-framework-benchmark for stable 2024-07-03 21:00:49 -04:00
Greg Johnston
59c18d945a update hackernews_axum to 0.7 2024-07-03 21:00:49 -04:00
Greg Johnston
0b750e17b5 fix ErrorBoundary starting in error state in CSR 2024-07-03 21:00:49 -04:00
Greg Johnston
dd6f1f4660 fix example tests 2024-07-03 21:00:49 -04:00
Greg Johnston
de07282b0a remove unnecessary logs 2024-07-03 21:00:49 -04:00
Greg Johnston
f6046fa8a7 update islands example 2024-07-03 21:00:49 -04:00
Greg Johnston
d945e27d72 chore: unused hooks 2024-07-03 21:00:49 -04:00
Greg Johnston
74a2486944 regression test for 7094dee150 2024-07-03 21:00:49 -04:00
Greg Johnston
142a7d64f9 fix: signals mark subscribers dirty, but don't say they're always dirty if they haven't changed 2024-07-03 21:00:49 -04:00
Greg Johnston
f87ce51ff9 make Routes fallback run lazily 2024-07-03 21:00:49 -04:00
Greg Johnston
5f979635a3 make ErrorBoundary fallback run lazily 2024-07-03 21:00:49 -04:00
Greg Johnston
f648bc2e85 examples: errors_axum 2024-07-03 21:00:49 -04:00
Greg Johnston
bcc75d46c0 chore(ci): fix examples 2024-07-03 21:00:49 -04:00
Greg Johnston
2e0d70765b enable reactive-graph hydration when hydration is enabled 2024-07-03 21:00:49 -04:00
Greg Johnston
24f6b6afb5 allow conversion directly from Arc signal types to MaybeSignal 2024-07-03 21:00:49 -04:00
Greg Johnston
ca0b2e6b4d expose Owner::shared_context() 2024-07-03 21:00:49 -04:00
Greg Johnston
4eb8c2a5b1 0.7 Provider component 2024-07-03 21:00:49 -04:00
Greg Johnston
a18de6eb4f fix tests 2024-07-03 21:00:49 -04:00
Greg Johnston
bf8844f040 fix reactive styles 2024-07-03 21:00:49 -04:00
Greg Johnston
da9553cc7a fix: correctly rebuild reactive attributes to avoid stale signals 2024-07-03 21:00:49 -04:00
Greg Johnston
5e3d4ac37a type-erase RenderEffeect functions for binary size improvements 2024-07-03 21:00:49 -04:00
luoxiaozero
4366bc156d feat: Attr exposes PhantomData field (#2641) 2024-07-03 21:00:49 -04:00
Greg Johnston
23bbba0ef8 chore(ci): remove warnings in tests 2024-07-03 21:00:49 -04:00
Greg Johnston
35f86535d0 feat: 0.7 query signals 2024-07-03 21:00:49 -04:00
Greg Johnston
1ca8e082c3 chore(ci): add Makefiles for smaller packages 2024-07-03 21:00:49 -04:00
Greg Johnston
b20542e22d chore(ci): only run semver checks if not labeled 'breaking' 2024-07-03 21:00:49 -04:00
Greg Johnston
abfff8f98d chore: clippy 2024-07-03 21:00:49 -04:00
Greg Johnston
46c89a5862 rename from new_serde to new 2024-07-03 21:00:49 -04:00
Greg Johnston
32518e46df remove most remaining marker/placeholder elements 2024-07-03 21:00:49 -04:00
Greg Johnston
639f801c79 add trait impls and encodings for SharedValue 2024-07-03 21:00:49 -04:00
Greg Johnston
924358d829 default to SerdeJson encoding for resources, use new_str() for Str encoding 2024-07-03 21:00:49 -04:00
Greg Johnston
91e7266103 feat: synchronous serialized values with SharedValue 2024-07-03 21:00:49 -04:00
Greg Johnston
347611ebec whenever we create a new root Owner, set it as the current owner, which will make it the default owner (e.g., during SSR) instead of None 2024-07-03 21:00:49 -04:00
Greg Johnston
424c3a7209 fix: can't memoize JS properties, because they can be set between signal updates by user input 2024-07-03 21:00:49 -04:00
Greg Johnston
4c078e8eb0 fix: correctly escape style and class attributes 2024-07-03 21:00:49 -04:00
Greg Johnston
0ed1420076 chore: clippy 2024-07-03 21:00:49 -04:00
Greg Johnston
5221675946 don't require spawn_local for actios 2024-07-03 21:00:49 -04:00
Greg Johnston
50d4d08b9b catch resource reads inside Signal during Suspense 2024-07-03 21:00:49 -04:00
Greg Johnston
b51215af8f fix CSS file names 2024-07-03 21:00:49 -04:00
Greg Johnston
e37f3ee756 update todo app csr 2024-07-03 21:00:49 -04:00
Greg Johnston
9c949f6d9a fix: correctly escape text nodes, except in script/style tags 2024-07-03 21:00:49 -04:00
Greg Johnston
59d3a9853f chore: clean up examples for CI 2024-07-03 21:00:49 -04:00
Greg Johnston
4df0a6c7e9 fix: provide matched route via context when rebuilding (so <A> works) 2024-07-03 21:00:49 -04:00
Greg Johnston
4560f5e31c unused 2024-07-03 21:00:49 -04:00
Greg Johnston
0887ada9a9 refactor insert_before_this to find parent lazily, and use it for rebuilding reactive components by replacing their whole contents 2024-07-03 21:00:49 -04:00
Greg Johnston
cddbd24eff make sure SendWrapper supports Futures 2024-07-03 21:00:49 -04:00
Greg Johnston
19db6d9388 move several complex examples into projects 2024-07-03 21:00:49 -04:00
Greg Johnston
466cc39411 update js-framework-benchmark example 2024-07-03 21:00:48 -04:00
Greg Johnston
5b39f7d27f fix test text 2024-07-03 21:00:48 -04:00
Greg Johnston
ab18371c32 fix: custom Stream implementation for streaming resource data that supports nested data/multiple polled values, rather than taking it all at once at the beginning 2024-07-03 21:00:48 -04:00
Greg Johnston
9d818d7796 suspense_tests: actually wait for other resource in nested case 2024-07-03 21:00:48 -04:00
Greg Johnston
5316066612 can save a lookup here 2024-07-03 21:00:48 -04:00
Greg Johnston
10af7b81cc remove unused workspace member 2024-07-03 21:00:48 -04:00
Greg Johnston
51b261d202 update workflows 2024-07-03 21:00:48 -04:00
Greg Johnston
b5e0c2b150 remove unused leptos_reactive integration 2024-07-03 21:00:48 -04:00
Greg Johnston
e322fb1b33 remove old router files 2024-07-03 21:00:48 -04:00
Greg Johnston
24c7fd954e fix Cargo.toml after merge 2024-07-03 21:00:42 -04:00
Greg Johnston
a303814d84 re-enable all routes 2024-07-03 21:00:42 -04:00
Greg Johnston
75d1b14e59 chore: clean up warnings and logging 2024-07-03 21:00:42 -04:00
Greg Johnston
3a7dbafda3 reverted Fn()/FnMut() change 2024-07-03 21:00:41 -04:00
Greg Johnston
3271bcda4e Revert "fix: constrain reactive rendering to Fn(), because using dry_resolve() for Suspense requires idempotent render functions so that they can be called once (to register resources) and called a second time to resolve"
This reverts commit 7ec5c77ba3e8f45bae04a7661a56741f95125adb.
2024-07-03 21:00:41 -04:00
Greg Johnston
36e4eca909 progress on updating suspense tests 2024-07-03 21:00:41 -04:00
Greg Johnston
23d1579d23 add server redirects 2024-07-03 21:00:41 -04:00
Greg Johnston
474fba0ab5 fix: relative path resolution 2024-07-03 21:00:41 -04:00
Greg Johnston
8725a329d2 simplifying todo examples 2024-07-03 21:00:41 -04:00
Greg Johnston
ffa0883b48 initial work updating suspense tests 2024-07-03 21:00:41 -04:00
Greg Johnston
b6ed91b81b clarify hydrate/csr warning 2024-07-03 21:00:41 -04:00
Greg Johnston
fdb298f254 export actions in prelude 2024-07-03 21:00:41 -04:00
Greg Johnston
93d0b3fa26 remove Into<_> by default for setting signals, because it interferes with type inference 2024-07-03 21:00:41 -04:00
Greg Johnston
ab57926d8f add support for unsync actions 2024-07-03 21:00:41 -04:00
Greg Johnston
6eeb22f1f0 simplifying and updating server fns example 2024-07-03 21:00:41 -04:00
Greg Johnston
648d96f316 change name to shell 2024-07-03 21:00:41 -04:00
Greg Johnston
a60e5235d7 update control flow components to new Fn() constraint 2024-07-03 21:00:41 -04:00
Greg Johnston
76deab365e fix: constrain reactive rendering to Fn(), because using dry_resolve() for Suspense requires idempotent render functions so that they can be called once (to register resources) and called a second time to resolve 2024-07-03 21:00:41 -04:00
Greg Johnston
b4948745c7 add Debug impl 2024-07-03 21:00:41 -04:00
Greg Johnston
059df60373 fix: actually concatenate nested routes during route generation 2024-07-03 21:00:41 -04:00
Greg Johnston
5f464298b3 reorganize Outlet export 2024-07-03 21:00:41 -04:00
Greg Johnston
9c304d8d75 examples: porting to 0.7 and cleaning up 2024-07-03 21:00:41 -04:00
Greg Johnston
ca909becc1 fix merge 2024-07-03 21:00:41 -04:00
Rakshith Ravi
f231503baf Update import statements in examples (#2625) 2024-07-03 21:00:41 -04:00
Greg Johnston
d1d6238d9b examples: use application 404 page 2024-07-03 21:00:41 -04:00
Greg Johnston
0543b05738 chore: cargo fmt 2024-07-03 21:00:41 -04:00
Greg Johnston
1a09f1f54a chore: cargo fmt 2024-07-03 21:00:41 -04:00
Greg Johnston
3201cacca1 add warnings if correct features not set for browser 2024-07-03 21:00:41 -04:00
Greg Johnston
454d179cf9 use csr feature so that reactivity runs 2024-07-03 21:00:41 -04:00
Greg Johnston
8c73455eeb chore: clippy 2024-07-03 21:00:41 -04:00
Greg Johnston
e8c5a69412 examples: fix input type so tests work, and update text to make the purpose clearer 2024-07-03 21:00:41 -04:00
Greg Johnston
6088da7342 remove leptos_reactive (moved into reactive_graph and leptos_server) 2024-07-03 21:00:41 -04:00
Greg Johnston
ea76a0f74e fix: Clone for ArcResource and default to SerdeJson for Resource 2024-07-03 21:00:31 -04:00
Greg Johnston
5e43d37601 chore: clippy 2024-07-03 21:00:31 -04:00
Greg Johnston
d70ef1178e fix: don't dispose of parent owners before Suspense children have been rendered 2024-07-03 21:00:31 -04:00
Greg Johnston
30556af5f6 warn if trying to use meta on server side without context 2024-07-03 21:00:31 -04:00
Greg Johnston
9255ad1840 only run RenderEffects when effects are enabled 2024-07-03 21:00:31 -04:00
Greg Johnston
e53d139fc1 add set_pending to <Transition/> 2024-07-03 21:00:31 -04:00
Greg Johnston
07f1eed36a simplify FlatRoutes logic by using existing OwnedView infrastructure 2024-07-03 21:00:31 -04:00
Greg Johnston
02587781ac reexport tick() for testing 2024-07-03 21:00:31 -04:00
Greg Johnston
d32d6d8fe8 fix: ensure that leptos_meta and leptos_router are in SSR mode if using one of the server integrations 2024-07-03 21:00:31 -04:00
Greg Johnston
c10a3d251d examples: update hackernews for SSR support 2024-07-03 21:00:31 -04:00
Greg Johnston
8550b54be6 fix: correctly notify multiple subscribers to same AsyncDerived 2024-07-03 21:00:31 -04:00
Greg Johnston
becc1075ab fix: Routes SSR 2024-07-03 21:00:31 -04:00
Greg Johnston
6b5467d451 fix: correct owner for HTML rendering in FlatRoutes 2024-07-03 21:00:31 -04:00
Greg Johnston
3375dbdcbc testing: provide tick() that can be called anywhere in tests 2024-07-03 21:00:30 -04:00
Greg Johnston
4842ef28e0 testing: provide tick() that can be called anywhere in tests 2024-07-03 21:00:30 -04:00
Greg Johnston
8945fc8724 fix portal tests 2024-07-03 21:00:30 -04:00
Greg Johnston
bee37de5c9 fix cleanups in render effects 2024-07-03 21:00:30 -04:00
Greg Johnston
b192136a4b chore: clippy 2024-07-03 21:00:30 -04:00
Greg Johnston
907b9df06e update counters_isomorphic 2024-07-03 21:00:30 -04:00
Greg Johnston
b6a62ab4d9 reexport spawn and spawn_local 2024-07-03 21:00:30 -04:00
Greg Johnston
e36fce4351 ReadSignal from stream 2024-07-03 21:00:30 -04:00
Greg Johnston
76ec0b78c2 refactor to allow rendering Resource directly in view 2024-07-03 21:00:30 -04:00
Greg Johnston
bec33e255d removed AnimatedShow example (duplicates the component docs) 2024-07-03 21:00:30 -04:00
Greg Johnston
cdcfcf6a0e properly serialize errors 2024-07-03 21:00:30 -04:00
Greg Johnston
422de7c8af chore: clear warning 2024-07-03 21:00:30 -04:00
Greg Johnston
662bd4fd78 pick up on server action error in both server and client 2024-07-03 21:00:30 -04:00
Greg Johnston
a4b4e2b3ac fmt and chores in examples 2024-07-03 21:00:30 -04:00
Greg Johnston
4e84a78307 fix: serialize an empty string into HTML so it still works as a text node 2024-07-03 21:00:30 -04:00
Greg Johnston
012535a2d7 fix: make router fallback lazy 2024-07-03 21:00:30 -04:00
Greg Johnston
43c953c4b9 add expect_context 2024-07-03 21:00:30 -04:00
Greg Johnston
143973960c fix attr:class when spreading onto a component 2024-07-03 21:00:30 -04:00
Greg Johnston
8a0f43affe add ServerAction error handling for any error type (closes #2325) 2024-07-03 21:00:30 -04:00
Greg Johnston
b55a19a533 remove unused import 2024-07-03 21:00:30 -04:00
Greg Johnston
21cdadf461 clean up example 2024-07-03 21:00:30 -04:00
Greg Johnston
d95e59a685 add CollectView 2024-07-03 21:00:30 -04:00
Greg Johnston
28468a0bae Actix todo_app_sqlite 2024-07-03 21:00:30 -04:00
Greg Johnston
b183bb981c fix: don't drop Owner in FlatRoutes until route has been rendered (thanks @benwis) 2024-07-03 21:00:30 -04:00
Greg Johnston
ee5e4bc5c0 fix: make sure all resource reads are registered 2024-07-03 21:00:30 -04:00
Greg Johnston
581d5d0469 chore: clippy and unused dependencies in integrations 2024-07-03 21:00:30 -04:00
Greg Johnston
d2bb6d5062 add some tracing and debug info to HTML elements 2024-07-03 21:00:30 -04:00
Greg Johnston
8a419705d8 refactor integrations and add Actix integration 2024-07-03 21:00:30 -04:00
Greg Johnston
0a7b025534 feat: 0.7 nonce support 2024-07-03 20:59:48 -04:00
Greg Johnston
060338e643 ResponseOptions support 2024-07-03 20:59:48 -04:00
Greg Johnston
8dee986164 fix counters tests 2024-07-03 20:59:48 -04:00
Greg Johnston
05166d2cb7 allow .children() on HTML elements 2024-07-03 20:59:48 -04:00
Greg Johnston
40d8cfb04b remove async demo 2024-07-03 20:59:48 -04:00
Greg Johnston
53fbe76761 fix: writing to lock that has a read 2024-07-03 20:59:48 -04:00
Greg Johnston
c3d8d1e828 allow untracking on write guards to support maybe_update 2024-07-03 20:59:48 -04:00
Greg Johnston
4636192fa3 feat: allow .write() on all writeable signals 2024-07-03 20:59:48 -04:00
Greg Johnston
02705dec31 feat: add .by_ref() to create a Future from an AsyncDerived (etc.) that takes a reference, rather than cloning 2024-07-03 20:59:48 -04:00
Greg Johnston
dc09faf7eb use impl trait in props 2024-07-03 20:59:48 -04:00
Greg Johnston
402eb95221 update wasm-bindgen testing approaches 2024-07-03 20:59:48 -04:00
Greg Johnston
d92b430fbf chore: clearing warnings in examples 2024-07-03 20:59:48 -04:00
Greg Johnston
030d06521e additional warnings 2024-07-03 20:59:48 -04:00
Greg Johnston
c8f2ef375b cargo fmt 2024-07-03 20:59:48 -04:00
Greg Johnston
8ed8b5605b chore: clear up... a few warnings 2024-07-03 20:59:48 -04:00
Greg Johnston
7c526cfa52 update sledgehammer integration 2024-07-03 20:59:48 -04:00
Greg Johnston
5e20b8fcca remove signal function setter Send-only implementation (dead code) 2024-07-03 20:59:48 -04:00
Greg Johnston
6b3ccdf2f4 remove signal function call Read implementations (dead code) 2024-07-03 20:59:48 -04:00
Greg Johnston
d7a321aa69 remove leptos_reactive dependency 2024-07-03 20:59:48 -04:00
Greg Johnston
abfa2d02fe oco merge issues 2024-07-03 20:59:48 -04:00
Greg Johnston
7b8edda5ae feat: return an async guard from .await rather than cloning the value every time 2024-07-03 20:59:48 -04:00
Greg Johnston
967bf2fdfc fix return type in async tests 2024-07-03 20:59:48 -04:00
Greg Johnston
274ac2ba34 fix tests that run effects 2024-07-03 20:59:48 -04:00
Greg Johnston
3b10fb54c6 feat: return Option from AsyncDerived.get() instead of AsyncState 2024-07-03 20:59:48 -04:00
Greg Johnston
1274c0890e example: restore ErrorBoundary 2024-07-03 20:59:48 -04:00
Greg Johnston
b05bbb5499 docs for Owner and context 2024-07-03 20:59:48 -04:00
Greg Johnston
77bce6d440 fix tests 2024-07-03 20:59:48 -04:00
Greg Johnston
4f01c4f61e poll AsyncDerived synchronously so that it has the correct value during hydration if it reads from a resource 2024-07-03 20:59:48 -04:00
Greg Johnston
0812f8d850 correct dirty-checking on AsyncDerived 2024-07-03 20:59:48 -04:00
Greg Johnston
059968ca58 fix regular suspense if nothing was read synchronously 2024-07-03 20:59:48 -04:00
Greg Johnston
b67229ca78 missing dry_resolve on Static 2024-07-03 20:59:48 -04:00
Greg Johnston
d59f234d88 feat: support *either* .await or reactive reads inside Suspense 2024-07-03 20:59:48 -04:00
Greg Johnston
1e2815b0a0 feat: 0.7 slots 2024-07-03 20:59:48 -04:00
Greg Johnston
4ae8a6d54c feat: enhanced spreading syntax 2024-07-03 20:59:48 -04:00
Greg Johnston
3c3e97e8b9 fix external navigations 2024-07-03 20:59:48 -04:00
Greg Johnston
83be4167d8 make WindowListenerHandle Send + Sync so it can be remove via on_cleanup 2024-07-03 20:59:48 -04:00
Greg Johnston
691b44baa1 restore ssr/hydration for Routes 2024-07-03 20:59:48 -04:00
Greg Johnston
c58f05e129 get nested Routes working again 2024-07-03 20:59:48 -04:00
Greg Johnston
ddfa4dd76f default to Params::get() giving an owned value (which you want in a derived signal), but use reference in the macro 2024-07-03 20:59:48 -04:00
Greg Johnston
1dd8175aa4 add proper dirty checking on AsyncDerived so it can read from memos properly 2024-07-03 20:59:48 -04:00
Greg Johnston
18c5e4c804 make NavigateOptions pub 2024-07-03 20:59:48 -04:00
Greg Johnston
65a30910b1 feat: owning memo 2024-07-03 20:59:48 -04:00
Greg Johnston
f0b6995f50 remove warnings in tests and only run if effects are enabled 2024-07-03 20:59:48 -04:00
Greg Johnston
ffce98f928 fix: prevent memos that have changed from re-triggering the running effect, by setting the Observer during .update_if_necessary() 2024-07-03 20:59:48 -04:00
Greg Johnston
3b71be89ca feat: add Popover API 2024-07-03 20:59:47 -04:00
Greg Johnston
941ea12420 fix Script children 2024-07-03 20:59:47 -04:00
Greg Johnston
e44504f8ba fix hydration of Suspend by including the missing placeholder it expects during hydration 2024-07-03 20:59:47 -04:00
Greg Johnston
9924f8fef7 add missing marker comments for Result 2024-07-03 20:59:47 -04:00
Greg Johnston
cb2f66a955 include marker comments in html len 2024-07-03 20:59:47 -04:00
Greg Johnston
c43379cb68 unused owner 2024-07-03 20:59:47 -04:00
Greg Johnston
71e34935e5 impl From/Into for Signal/ArcSignal 2024-07-03 20:59:47 -04:00
Greg Johnston
ab9a1d8b0a add ArcSignal::derive() 2024-07-03 20:59:47 -04:00
Greg Johnston
05db18e2b8 routing progress indicator 2024-07-03 20:59:47 -04:00
Greg Johnston
b530b46064 missing min attribute 2024-07-03 20:59:47 -04:00
Greg Johnston
ebeec2c3bd clean up 2024-07-03 20:59:47 -04:00
Greg Johnston
a4304876bf remove log 2024-07-03 20:59:47 -04:00
Greg Johnston
38ca7137e0 relax trait bounds on reactive types where possible 2024-07-03 20:59:47 -04:00
Greg Johnston
9b6108dae7 add SignalSetter 2024-07-03 20:59:47 -04:00
Greg Johnston
6092971f7c use transition between navigations 2024-07-03 20:59:47 -04:00
Greg Johnston
80e5274a88 add async transitions that wait for any AsyncDerived created/triggered under them before resolving 2024-07-03 20:59:47 -04:00
Greg Johnston
0d705c3318 resolve() on OwnedView 2024-07-03 20:59:47 -04:00
Greg Johnston
1c3b157293 restore hydration feature for some of its feature-gating benefits for Resource deserialization 2024-07-03 20:59:47 -04:00
Greg Johnston
ff950daa6e cargo fmt 2024-07-03 20:59:47 -04:00
Greg Johnston
af1cb9fca1 create separate URL/params signals for each route, to prevent updating them and running side effects while navigating away 2024-07-03 20:59:47 -04:00
Greg Johnston
d82e43f526 fmt 2024-07-03 20:59:47 -04:00
Greg Johnston
b95b8ea43d distinguish between dirty and check in effects, so that memos and signals both work correctly 2024-07-03 20:59:47 -04:00
Greg Johnston
077dd85964 remove unused feature 2024-07-03 20:59:47 -04:00
Greg Johnston
f24af45e19 updated future impls 2024-07-03 20:59:47 -04:00
Greg Johnston
ce2390d6d0 check whether ArcAsyncDerived actually needs to run when marked check 2024-07-03 20:59:47 -04:00
Greg Johnston
a82a72400d add ancestry debugging for owners 2024-07-03 20:59:47 -04:00
Greg Johnston
e37ca85bdd lazy Future construction for AsyncDerived 2024-07-03 20:59:47 -04:00
Greg Johnston
de138b397c support Resource in CSR for backward-compat 2024-07-03 20:59:47 -04:00
Greg Johnston
28ae64bc06 allow let: syntax to work 2024-07-03 20:59:47 -04:00
Greg Johnston
95b4cd3841 scope Suspense/Transition correctly within ownership tree 2024-07-03 20:59:47 -04:00
Greg Johnston
6f7522381c upgrading hackernews example 2024-07-03 20:59:47 -04:00
Greg Johnston
d6997bddc5 immediately commit URL signal updates 2024-07-03 20:59:47 -04:00
Greg Johnston
7de95ff99f reexport A from router::components 2024-07-03 20:59:47 -04:00
Greg Johnston
fa28d56928 add IntoAny to tachys prelude 2024-07-03 20:59:47 -04:00
Greg Johnston
3f2cdf77a3 finish support for innerHTML 2024-07-03 20:59:47 -04:00
Greg Johnston
f8500f416f feat: iterating over items in children with ChildrenFragment, ChildrenFragmentFn, ChildrenFragmentMut 2024-07-03 20:59:47 -04:00
Greg Johnston
2f81a81c06 only warn about non-reactive accesses if effects are enabled 2024-07-03 20:59:47 -04:00
Greg Johnston
ffb8e8bea3 only run effects on client 2024-07-03 20:59:47 -04:00
Greg Johnston
6f158a8974 resolve() implementation for AnyView 2024-07-03 20:59:47 -04:00
Greg Johnston
2cc1ec9b3c fix deadlock on nested Signals 2024-07-03 20:59:47 -04:00
Greg Johnston
dbed68e5ba fix FlatRouter SSR/hydration after lazy routes 2024-07-03 20:59:47 -04:00
Greg Johnston
1a1d2e1c07 feat: nested islands with context for 0.7 2024-07-03 20:59:47 -04:00
Greg Johnston
b3871dea75 fix: correct Send + Sync bounds for children 2024-07-03 20:59:47 -04:00
Greg Johnston
eba4711fa7 feat: minimal island support in 0.7 2024-07-03 20:59:47 -04:00
Greg Johnston
e8273348db docs: full docs and doctests for Action/MultiAction 2024-07-03 20:59:47 -04:00
Greg Johnston
af051aa72a remove support for rendering guards directly, as they are !Send and holding onto them in State is also a bad idea 2024-07-03 20:59:47 -04:00
Greg Johnston
e9ec7412fa support nightly static values for style:key="value" 2024-07-03 20:59:47 -04:00
Greg Johnston
e5757c56b7 revert to using .get() for function calls 2024-07-03 20:59:47 -04:00
Greg Johnston
031d78cd5a fix: only rerun effects if they have dirty ancestors (or it's the first run) 2024-07-03 20:59:47 -04:00
Greg Johnston
a0ba356cc1 start working on porting over docs and tests and 0.7... 2024-07-03 20:59:47 -04:00
Greg Johnston
adf6a8e368 fix meta issue with attributes 2024-07-03 20:59:47 -04:00
Greg Johnston
6eed4e8253 fix tracing issue 2024-07-03 20:59:47 -04:00
Greg Johnston
88d311a0c5 chore: get tests in a working state 2024-07-03 20:59:47 -04:00
Greg Johnston
0d7db85e9e fix: make Selector Send/Sync 2024-07-03 20:59:47 -04:00
Greg Johnston
2f77fb1892 docs: runtime warning if you use .track() outside a tracking context 2024-07-03 20:59:47 -04:00
Greg Johnston
a66662397c preliminary tracing for tachys 2024-07-03 20:59:47 -04:00
Greg Johnston
105e2c7686 chore: warnings 2024-07-03 20:59:47 -04:00
Greg Johnston
324bebcf50 examples: router in 0.7 2024-07-03 20:59:47 -04:00
Greg Johnston
e553ad3a68 fix: passing context through router 2024-07-03 20:59:47 -04:00
Greg Johnston
b7ecac308f chore: fix warnings about variable case 2024-07-03 20:59:47 -04:00
Greg Johnston
79ebd416a3 examples: timer in 0.7 2024-07-03 20:59:47 -04:00
Greg Johnston
4e2b9b1878 feat: Portals in 0.7 2024-07-03 20:59:47 -04:00
Greg Johnston
c1a6f2f479 allow either eager or lazy routes 2024-07-03 20:59:47 -04:00
Greg Johnston
fb855cb07f update StoredValue API in callbacks 2024-07-03 20:59:47 -04:00
Greg Johnston
1850231c92 chore: clippy warnings 2024-07-03 20:59:47 -04:00
Greg Johnston
55c7b84a00 smooth out StoredValue APIs 2024-07-03 20:59:47 -04:00
Greg Johnston
4e2646513f MaybeSignal and MaybeProp 2024-07-03 20:59:47 -04:00
Marc-Stefan Cassola
9a75edd34c added a few old deprecated functions to help users port (#2580) 2024-07-03 20:59:47 -04:00
Greg Johnston
81afa8d0c8 implement With(Untracked) for Signal 2024-07-03 20:59:47 -04:00
Greg Johnston
d3919d3568 don't over-rerender nested router 2024-07-03 20:59:47 -04:00
Greg Johnston
5b23fddd02 initial async routing work (to support bundle splitting) 2024-07-03 20:59:47 -04:00
Greg Johnston
8de1f86db3 noop attribute 'spreading' for routers 2024-07-03 20:59:46 -04:00
Greg Johnston
97901bfedb support arbitrary attributes on components in view 2024-07-03 20:59:46 -04:00
Greg Johnston
f6eb7fa8b6 full attribute spreading 2024-07-03 20:59:46 -04:00
Greg Johnston
36ccc3d834 update counter_without_macros imports 2024-07-03 20:59:46 -04:00
Greg Johnston
82ba7195c8 stashing 2024-07-03 20:59:46 -04:00
Greg Johnston
5f7cdb1091 give a route to upgrade any attribute into a cloneable one 2024-07-03 20:59:46 -04:00
Greg Johnston
e3a0f59f4a work on attribute spreading 2024-07-03 20:59:46 -04:00
Greg Johnston
b77e127588 disable AddAnyAttr again now that I remember why it was broken 2024-07-03 20:59:46 -04:00
Greg Johnston
c0d51ede3d reenable AnyAttr 2024-07-03 20:59:46 -04:00
Greg Johnston
49b2b5047e reorganizing exports and updating examples 2024-07-03 20:59:46 -04:00
Greg Johnston
058d357e59 prep for preview release 2024-07-03 20:59:46 -04:00
Greg Johnston
6b0a47ae8a fix reorganized exports 2024-07-03 20:59:46 -04:00
Greg Johnston
49de4bc671 prep for preview release 2024-07-03 20:59:46 -04:00
Greg Johnston
2033ce251e prep for preview release 2024-07-03 20:59:46 -04:00
Greg Johnston
c37a4e02a6 prep for preview release 2024-07-03 20:59:46 -04:00
Greg Johnston
39357bf9a7 module restructuring for 0.7 2024-07-03 20:59:46 -04:00
Greg Johnston
5ead1ec3df let ErrorBoundary own the fallback 2024-07-03 20:59:46 -04:00
Greg Johnston
1826586d03 make Suspend a transparent wrapper 2024-07-03 20:59:46 -04:00
Greg Johnston
ad29695eac provide params properly in FlatRouter 2024-07-03 20:59:46 -04:00
Greg Johnston
76a12a9bf4 clear some warnings 2024-07-03 20:59:46 -04:00
Greg Johnston
8ce815f210 rename TupleBuilder to NextTuple and prep for release 2024-07-03 20:59:46 -04:00
Greg Johnston
e9a05f095e prep for preview release 2024-07-03 20:59:46 -04:00
Greg Johnston
c114df5ebb prep for preview release 2024-07-03 20:59:46 -04:00
Greg Johnston
17977d8fb7 prep for preview release 2024-07-03 20:59:46 -04:00
Greg Johnston
455cbc07a6 prep for preview release 2024-07-03 20:59:46 -04:00
Greg Johnston
272b6da00c rename any_error 2024-07-03 20:59:46 -04:00
Greg Johnston
09cc5df975 prep for preview release 2024-07-03 20:59:46 -04:00
Greg Johnston
106aaaf412 remove twiggy file 2024-07-03 20:59:46 -04:00
Greg Johnston
521172ef5f move router crates 2024-07-03 20:59:46 -04:00
Greg Johnston
51d0ed4735 comparison demo 2024-07-03 20:59:33 -04:00
Greg Johnston
05bd7a6276 SSR optimizations for binary size, and flat router 2024-07-03 20:59:33 -04:00
Greg Johnston
7f911ac26a work on Axum integration and on error boundaries 2024-07-03 20:57:51 -04:00
Greg Johnston
3d0810bb5e stash 2024-07-03 20:57:51 -04:00
Greg Johnston
49f1fce64d feat: add <A> 2024-07-03 20:57:51 -04:00
Greg Johnston
86377d208d add use_navigate and Redirect 2024-07-03 20:57:51 -04:00
Greg Johnston
d07bcb1bb4 preliminary use_navigate work 2024-07-03 20:57:51 -04:00
Greg Johnston
9389195e6b fix fallback => match update 2024-07-03 20:57:51 -04:00
Greg Johnston
b98ba8e2fa add more hooks and primitives to router 2024-07-03 20:57:51 -04:00
Greg Johnston
005766495c nested route CSR working 2024-07-03 20:57:51 -04:00
Greg Johnston
1cb9a82239 continuing on nested routes 2024-07-03 20:57:51 -04:00
Greg Johnston
fcdc5f8b9e continuing on nested routes 2024-07-03 20:57:51 -04:00
Greg Johnston
aa624fccc7 working on reconfiguring nested routing 2024-07-03 20:57:51 -04:00
Greg Johnston
d742179c2f make placeholder-finding code consistent across container types 2024-07-03 20:57:51 -04:00
Greg Johnston
470f7f15fe remove logs 2024-07-03 20:57:51 -04:00
Greg Johnston
4f45b1032e fix Transition hydration 2024-07-03 20:57:51 -04:00
Greg Johnston
5f3623574d remove TryCatch/fallible rendering in favor of better ErrorBoundary model 2024-07-03 20:57:51 -04:00
Greg Johnston
fbde9eaefa finish todo_app_sqlite_axum 2024-07-03 20:57:51 -04:00
Greg Johnston
83067627db fix Vec hydration 2024-07-03 20:57:51 -04:00
Greg Johnston
53d51e03ff add MultiActionForm 2024-07-03 20:57:51 -04:00
Greg Johnston
0205d715c2 add MultiAction/ServerMultiAction 2024-07-03 20:57:51 -04:00
Greg Johnston
05b7e83778 stash 2024-07-03 20:57:51 -04:00
Greg Johnston
4d0339ee05 ErrorBoundary SSR and serialization of errors to support hydration 2024-07-03 20:57:51 -04:00
Greg Johnston
fba207df08 get types working with nested ErrorBoundary/Suspense 2024-07-03 20:57:51 -04:00
Greg Johnston
a16367215c probably as far as I can go with the current SuspenseBoundary approach 2024-07-03 20:57:51 -04:00
Greg Johnston
376b90d60b fix static types 2024-07-03 20:57:51 -04:00
Greg Johnston
20b0894b9f fix cancellation logic for server fn requests 2024-07-03 20:57:51 -04:00
Greg Johnston
c197131b6b only subscribe to memo manually if already loaded 2024-07-03 20:57:51 -04:00
Greg Johnston
1ea18bb3eb Suspense SSR 2024-07-03 20:57:51 -04:00
Greg Johnston
62decfde51 updates toward todo_app_sqlite 2024-07-03 20:57:51 -04:00
Greg Johnston
8f3ecd84b2 fix stable examples 2024-07-03 20:57:51 -04:00
Greg Johnston
6b1f6563d2 implement rendering traits for signals directly on stable 2024-07-03 20:57:51 -04:00
Greg Johnston
b5bfa41400 suspend!() macro 2024-07-03 20:57:51 -04:00
Greg Johnston
018f347673 add Transition 2024-07-03 20:57:51 -04:00
Greg Johnston
ab35ea7d21 loosen requirements for Show 2024-07-03 20:57:51 -04:00
Greg Johnston
cb0a0292d5 working on examples 2024-07-03 20:57:51 -04:00
Greg Johnston
351e42e885 finish TodoMVC example 2024-07-03 20:57:51 -04:00
Greg Johnston
3a89396ccc simplify Suspense: this should still work with hydration 2024-07-03 20:57:51 -04:00
Greg Johnston
32183692c0 working model for Suspense with new version 2024-07-03 20:57:51 -04:00
Greg Johnston
4cf95432b7 probably as far as I can go with the current SuspenseBoundary approach 2024-07-03 20:57:51 -04:00
Greg Johnston
b24e52773f fix ErrorBoundary/Suspense 2024-07-03 20:57:51 -04:00
Greg Johnston
3bcf7c7355 feat: ErrorBoundary and Suspense 2024-07-03 20:57:51 -04:00
Greg Johnston
be971d018a feat: ErrorBoundary 2024-07-03 20:57:51 -04:00
Greg Johnston
1f391d08c0 fix Cargo.toml merge issues 2024-07-03 20:57:51 -04:00
Greg Johnston
8682487650 working on examples 2024-07-03 20:57:50 -04:00
Greg Johnston
3c062e5b3e styling with CSS 2024-07-03 20:57:50 -04:00
Greg Johnston
6117cfa849 example with isomorphic GTK/web design system 2024-07-03 20:57:50 -04:00
Greg Johnston
7344618160 gtk example 2024-07-03 20:57:50 -04:00
Greg Johnston
83696c14da ErrorBoundary component 2024-07-03 20:57:50 -04:00
Greg Johnston
eacaaaec90 Suspense/Transition components 2024-07-03 20:57:50 -04:00
Greg Johnston
c51e8f3569 GTK example for 0.7 2024-07-03 20:57:50 -04:00
Greg Johnston
64fc1b8bd1 add serde-wasm-bindgen encoding for resources 2024-07-03 20:57:50 -04:00
Greg Johnston
1b4ae08943 add typed children 2024-07-03 20:57:50 -04:00
Greg Johnston
eff6aa2459 scope Arena to each request 2024-07-03 20:57:50 -04:00
Greg Johnston
8e6c2e50b4 correctly omit HTML-generating code from AnyView 2024-07-03 20:57:50 -04:00
Greg Johnston
dd9aa9e4cd experimental sledgehammer Renderer backend 2024-07-03 20:57:50 -04:00
Greg Johnston
47e11acf80 fix async context issues, add flat routing 2024-07-03 20:57:50 -04:00
Greg Johnston
3bed48abaa test more dynamic string length work 2024-07-03 20:57:50 -04:00
Greg Johnston
a72221de2a attribute value escaping 2024-07-03 20:57:50 -04:00
Greg Johnston
0243a705fa work related to 0.7 blog port 2024-07-03 20:57:50 -04:00
Greg Johnston
abaab492c0 work related to 0.7 blog port 2024-07-03 20:57:22 -04:00
Greg Johnston
935bdd2d61 completing work on meta 2024-07-03 20:57:09 -04:00
Greg Johnston
c5679b9a2a completing work on meta 2024-07-03 20:57:09 -04:00
Greg Johnston
d2ef1c833e initial work on meta 2024-07-03 20:57:09 -04:00
Greg Johnston
e357ca5d02 initial work on meta 2024-07-03 20:57:09 -04:00
Greg Johnston
e913ad7439 stash 2024-07-03 20:56:37 -04:00
Greg Johnston
6b57437514 fix nested route rebuilding 2024-07-03 20:56:37 -04:00
Greg Johnston
19c965bb35 navigation between nested routes 2024-07-03 20:56:37 -04:00
Greg Johnston
657d0057f2 initial stage for working nested route rendering 2024-07-03 20:56:37 -04:00
Greg Johnston
45f5a4ea39 stash 2024-07-03 20:56:37 -04:00
Greg Johnston
feeab73833 stash 2024-07-03 20:56:37 -04:00
Greg Johnston
10c142a655 get basic routing working 2024-07-03 20:56:37 -04:00
Greg Johnston
d97e72d2f4 stash 2024-07-03 20:56:37 -04:00
Greg Johnston
3952fa4952 reorganize 2024-07-03 20:56:37 -04:00
Greg Johnston
87772466d1 working on nesting routing 2024-07-03 20:56:37 -04:00
Greg Johnston
8479fddf92 reorganize 2024-07-03 20:56:37 -04:00
Greg Johnston
8b738d3a2a stash 2024-07-03 20:56:37 -04:00
Greg Johnston
df7f83165b abstract interface to walk nested routes and to access views 2024-07-03 20:56:37 -04:00
Greg Johnston
f54b4f3d1e reorganize and clean up 2024-07-03 20:56:37 -04:00
Greg Johnston
21a8bd31c7 nested route matching 2024-07-03 20:56:37 -04:00
Greg Johnston
02e9e48d89 stash 2024-07-03 20:56:37 -04:00
Greg Johnston
e0a1b7a813 stash 2024-07-03 20:56:37 -04:00
Greg Johnston
748c600e59 work on routing utils 2024-07-03 20:56:37 -04:00
Greg Johnston
26cacbd379 nested route matching working 2024-07-03 20:56:37 -04:00
Greg Johnston
36df86b3b7 stash 2024-07-03 20:56:37 -04:00
Greg Johnston
57fd178afb use either_of crate 2024-07-03 20:56:37 -04:00
Greg Johnston
ce559e1e22 nested routes take 1 2024-07-03 20:56:37 -04:00
Greg Johnston
ff51f064d4 split EitherOfX into its own crate 2024-07-03 20:56:37 -04:00
Greg Johnston
3379462633 work on routing 2024-07-03 20:56:37 -04:00
Greg Johnston
3e3359bea1 work on routing 2024-07-03 20:56:37 -04:00
Greg Johnston
0702b02011 set up routing 2024-07-03 20:56:37 -04:00
Greg Johnston
5af8ca4202 clear warning 2024-07-03 20:56:37 -04:00
Greg Johnston
17e822c14b chore: clear warnings 2024-07-03 20:56:37 -04:00
Greg Johnston
3db9e627db pass on: to components (and lay basis for passing all other attributes) 2024-07-03 20:56:37 -04:00
Greg Johnston
1cb0c6c5c8 working on AddAttr 2024-07-03 20:56:37 -04:00
Greg Johnston
de81d9d92f remove boilerplate: require that Node, Element, etc. types always be Clone + 'static 2024-07-03 20:56:37 -04:00
Greg Johnston
6a107cfa23 parent_child example 2024-07-03 20:56:37 -04:00
Greg Johnston
93d00ae968 use AnyError for all try_ rendering errors, so that they can compose 2024-07-03 20:56:37 -04:00
Greg Johnston
cd322144f7 support for guards with class: syntax 2024-07-03 20:56:37 -04:00
Greg Johnston
d89025e347 add Borrow implementation to make it easier to abstract over T and Guard<T> 2024-07-03 20:56:37 -04:00
Greg Johnston
ebc7bcd469 finish error boundary (fix last state transition issue) 2024-07-03 20:56:37 -04:00
Greg Johnston
597dc6c625 progress on error boundary that works with nested reactivity 2024-07-03 20:56:37 -04:00
Greg Johnston
45daa770fe progress on error boundary that works with nested reactivity 2024-07-03 20:56:37 -04:00
Greg Johnston
16b282da9e error example 2024-07-03 20:56:37 -04:00
Greg Johnston
dccf5a010d enable event delegation 2024-07-03 20:56:37 -04:00
Greg Johnston
5cdfdf52bd fix release build 2024-07-03 20:56:37 -04:00
Greg Johnston
9fc3b8b52b update TODO.md 2024-07-03 20:56:37 -04:00
Greg Johnston
d4555671e1 store effects in reactive system 2024-07-03 20:56:37 -04:00
Greg Johnston
c83a3b537c todomvc example 2024-07-03 20:56:37 -04:00
Greg Johnston
d09d6acbf1 work on async demo 2024-07-03 20:56:37 -04:00
Greg Johnston
9d93c451a6 clone values for Futures 2024-07-03 20:56:36 -04:00
Greg Johnston
ca298ea3d8 make guard types more nestable/flexible so that we can implement render traits on any of them 2024-07-03 20:56:36 -04:00
Greg Johnston
0339392658 work on async demo 2024-07-03 20:56:36 -04:00
Greg Johnston
ae36060ebe work on async demo 2024-07-03 20:56:36 -04:00
Greg Johnston
46a839bf43 feat: create generic any_spawner crate to share between reactive system and renderer 2024-07-03 20:56:36 -04:00
Greg Johnston
c274518c18 add other methods on Stored 2024-07-03 20:56:36 -04:00
Greg Johnston
b204f569eb work on async demo 2024-07-03 20:56:36 -04:00
Greg Johnston
a3286fd9b6 stash: working on jsfb 2024-07-03 20:56:36 -04:00
Greg Johnston
9fd5a646f1 chore: remove unnecessary log 2024-07-03 20:56:36 -04:00
Greg Johnston
65b2042682 fix: correct owner for rows of For, correct cleanup of arenas 2024-07-03 20:56:36 -04:00
Greg Johnston
c57347edd7 fix: close memory leak in tasks waiting on channels 2024-07-03 20:56:36 -04:00
Greg Johnston
8f7db58b7c feat: typed event targets 2024-07-03 20:56:36 -04:00
Greg Johnston
bae1312f06 working on examples 2024-07-03 20:56:36 -04:00
Greg Johnston
5e9a69a9ca begin migrating to leptos and leptos_dom packages 2024-07-03 20:56:36 -04:00
Greg Johnston
bcfe54b1ca stash 2024-07-03 20:55:53 -04:00
Greg Johnston
2a2a3d1461 stash 2024-07-03 20:55:53 -04:00
Greg Johnston
b70c4da854 chore: remove leptos_reactive and add reactive_graph 2024-07-03 20:55:53 -04:00
Greg Johnston
0436a82c65 feat: improved ergonomics of read guards 2024-07-03 20:55:53 -04:00
Greg Johnston
6260ca49b0 feat: add Readable implementation for all types 2024-07-03 20:55:52 -04:00
Greg Johnston
d045ad335d feat: add no_std support in appropriate crates 2024-07-03 20:55:52 -04:00
Greg Johnston
3bb34b2358 feat: tachys 2024-07-03 20:55:52 -04:00
Greg Johnston
27b2eca74c feat: add Fn traits 2024-07-03 20:55:52 -04:00
Greg Johnston
1a525fe9e7 docs: note re: execution order (see #2261 and #2262) 2024-07-03 20:55:52 -04:00
Greg Johnston
6f24d29bcf feat: modular SharedContext for hydration 2024-07-03 20:55:52 -04:00
Greg Johnston
d2f88f004d chore: split OrPoisoned trait into its own crate for reuse 2024-07-03 20:55:52 -04:00
Greg Johnston
33f0720be1 feat: modular, trait-based, Send/Sync reactive system 2024-07-03 20:55:52 -04:00
594 changed files with 52246 additions and 37656 deletions

View File

@@ -14,7 +14,7 @@ jobs:
test:
needs: [get-leptos-changed]
if: needs.get-leptos-changed.outputs.leptos_changed == 'true'
if: needs.get-leptos-changed.outputs.leptos_changed == 'true' && github.event.pull_request.labels[0].name != 'breaking'
name: Run semver check (nightly-2024-04-14)
runs-on: ubuntu-latest

View File

@@ -20,6 +20,11 @@ jobs:
matrix:
directory:
[
any_error,
any_spawner,
const_str_slice_concat,
either_of,
hydration_context,
integrations/actix,
integrations/axum,
integrations/utils,
@@ -28,10 +33,14 @@ jobs:
leptos_dom,
leptos_hot_reload,
leptos_macro,
leptos_reactive,
leptos_server,
meta,
next_tuple,
oco,
or_poisoned,
reactive_graph,
router,
router_macro,
server_fn,
server_fn/server_fn_macro_default,
server_fn_macro,

View File

@@ -24,17 +24,29 @@ jobs:
uses: tj-actions/changed-files@v43
with:
files: |
integrations/**
any_error/**
any_spawner/**
const_str_slice_concat/**
either_of/**
hydration_context/**
integrations/actix/**
integrations/axum/**
integrations/utils/**
leptos/**
leptos_config/**
leptos_dom/**
leptos_hot_reload/**
leptos_macro/**
leptos_reactive/**
leptos_server/**
meta/**
next_tuple/**
oco/**
or_poisoned/**
reactive_graph/**
router/**
router_macro/**
server_fn/**
server_fn/server_fn_macro_default/**
server_fn_macro/**
- name: List source files that changed

View File

@@ -3,18 +3,28 @@ resolver = "2"
members = [
# utilities
"oco",
"any_spawner",
"const_str_slice_concat",
"either_of",
"next_tuple",
"oco",
"or_poisoned",
# core
"hydration_context",
"leptos",
"leptos_dom",
"leptos_config",
"leptos_hot_reload",
"leptos_macro",
"leptos_reactive",
"leptos_server",
"reactive_graph",
"reactive_stores",
"reactive_stores_macro",
"server_fn",
"server_fn_macro",
"server_fn/server_fn_macro_default",
"tachys",
# integrations
"integrations/actix",
@@ -24,28 +34,40 @@ members = [
# libraries
"meta",
"router",
"router_macro",
"any_error",
]
exclude = ["benchmarks", "examples"]
[workspace.package]
version = "0.6.12"
version = "0.7.0-alpha"
rust-version = "1.75"
[workspace.dependencies]
oco_ref = { path = "./oco", version = "0.1.0" }
leptos = { path = "./leptos", version = "0.6.12" }
leptos_dom = { path = "./leptos_dom", version = "0.6.12" }
leptos_hot_reload = { path = "./leptos_hot_reload", version = "0.6.12" }
leptos_macro = { path = "./leptos_macro", version = "0.6.12" }
leptos_reactive = { path = "./leptos_reactive", version = "0.6.12" }
leptos_server = { path = "./leptos_server", version = "0.6.12" }
server_fn = { path = "./server_fn", version = "0.6.12" }
server_fn_macro = { path = "./server_fn_macro", version = "0.6.12" }
server_fn_macro_default = { path = "./server_fn/server_fn_macro_default", version = "0.6" }
leptos_config = { path = "./leptos_config", version = "0.6.12" }
leptos_router = { path = "./router", version = "0.6.12" }
leptos_meta = { path = "./meta", version = "0.6.12" }
leptos_integration_utils = { path = "./integrations/utils", version = "0.6.12" }
throw_error = { path = "./any_error/", version = "0.1" }
any_spawner = { path = "./any_spawner/", version = "0.1" }
const_str_slice_concat = { path = "./const_str_slice_concat", version = "0.1" }
either_of = { path = "./either_of/", version = "0.1" }
hydration_context = { path = "./hydration_context", version = "0.2.0-alpha" }
leptos = { path = "./leptos", version = "0.7.0-alpha" }
leptos_config = { path = "./leptos_config", version = "0.7.0-alpha" }
leptos_dom = { path = "./leptos_dom", version = "0.7.0-alpha" }
leptos_hot_reload = { path = "./leptos_hot_reload", version = "0.7.0-alpha" }
leptos_integration_utils = { path = "./integrations/utils", version = "0.7.0-alpha" }
leptos_macro = { path = "./leptos_macro", version = "0.7.0-alpha" }
leptos_router = { path = "./router", version = "0.7.0-alpha" }
leptos_server = { path = "./leptos_server", version = "0.7.0-alpha" }
leptos_meta = { path = "./meta", version = "0.7.0-alpha" }
next_tuple = { path = "./next_tuple", version = "0.1.0-alpha" }
oco_ref = { path = "./oco", version = "0.2" }
or_poisoned = { path = "./or_poisoned", version = "0.1" }
reactive_graph = { path = "./reactive_graph", version = "0.1.0-alpha" }
reactive_stores = { path = "./reactive_stores", version = "0.1.0-alpha" }
reactive_stores_macro = { path = "./reactive_stores_macro", version = "0.1.0-alpha" }
server_fn = { path = "./server_fn", version = "0.7.0-alpha" }
server_fn_macro = { path = "./server_fn_macro", version = "0.7.0-alpha" }
server_fn_macro_default = { path = "./server_fn/server_fn_macro_default", version = "0.7.0-alpha" }
tachys = { path = "./tachys", version = "0.1.0-alpha" }
[profile.release]
codegen-units = 1

40
TODO.md Normal file
View File

@@ -0,0 +1,40 @@
- core examples
- [x] counter
- [x] counters
- [x] fetch
- [x] todomvc
- [x] error_boundary
- [x] parent\_child
- [x] on: on components
- [ ] router
- [ ] slots
- [ ] hackernews
- [ ] counter\_isomorphic
- [ ] todo\_app\_sqlite
- other ssr examples
- [ ] error boundary SSR
- reactivity
- Signal wrappers
- SignalDispose implementations on all Copy types
- untracked access warnings
- ErrorBoundary
- [ ] RenderHtml implementation
- [ ] Separate component?
- Suspense/Transition components?
- callbacks
- unsync StoredValue
- SSR
- escaping HTML correctly (attributes + text nodes)
- router
- nested routes
- trailing slashes
- \_meta package (and use in hackernews)
- integrations
- update tests
- hackernews example
- TODOs
- Suspense/Transition/Await components
- nicer routing components
- async routing (waiting for data to load before navigation)
- `<A>` component
- figure out rebuilding issues: list (needs new signal IDs) vs. regular rebuild

13
any_error/Cargo.toml Normal file
View File

@@ -0,0 +1,13 @@
[package]
name = "throw_error"
edition = "2021"
version = "0.1.0"
authors = ["Greg Johnston"]
license = "MIT"
readme = "../README.md"
repository = "https://github.com/leptos-rs/leptos"
description = "Utilities for wrapping, throwing, and catching errors."
rust-version.workspace = true
[dependencies]
pin-project-lite = "0.2"

2
any_error/README.md Normal file
View File

@@ -0,0 +1,2 @@
A utility library for wrapping arbitrary errors, and for “throwing” errors in a way
that can be caught by user-defined error hooks.

148
any_error/src/lib.rs Normal file
View File

@@ -0,0 +1,148 @@
#![forbid(unsafe_code)]
#![deny(missing_docs)]
//! A utility library for wrapping arbitrary errors, and for “throwing” errors in a way
//! that can be caught by user-defined error hooks.
use std::{
cell::RefCell,
error,
fmt::{self, Display},
future::Future,
ops,
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
/* Wrapper Types */
/// This is a result type into which any error can be converted.
///
/// Results are stored as [`Error`].
pub type Result<T, E = Error> = core::result::Result<T, E>;
/// A generic wrapper for any error.
#[derive(Debug, Clone)]
#[repr(transparent)]
pub struct Error(Arc<dyn error::Error + Send + Sync>);
impl Error {
/// Converts the wrapper into the inner reference-counted error.
pub fn into_inner(self) -> Arc<dyn error::Error + Send + Sync> {
Arc::clone(&self.0)
}
}
impl ops::Deref for Error {
type Target = Arc<dyn error::Error + Send + Sync>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl<T> From<T> for Error
where
T: error::Error + Send + Sync + 'static,
{
fn from(value: T) -> Self {
Error(Arc::new(value))
}
}
/// Implements behavior that allows for global or scoped error handling.
///
/// This allows for both "throwing" errors to register them, and "clearing" errors when they are no
/// longer valid. This is useful for something like a user interface, in which an error can be
/// "thrown" on some invalid user input, and later "cleared" if the user corrects the input.
/// Keeping a unique identifier for each error allows the UI to be updated accordingly.
pub trait ErrorHook: Send + Sync {
/// Handles the given error, returning a unique identifier.
fn throw(&self, error: Error) -> ErrorId;
/// Clears the error associated with the given identifier.
fn clear(&self, id: &ErrorId);
}
/// A unique identifier for an error. This is returned when you call [`throw`], which calls a
/// global error handler.
#[derive(Debug, PartialEq, Eq, Hash, Clone, Default)]
pub struct ErrorId(usize);
impl Display for ErrorId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&self.0, f)
}
}
impl From<usize> for ErrorId {
fn from(value: usize) -> Self {
Self(value)
}
}
thread_local! {
static ERROR_HOOK: RefCell<Option<Arc<dyn ErrorHook>>> = RefCell::new(None);
}
/// Sets the current thread-local error hook, which will be invoked when [`throw`] is called.
pub fn set_error_hook(hook: Arc<dyn ErrorHook>) {
ERROR_HOOK.with_borrow_mut(|this| *this = Some(hook))
}
/// Invokes the error hook set by [`set_error_hook`] with the given error.
pub fn throw(error: impl Into<Error>) -> ErrorId {
ERROR_HOOK
.with_borrow(|hook| hook.as_ref().map(|hook| hook.throw(error.into())))
.unwrap_or_default()
}
/// Clears the given error from the current error hook.
pub fn clear(id: &ErrorId) {
ERROR_HOOK
.with_borrow(|hook| hook.as_ref().map(|hook| hook.clear(id)))
.unwrap_or_default()
}
pin_project_lite::pin_project! {
/// A [`Future`] that reads the error hook that is set when it is created, and sets this as the
/// current error hook whenever it is polled.
pub struct ErrorHookFuture<Fut> {
hook: Option<Arc<dyn ErrorHook>>,
#[pin]
inner: Fut
}
}
impl<Fut> ErrorHookFuture<Fut> {
/// Reads the current hook and wraps the given [`Future`], returning a new `Future` that will
/// set the error hook whenever it is polled.
pub fn new(inner: Fut) -> Self {
Self {
hook: ERROR_HOOK.with_borrow(Clone::clone),
inner,
}
}
}
impl<Fut> Future for ErrorHookFuture<Fut>
where
Fut: Future,
{
type Output = Fut::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
if let Some(hook) = &this.hook {
set_error_hook(Arc::clone(hook))
}
this.inner.poll(cx)
}
}

30
any_spawner/Cargo.toml Normal file
View File

@@ -0,0 +1,30 @@
[package]
name = "any_spawner"
edition = "2021"
version = "0.1.1"
authors = ["Greg Johnston"]
license = "MIT"
readme = "../README.md"
repository = "https://github.com/leptos-rs/leptos"
description = "Spawn asynchronous tasks in an executor-independent way."
[dependencies]
futures = "0.3"
glib = { version = "0.19", optional = true }
thiserror = "1"
tokio = { version = "1", optional = true, default-features = false, features = [
"rt",
] }
tracing = { version = "0.1", optional = true }
wasm-bindgen-futures = { version = "0.4", optional = true }
[features]
tracing = ["dep:tracing"]
tokio = ["dep:tokio"]
glib = ["dep:glib"]
wasm-bindgen = ["dep:wasm-bindgen-futures"]
futures-executor = ["futures/thread-pool", "futures/executor"]
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

26
any_spawner/README.md Normal file
View File

@@ -0,0 +1,26 @@
This crate makes it easier to write asynchronous code that is executor-agnostic, by providing a
utility that can be used to spawn tasks in a variety of executors.
It only supports single executor per program, but that executor can be set at runtime, anywhere
in your crate (or an application that depends on it).
This can be extended to support any executor or runtime that supports spawning [`Future`]s.
This is a least common denominator implementation in many ways. Limitations include:
- setting an executor is a one-time, global action
- no "join handle" or other result is returned from the spawn
- the `Future` must output `()`
```rust
use any_spawner::Executor;
Executor::init_futures_executor()
.expect("executor should only be initialized once");
// spawn a thread-safe Future
Executor::spawn(async { /* ... */ });
// spawn a Future that is !Send
Executor::spawn_local(async { /* ... */ });
```

244
any_spawner/src/lib.rs Normal file
View File

@@ -0,0 +1,244 @@
//! This crate makes it easier to write asynchronous code that is executor-agnostic, by providing a
//! utility that can be used to spawn tasks in a variety of executors.
//!
//! It only supports single executor per program, but that executor can be set at runtime, anywhere
//! in your crate (or an application that depends on it).
//!
//! This can be extended to support any executor or runtime that supports spawning [`Future`]s.
//!
//! This is a least common denominator implementation in many ways. Limitations include:
//! - setting an executor is a one-time, global action
//! - no "join handle" or other result is returned from the spawn
//! - the `Future` must output `()`
//!
//! ```rust
//! use any_spawner::Executor;
//!
//! // make sure an Executor has been initialized with one of the init_ functions
//!
//! # if false {
//! // spawn a thread-safe Future
//! Executor::spawn(async { /* ... */ });
//!
//! // spawn a Future that is !Send
//! Executor::spawn_local(async { /* ... */ });
//! # }
//! ```
#![forbid(unsafe_code)]
#![deny(missing_docs)]
use std::{future::Future, pin::Pin, sync::OnceLock};
use thiserror::Error;
pub(crate) type PinnedFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;
pub(crate) type PinnedLocalFuture<T> = Pin<Box<dyn Future<Output = T>>>;
static SPAWN: OnceLock<fn(PinnedFuture<()>)> = OnceLock::new();
static SPAWN_LOCAL: OnceLock<fn(PinnedLocalFuture<()>)> = OnceLock::new();
/// Errors that can occur when using the executor.
#[derive(Error, Debug)]
pub enum ExecutorError {
/// The executor has already been set.
#[error("Executor has already been set.")]
AlreadySet,
}
/// A global async executor that can spawn tasks.
pub struct Executor;
impl Executor {
/// Spawns a thread-safe [`Future`].
/// ```rust
/// use any_spawner::Executor;
/// # if false {
/// // spawn a thread-safe Future
/// Executor::spawn(async { /* ... */ });
/// # }
/// ```
#[track_caller]
pub fn spawn(fut: impl Future<Output = ()> + Send + 'static) {
if let Some(spawner) = SPAWN.get() {
spawner(Box::pin(fut))
} else {
#[cfg(all(debug_assertions, feature = "tracing"))]
tracing::error!(
"At {}, tried to spawn a Future with Executor::spawn() before \
the Executor had been set.",
std::panic::Location::caller()
);
#[cfg(all(debug_assertions, not(feature = "tracing")))]
panic!(
"At {}, tried to spawn a Future with Executor::spawn() before \
the Executor had been set.",
std::panic::Location::caller()
);
}
}
/// Spawns a [`Future`] that cannot be sent across threads.
/// ```rust
/// use any_spawner::Executor;
///
/// # if false {
/// // spawn a thread-safe Future
/// Executor::spawn_local(async { /* ... */ });
/// # }
/// ```
#[track_caller]
pub fn spawn_local(fut: impl Future<Output = ()> + 'static) {
if let Some(spawner) = SPAWN_LOCAL.get() {
spawner(Box::pin(fut))
} else {
#[cfg(all(debug_assertions, feature = "tracing"))]
tracing::error!(
"At {}, tried to spawn a Future with Executor::spawn_local() \
before the Executor had been set.",
std::panic::Location::caller()
);
#[cfg(all(debug_assertions, not(feature = "tracing")))]
panic!(
"At {}, tried to spawn a Future with Executor::spawn_local() \
before the Executor had been set.",
std::panic::Location::caller()
);
}
}
/// Waits until the next "tick" of the current async executor.
pub async fn tick() {
let (tx, rx) = futures::channel::oneshot::channel();
Executor::spawn(async move {
_ = tx.send(());
});
_ = rx.await;
}
}
impl Executor {
/// Globally sets the [`tokio`] runtime as the executor used to spawn tasks.
///
/// Returns `Err(_)` if an executor has already been set.
///
/// Requires the `tokio` feature to be activated on this crate.
#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
pub fn init_tokio() -> Result<(), ExecutorError> {
SPAWN
.set(|fut| {
tokio::spawn(fut);
})
.map_err(|_| ExecutorError::AlreadySet)?;
SPAWN_LOCAL
.set(|fut| {
tokio::task::spawn_local(fut);
})
.map_err(|_| ExecutorError::AlreadySet)?;
Ok(())
}
/// Globally sets the [`wasm-bindgen-futures`] runtime as the executor used to spawn tasks.
///
/// Returns `Err(_)` if an executor has already been set.
///
/// Requires the `wasm-bindgen` feature to be activated on this crate.
#[cfg(feature = "wasm-bindgen")]
#[cfg_attr(docsrs, doc(cfg(feature = "wasm-bindgen")))]
pub fn init_wasm_bindgen() -> Result<(), ExecutorError> {
SPAWN
.set(|fut| {
wasm_bindgen_futures::spawn_local(fut);
})
.map_err(|_| ExecutorError::AlreadySet)?;
SPAWN_LOCAL
.set(|fut| {
wasm_bindgen_futures::spawn_local(fut);
})
.map_err(|_| ExecutorError::AlreadySet)?;
Ok(())
}
/// Globally sets the [`glib`] runtime as the executor used to spawn tasks.
///
/// Returns `Err(_)` if an executor has already been set.
///
/// Requires the `glib` feature to be activated on this crate.
#[cfg(feature = "glib")]
#[cfg_attr(docsrs, doc(cfg(feature = "glib")))]
pub fn init_glib() -> Result<(), ExecutorError> {
SPAWN
.set(|fut| {
let main_context = glib::MainContext::default();
main_context.spawn(fut);
})
.map_err(|_| ExecutorError::AlreadySet)?;
SPAWN_LOCAL
.set(|fut| {
let main_context = glib::MainContext::default();
main_context.spawn_local(fut);
})
.map_err(|_| ExecutorError::AlreadySet)?;
Ok(())
}
/// Globally sets the [`futures`] executor as the executor used to spawn tasks,
/// lazily creating a thread pool to spawn tasks into.
///
/// Returns `Err(_)` if an executor has already been set.
///
/// Requires the `futures-executor` feature to be activated on this crate.
#[cfg(feature = "futures-executor")]
#[cfg_attr(docsrs, doc(cfg(feature = "futures-executor")))]
pub fn init_futures_executor() -> Result<(), ExecutorError> {
use futures::{
executor::{LocalPool, ThreadPool},
task::{LocalSpawnExt, SpawnExt},
};
static THREAD_POOL: OnceLock<ThreadPool> = OnceLock::new();
thread_local! {
static LOCAL_POOL: LocalPool = LocalPool::new();
}
fn get_thread_pool() -> &'static ThreadPool {
THREAD_POOL.get_or_init(|| {
ThreadPool::new()
.expect("could not create futures executor ThreadPool")
})
}
SPAWN
.set(|fut| {
get_thread_pool()
.spawn(fut)
.expect("failed to spawn future");
})
.map_err(|_| ExecutorError::AlreadySet)?;
SPAWN_LOCAL
.set(|fut| {
LOCAL_POOL.with(|pool| {
let spawner = pool.spawner();
spawner.spawn_local(fut).expect("failed to spawn future");
});
})
.map_err(|_| ExecutorError::AlreadySet)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
#[cfg(feature = "futures-executor")]
#[test]
fn can_spawn_local_future() {
use crate::Executor;
use std::rc::Rc;
Executor::init_futures_executor().expect("couldn't set executor");
let rc = Rc::new(());
Executor::spawn_local(async {
_ = rc;
});
Executor::spawn(async {});
}
}

View File

@@ -0,0 +1,12 @@
[package]
name = "const_str_slice_concat"
edition = "2021"
version = "0.1.0"
authors = ["Greg Johnston"]
license = "MIT"
readme = "../README.md"
repository = "https://github.com/leptos-rs/leptos"
description = "Utilities for const concatenation of string slices."
rust-version.workspace = true
[dependencies]

View File

@@ -0,0 +1 @@
extend = { path = "../cargo-make/main.toml" }

View File

@@ -0,0 +1,139 @@
#![no_std]
#![forbid(unsafe_code)]
#![deny(missing_docs)]
//! Utilities for const concatenation of string slices.
pub(crate) const MAX_TEMPLATE_SIZE: usize = 4096;
/// Converts a zero-terminated buffer of bytes into a UTF-8 string.
pub const fn str_from_buffer(buf: &[u8; MAX_TEMPLATE_SIZE]) -> &str {
match core::ffi::CStr::from_bytes_until_nul(buf) {
Ok(cstr) => match cstr.to_str() {
Ok(str) => str,
Err(_) => panic!("TEMPLATE FAILURE"),
},
Err(_) => panic!("TEMPLATE FAILURE"),
}
}
/// Concatenates any number of static strings into a single array.
// credit to Rainer Stropek, "Constant fun," Rust Linz, June 2022
pub const fn const_concat(
strs: &'static [&'static str],
) -> [u8; MAX_TEMPLATE_SIZE] {
let mut buffer = [0; MAX_TEMPLATE_SIZE];
let mut position = 0;
let mut remaining = strs;
while let [current, tail @ ..] = remaining {
let x = current.as_bytes();
let mut i = 0;
// have it iterate over bytes manually, because, again,
// no mutable refernces in const fns
while i < x.len() {
buffer[position] = x[i];
position += 1;
i += 1;
}
remaining = tail;
}
buffer
}
/// Converts a zero-terminated buffer of bytes into a UTF-8 string with the given prefix.
pub const fn const_concat_with_prefix(
strs: &'static [&'static str],
prefix: &'static str,
suffix: &'static str,
) -> [u8; MAX_TEMPLATE_SIZE] {
let mut buffer = [0; MAX_TEMPLATE_SIZE];
let mut position = 0;
let mut remaining = strs;
while let [current, tail @ ..] = remaining {
let x = current.as_bytes();
let mut i = 0;
// have it iterate over bytes manually, because, again,
// no mutable refernces in const fns
while i < x.len() {
buffer[position] = x[i];
position += 1;
i += 1;
}
remaining = tail;
}
if buffer[0] == 0 {
buffer
} else {
let mut new_buf = [0; MAX_TEMPLATE_SIZE];
let prefix = prefix.as_bytes();
let suffix = suffix.as_bytes();
let mut position = 0;
let mut i = 0;
while i < prefix.len() {
new_buf[position] = prefix[i];
position += 1;
i += 1;
}
i = 0;
while i < buffer.len() {
if buffer[i] == 0 {
break;
}
new_buf[position] = buffer[i];
position += 1;
i += 1;
}
i = 0;
while i < suffix.len() {
new_buf[position] = suffix[i];
position += 1;
i += 1;
}
new_buf
}
}
/// Converts any number of strings into a UTF-8 string, separated by the given string.
pub const fn const_concat_with_separator(
strs: &[&str],
separator: &'static str,
) -> [u8; MAX_TEMPLATE_SIZE] {
let mut buffer = [0; MAX_TEMPLATE_SIZE];
let mut position = 0;
let mut remaining = strs;
while let [current, tail @ ..] = remaining {
let x = current.as_bytes();
let mut i = 0;
// have it iterate over bytes manually, because, again,
// no mutable refernces in const fns
while i < x.len() {
buffer[position] = x[i];
position += 1;
i += 1;
}
if !x.is_empty() {
let mut position = 0;
let separator = separator.as_bytes();
while i < separator.len() {
buffer[position] = separator[i];
position += 1;
i += 1;
}
}
remaining = tail;
}
buffer
}

13
either_of/Cargo.toml Normal file
View File

@@ -0,0 +1,13 @@
[package]
name = "either_of"
edition = "2021"
version = "0.1.0"
authors = ["Greg Johnston"]
license = "MIT"
readme = "../README.md"
repository = "https://github.com/leptos-rs/leptos"
description = "Utilities for working with enumerated types that contain one of 2..n other types."
rust-version.workspace = true
[dependencies]
pin-project-lite = "0.2"

1
either_of/Makefile.toml Normal file
View File

@@ -0,0 +1 @@
extend = { path = "../cargo-make/main.toml" }

1
either_of/README.md Normal file
View File

@@ -0,0 +1 @@
Utilities for working with enumerated types that contain one of `2..n` other types.

135
either_of/src/lib.rs Normal file
View File

@@ -0,0 +1,135 @@
#![no_std]
#![forbid(unsafe_code)]
//! Utilities for working with enumerated types that contain one of `2..n` other types.
use core::{
fmt::Display,
future::Future,
pin::Pin,
task::{Context, Poll},
};
use pin_project_lite::pin_project;
#[derive(Debug, Clone, Copy)]
pub enum Either<A, B> {
Left(A),
Right(B),
}
impl<Item, A, B> Iterator for Either<A, B>
where
A: Iterator<Item = Item>,
B: Iterator<Item = Item>,
{
type Item = Item;
fn next(&mut self) -> Option<Self::Item> {
match self {
Either::Left(i) => i.next(),
Either::Right(i) => i.next(),
}
}
}
pin_project! {
#[project = EitherFutureProj]
pub enum EitherFuture<A, B> {
Left { #[pin] inner: A },
Right { #[pin] inner: B },
}
}
impl<A, B> Future for EitherFuture<A, B>
where
A: Future,
B: Future,
{
type Output = Either<A::Output, B::Output>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
match this {
EitherFutureProj::Left { inner } => match inner.poll(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(inner) => Poll::Ready(Either::Left(inner)),
},
EitherFutureProj::Right { inner } => match inner.poll(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(inner) => Poll::Ready(Either::Right(inner)),
},
}
}
}
macro_rules! tuples {
($name:ident + $fut_name:ident + $fut_proj:ident => $($ty:ident),*) => {
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum $name<$($ty,)*> {
$($ty ($ty),)*
}
impl<$($ty,)*> Display for $name<$($ty,)*>
where
$($ty: Display,)*
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
$($name::$ty(this) => this.fmt(f),)*
}
}
}
impl<Item, $($ty,)*> Iterator for $name<$($ty,)*>
where
$($ty: Iterator<Item = Item>,)*
{
type Item = Item;
fn next(&mut self) -> Option<Self::Item> {
match self {
$($name::$ty(i) => i.next(),)*
}
}
}
pin_project! {
#[project = $fut_proj]
pub enum $fut_name<$($ty,)*> {
$($ty { #[pin] inner: $ty },)*
}
}
impl<$($ty,)*> Future for $fut_name<$($ty,)*>
where
$($ty: Future,)*
{
type Output = $name<$($ty::Output,)*>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
match this {
$($fut_proj::$ty { inner } => match inner.poll(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(inner) => Poll::Ready($name::$ty(inner)),
},)*
}
}
}
}
}
tuples!(EitherOf3 + EitherOf3Future + EitherOf3FutureProj => A, B, C);
tuples!(EitherOf4 + EitherOf4Future + EitherOf4FutureProj => A, B, C, D);
tuples!(EitherOf5 + EitherOf5Future + EitherOf5FutureProj => A, B, C, D, E);
tuples!(EitherOf6 + EitherOf6Future + EitherOf6FutureProj => A, B, C, D, E, F);
tuples!(EitherOf7 + EitherOf7Future + EitherOf7FutureProj => A, B, C, D, E, F, G);
tuples!(EitherOf8 + EitherOf8Future + EitherOf8FutureProj => A, B, C, D, E, F, G, H);
tuples!(EitherOf9 + EitherOf9Future + EitherOf9FutureProj => A, B, C, D, E, F, G, H, I);
tuples!(EitherOf10 + EitherOf10Future + EitherOf10FutureProj => A, B, C, D, E, F, G, H, I, J);
tuples!(EitherOf11 + EitherOf11Future + EitherOf11FutureProj => A, B, C, D, E, F, G, H, I, J, K);
tuples!(EitherOf12 + EitherOf12Future + EitherOf12FutureProj => A, B, C, D, E, F, G, H, I, J, K, L);
tuples!(EitherOf13 + EitherOf13Future + EitherOf13FutureProj => A, B, C, D, E, F, G, H, I, J, K, L, M);
tuples!(EitherOf14 + EitherOf14Future + EitherOf14FutureProj => A, B, C, D, E, F, G, H, I, J, K, L, M, N);
tuples!(EitherOf15 + EitherOf15Future + EitherOf15FutureProj => A, B, C, D, E, F, G, H, I, J, K, L, M, N, O);
tuples!(EitherOf16 + EitherOf16Future + EitherOf16FutureProj => A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P);

View File

@@ -11,7 +11,6 @@ actix-files = { version = "0.6", optional = true }
actix-web = { version = "4", optional = true, features = ["macros"] }
console_error_panic_hook = "0.1"
cfg-if = "1"
http = { version = "0.2", optional = true }
leptos = { path = "../../leptos" }
leptos_meta = { path = "../../meta" }
leptos_actix = { path = "../../integrations/actix", optional = true }
@@ -20,8 +19,8 @@ wasm-bindgen = "0.2"
serde = { version = "1", features = ["derive"] }
[features]
csr = ["leptos/csr", "leptos_meta/csr", "leptos_router/csr"]
hydrate = ["leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate"]
csr = ["leptos/csr"]
hydrate = ["leptos/hydrate"]
ssr = [
"dep:actix-files",
"dep:actix-web",

View File

@@ -1,27 +1,18 @@
use leptos::*;
use leptos_meta::*;
use leptos_router::*;
use leptos::{logging, prelude::*};
use leptos_router::{
components::{FlatRoutes, Route, Router},
StaticSegment,
};
#[component]
pub fn App() -> impl IntoView {
// Provides context that manages stylesheets, titles, meta tags, etc.
provide_meta_context();
view! {
// injects a stylesheet into the document <head>
// id=leptos means cargo-leptos will hot-reload this stylesheet
<Stylesheet id="leptos" href="/pkg/leptos_start.css"/>
// sets the document title
<Title text="Welcome to Leptos"/>
// content for this welcome page
<Router>
<main id="app">
<Routes>
<Route path="" view=HomePage/>
<Route path="/*any" view=NotFound/>
</Routes>
<FlatRoutes fallback=NotFound>
<Route path=StaticSegment("") view=HomePage/>
</FlatRoutes>
</main>
</Router>
}
@@ -43,7 +34,7 @@ async fn do_something(
/// Renders the home page of your application.
#[component]
fn HomePage() -> impl IntoView {
let do_something_action = Action::<DoSomething, _>::server();
let do_something_action = ServerAction::<DoSomething>::new();
let value = Signal::derive(move || {
do_something_action
.value()
@@ -57,17 +48,12 @@ fn HomePage() -> impl IntoView {
view! {
<h1>"Test the action form!"</h1>
<ErrorBoundary fallback=move |error| format!("{:#?}", error
.get()
.into_iter()
.next()
.unwrap()
.1.into_inner()
.to_string())
>
{value}
<ActionForm action=do_something_action class="form">
<label>Should error: <input type="checkbox" name="should_error"/></label>
<ErrorBoundary fallback=move |error| {
move || format!("{:#?}", error.get())
}>
<pre>{value}</pre>
<ActionForm action=do_something_action attr:class="form">
<label>"Should error: "<input type="checkbox" name="should_error"/></label>
<button type="submit">Submit</button>
</ActionForm>
</ErrorBoundary>
@@ -91,7 +77,5 @@ fn NotFound() -> impl IntoView {
resp.set_status(actix_web::http::StatusCode::NOT_FOUND);
}
view! {
<h1>"Not Found"</h1>
}
view! { <h1>"Not Found"</h1> }
}

View File

@@ -1,18 +1,11 @@
pub mod app;
use cfg_if::cfg_if;
cfg_if! {
if #[cfg(feature = "hydrate")] {
#[cfg(feature = "hydrate")]
#[wasm_bindgen::prelude::wasm_bindgen]
pub fn hydrate() {
use app::*;
use wasm_bindgen::prelude::wasm_bindgen;
console_error_panic_hook::set_once();
#[wasm_bindgen]
pub fn hydrate() {
use app::*;
console_error_panic_hook::set_once();
leptos::mount_to_body(App);
}
}
leptos::mount::hydrate_body(App);
}

View File

@@ -4,25 +4,47 @@ async fn main() -> std::io::Result<()> {
use action_form_error_handling::app::*;
use actix_files::Files;
use actix_web::*;
use leptos::*;
use leptos::prelude::*;
use leptos_actix::{generate_route_list, LeptosRoutes};
use leptos_meta::MetaTags;
let conf = get_configuration(None).await.unwrap();
let addr = conf.leptos_options.site_addr;
// Generate the list of routes in your Leptos App
let routes = generate_route_list(App);
let conf = get_configuration(None).unwrap();
let addr = conf.leptos_options.site_addr;
println!("listening on http://{}", &addr);
HttpServer::new(move || {
// Generate the list of routes in your Leptos App
let routes = generate_route_list(App);
let leptos_options = &conf.leptos_options;
let site_root = &leptos_options.site_root;
App::new()
.route("/api/{tail:.*}", leptos_actix::handle_server_fns())
// serve JS/WASM/CSS from `pkg`
.service(Files::new("/pkg", format!("{site_root}/pkg")))
.leptos_routes(leptos_options.to_owned(), routes.to_owned(), App)
.app_data(web::Data::new(leptos_options.to_owned()))
.leptos_routes(routes, {
let leptos_options = leptos_options.clone();
move || {
use leptos::prelude::*;
view! {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta
name="viewport"
content="width=device-width, initial-scale=1"
/>
<AutoReload options=leptos_options.clone()/>
<HydrationScripts options=leptos_options.clone()/>
<MetaTags/>
</head>
<body>
<App/>
</body>
</html>
}
}})
//.wrap(middleware::Compress::default())
})
.bind(&addr)?
@@ -44,10 +66,9 @@ pub fn main() {
// prefer using `cargo leptos serve` instead
// to run: `trunk serve --open --features csr`
use action_form_error_handling::app::*;
use leptos::*;
use wasm_bindgen::prelude::wasm_bindgen;
use leptos::prelude::*;
console_error_panic_hook::set_once();
leptos::mount_to_body(App);
mount_to_body(App);
}

View File

@@ -1,14 +0,0 @@
[package]
name = "animated-show"
version = "0.1.0"
edition = "2021"
[profile.release]
codegen-units = 1
lto = true
[dependencies]
leptos = { path = "../../leptos", features = ["csr"] }
console_log = "1"
log = "0.4"
console_error_panic_hook = "0.1.7"

View File

@@ -1,14 +0,0 @@
# Animated Show Example
This is a very simple example of the `<AnimatedShow>` component.
The `<AnimatedShow>` component is an extension for the `<Show>` component and it will not take in a fallback, but it will unmount the component from the DOM after a given duration. This makes it possible to have really easy unmount animations with just
CSS.
## Getting Started
See the [Examples README](../README.md) for setup and run instructions.
## Quick Start
Run `trunk serve --open` to run this example.

View File

@@ -1,42 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<link data-trunk rel="rust" data-wasm-opt="z"/>
<link data-trunk rel="icon" type="image/ico" href="/public/favicon.ico"/>
<style>
.hover-me {
width: 100px;
margin: 1rem;
padding: 1rem;
text-align: center;
cursor: pointer;
border: 1px solid grey;
}
.here-i-am {
width: 100px;
margin: 1rem;
padding: 1rem;
text-align: center;
color: white;
font-weight: bold;
background: black;
}
@keyframes fade-in {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes fade-out {
from { opacity: 1; }
to { opacity: 0; }
}
.fade-in-1000 {
animation: 1000ms fade-in forwards;
}
.fade-out-1000 {
animation: 1000ms fade-out forwards;
}
</style>
</head>
<body></body>
</html>

View File

@@ -1,34 +0,0 @@
use core::time::Duration;
use leptos::*;
#[component]
pub fn App() -> impl IntoView {
let show = create_rw_signal(false);
// the CSS classes in this example are just written directly inside the `index.html`
view! {
<div
class="hover-me"
on:mouseenter=move |_| show.set(true)
on:mouseleave=move |_| show.set(false)
>
"Hover Me"
</div>
<AnimatedShow
when=show
// optional CSS class which will be applied if `when == true`
show_class="fade-in-1000"
// optional CSS class which will be applied if `when == false` and before the
// `hide_delay` starts -> makes CSS unmount animations really easy
hide_class="fade-out-1000"
// the given unmount delay which should match your unmount animation duration
hide_delay=Duration::from_millis(1000)
>
// provide any `Children` inside here
<div class="here-i-am">
"Here I Am!"
</div>
</AnimatedShow>
}
}

View File

@@ -1,8 +0,0 @@
use animated_show::App;
use leptos::*;
pub fn main() {
_ = console_log::init_with_level(log::Level::Debug);
console_error_panic_hook::set_once();
mount_to_body(App);
}

View File

@@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"
[profile.release]
opt-level = 'z'
codegen-units = 1
lto = true
@@ -12,6 +13,7 @@ leptos = { path = "../../leptos", features = ["csr"] }
console_log = "1"
log = "0.4"
console_error_panic_hook = "0.1.7"
gloo-timers = { version = "0.3.0", features = ["futures"] }
[dev-dependencies]
wasm-bindgen = "0.2"

View File

@@ -1,4 +1,4 @@
use leptos::*;
use leptos::prelude::*;
/// A simple counter component.
///
@@ -10,14 +10,33 @@ pub fn SimpleCounter(
/// The change that should be applied each time the button is clicked.
step: i32,
) -> impl IntoView {
let (value, set_value) = create_signal(initial_value);
/*let (value, set_value) = signal(initial_value);
view! {
<div>
<button on:click=move |_| set_value.set(0)>"Clear"</button>
<button on:click=move |_| set_value.update(|value| *value -= step)>"-1"</button>
<button on:click=move |_| *set_value.write() -= step>"-1"</button>
<span>"Value: " {value} "!"</span>
<button on:click=move |_| set_value.update(|value| *value += step)>"+1"</button>
</div>
}
}*/
App()
}
use gloo_timers::future::TimeoutFuture;
use leptos::{html::Input, prelude::*};
#[component]
fn Widget() -> impl IntoView {
let input_ref = NodeRef::<Input>::new();
Effect::new(move |_| {
let Some(_) = input_ref.get() else {
log!("no ref");
return;
};
log!("ref");
});
view! { <input node_ref=input_ref type="text"/> }
}

View File

@@ -1,15 +1,10 @@
use counter::SimpleCounter;
use leptos::*;
use leptos::prelude::*;
pub fn main() {
_ = console_log::init_with_level(log::Level::Debug);
console_error_panic_hook::set_once();
mount_to_body(|| {
view! {
<SimpleCounter
initial_value=0
step=1
/>
}
view! { <SimpleCounter initial_value=0 step=1/> }
})
}

View File

@@ -1,19 +1,21 @@
use counter::*;
use leptos::*;
use leptos::mount::mount_to;
use leptos::prelude::*;
use leptos::spawn::tick;
use wasm_bindgen::JsCast;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
fn clear() {
let document = leptos::document();
async fn clear() {
let document = document();
let test_wrapper = document.create_element("section").unwrap();
let _ = document.body().unwrap().append_child(&test_wrapper);
// start by rendering our counter and mounting it to the DOM
// note that we start at the initial value of 10
mount_to(
let _dispose = mount_to(
test_wrapper.clone().unchecked_into(),
|| view! { <SimpleCounter initial_value=10 step=1/> },
);
@@ -30,59 +32,63 @@ fn clear() {
// now let's click the `clear` button
clear.click();
// the reactive system is built on top of the async system, so changes are not reflected
// synchronously in the DOM
// in order to detect the changes here, we'll just yield for a brief time after each change,
// allowing the effects that update the view to run
tick().await;
// now let's test the <div> against the expected value
// we can do this by testing its `outerHTML`
let runtime = create_runtime();
assert_eq!(
div.outer_html(),
// here we spawn a mini reactive system, just to render the
// test case
{
// it's as if we're creating it with a value of 0, right?
let (value, _set_value) = create_signal(0);
assert_eq!(div.outer_html(), {
// it's as if we're creating it with a value of 0, right?
let (value, _set_value) = signal(0);
// we can remove the event listeners because they're not rendered to HTML
view! {
<div>
<button>"Clear"</button>
<button>"-1"</button>
<span>"Value: " {value} "!"</span>
<button>"+1"</button>
</div>
}
// the view returned an HtmlElement<Div>, which is a smart pointer for
// a DOM element. So we can still just call .outer_html()
.outer_html()
// we can remove the event listeners because they're not rendered to HTML
view! {
<div>
<button>"Clear"</button>
<button>"-1"</button>
<span>"Value: " {value} "!"</span>
<button>"+1"</button>
</div>
}
);
// Leptos supports multiple backend renderers for HTML elements
// .into_view() here is just a convenient way of specifying "use the regular DOM renderer"
.into_view()
// views are lazy -- they describe a DOM tree but don't create it yet
// calling .build() will actually build the DOM elements
.build()
// .build() returned an ElementState, which is a smart pointer for
// a DOM element. So we can still just call .outer_html(), which access the outerHTML on
// the actual DOM element
.outer_html()
});
// There's actually an easier way to do this...
// We can just test against a <SimpleCounter/> with the initial value 0
assert_eq!(test_wrapper.inner_html(), {
let comparison_wrapper = document.create_element("section").unwrap();
leptos::mount_to(
let _dispose = mount_to(
comparison_wrapper.clone().unchecked_into(),
|| view! { <SimpleCounter initial_value=0 step=1/>},
);
comparison_wrapper.inner_html()
});
runtime.dispose();
}
#[wasm_bindgen_test]
fn inc() {
let document = leptos::document();
async fn inc() {
let document = document();
let test_wrapper = document.create_element("section").unwrap();
let _ = document.body().unwrap().append_child(&test_wrapper);
mount_to(
let _dispose = mount_to(
test_wrapper.clone().unchecked_into(),
|| view! { <SimpleCounter initial_value=0 step=1/> },
);
// You can do testing with vanilla DOM operations
let _document = leptos::document();
let div = test_wrapper.query_selector("div").unwrap().unwrap();
let clear = div
.first_child()
@@ -108,6 +114,8 @@ fn inc() {
inc.click();
inc.click();
tick().await;
assert_eq!(text.text_content(), Some("Value: 2!".to_string()));
dec.click();
@@ -115,19 +123,21 @@ fn inc() {
dec.click();
dec.click();
tick().await;
assert_eq!(text.text_content(), Some("Value: -2!".to_string()));
clear.click();
assert_eq!(text.text_content(), Some("Value: 0!".to_string()));
tick().await;
let runtime = create_runtime();
assert_eq!(text.text_content(), Some("Value: 0!".to_string()));
// Or you can test against a sample view!
assert_eq!(
div.outer_html(),
{
let (value, _) = create_signal(0);
let (value, _) = signal(0);
view! {
<div>
<button>"Clear"</button>
@@ -137,16 +147,20 @@ fn inc() {
</div>
}
}
.into_view()
.build()
.outer_html()
);
inc.click();
tick().await;
assert_eq!(
div.outer_html(),
{
// because we've clicked, it's as if the signal is starting at 1
let (value, _) = create_signal(1);
let (value, _) = signal(1);
view! {
<div>
<button>"Clear"</button>
@@ -156,8 +170,8 @@ fn inc() {
</div>
}
}
.into_view()
.build()
.outer_html()
);
runtime.dispose();
}

View File

@@ -20,7 +20,6 @@ futures = "0.3"
lazy_static = "1"
leptos = { path = "../../leptos" }
leptos_actix = { path = "../../integrations/actix", optional = true }
leptos_meta = { path = "../../meta" }
leptos_router = { path = "../../router" }
log = "0.4"
once_cell = "1.18"
@@ -29,16 +28,16 @@ wasm-bindgen = "0.2"
serde = { version = "1", features = ["derive"] }
simple_logger = "4.3"
tracing = { version = "0.1", optional = true }
send_wrapper = "0.6.0"
[features]
hydrate = ["leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate"]
hydrate = ["leptos/hydrate"]
ssr = [
"dep:actix-files",
"dep:actix-web",
"dep:tracing",
"leptos/ssr",
"leptos_actix",
"leptos_meta/ssr",
"leptos_router/ssr",
]

View File

@@ -1,13 +1,14 @@
use leptos::*;
use leptos_meta::*;
use leptos_router::*;
use leptos::{prelude::*, reactive_graph::actions::Action};
use leptos_router::{
components::{FlatRoutes, Route, Router, A},
StaticSegment,
};
#[cfg(feature = "ssr")]
use tracing::instrument;
#[cfg(feature = "ssr")]
pub mod ssr_imports {
pub use broadcaster::BroadcastChannel;
pub use once_cell::sync::OnceCell;
pub use std::sync::atomic::{AtomicI32, Ordering};
pub static COUNT: AtomicI32 = AtomicI32::new(0);
@@ -15,14 +16,6 @@ pub mod ssr_imports {
lazy_static::lazy_static! {
pub static ref COUNT_CHANNEL: BroadcastChannel<i32> = BroadcastChannel::new();
}
static LOG_INIT: OnceCell<()> = OnceCell::new();
pub fn init_logging() {
LOG_INIT.get_or_init(|| {
simple_logger::SimpleLogger::new().env().init().unwrap();
});
}
}
#[server]
@@ -59,10 +52,6 @@ pub async fn clear_server_count() -> Result<i32, ServerFnError> {
}
#[component]
pub fn Counters() -> impl IntoView {
#[cfg(feature = "ssr")]
ssr_imports::init_logging();
provide_meta_context();
view! {
<Router>
<header>
@@ -85,28 +74,21 @@ pub fn Counters() -> impl IntoView {
</li>
</ul>
</nav>
<Link rel="shortcut icon" type_="image/ico" href="/favicon.ico"/>
<main>
<Routes>
<FlatRoutes fallback=|| "Not found.">
<Route
path=""
view=|| {
view! { <Counter/> }
}
path=StaticSegment("")
view=Counter
/>
<Route
path="form"
view=|| {
view! { <FormCounter/> }
}
path=StaticSegment("form")
view=FormCounter
/>
<Route
path="multi"
view=|| {
view! { <MultiuserCounter/> }
}
path=StaticSegment("multi")
view=MultiuserCounter
/>
</Routes>
</FlatRoutes>
</main>
</Router>
}
@@ -118,10 +100,10 @@ pub fn Counters() -> impl IntoView {
// This is the typical pattern for a CRUD app
#[component]
pub fn Counter() -> impl IntoView {
let dec = create_action(|_: &()| adjust_server_count(-1, "decing".into()));
let inc = create_action(|_: &()| adjust_server_count(1, "incing".into()));
let clear = create_action(|_: &()| clear_server_count());
let counter = create_resource(
let dec = Action::new(|_: &()| adjust_server_count(-1, "decing".into()));
let inc = Action::new(|_: &()| adjust_server_count(1, "incing".into()));
let clear = Action::new(|_: &()| clear_server_count());
let counter = Resource::new(
move || {
(
dec.version().get(),
@@ -138,27 +120,17 @@ pub fn Counter() -> impl IntoView {
<p>
"This counter sets the value on the server and automatically reloads the new value."
</p>
<div>
<button on:click=move |_| clear.dispatch(())>"Clear"</button>
<button on:click=move |_| dec.dispatch(())>"-1"</button>
<span>
"Value: "
<Suspense>
{move || counter.and_then(|count| *count)} "!"
</Suspense>
</span>
<button on:click=move |_| inc.dispatch(())>"+1"</button>
</div>
<Suspense>
{move || {
counter.get().and_then(|res| match res {
Ok(_) => None,
Err(e) => Some(e),
}).map(|msg| {
view! { <p>"Error: " {msg.to_string()}</p> }
})
}}
</Suspense>
<ErrorBoundary fallback=|errors| move || format!("Error: {:#?}", errors.get())>
<div>
<button on:click=move |_| clear.dispatch(())>"Clear"</button>
<button on:click=move |_| dec.dispatch(())>"-1"</button>
<span>
"Value: "
<Suspense>{counter} "!" </Suspense>
</span>
<button on:click=move |_| inc.dispatch(())>"+1"</button>
</div>
</ErrorBoundary>
</div>
}
}
@@ -170,10 +142,10 @@ pub fn Counter() -> impl IntoView {
pub fn FormCounter() -> impl IntoView {
// these struct names are auto-generated by #[server]
// they are just the PascalCased versions of the function names
let adjust = create_server_action::<AdjustServerCount>();
let clear = create_server_action::<ClearServerCount>();
let adjust = ServerAction::<AdjustServerCount>::new();
let clear = ServerAction::<ClearServerCount>::new();
let counter = create_resource(
let counter = Resource::new(
move || (adjust.version().get(), clear.version().get()),
|_| {
log::debug!("FormCounter running fetcher");
@@ -204,7 +176,7 @@ pub fn FormCounter() -> impl IntoView {
<input type="hidden" name="msg" value="form value down"/>
<input type="submit" value="-1"/>
</ActionForm>
<span>"Value: " <Suspense>{move || value().to_string()} "!"</Suspense></span>
<span>"Value: " <Suspense>{value} "!"</Suspense></span>
<ActionForm action=adjust>
<input type="hidden" name="delta" value="1"/>
<input type="hidden" name="msg" value="form value up"/>
@@ -222,19 +194,21 @@ pub fn FormCounter() -> impl IntoView {
#[component]
pub fn MultiuserCounter() -> impl IntoView {
let dec =
create_action(|_: &()| adjust_server_count(-1, "dec dec goose".into()));
Action::new(|_: &()| adjust_server_count(-1, "dec dec goose".into()));
let inc =
create_action(|_: &()| adjust_server_count(1, "inc inc moose".into()));
let clear = create_action(|_: &()| clear_server_count());
Action::new(|_: &()| adjust_server_count(1, "inc inc moose".into()));
let clear = Action::new(|_: &()| clear_server_count());
#[cfg(not(feature = "ssr"))]
let multiplayer_value = {
use futures::StreamExt;
use send_wrapper::SendWrapper;
let mut source =
let mut source = SendWrapper::new(
gloo_net::eventsource::futures::EventSource::new("/api/events")
.expect("couldn't connect to SSE stream");
let s = create_signal_from_stream(
.expect("couldn't connect to SSE stream"),
);
let s = ReadSignal::from_stream_unsync(
source
.subscribe("message")
.unwrap()
@@ -248,12 +222,12 @@ pub fn MultiuserCounter() -> impl IntoView {
}),
);
on_cleanup(move || source.close());
on_cleanup(move || source.take().close());
s
};
#[cfg(feature = "ssr")]
let (multiplayer_value, _) = create_signal(None::<i32>);
let (multiplayer_value, _) = signal(None::<i32>);
view! {
<div>

View File

@@ -3,11 +3,10 @@ pub mod counters;
#[cfg(feature = "hydrate")]
#[wasm_bindgen::prelude::wasm_bindgen]
pub fn hydrate() {
use crate::counters::*;
use leptos::*;
use crate::counters::Counters;
_ = console_log::init_with_level(log::Level::Debug);
console_error_panic_hook::set_once();
mount_to_body(Counters);
leptos::mount::hydrate_body(Counters);
}

View File

@@ -3,7 +3,6 @@ mod counters;
use crate::counters::*;
use actix_files::Files;
use actix_web::*;
use leptos::*;
use leptos_actix::{generate_route_list, LeptosRoutes};
#[get("/api/events")]
@@ -27,26 +26,44 @@ async fn counter_events() -> impl Responder {
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use leptos::prelude::*;
// Setting this to None means we'll be using cargo-leptos and its env vars.
// when not using cargo-leptos None must be replaced with Some("Cargo.toml")
let conf = get_configuration(None).await.unwrap();
let conf = get_configuration(None).unwrap();
let addr = conf.leptos_options.site_addr;
let routes = generate_route_list(Counters);
println!("listening on http://{}", &addr);
HttpServer::new(move || {
// Generate the list of routes in your Leptos App
let routes = generate_route_list(Counters);
let leptos_options = &conf.leptos_options;
let site_root = &leptos_options.site_root;
App::new()
.service(counter_events)
.leptos_routes(
leptos_options.to_owned(),
routes.to_owned(),
Counters,
)
.leptos_routes(routes, {
let leptos_options = leptos_options.clone();
move || {
view! {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta
name="viewport"
content="width=device-width, initial-scale=1"
/>
<AutoReload options=leptos_options.clone()/>
<HydrationScripts options=leptos_options.clone()/>
</head>
<body>
<Counters/>
</body>
</html>
}
}})
.service(Files::new("/", site_root))
//.wrap(middleware::Compress::default())
})
.bind(&addr)?
.run()

View File

@@ -9,9 +9,7 @@ lto = true
[dependencies]
leptos = { path = "../../leptos", features = ["csr"] }
leptos_router = { path = "../../router", features = ["csr"] }
console_log = "1"
log = "0.4"
leptos_router = { path = "../../router", features = [] }
console_error_panic_hook = "0.1.7"
[dev-dependencies]

View File

@@ -1,17 +1,17 @@
use leptos::*;
use leptos_router::*;
use leptos::prelude::*;
use leptos_router::hooks::query_signal;
/// A simple counter component.
///
/// You can use doc comments like this to document your component.
#[component]
pub fn SimpleQueryCounter() -> impl IntoView {
let (count, set_count) = create_query_signal::<i32>("count");
let (count, set_count) = query_signal::<i32>("count");
let clear = move |_| set_count.set(None);
let decrement = move |_| set_count.set(Some(count.get().unwrap_or(0) - 1));
let increment = move |_| set_count.set(Some(count.get().unwrap_or(0) + 1));
let (msg, set_msg) = create_query_signal::<String>("message");
let (msg, set_msg) = query_signal::<String>("message");
let update_msg = move |ev| {
let new_msg = event_target_value(&ev);
if new_msg.is_empty() {

View File

@@ -1,16 +1,13 @@
use counter_url_query::SimpleQueryCounter;
use leptos::*;
use leptos_router::*;
use leptos::prelude::*;
use leptos_router::components::Router;
pub fn main() {
_ = console_log::init_with_level(log::Level::Debug);
console_error_panic_hook::set_once();
mount_to_body(|| {
leptos::mount::mount_to_body(|| {
view! {
<Router>
<Routes>
<Route path="" view=SimpleQueryCounter />
</Routes>
<SimpleQueryCounter/>
</Router>
}
})

View File

@@ -10,8 +10,6 @@ lto = true
[dependencies]
leptos = { path = "../../leptos", features = ["csr"] }
console_log = "1"
log = "0.4"
console_error_panic_hook = "0.1.7"
[dev-dependencies]

View File

@@ -1,9 +1,16 @@
use leptos::{html::*, *};
use leptos::{
ev,
html::{button, div, span},
prelude::*,
};
/// A simple counter view.
// A component is really just a function call: it runs once to create the DOM and reactive system
pub fn counter(initial_value: i32, step: u32) -> impl IntoView {
let count = RwSignal::new(Count::new(initial_value, step));
Effect::new(move |_| {
leptos::logging::log!("count = {:?}", count.get());
});
// the function name is the same as the HTML tag name
div()
@@ -44,6 +51,7 @@ impl Count {
}
pub fn value(&self) -> i32 {
leptos::logging::log!("value = {}", self.value);
self.value
}

View File

@@ -1,9 +1,7 @@
use counter_without_macros::counter;
use leptos::*;
/// Show the counter
pub fn main() {
_ = console_log::init_with_level(log::Level::Debug);
console_error_panic_hook::set_once();
mount_to_body(|| counter(0, 1))
leptos::mount::mount_to_body(|| counter(0, 1))
}

View File

@@ -1,5 +1,5 @@
use counter_without_macros::counter;
use leptos::*;
use leptos::{prelude::*, spawn::tick};
use pretty_assertions::assert_eq;
use wasm_bindgen::JsCast;
use wasm_bindgen_test::*;
@@ -8,27 +8,32 @@ use web_sys::HtmlElement;
wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
fn should_increment_counter() {
async fn should_increment_counter() {
open_counter();
click_increment();
click_increment();
// reactive changes run asynchronously, so yield briefly before observing the DOM
tick().await;
assert_eq!(see_text(), Some("Value: 2!".to_string()));
}
#[wasm_bindgen_test]
fn should_decrement_counter() {
async fn should_decrement_counter() {
open_counter();
click_decrement();
click_decrement();
tick().await;
assert_eq!(see_text(), Some("Value: -2!".to_string()));
}
#[wasm_bindgen_test]
fn should_clear_counter() {
async fn should_clear_counter() {
open_counter();
click_increment();
@@ -36,18 +41,18 @@ fn should_clear_counter() {
click_clear();
tick().await;
assert_eq!(see_text(), Some("Value: 0!".to_string()));
}
fn open_counter() {
remove_existing_counter();
mount_to_body(move || counter(0, 1));
leptos::mount::mount_to_body(move || counter(0, 1));
}
fn remove_existing_counter() {
if let Some(counter) =
leptos::document().query_selector("body div").unwrap()
{
if let Some(counter) = document().query_selector("body div").unwrap() {
counter.remove();
}
}
@@ -74,7 +79,7 @@ fn see_text() -> Option<String> {
fn find_by_text(text: &str) -> HtmlElement {
let xpath = format!("//*[text()='{}']", text);
let document = leptos::document();
let document = document();
document
.evaluate(&xpath, &document)
.unwrap()

View File

@@ -5,8 +5,6 @@ edition = "2021"
[dependencies]
leptos = { path = "../../leptos", features = ["csr"] }
log = "0.4"
console_log = "1"
console_error_panic_hook = "0.1.7"
[dev-dependencies]

View File

@@ -1,8 +1,8 @@
use leptos::*;
use leptos::prelude::{signal::*, *};
const MANY_COUNTERS: usize = 1000;
type CounterHolder = Vec<(usize, (ReadSignal<i32>, WriteSignal<i32>))>;
type CounterHolder = Vec<(usize, ArcRwSignal<i32>)>;
#[derive(Copy, Clone)]
struct CounterUpdater {
@@ -11,13 +11,13 @@ struct CounterUpdater {
#[component]
pub fn Counters() -> impl IntoView {
let (next_counter_id, set_next_counter_id) = create_signal(0);
let (counters, set_counters) = create_signal::<CounterHolder>(vec![]);
let (next_counter_id, set_next_counter_id) = signal(0);
let (counters, set_counters) = signal::<CounterHolder>(vec![]);
provide_context(CounterUpdater { set_counters });
let add_counter = move |_| {
let id = next_counter_id.get();
let sig = create_signal(0);
let sig = ArcRwSignal::new(0);
set_counters.update(move |counters| counters.push((id, sig)));
set_next_counter_id.update(|id| *id += 1);
};
@@ -25,7 +25,7 @@ pub fn Counters() -> impl IntoView {
let add_many_counters = move |_| {
let next_id = next_counter_id.get();
let new_counters = (next_id..next_id + MANY_COUNTERS).map(|id| {
let signal = create_signal(0);
let signal = ArcRwSignal::new(0);
(id, signal)
});
@@ -39,78 +39,55 @@ pub fn Counters() -> impl IntoView {
view! {
<div>
<button on:click=add_counter>
"Add Counter"
</button>
<button on:click=add_many_counters>
{format!("Add {MANY_COUNTERS} Counters")}
</button>
<button on:click=clear_counters>
"Clear Counters"
</button>
<button on:click=add_counter>"Add Counter"</button>
<button on:click=add_many_counters>{format!("Add {MANY_COUNTERS} Counters")}</button>
<button on:click=clear_counters>"Clear Counters"</button>
<p>
"Total: "
<span>{move ||
counters.get()
.iter()
.map(|(_, (count, _))| count.get())
.sum::<i32>()
.to_string()
}</span>
" from "
<span>{move || counters.get().len().to_string()}</span>
<span>
{move || {
counters.get().iter().map(|(_, count)| count.get()).sum::<i32>().to_string()
}}
</span> " from " <span>{move || counters.get().len().to_string()}</span>
" counters."
</p>
<ul>
<For
each=move || counters.get()
key=|counter| counter.0
children=move |(id, (value, set_value)): (usize, (ReadSignal<i32>, WriteSignal<i32>))| {
view! {
<Counter id value set_value/>
}
children=move |(id, value)| {
view! { <Counter id value/> }
}
/>
</ul>
</div>
}
}
#[component]
fn Counter(
id: usize,
value: ReadSignal<i32>,
set_value: WriteSignal<i32>,
) -> impl IntoView {
fn Counter(id: usize, value: ArcRwSignal<i32>) -> impl IntoView {
let value = RwSignal::from(value);
let CounterUpdater { set_counters } = use_context().unwrap();
let input = move |ev| {
set_value
.set(event_target_value(&ev).parse::<i32>().unwrap_or_default())
};
// this will run when the scope is disposed, i.e., when this row is deleted
// because the signal was created in the parent scope, it won't be disposed
// of until the parent scope is. but we no longer need it, so we'll dispose of
// it when this row is deleted, instead. if we don't dispose of it here,
// this memory will "leak," i.e., the signal will continue to exist until the
// parent component is removed. in the case of this component, where it's the
// root, that's the lifetime of the program.
on_cleanup(move || {
log::debug!("deleted a row");
value.dispose();
});
view! {
<li>
<button on:click=move |_| set_value.update(move |value| *value -= 1)>"-1"</button>
<input type="text"
prop:value={value}
on:input=input
<button on:click=move |_| value.update(move |value| *value -= 1)>"-1"</button>
<input
type="text"
prop:value=value
on:input:target=move |ev| {
value.set(ev.target().value().parse::<i32>().unwrap_or_default())
}
/>
<span>{value}</span>
<button on:click=move |_| set_value.update(move |value| *value += 1)>"+1"</button>
<button on:click=move |_| set_counters.update(move |counters| counters.retain(|(counter_id, _)| counter_id != &id))>"x"</button>
<button on:click=move |_| value.update(move |value| *value += 1)>"+1"</button>
<button on:click=move |_| {
set_counters
.update(move |counters| counters.retain(|(counter_id, _)| counter_id != &id))
}>"x"</button>
</li>
}
}

View File

@@ -1,8 +1,6 @@
use counters::Counters;
use leptos::*;
fn main() {
_ = console_log::init_with_level(log::Level::Debug);
console_error_panic_hook::set_once();
mount_to_body(|| view! { <Counters/> })
leptos::mount::mount_to_body(Counters)
}

View File

@@ -3,14 +3,15 @@ use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
use counters::Counters;
use leptos::*;
use leptos::prelude::*;
use leptos::spawn::tick;
use web_sys::HtmlElement;
#[wasm_bindgen_test]
fn inc() {
mount_to_body(|| view! { <Counters/> });
async fn inc() {
mount_to_body(Counters);
let document = leptos::document();
let document = document();
let div = document.query_selector("div").unwrap().unwrap();
let add_counter = div
.first_child()
@@ -18,31 +19,33 @@ fn inc() {
.dyn_into::<HtmlElement>()
.unwrap();
assert_eq!(
div.inner_html(),
"<button>Add Counter</button><button>Add 1000 \
Counters</button><button>Clear Counters</button><p>Total: \
<span>0</span> from <span>0</span> counters.</p><ul><!----></ul>"
);
// add 3 counters
add_counter.click();
add_counter.click();
add_counter.click();
tick().await;
// check HTML
assert_eq!(
div.inner_html(),
"<button>Add Counter</button><button>Add 1000 \
Counters</button><button>Clear Counters</button><p>Total: <span><!-- \
<DynChild> -->0<!-- </DynChild> --></span> from <span><!-- \
<DynChild> -->3<!-- </DynChild> --></span> counters.</p><ul><!-- \
<Each> --><!-- <EachItem> --><!-- <Counter> \
--><li><button>-1</button><input type=\"text\"><span><!-- <DynChild> \
-->0<!-- </DynChild> \
--></span><button>+1</button><button>x</button></li><!-- </Counter> \
--><!-- </EachItem> --><!-- <EachItem> --><!-- <Counter> \
--><li><button>-1</button><input type=\"text\"><span><!-- <DynChild> \
-->0<!-- </DynChild> \
--></span><button>+1</button><button>x</button></li><!-- </Counter> \
--><!-- </EachItem> --><!-- <EachItem> --><!-- <Counter> \
--><li><button>-1</button><input type=\"text\"><span><!-- <DynChild> \
-->0<!-- </DynChild> \
--></span><button>+1</button><button>x</button></li><!-- </Counter> \
--><!-- </EachItem> --><!-- </Each> --></ul>"
Counters</button><button>Clear Counters</button><p>Total: \
<span>0</span> from <span>3</span> \
counters.</p><ul><li><button>-1</button><input \
type=\"text\"><span>0</span><button>+1</button><button>x</button></\
li><li><button>-1</button><input \
type=\"text\"><span>0</span><button>+1</button><button>x</button></\
li><li><button>-1</button><input \
type=\"text\"><span>0</span><button>+1</button><button>x</button></\
li><!----></ul>"
);
let counters = div
@@ -71,25 +74,20 @@ fn inc() {
}
}
tick().await;
assert_eq!(
div.inner_html(),
"<button>Add Counter</button><button>Add 1000 \
Counters</button><button>Clear Counters</button><p>Total: <span><!-- \
<DynChild> -->6<!-- </DynChild> --></span> from <span><!-- \
<DynChild> -->3<!-- </DynChild> --></span> counters.</p><ul><!-- \
<Each> --><!-- <EachItem> --><!-- <Counter> \
--><li><button>-1</button><input type=\"text\"><span><!-- <DynChild> \
-->1<!-- </DynChild> \
--></span><button>+1</button><button>x</button></li><!-- </Counter> \
--><!-- </EachItem> --><!-- <EachItem> --><!-- <Counter> \
--><li><button>-1</button><input type=\"text\"><span><!-- <DynChild> \
-->2<!-- </DynChild> \
--></span><button>+1</button><button>x</button></li><!-- </Counter> \
--><!-- </EachItem> --><!-- <EachItem> --><!-- <Counter> \
--><li><button>-1</button><input type=\"text\"><span><!-- <DynChild> \
-->3<!-- </DynChild> \
--></span><button>+1</button><button>x</button></li><!-- </Counter> \
--><!-- </EachItem> --><!-- </Each> --></ul>"
Counters</button><button>Clear Counters</button><p>Total: \
<span>6</span> from <span>3</span> \
counters.</p><ul><li><button>-1</button><input \
type=\"text\"><span>1</span><button>+1</button><button>x</button></\
li><li><button>-1</button><input \
type=\"text\"><span>2</span><button>+1</button><button>x</button></\
li><li><button>-1</button><input \
type=\"text\"><span>3</span><button>+1</button><button>x</button></\
li><!----></ul>"
);
// remove the first counter
@@ -101,20 +99,17 @@ fn inc() {
.unchecked_into::<HtmlElement>()
.click();
tick().await;
assert_eq!(
div.inner_html(),
"<button>Add Counter</button><button>Add 1000 \
Counters</button><button>Clear Counters</button><p>Total: <span><!-- \
<DynChild> -->5<!-- </DynChild> --></span> from <span><!-- \
<DynChild> -->2<!-- </DynChild> --></span> counters.</p><ul><!-- \
<Each> --><!-- <EachItem> --><!-- <EachItem> --><!-- <Counter> \
--><li><button>-1</button><input type=\"text\"><span><!-- <DynChild> \
-->2<!-- </DynChild> \
--></span><button>+1</button><button>x</button></li><!-- </Counter> \
--><!-- </EachItem> --><!-- <EachItem> --><!-- <Counter> \
--><li><button>-1</button><input type=\"text\"><span><!-- <DynChild> \
-->3<!-- </DynChild> \
--></span><button>+1</button><button>x</button></li><!-- </Counter> \
--><!-- </EachItem> --><!-- </Each> --></ul>"
Counters</button><button>Clear Counters</button><p>Total: \
<span>5</span> from <span>2</span> \
counters.</p><ul><li><button>-1</button><input \
type=\"text\"><span>2</span><button>+1</button><button>x</button></\
li><li><button>-1</button><input \
type=\"text\"><span>3</span><button>+1</button><button>x</button></\
li><!----></ul>"
);
}

View File

@@ -13,5 +13,4 @@ web-sys = { version = "0.3", features = ["Clipboard", "Navigator"] }
[dev-dependencies]
wasm-bindgen-test = "0.3.0"
wasm-bindgen = "0.2"
web-sys = "0.3"
gloo-timers = { version = "0.3", features = ["futures"] }
web-sys = { version = "0.3", features = ["NodeList"] }

View File

@@ -1,25 +1,26 @@
use leptos::{ev::click, html::AnyElement, *};
use leptos::{ev::click, prelude::*};
use web_sys::Element;
// no extra parameter
pub fn highlight(el: HtmlElement<AnyElement>) {
pub fn highlight(el: Element) {
let mut highlighted = false;
let _ = el.clone().on(click, move |_| {
let handle = el.clone().on(click, move |_| {
highlighted = !highlighted;
if highlighted {
let _ = el.clone().style("background-color", "yellow");
el.style(("background-color", "yellow"));
} else {
let _ = el.clone().style("background-color", "transparent");
el.style(("background-color", "transparent"));
}
});
on_cleanup(move || drop(handle));
}
// one extra parameter
pub fn copy_to_clipboard(el: HtmlElement<AnyElement>, content: &str) {
let content = content.to_string();
let _ = el.clone().on(click, move |evt| {
pub fn copy_to_clipboard(el: Element, content: &str) {
let content = content.to_owned();
let handle = el.clone().on(click, move |evt| {
evt.prevent_default();
evt.stop_propagation();
@@ -29,8 +30,9 @@ pub fn copy_to_clipboard(el: HtmlElement<AnyElement>, content: &str) {
.expect("navigator.clipboard to be available")
.write_text(&content);
let _ = el.clone().inner_html(format!("Copied \"{}\"", &content));
el.set_inner_html(&format!("Copied \"{}\"", &content));
});
on_cleanup(move || drop(handle));
}
// custom parameter
@@ -52,14 +54,18 @@ impl From<()> for Amount {
}
// .into() will automatically be called on the parameter
pub fn add_dot(el: HtmlElement<AnyElement>, amount: Amount) {
_ = el.clone().on(click, move |_| {
pub fn add_dot(el: Element, amount: Amount) {
use leptos::wasm_bindgen::JsCast;
let el = el.unchecked_into::<web_sys::HtmlElement>();
let handle = el.clone().on(click, move |_| {
el.set_inner_text(&format!(
"{}{}",
el.inner_text(),
".".repeat(amount.0)
))
})
});
on_cleanup(move || drop(handle));
}
#[component]

View File

@@ -1,5 +1,5 @@
use directives::App;
use leptos::*;
use leptos::prelude::*;
fn main() {
_ = console_log::init_with_level(log::Level::Debug);

View File

@@ -1,19 +1,16 @@
use gloo_timers::future::sleep;
use std::time::Duration;
use directives::App;
use leptos::{prelude::*, spawn::tick};
use wasm_bindgen::JsCast;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
use directives::App;
use leptos::*;
use web_sys::HtmlElement;
wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
async fn test_directives() {
mount_to_body(|| view! { <App/> });
sleep(Duration::ZERO).await;
leptos::mount::mount_to_body(App);
tick().await;
let document = leptos::document();
let document = document();
let paragraphs = document.query_selector_all("p").unwrap();
assert_eq!(paragraphs.length(), 3);

View File

@@ -8,7 +8,7 @@ test.describe("Clear Number", () => {
await ui.clearInput();
await expect(ui.errorMessage).toHaveText("Not a number! Errors: ");
await expect(ui.errorMessage).toHaveText("Not an integer! Errors: ");
});
test("should see the error list", async ({ page }) => {
const ui = new HomePage(page);

View File

@@ -14,7 +14,7 @@ export class HomePage {
this.pageTitle = page.locator("h1");
this.numberInput = page.getByLabel(
"Type a number (or something that's not a number!)"
"Type an integer (or something that's not an integer!)"
);
this.successMessage = page.locator("label p");
this.errorMessage = page.locator("div p");

View File

@@ -1,38 +1,44 @@
use leptos::*;
use leptos::prelude::*;
#[component]
pub fn App() -> impl IntoView {
let (value, set_value) = create_signal(Ok(0));
// when input changes, try to parse a number from the input
let on_input =
move |ev| set_value.set(event_target_value(&ev).parse::<i32>());
let (value, set_value) = signal("".parse::<i32>());
view! {
<h1>"Error Handling"</h1>
<label>
"Type a number (or something that's not a number!)"
<input type="number" on:input=on_input/>
// If an `Err(_) had been rendered inside the <ErrorBoundary/>,
"Type an integer (or something that's not an integer!)"
<input
type="number"
value=move || value.get().unwrap_or_default()
// when input changes, try to parse a number from the input
on:input:target=move |ev| set_value.set(ev.target().value().parse::<i32>())
/>
// If an `Err(_) has been rendered inside the <ErrorBoundary/>,
// the fallback will be displayed. Otherwise, the children of the
// <ErrorBoundary/> will be displayed.
<ErrorBoundary
// the fallback receives a signal containing current errors
fallback=|errors| view! {
// the fallback receives a signal containing current errors
<ErrorBoundary fallback=|errors| {
let errors = errors.clone();
view! {
<div class="error">
<p>"Not a number! Errors: "</p>
<p>"Not an integer! Errors: "</p>
// we can render a list of errors
// as strings, if we'd like
<ul>
{move || errors.get()
.into_iter()
.map(|(_, e)| view! { <li>{e.to_string()}</li>})
.collect_view()
}
{move || {
errors
.read()
.iter()
.map(|(_, e)| view! { <li>{e.to_string()}</li> })
.collect::<Vec<_>>()
}}
</ul>
</div>
}
>
}>
<p>
"You entered "
// because `value` is `Result<i32, _>`,

View File

@@ -1,12 +1,8 @@
use error_boundary::*;
use leptos::*;
use leptos::prelude::*;
pub fn main() {
_ = console_log::init_with_level(log::Level::Debug);
console_error_panic_hook::set_once();
mount_to_body(|| {
view! {
<App/>
}
})
mount_to_body(App)
}

View File

@@ -7,15 +7,12 @@ edition = "2021"
crate-type = ["cdylib", "rlib"]
[dependencies]
console_log = "1.0"
console_error_panic_hook = "0.1"
leptos = { path = "../../leptos" }
leptos_axum = { path = "../../integrations/axum", optional = true }
leptos_meta = { path = "../../meta" }
leptos_router = { path = "../../router" }
log = "0.4"
serde = { version = "1", features = ["derive"] }
simple_logger = "4.0"
axum = { version = "0.7", optional = true }
tower = { version = "0.4", optional = true }
tower-http = { version = "0.5", features = ["fs"], optional = true }
@@ -25,7 +22,7 @@ thiserror = "1.0"
wasm-bindgen = "0.2"
[features]
hydrate = ["leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate"]
hydrate = ["leptos/hydrate"]
ssr = [
"dep:axum",
"dep:tower",

View File

@@ -1,5 +1,5 @@
use crate::errors::AppError;
use leptos::{logging::log, *};
use leptos::{logging::log, prelude::*};
#[cfg(feature = "ssr")]
use leptos_axum::ResponseOptions;
@@ -7,25 +7,18 @@ use leptos_axum::ResponseOptions;
// Feel free to do more complicated things here than just displaying them.
#[component]
pub fn ErrorTemplate(
#[prop(optional)] outside_errors: Option<Errors>,
#[prop(optional)] errors: Option<RwSignal<Errors>>,
#[prop(into)] errors: MaybeSignal<Errors>,
) -> impl IntoView {
let errors = match outside_errors {
Some(e) => create_rw_signal(e),
None => match errors {
Some(e) => e,
None => panic!("No Errors found and we expected errors!"),
},
};
// Get Errors from Signal
// Downcast lets us take a type that implements `std::error::Error`
let errors: Vec<AppError> = errors
.get_untracked()
.into_iter()
.filter_map(|(_, v)| v.downcast_ref::<AppError>().cloned())
.collect();
log!("Errors: {errors:#?}");
let errors = Memo::new(move |_| {
errors
.get_untracked()
.into_iter()
.filter_map(|(_, v)| v.downcast_ref::<AppError>().cloned())
.collect::<Vec<_>>()
});
log!("Errors: {:#?}", &*errors.read_untracked());
// Only the response code for the first error is actually sent from the server
// this may be customized by the specific application
@@ -33,26 +26,30 @@ pub fn ErrorTemplate(
{
let response = use_context::<ResponseOptions>();
if let Some(response) = response {
response.set_status(errors[0].status_code());
response.set_status(errors.read_untracked()[0].status_code());
}
}
view! {
<h1>{if errors.len() > 1 {"Errors"} else {"Error"}}</h1>
<For
// a function that returns the items we're iterating over; a signal is fine
each= move || {errors.clone().into_iter().enumerate()}
// a unique key for each item as a reference
key=|(index, _)| *index
// renders each item to a view
children=move |error| {
let error_string = error.1.to_string();
let error_code= error.1.status_code();
view! {
<h2>{error_code.to_string()}</h2>
<p>"Error: " {error_string}</p>
}
}
/>
<h1>{move || {
if errors.read().len() > 1 {
"Errors"
} else {
"Error"
}}}
</h1>
{move || {
errors.get()
.into_iter()
.map(|error| {
let error_string = error.to_string();
let error_code= error.status_code();
view! {
<h2>{error_code.to_string()}</h2>
<p>"Error: " {error_string}</p>
}
})
.collect_view()
}}
}
}

View File

@@ -1,7 +1,7 @@
use http::status::StatusCode;
use thiserror::Error;
#[derive(Debug, Clone, Error)]
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum AppError {
#[error("Not Found")]
NotFound,

View File

@@ -1,48 +0,0 @@
use crate::landing::App;
use axum::{
body::Body,
extract::State,
http::{Request, Response, StatusCode, Uri},
response::{IntoResponse, Response as AxumResponse},
};
use leptos::{view, LeptosOptions};
use tower::ServiceExt;
use tower_http::services::ServeDir;
pub async fn file_and_error_handler(
uri: Uri,
State(options): State<LeptosOptions>,
req: Request<Body>,
) -> AxumResponse {
let root = options.site_root.clone();
let res = get_static_file(uri.clone(), &root).await.unwrap();
if res.status() == StatusCode::OK {
res.into_response()
} else {
let handler = leptos_axum::render_app_to_stream(
options.to_owned(),
move || view! { <App/> },
);
handler(req).await.into_response()
}
}
async fn get_static_file(
uri: Uri,
root: &str,
) -> Result<Response<Body>, (StatusCode, String)> {
let req = Request::builder()
.uri(uri.clone())
.body(Body::empty())
.unwrap();
// `ServeDir` implements `tower::Service` so we can call it with `tower::ServiceExt::oneshot`
// This path is relative to the cargo root
match ServeDir::new(root).oneshot(req).await {
Ok(res) => Ok(res.into_response()),
Err(err) => Err((
StatusCode::INTERNAL_SERVER_ERROR,
format!("Something went wrong: {err}"),
)),
}
}

View File

@@ -1,7 +1,10 @@
use crate::{error_template::ErrorTemplate, errors::AppError};
use leptos::*;
use leptos::prelude::*;
use leptos_meta::*;
use leptos_router::*;
use leptos_router::{
components::{Route, Router, Routes},
StaticSegment,
};
#[server(CauseInternalServerError, "/api")]
pub async fn cause_internal_server_error() -> Result<(), ServerFnError> {
@@ -13,28 +16,44 @@ pub async fn cause_internal_server_error() -> Result<(), ServerFnError> {
))
}
pub fn shell(options: LeptosOptions) -> impl IntoView {
view! {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<AutoReload options=options.clone() />
<HydrationScripts options/>
<MetaTags/>
</head>
<body>
<App/>
</body>
</html>
}
}
#[component]
pub fn App() -> impl IntoView {
//let id = use_context::<String>();
provide_meta_context();
view! {
<Link rel="shortcut icon" type_="image/ico" href="/favicon.ico"/>
<Stylesheet id="leptos" href="/pkg/errors_axum.css"/>
<Router fallback=|| {
let mut outside_errors = Errors::default();
outside_errors.insert_with_default_key(AppError::NotFound);
view! {
<ErrorTemplate outside_errors/>
}
.into_view()
}>
<Router>
<header>
<h1>"Error Examples:"</h1>
</header>
<main>
<Routes>
<Route path="" view=ExampleErrors/>
<Routes fallback=|| {
let mut errors = Errors::default();
errors.insert_with_default_key(AppError::NotFound);
view! {
<ErrorTemplate errors/>
}
.into_view()
}>
<Route path=StaticSegment("") view=ExampleErrors/>
</Routes>
</main>
</Router>
@@ -44,7 +63,7 @@ pub fn App() -> impl IntoView {
#[component]
pub fn ExampleErrors() -> impl IntoView {
let generate_internal_error =
create_server_action::<CauseInternalServerError>();
ServerAction::<CauseInternalServerError>::new();
view! {
<p>
@@ -54,18 +73,18 @@ pub fn ExampleErrors() -> impl IntoView {
</p>
<p>
"After pressing this button check browser network tools. Can be used even when WASM is blocked:"
<ActionForm action=generate_internal_error>
<input name="error1" type="submit" value="Generate Internal Server Error"/>
</ActionForm>
</p>
<ActionForm action=generate_internal_error>
<input name="error1" type="submit" value="Generate Internal Server Error"/>
</ActionForm>
<p>"The following <div> will always contain an error and cause this page to produce status 500. Check browser dev tools. "</p>
<div>
// note that the error boundaries could be placed above in the Router or lower down
// in a particular route. The generated errors on the entire page contribute to the
// final status code sent by the server when producing ssr pages.
<ErrorBoundary fallback=|errors| view!{ <ErrorTemplate errors=errors/>}>
<ReturnsError/>
</ErrorBoundary>
// note that the error boundaries could be placed above in the Router or lower down
// in a particular route. The generated errors on the entire page contribute to the
// final status code sent by the server when producing ssr pages.
<ErrorBoundary fallback=|errors| view!{ <ErrorTemplate errors/>}>
<ReturnsError/>
</ErrorBoundary>
</div>
}
}

View File

@@ -1,21 +1,11 @@
pub mod error_template;
pub mod errors;
#[cfg(feature = "ssr")]
pub mod fallback;
pub mod landing;
use wasm_bindgen::prelude::wasm_bindgen;
#[cfg(feature = "hydrate")]
#[wasm_bindgen]
#[wasm_bindgen::prelude::wasm_bindgen]
pub fn hydrate() {
use crate::landing::*;
use leptos::*;
_ = console_log::init_with_level(log::Level::Debug);
use crate::landing::App;
console_error_panic_hook::set_once();
leptos::mount_to_body(|| {
view! { <App/> }
});
leptos::mount::hydrate_body(App);
}

View File

@@ -1,15 +1,17 @@
#[cfg(feature = "ssr")]
mod ssr_imports {
use axum::extract::State;
pub use axum::{
body::Body as AxumBody,
extract::{Path, State},
extract::Path,
http::Request,
response::{IntoResponse, Response},
routing::get,
Router,
};
pub use errors_axum::{fallback::*, landing::App};
pub use leptos::{logging::log, *};
use errors_axum::landing::shell;
pub use errors_axum::landing::App;
use leptos::{config::LeptosOptions, context::provide_context};
pub use leptos_axum::{generate_route_list, LeptosRoutes};
// This custom handler lets us provide Axum State via context
@@ -19,11 +21,10 @@ mod ssr_imports {
req: Request<AxumBody>,
) -> Response {
let handler = leptos_axum::render_app_to_stream_with_context(
options.clone(),
move || {
provide_context(id.clone());
},
App,
move || shell(options.clone()),
);
handler(req).await.into_response()
}
@@ -32,18 +33,12 @@ mod ssr_imports {
#[cfg(feature = "ssr")]
#[tokio::main]
async fn main() {
use errors_axum::landing::shell;
use leptos::config::get_configuration;
use ssr_imports::*;
simple_logger::init_with_level(log::Level::Debug)
.expect("couldn't initialize logging");
// Explicit server function registration is no longer required
// on the main branch. On 0.3.0 and earlier, uncomment the lines
// below to register the server functions.
// _ = CauseInternalServerError::register();
// Setting this to None means we'll be using cargo-leptos and its env vars
let conf = get_configuration(None).await.unwrap();
let conf = get_configuration(None).unwrap();
let leptos_options = conf.leptos_options;
let addr = leptos_options.site_addr;
let routes = generate_route_list(App);
@@ -51,13 +46,16 @@ async fn main() {
// build our application with a route
let app = Router::new()
.route("/special/:id", get(custom_handler))
.leptos_routes(&leptos_options, routes, App)
.fallback(file_and_error_handler)
.leptos_routes(&leptos_options, routes, {
let leptos_options = leptos_options.clone();
move || shell(leptos_options.clone())
})
.fallback(leptos_axum::file_and_error_handler(shell))
.with_state(leptos_options);
// run our app with hyper
// `axum::Server` is a re-export of `hyper::Server`
log!("listening on http://{}", &addr);
println!("listening on http://{}", &addr);
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
axum::serve(listener, app.into_make_service())
.await

View File

@@ -8,13 +8,17 @@ codegen-units = 1
lto = true
[dependencies]
leptos = { path = "../../leptos", features = ["csr"] }
leptos = { path = "../../leptos", features = ["csr", "tracing"] }
reqwasm = "0.5"
gloo-timers = { version = "0.3", features = ["futures"] }
serde = { version = "1", features = ["derive"] }
log = "0.4"
console_log = "1"
console_error_panic_hook = "0.1"
thiserror = "1"
tracing = "0.1"
tracing-subscriber = "0.3"
tracing-subscriber-wasm = "0.1"
[dev-dependencies]
wasm-bindgen-test = "0.3"

View File

@@ -1,4 +1,5 @@
use leptos::{error::Result, *};
use leptos::prelude::*;
use leptos::tachys::html::style::style;
use serde::{Deserialize, Serialize};
use thiserror::Error;
@@ -17,6 +18,7 @@ type CatCount = usize;
async fn fetch_cats(count: CatCount) -> Result<Vec<String>> {
if count > 0 {
gloo_timers::future::TimeoutFuture::new(1000).await;
// make the request
let res = reqwasm::http::Request::get(&format!(
"https://api.thecatapi.com/v1/images/search?limit={count}",
@@ -33,26 +35,26 @@ async fn fetch_cats(count: CatCount) -> Result<Vec<String>> {
.collect::<Vec<_>>();
Ok(res)
} else {
Err(CatError::NonZeroCats.into())
Err(CatError::NonZeroCats)?
}
}
pub fn fetch_example() -> impl IntoView {
let (cat_count, set_cat_count) = create_signal::<CatCount>(0);
let (cat_count, set_cat_count) = signal::<CatCount>(1);
// we use local_resource here because
// 1) our error type isn't serializable/deserializable
// 2) we're not doing server-side rendering in this example anyway
// (during SSR, create_resource will begin loading on the server and resolve on the client)
let cats = create_local_resource(move || cat_count.get(), fetch_cats);
// we use new_unsync here because the reqwasm request type isn't Send
// if we were doing SSR, then
// 1) we'd want to use a Resource, so the data would be serialized to the client
// 2) we'd need to make sure there was a thread-local spawner set up
let cats = AsyncDerived::new_unsync(move || fetch_cats(cat_count.get()));
let fallback = move |errors: RwSignal<Errors>| {
let fallback = move |errors: ArcRwSignal<Errors>| {
let error_list = move || {
errors.with(|errors| {
errors
.iter()
.map(|(_, e)| view! { <li>{e.to_string()}</li> })
.collect_view()
.collect::<Vec<_>>()
})
};
@@ -64,17 +66,7 @@ pub fn fetch_example() -> impl IntoView {
}
};
// the renderer can handle Option<_> and Result<_> states
// by displaying nothing for None if the resource is still loading
// and by using the ErrorBoundary fallback to catch Err(_)
// so we'll just use `.and_then()` to map over the happy path
let cats_view = move || {
cats.and_then(|data| {
data.iter()
.map(|s| view! { <p><img src={s}/></p> })
.collect_view()
})
};
let spreadable = style(("background-color", "AliceBlue"));
view! {
<div>
@@ -83,19 +75,32 @@ pub fn fetch_example() -> impl IntoView {
<input
type="number"
prop:value=move || cat_count.get().to_string()
on:input=move |ev| {
let val = event_target_value(&ev).parse::<CatCount>().unwrap_or(0);
on:input:target=move |ev| {
let val = ev.target().value().parse::<CatCount>().unwrap_or(0);
set_cat_count.set(val);
}
/>
</label>
<Transition fallback=move || {
view! { <div>"Loading (Suspense Fallback)..."</div> }
}>
<Transition fallback=|| view! { <div>"Loading..."</div> } {..spreadable}>
<ErrorBoundary fallback>
<div>
{cats_view}
</div>
<ul>
{move || Suspend(async move {
cats.await
.map(|cats| {
cats.iter()
.map(|s| {
view! {
<li>
<img src=s.clone()/>
</li>
}
})
.collect::<Vec<_>>()
})
})}
</ul>
</ErrorBoundary>
</Transition>
</div>

View File

@@ -1,8 +1,21 @@
use fetch::fetch_example;
use leptos::*;
use leptos::prelude::*;
pub fn main() {
_ = console_log::init_with_level(log::Level::Debug);
use tracing_subscriber::fmt;
use tracing_subscriber_wasm::MakeConsoleWriter;
fmt()
.with_writer(
// To avoide trace events in the browser from showing their
// JS backtrace, which is very annoying, in my opinion
MakeConsoleWriter::default()
.map_trace_level_to(tracing::Level::DEBUG),
)
// For some reason, if we don't do this in the browser, we get
// a runtime error.
.without_time()
.init();
console_error_panic_hook::set_once();
mount_to_body(fetch_example)
}

View File

@@ -5,4 +5,14 @@ edition = "2021"
[dependencies]
leptos = { path = "../../leptos", features = ["csr"] }
gtk = { version = "0.5.0", package = "gtk4" }
throw_error = { path = "../../any_error/" }
any_spawner = { path = "../../any_spawner/" }
next_tuple = { path = "../../next_tuple/" }
gtk = { version = "0.8.0", package = "gtk4", optional = true }
paste = "1.0.14"
console_error_panic_hook = { version = "0.1", optional = true }
[features]
gtk = ["dep:gtk", "any_spawner/glib"]
wasm = ["any_spawner/wasm-bindgen", "dep:console_error_panic_hook"]

8
examples/gtk/index.html Normal file
View File

@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<meta name="color-scheme" content="dark">
<link rel="css" href="style.css" data-trunk>
</head>
<body></body>
</html>

View File

@@ -0,0 +1,633 @@
use self::properties::Connect;
use gtk::{
ffi::GtkWidget,
glib::{
object::{IsA, IsClass, ObjectExt},
Object, Value,
},
prelude::{Cast, WidgetExt},
Label, Orientation, Widget,
};
use leptos::{
reactive_graph::effect::RenderEffect,
tachys::{
renderer::{CastFrom, Renderer},
view::{Mountable, Render},
},
};
use next_tuple::NextTuple;
use std::{borrow::Cow, marker::PhantomData};
#[derive(Debug)]
pub struct LeptosGtk;
#[derive(Debug, Clone)]
pub struct Element(pub Widget);
impl Element {
pub fn remove(&self) {
self.0.unparent();
}
}
#[derive(Debug, Clone)]
pub struct Text(pub Element);
impl<T> From<T> for Element
where
T: Into<Widget>,
{
fn from(value: T) -> Self {
Element(value.into())
}
}
impl Mountable<LeptosGtk> for Element {
fn unmount(&mut self) {
self.remove()
}
fn mount(
&mut self,
parent: &<LeptosGtk as Renderer>::Element,
marker: Option<&<LeptosGtk as Renderer>::Node>,
) {
self.0
.insert_before(&parent.0, marker.as_ref().map(|m| &m.0));
}
fn insert_before_this(&self,
child: &mut dyn Mountable<LeptosGtk>,
) -> bool {
child.mount(parent, Some(self.as_ref()));
true
}
}
impl Mountable<LeptosGtk> for Text {
fn unmount(&mut self) {
self.0.remove()
}
fn mount(
&mut self,
parent: &<LeptosGtk as Renderer>::Element,
marker: Option<&<LeptosGtk as Renderer>::Node>,
) {
self.0
.0
.insert_before(&parent.0, marker.as_ref().map(|m| &m.0));
}
fn insert_before_this(&self,
child: &mut dyn Mountable<LeptosGtk>,
) -> bool {
child.mount(parent, Some(self.as_ref()));
true
}
}
impl CastFrom<Element> for Element {
fn cast_from(source: Element) -> Option<Self> {
Some(source)
}
}
impl CastFrom<Element> for Text {
fn cast_from(source: Element) -> Option<Self> {
source
.0
.downcast::<Label>()
.ok()
.map(|n| Text(Element::from(n)))
}
}
impl AsRef<Element> for Element {
fn as_ref(&self) -> &Element {
self
}
}
impl AsRef<Element> for Text {
fn as_ref(&self) -> &Element {
&self.0
}
}
impl Renderer for LeptosGtk {
type Node = Element;
type Element = Element;
type Text = Text;
type Placeholder = Element;
fn intern(text: &str) -> &str {
text
}
fn create_text_node(text: &str) -> Self::Text {
Text(Element::from(Label::new(Some(text))))
}
fn create_placeholder() -> Self::Placeholder {
let label = Label::new(None);
label.set_visible(false);
Element::from(label)
}
fn set_text(node: &Self::Text, text: &str) {
let node_as_text = node.0 .0.downcast_ref::<Label>().unwrap();
node_as_text.set_label(text);
}
fn set_attribute(node: &Self::Element, name: &str, value: &str) {
node.0.set_property(name, value);
}
fn remove_attribute(node: &Self::Element, name: &str) {
node.0.set_property(name, None::<&str>);
}
fn insert_node(
parent: &Self::Element,
new_child: &Self::Node,
marker: Option<&Self::Node>,
) {
new_child
.0
.insert_before(&parent.0, marker.as_ref().map(|n| &n.0));
}
fn remove_node(
parent: &Self::Element,
child: &Self::Node,
) -> Option<Self::Node> {
todo!()
}
fn remove(node: &Self::Node) {
todo!()
}
fn get_parent(node: &Self::Node) -> Option<Self::Node> {
node.0.parent().map(Element::from)
}
fn first_child(node: &Self::Node) -> Option<Self::Node> {
todo!()
}
fn next_sibling(node: &Self::Node) -> Option<Self::Node> {
todo!()
}
fn log_node(node: &Self::Node) {
todo!()
}
fn clear_children(parent: &Self::Element) {
todo!()
}
}
pub fn root<Chil>(children: Chil) -> (Widget, impl Mountable<LeptosGtk>)
where
Chil: Render<LeptosGtk>,
{
let state = r#box()
.orientation(Orientation::Vertical)
.spacing(12)
.child(children)
.build();
(state.as_widget().clone(), state)
}
pub trait WidgetClass {
type Widget: Into<Widget> + IsA<Object> + IsClass;
}
pub struct LGtkWidget<Widg, Props, Chil> {
widget: PhantomData<Widg>,
properties: Props,
children: Chil,
}
impl<Widg, Props, Chil> LGtkWidget<Widg, Props, Chil>
where
Widg: WidgetClass,
Chil: NextTuple,
{
pub fn child<T>(
self,
child: T,
) -> LGtkWidget<Widg, Props, Chil::Output<T>> {
let LGtkWidget {
widget,
properties,
children,
} = self;
LGtkWidget {
widget,
properties,
children: children.next_tuple(child),
}
}
}
impl<Widg, Props, Chil> LGtkWidget<Widg, Props, Chil>
where
Widg: WidgetClass,
Props: NextTuple,
Chil: Render<LeptosGtk>,
{
pub fn connect<F>(
self,
signal_name: &'static str,
callback: F,
) -> LGtkWidget<Widg, Props::Output<Connect<F>>, Chil>
where
F: Fn(&[Value]) -> Option<Value> + Send + Sync + 'static,
{
let LGtkWidget {
widget,
properties,
children,
} = self;
LGtkWidget {
widget,
properties: properties.next_tuple(Connect {
signal_name,
callback,
}),
children,
}
}
}
pub struct LGtkWidgetState<Widg, Props, Chil>
where
Chil: Render<LeptosGtk>,
Props: Property,
Widg: WidgetClass,
{
ty: PhantomData<Widg>,
widget: Element,
properties: Props::State,
children: Chil::State,
}
impl<Widg, Props, Chil> LGtkWidgetState<Widg, Props, Chil>
where
Chil: Render<LeptosGtk>,
Props: Property,
Widg: WidgetClass,
{
pub fn as_widget(&self) -> &Widget {
&self.widget.0
}
}
impl<Widg, Props, Chil> Render<LeptosGtk> for LGtkWidget<Widg, Props, Chil>
where
Widg: WidgetClass,
Props: Property,
Chil: Render<LeptosGtk>,
{
type State = LGtkWidgetState<Widg, Props, Chil>;
fn build(self) -> Self::State {
let widget = Object::new::<Widg::Widget>();
let widget = Element::from(widget);
let properties = self.properties.build(&widget);
let mut children = self.children.build();
children.mount(&widget, None);
LGtkWidgetState {
ty: PhantomData,
widget,
properties,
children,
}
}
fn rebuild(self, state: &mut Self::State) {
self.properties
.rebuild(&state.widget, &mut state.properties);
self.children.rebuild(&mut state.children);
}
}
impl<Widg, Props, Chil> Mountable<LeptosGtk>
for LGtkWidgetState<Widg, Props, Chil>
where
Widg: WidgetClass,
Props: Property,
Chil: Render<LeptosGtk>,
{
fn unmount(&mut self) {
self.children.unmount();
self.widget.remove();
}
fn mount(
&mut self,
parent: &<LeptosGtk as Renderer>::Element,
marker: Option<&<LeptosGtk as Renderer>::Node>,
) {
println!("mounting {}", std::any::type_name::<Widg>());
self.children.mount(&self.widget, None);
LeptosGtk::insert_node(parent, &self.widget, marker);
}
fn insert_before_this(&self,
child: &mut dyn Mountable<LeptosGtk>,
) -> bool {
child.mount(parent, Some(self.widget.as_ref()));
true
}
}
pub trait Property {
type State;
fn build(self, element: &Element) -> Self::State;
fn rebuild(self, element: &Element, state: &mut Self::State);
}
impl<T, F> Property for F
where
T: Property,
T::State: 'static,
F: Fn() -> T + 'static,
{
type State = RenderEffect<T::State>;
fn build(self, widget: &Element) -> Self::State {
let widget = widget.clone();
RenderEffect::new(move |prev| {
let value = self();
if let Some(mut prev) = prev {
value.rebuild(&widget, &mut prev);
prev
} else {
value.build(&widget)
}
})
}
fn rebuild(self, widget: &Element, state: &mut Self::State) {}
}
pub fn button() -> LGtkWidget<gtk::Button, (), ()> {
LGtkWidget {
widget: PhantomData,
properties: (),
children: (),
}
}
pub fn r#box() -> LGtkWidget<gtk::Box, (), ()> {
LGtkWidget {
widget: PhantomData,
properties: (),
children: (),
}
}
mod widgets {
use super::WidgetClass;
impl WidgetClass for gtk::Button {
type Widget = Self;
}
impl WidgetClass for gtk::Box {
type Widget = Self;
}
}
pub mod properties {
use super::{
Element, LGtkWidget, LGtkWidgetState, LeptosGtk, Property, WidgetClass,
};
use gtk::glib::{object::ObjectExt, Value};
use leptos::tachys::{renderer::Renderer, view::Render};
use next_tuple::NextTuple;
pub struct Connect<F>
where
F: Fn(&[Value]) -> Option<Value> + Send + Sync + 'static,
{
pub signal_name: &'static str,
pub callback: F,
}
impl<F> Property for Connect<F>
where
F: Fn(&[Value]) -> Option<Value> + Send + Sync + 'static,
{
type State = ();
fn build(self, element: &Element) -> Self::State {
element.0.connect(self.signal_name, false, self.callback);
}
fn rebuild(self, element: &Element, state: &mut Self::State) {}
}
/* examples for macro */
pub struct Orientation {
value: gtk::Orientation,
}
pub struct OrientationState {
value: gtk::Orientation,
}
impl Property for Orientation {
type State = OrientationState;
fn build(self, element: &Element) -> Self::State {
element.0.set_property("orientation", self.value);
OrientationState { value: self.value }
}
fn rebuild(self, element: &Element, state: &mut Self::State) {
if self.value != state.value {
element.0.set_property("orientation", self.value);
state.value = self.value;
}
}
}
impl<Widg, Props, Chil> LGtkWidget<Widg, Props, Chil>
where
Widg: WidgetClass,
Props: NextTuple,
Chil: Render<LeptosGtk>,
{
pub fn orientation(
self,
value: impl Into<gtk::Orientation>,
) -> LGtkWidget<Widg, Props::Output<Orientation>, Chil> {
let LGtkWidget {
widget,
properties,
children,
} = self;
LGtkWidget {
widget,
properties: properties.next_tuple(Orientation {
value: value.into(),
}),
children,
}
}
}
pub struct Spacing {
value: i32,
}
pub struct SpacingState {
value: i32,
}
impl Property for Spacing {
type State = SpacingState;
fn build(self, element: &Element) -> Self::State {
element.0.set_property("spacing", self.value);
SpacingState { value: self.value }
}
fn rebuild(self, element: &Element, state: &mut Self::State) {
if self.value != state.value {
element.0.set_property("spacing", self.value);
state.value = self.value;
}
}
}
impl<Widg, Props, Chil> LGtkWidget<Widg, Props, Chil>
where
Widg: WidgetClass,
Props: NextTuple,
Chil: Render<LeptosGtk>,
{
pub fn spacing(
self,
value: impl Into<i32>,
) -> LGtkWidget<Widg, Props::Output<Spacing>, Chil> {
let LGtkWidget {
widget,
properties,
children,
} = self;
LGtkWidget {
widget,
properties: properties.next_tuple(Spacing {
value: value.into(),
}),
children,
}
}
}
/* end examples for properties macro */
pub struct Label {
value: String,
}
impl Label {
pub fn new(value: impl Into<String>) -> Self {
Self {
value: value.into(),
}
}
}
pub struct LabelState {
value: String,
}
impl Property for Label {
type State = LabelState;
fn build(self, element: &Element) -> Self::State {
LeptosGtk::set_attribute(element, "label", &self.value);
LabelState { value: self.value }
}
fn rebuild(self, element: &Element, state: &mut Self::State) {
todo!()
}
}
impl Property for () {
type State = ();
fn build(self, _element: &Element) -> Self::State {}
fn rebuild(self, _element: &Element, _state: &mut Self::State) {}
}
macro_rules! tuples {
($($ty:ident),* $(,)?) => {
impl<$($ty,)*> Property for ($($ty,)*)
where $($ty: Property,)*
{
type State = ($($ty::State,)*);
fn build(self, element: &Element) -> Self::State {
#[allow(non_snake_case)]
let ($($ty,)*) = self;
($($ty.build(element),)*)
}
fn rebuild(self, element: &Element, state: &mut Self::State) {
paste::paste! {
#[allow(non_snake_case)]
let ($($ty,)*) = self;
#[allow(non_snake_case)]
let ($([<state_ $ty:lower>],)*) = state;
$($ty.rebuild(element, [<state_ $ty:lower>]));*
}
}
}
}
}
tuples!(A);
tuples!(A, B);
tuples!(A, B, C);
tuples!(A, B, C, D);
tuples!(A, B, C, D, E);
tuples!(A, B, C, D, E, F);
tuples!(A, B, C, D, E, F, G);
tuples!(A, B, C, D, E, F, G, H);
tuples!(A, B, C, D, E, F, G, H, I);
tuples!(A, B, C, D, E, F, G, H, I, J);
tuples!(A, B, C, D, E, F, G, H, I, J, K);
tuples!(A, B, C, D, E, F, G, H, I, J, K, L);
tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M);
tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, N);
tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O);
tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P);
tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q);
tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R);
tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S);
tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T);
tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U);
tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V);
tuples!(
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W
);
tuples!(
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X
);
tuples!(
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X,
Y
);
}

View File

@@ -1,59 +1,175 @@
use gtk::{prelude::*, Application, ApplicationWindow, Button};
use leptos::*;
#[cfg(feature = "gtk")]
use gtk::{
glib::Value, prelude::*, Application, ApplicationWindow, Orientation,
Widget,
};
#[cfg(feature = "wasm")]
use leptos::tachys::{dom::body, html::element, html::event as ev};
use leptos::{
logging,
prelude::*,
reactive_graph::{effect::Effect, owner::Owner, signal::RwSignal},
Executor, For, ForProps,
};
#[cfg(feature = "gtk")]
use leptos_gtk::{Element, LGtkWidget, LeptosGtk};
use std::{mem, thread, time::Duration};
#[cfg(feature = "gtk")]
mod leptos_gtk;
const APP_ID: &str = "dev.leptos.Counter";
// Basic GTK app setup from https://gtk-rs.org/gtk4-rs/stable/latest/book/hello_world.html
fn main() {
let _ = create_runtime();
// Create a new application
let app = Application::builder().application_id(APP_ID).build();
// use the glib event loop to power the reactive system
#[cfg(feature = "gtk")]
{
_ = Executor::init_glib();
let app = Application::builder().application_id(APP_ID).build();
// Connect to "activate" signal of `app`
app.connect_activate(build_ui);
app.connect_startup(|_| load_css());
// Run the application
app.run();
app.connect_activate(|app| {
// Connect to "activate" signal of `app`
let owner = Owner::new();
let view = owner.with(ui);
let (root, state) = leptos_gtk::root(view);
let window = ApplicationWindow::builder()
.application(app)
.title("TachyGTK")
.child(&root)
.build();
// Present window
window.present();
mem::forget((owner, state));
});
app.run();
}
#[cfg(all(feature = "wasm", not(feature = "gtk")))]
{
console_error_panic_hook::set_once();
_ = Executor::init_wasm_bindgen();
let owner = Owner::new();
let view = owner.with(ui);
let mut state = view.build();
state.mount(&body().into(), None);
mem::forget((owner, state));
}
}
fn build_ui(app: &Application) {
let button = counter_button();
#[cfg(feature = "gtk")]
type Rndr = LeptosGtk;
#[cfg(all(feature = "wasm", not(feature = "gtk")))]
type Rndr = Dom;
// Create a window and set the title
let window = ApplicationWindow::builder()
.application(app)
.title("Leptos-GTK")
.child(&button)
.build();
fn ui() -> impl Render<Rndr> {
let value = RwSignal::new(0);
let rows = RwSignal::new(vec![1, 2, 3, 4, 5]);
// Present window
window.present();
}
fn counter_button() -> Button {
let (value, set_value) = create_signal(0);
// Create a button with label and margins
let button = Button::builder()
.label("Count: ")
.margin_top(12)
.margin_bottom(12)
.margin_start(12)
.margin_end(12)
.build();
// Connect to "clicked" signal of `button`
button.connect_clicked(move |_| {
// Set the label to "Hello World!" after the button has been clicked on
set_value.update(|value| *value += 1);
Effect::new(move |_| {
logging::log!("value = {}", value.get());
});
create_effect({
let button = button.clone();
move |_| {
button.set_label(&format!("Count: {}", value.get()));
}
// just an example of multithreaded reactivity
#[cfg(feature = "gtk")]
thread::spawn(move || loop {
thread::sleep(Duration::from_millis(250));
value.update(|n| *n += 1);
});
button
vstack((
hstack((
button("-1", move || value.update(|n| *n -= 1)),
move || value.get().to_string(),
button("+1", move || value.update(|n| *n += 1)),
)),
button("Swap", move || {
rows.update(|items| {
items.swap(1, 3);
})
}),
hstack(For(ForProps::builder()
.each(move || rows.get())
.key(|k| *k)
.children(|v| v)
.build())),
))
}
fn button(
label: impl Render<Rndr>,
callback: impl Fn() + Send + Sync + 'static,
) -> impl Render<Rndr> {
#[cfg(feature = "gtk")]
{
leptos_gtk::button()
.child(label)
.connect("clicked", move |_| {
callback();
None
})
}
#[cfg(all(feature = "wasm", not(feature = "gtk")))]
{
element::button()
.on(ev::click, move |_| callback())
.child(label)
}
}
fn vstack(children: impl Render<Rndr>) -> impl Render<Rndr> {
#[cfg(feature = "gtk")]
{
leptos_gtk::r#box()
.orientation(Orientation::Vertical)
.spacing(12)
.child(children)
}
#[cfg(all(feature = "wasm", not(feature = "gtk")))]
{
element::div()
.style(("display", "flex"))
.style(("flex-direction", "column"))
.style(("align-items", "center"))
.style(("justify-content", "center"))
.style(("margin", "1rem"))
.child(children)
}
}
fn hstack(children: impl Render<Rndr>) -> impl Render<Rndr> {
#[cfg(feature = "gtk")]
{
leptos_gtk::r#box()
.orientation(Orientation::Horizontal)
.spacing(12)
.child(children)
}
#[cfg(all(feature = "wasm", not(feature = "gtk")))]
{
element::div()
.style(("display", "flex"))
.style(("align-items", "center"))
.style(("justify-content", "center"))
.style(("margin", "1rem"))
.child(children)
}
}
#[cfg(feature = "gtk")]
fn load_css() {
use gtk::{gdk::Display, CssProvider};
let provider = CssProvider::new();
provider.load_from_path("style.css");
// Add the provider to the default screen
gtk::style_context_add_provider_for_display(
&Display::default().expect("Could not connect to a display."),
&provider,
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
);
}

View File

@@ -8,6 +8,8 @@ crate-type = ["cdylib", "rlib"]
[profile.release]
codegen-units = 1
opt-level = "z"
panic = "abort"
lto = true
[dependencies]
@@ -23,21 +25,18 @@ log = "0.4"
serde = { version = "1", features = ["derive"] }
gloo-net = { version = "0.2", features = ["http"] }
reqwest = { version = "0.11", features = ["json"] }
tracing = "0.1"
# openssl = { version = "0.10", features = ["v110"] }
wasm-bindgen = "0.2"
web-sys = { version = "0.3", features = ["AbortController", "AbortSignal"] }
send_wrapper = "0.6.0"
[features]
csr = ["leptos/csr", "leptos_meta/csr", "leptos_router/csr"]
hydrate = ["leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate"]
csr = ["leptos/csr"]
hydrate = ["leptos/hydrate"]
ssr = [
"dep:actix-files",
"dep:actix-web",
"dep:leptos_actix",
"leptos/ssr",
"leptos_meta/ssr",
"leptos_router/ssr",
]
[profile.wasm-release]

View File

@@ -1,4 +1,4 @@
use leptos::Serializable;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
pub fn story(path: &str) -> String {
@@ -10,46 +10,51 @@ pub fn user(path: &str) -> String {
}
#[cfg(not(feature = "ssr"))]
pub async fn fetch_api<T>(path: &str) -> Option<T>
pub fn fetch_api<T>(
path: &str,
) -> impl std::future::Future<Output = Option<T>> + Send + '_
where
T: Serializable,
T: Serialize + DeserializeOwned,
{
let abort_controller = web_sys::AbortController::new().ok();
let abort_signal = abort_controller.as_ref().map(|a| a.signal());
use leptos::prelude::on_cleanup;
use send_wrapper::SendWrapper;
// abort in-flight requests if, e.g., we've navigated away from this page
leptos::on_cleanup(move || {
if let Some(abort_controller) = abort_controller {
abort_controller.abort()
}
});
SendWrapper::new(async move {
let abort_controller =
SendWrapper::new(web_sys::AbortController::new().ok());
let abort_signal = abort_controller.as_ref().map(|a| a.signal());
let json = gloo_net::http::Request::get(path)
.abort_signal(abort_signal.as_ref())
.send()
.await
.map_err(|e| log::error!("{e}"))
.ok()?
.text()
.await
.ok()?;
// abort in-flight requests if, e.g., we've navigated away from this page
on_cleanup(move || {
if let Some(abort_controller) = abort_controller.take() {
abort_controller.abort()
}
});
T::de(&json).ok()
gloo_net::http::Request::get(path)
.abort_signal(abort_signal.as_ref())
.send()
.await
.map_err(|e| log::error!("{e}"))
.ok()?
.json()
.await
.ok()
})
}
#[cfg(feature = "ssr")]
pub async fn fetch_api<T>(path: &str) -> Option<T>
where
T: Serializable,
T: Serialize + DeserializeOwned,
{
let json = reqwest::get(path)
reqwest::get(path)
.await
.map_err(|e| log::error!("{e}"))
.ok()?
.text()
.json()
.await
.ok()?;
T::de(&json).map_err(|e| log::error!("{e}")).ok()
.ok()
}
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone)]

View File

@@ -1,32 +1,37 @@
use leptos::*;
use leptos_meta::*;
use leptos_router::*;
use leptos::prelude::*;
mod api;
mod routes;
use leptos_meta::{provide_meta_context, Link, Meta, Stylesheet};
use leptos_router::{
components::{FlatRoutes, Route, Router, RoutingProgress},
ParamSegment, StaticSegment,
};
use routes::{nav::*, stories::*, story::*, users::*};
use std::time::Duration;
#[component]
pub fn App() -> impl IntoView {
provide_meta_context();
let (is_routing, set_is_routing) = create_signal(false);
let (is_routing, set_is_routing) = signal(false);
view! {
<Stylesheet id="leptos" href="/pkg/hackernews.css"/>
<Link rel="shortcut icon" type_="image/ico" href="/favicon.ico"/>
<Meta name="description" content="Leptos implementation of a HackerNews demo."/>
// adding `set_is_routing` causes the router to wait for async data to load on new pages
<Router set_is_routing>
// shows a progress bar while async data are loading
<div class="routing-progress">
<RoutingProgress is_routing max_time=std::time::Duration::from_millis(250)/>
<RoutingProgress is_routing max_time=Duration::from_millis(250)/>
</div>
<Nav />
<main>
<Routes>
<Route path="users/:id" view=User/>
<Route path="stories/:id" view=Story/>
<Route path=":stories?" view=Stories/>
</Routes>
<FlatRoutes fallback=|| "Not found.">
<Route path=(StaticSegment("users"), ParamSegment("id")) view=User/>
<Route path=(StaticSegment("stories"), ParamSegment("id")) view=Story/>
<Route path=ParamSegment("stories") view=Stories/>
// TODO allow optional params without duplication
<Route path=StaticSegment("") view=Stories/>
</FlatRoutes>
</main>
</Router>
}
@@ -35,7 +40,6 @@ pub fn App() -> impl IntoView {
#[cfg(feature = "hydrate")]
#[wasm_bindgen::prelude::wasm_bindgen]
pub fn hydrate() {
_ = console_log::init_with_level(log::Level::Debug);
console_error_panic_hook::set_once();
leptos::mount_to_body(App);
leptos::mount::hydrate_body(App);
}

View File

@@ -4,7 +4,6 @@ mod ssr_imports {
pub use actix_files::Files;
pub use actix_web::*;
pub use hackernews::App;
pub use leptos_actix::{generate_route_list, LeptosRoutes};
#[get("/style.css")]
pub async fn css() -> impl Responder {
@@ -19,24 +18,44 @@ mod ssr_imports {
#[cfg(feature = "ssr")]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use leptos::get_configuration;
use leptos::prelude::*;
use leptos_actix::{generate_route_list, LeptosRoutes};
use leptos_meta::MetaTags;
use ssr_imports::*;
// Setting this to None means we'll be using cargo-leptos and its env vars.
let conf = get_configuration(None).await.unwrap();
let conf = get_configuration(None).unwrap();
let addr = conf.leptos_options.site_addr;
// Generate the list of routes in your Leptos App
let routes = generate_route_list(App);
HttpServer::new(move || {
// Generate the list of routes in your Leptos App
let routes = generate_route_list(App);
let leptos_options = &conf.leptos_options;
let site_root = &leptos_options.site_root;
App::new()
.service(css)
.service(favicon)
.leptos_routes(leptos_options.to_owned(), routes.to_owned(), App)
.leptos_routes(routes, {
let leptos_options = leptos_options.clone();
move || {
use leptos::prelude::*;
view! {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<AutoReload options=leptos_options.clone() />
<HydrationScripts options=leptos_options.clone()/>
<MetaTags/>
</head>
<body>
<App/>
</body>
</html>
}
}})
.service(Files::new("/", site_root))
//.wrap(middleware::Compress::default())
})
@@ -49,8 +68,6 @@ async fn main() -> std::io::Result<()> {
#[cfg(not(feature = "ssr"))]
fn main() {
use hackernews::App;
_ = console_log::init_with_level(log::Level::Debug);
console_error_panic_hook::set_once();
leptos::mount_to_body(App)
leptos::mount::mount_to_body(App)
}

View File

@@ -1,5 +1,5 @@
use leptos::{component, view, IntoView};
use leptos_router::*;
use leptos::prelude::*;
use leptos_router::components::A;
#[component]
pub fn Nav() -> impl IntoView {

View File

@@ -1,6 +1,9 @@
use crate::api;
use leptos::*;
use leptos_router::*;
use leptos::{either::Either, prelude::*};
use leptos_router::{
components::A,
hooks::{use_params_map, use_query_map},
};
fn category(from: &str) -> &'static str {
match from {
@@ -18,47 +21,50 @@ pub fn Stories() -> impl IntoView {
let params = use_params_map();
let page = move || {
query
.with(|q| q.get("page").and_then(|page| page.parse::<usize>().ok()))
.read()
.get("page")
.and_then(|page| page.parse::<usize>().ok())
.unwrap_or(1)
};
let story_type = move || {
params
.with(|p| p.get("stories").cloned())
.read()
.get("stories")
.unwrap_or_else(|| "top".to_string())
};
let stories = create_resource(
let stories = Resource::new(
move || (page(), story_type()),
move |(page, story_type)| async move {
let path = format!("{}?page={}", category(&story_type), page);
api::fetch_api::<Vec<api::Story>>(&api::story(&path)).await
},
);
let (pending, set_pending) = create_signal(false);
let (pending, set_pending) = signal(false);
let hide_more_link = move || {
stories.get().unwrap_or(None).unwrap_or_default().len() < 28
|| pending.get()
};
let hide_more_link = move || match &*stories.read() {
Some(Some(stories)) => stories.len() < 28,
_ => true
} || pending.get();
view! {
<div class="news-view">
<div class="news-list-nav">
<span>
{move || if page() > 1 {
view! {
Either::Left(view! {
<a class="page-link"
href=move || format!("/{}?page={}", story_type(), page() - 1)
attr:aria_label="Previous Page"
aria-label="Previous Page"
>
"< prev"
</a>
}.into_any()
})
} else {
view! {
Either::Right(view! {
<span class="page-link disabled" aria-hidden="true">
"< prev"
</span>
}.into_any()
})
}}
</span>
<span>"page " {page}</span>
@@ -81,23 +87,19 @@ pub fn Stories() -> impl IntoView {
fallback=move || view! { <p>"Loading..."</p> }
set_pending
>
{move || match stories.get() {
None => None,
Some(None) => Some(view! { <p>"Error loading stories."</p> }.into_any()),
Some(Some(stories)) => {
Some(view! {
<ul>
<For
each=move || stories.clone()
key=|story| story.id
let:story
>
<Story story/>
</For>
</ul>
}.into_any())
}
}}
<Show when=move || stories.read().as_ref().map(Option::is_none).unwrap_or(false)>
>
<p>"Error loading stories."</p>
</Show>
<ul>
<For
each=move || stories.get().unwrap_or_default().unwrap_or_default()
key=|story| story.id
let:story
>
<Story story/>
</For>
</ul>
</Transition>
</div>
</main>
@@ -112,23 +114,23 @@ fn Story(story: api::Story) -> impl IntoView {
<span class="score">{story.points}</span>
<span class="title">
{if !story.url.starts_with("item?id=") {
view! {
Either::Left(view! {
<span>
<a href=story.url target="_blank" rel="noreferrer">
{story.title.clone()}
</a>
<span class="host">"("{story.domain}")"</span>
</span>
}.into_view()
})
} else {
let title = story.title.clone();
view! { <A href=format!("/stories/{}", story.id)>{title.clone()}</A> }.into_view()
Either::Right(view! { <A href=format!("/stories/{}", story.id)>{title}</A> })
}}
</span>
<br />
<span class="meta">
{if story.story_type != "job" {
view! {
Either::Left(view! {
<span>
{"by "}
{story.user.map(|user| view ! { <A href=format!("/users/{user}")>{user.clone()}</A>})}
@@ -141,10 +143,10 @@ fn Story(story: api::Story) -> impl IntoView {
}}
</A>
</span>
}.into_view()
})
} else {
let title = story.title.clone();
view! { <A href=format!("/item/{}", story.id)>{title.clone()}</A> }.into_view()
Either::Right(view! { <A href=format!("/item/{}", story.id)>{title}</A> })
}}
</span>
{(story.story_type != "link").then(|| view! {

View File

@@ -1,13 +1,15 @@
use crate::api;
use leptos::*;
use leptos_meta::*;
use leptos_router::*;
use leptos::either::Either;
use leptos::prelude::*;
use leptos_meta::Meta;
use leptos_router::components::A;
use leptos_router::hooks::use_params_map;
#[component]
pub fn Story() -> impl IntoView {
let params = use_params_map();
let story = create_resource(
move || params.get().get("id").cloned().unwrap_or_default(),
let story = Resource::new(
move || params.read().get("id").unwrap_or_default(),
move |id| async move {
if id.is_empty() {
None
@@ -17,19 +19,13 @@ pub fn Story() -> impl IntoView {
}
},
);
let meta_description = move || {
story
.get()
.and_then(|story| story.map(|story| story.title))
.unwrap_or_else(|| "Loading story...".to_string())
};
view! {
<Suspense fallback=|| view! { "Loading..." }>
<Meta name="description" content=meta_description/>
{move || story.get().map(|story| match story {
None => view! { <div class="item-view">"Error loading this story."</div> },
Some(story) => view! {
Suspense(SuspenseProps::builder().fallback(|| "Loading...").children(ToChildren::to_children(move || Suspend(async move {
match story.await.clone() {
None => Either::Left("Story not found."),
Some(story) => {
Either::Right(view! {
<Meta name="description" content=story.title.clone()/>
<div class="item-view">
<div class="item-view-header">
<a href=story.url target="_blank">
@@ -46,32 +42,33 @@ pub fn Story() -> impl IntoView {
</p>})}
</div>
<div class="item-view-comments">
<p class="item-view-comments-header">
{if story.comments_count.unwrap_or_default() > 0 {
format!("{} comments", story.comments_count.unwrap_or_default())
} else {
"No comments yet.".into()
}}
</p>
<ul class="comment-children">
<For
each=move || story.comments.clone().unwrap_or_default()
key=|comment| comment.id
let:comment
>
<Comment comment />
</For>
</ul>
<p class="item-view-comments-header">
{if story.comments_count.unwrap_or_default() > 0 {
format!("{} comments", story.comments_count.unwrap_or_default())
} else {
"No comments yet.".into()
}}
</p>
<ul class="comment-children">
<For
each=move || story.comments.clone().unwrap_or_default()
key=|comment| comment.id
let:comment
>
<Comment comment />
</For>
</ul>
</div>
</div>
</div>
}})}
</Suspense>
}
})
}
}
}))).build())
}
#[component]
pub fn Comment(comment: api::Comment) -> impl IntoView {
let (open, set_open) = create_signal(true);
let (open, set_open) = signal(true);
view! {
<li class="comment">
@@ -113,7 +110,7 @@ pub fn Comment(comment: api::Comment) -> impl IntoView {
}
})}
</li>
}
}.into_any()
}
fn pluralize(n: usize) -> &'static str {

View File

@@ -1,12 +1,13 @@
use crate::api::{self, User};
use leptos::*;
use leptos_router::*;
use leptos::server::Resource;
use leptos::{either::Either, prelude::*};
use leptos_router::hooks::use_params_map;
#[component]
pub fn User() -> impl IntoView {
let params = use_params_map();
let user = create_resource(
move || params.get().get("id").cloned().unwrap_or_default(),
let user = Resource::new(
move || params.read().get("id").unwrap_or_default(),
move |id| async move {
if id.is_empty() {
None
@@ -18,11 +19,11 @@ pub fn User() -> impl IntoView {
view! {
<div class="user-view">
<Suspense fallback=|| view! { "Loading..." }>
{move || user.get().map(|user| match user {
None => view! { <h1>"User not found."</h1> }.into_view(),
Some(user) => view! {
{move || Suspend(async move { match user.await.clone() {
None => Either::Left(view! { <h1>"User not found."</h1> }),
Some(user) => Either::Right(view! {
<div>
<h1>"User: " {&user.id}</h1>
<h1>"User: " {user.id.clone()}</h1>
<ul class="meta">
<li>
<span class="label">"Created: "</span> {user.created}
@@ -38,8 +39,8 @@ pub fn User() -> impl IntoView {
<a href=format!("https://news.ycombinator.com/threads?id={}", user.id)>"comments"</a>
</p>
</div>
}.into_view()
})}
})
}})}
</Suspense>
</div>
}

View File

@@ -11,14 +11,11 @@ codegen-units = 1
lto = true
[dependencies]
console_log = "1.0"
console_error_panic_hook = "0.1"
leptos = { path = "../../leptos" }
leptos_axum = { path = "../../integrations/axum", optional = true }
leptos_meta = { path = "../../meta" }
leptos_router = { path = "../../router" }
log = "0.4"
simple_logger = "4.0"
serde = { version = "1.0", features = ["derive"] }
tracing = "0.1"
gloo-net = { version = "0.4", features = ["http"] }
@@ -30,11 +27,12 @@ tokio = { version = "1", features = ["full"], optional = true }
http = { version = "1.0", optional = true }
web-sys = { version = "0.3", features = ["AbortController", "AbortSignal"] }
wasm-bindgen = "0.2"
send_wrapper = { version = "0.6.0", features = ["futures"] }
[features]
default = ["csr"]
csr = ["leptos/csr", "leptos_meta/csr", "leptos_router/csr"]
hydrate = ["leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate"]
csr = ["leptos/csr"]
hydrate = ["leptos/hydrate"]
ssr = [
"dep:axum",
"dep:tower",

View File

@@ -1,4 +1,5 @@
use leptos::Serializable;
use leptos::logging;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
pub fn story(path: &str) -> String {
@@ -10,46 +11,51 @@ pub fn user(path: &str) -> String {
}
#[cfg(not(feature = "ssr"))]
pub async fn fetch_api<T>(path: &str) -> Option<T>
pub fn fetch_api<T>(
path: &str,
) -> impl std::future::Future<Output = Option<T>> + Send + '_
where
T: Serializable,
T: Serialize + DeserializeOwned,
{
let abort_controller = web_sys::AbortController::new().ok();
let abort_signal = abort_controller.as_ref().map(|a| a.signal());
use leptos::prelude::on_cleanup;
use send_wrapper::SendWrapper;
// abort in-flight requests if e.g., we've navigated away from this page
leptos::on_cleanup(move || {
if let Some(abort_controller) = abort_controller {
abort_controller.abort()
}
});
SendWrapper::new(async move {
let abort_controller =
SendWrapper::new(web_sys::AbortController::new().ok());
let abort_signal = abort_controller.as_ref().map(|a| a.signal());
let json = gloo_net::http::Request::get(path)
.abort_signal(abort_signal.as_ref())
.send()
.await
.map_err(|e| log::error!("{e}"))
.ok()?
.text()
.await
.ok()?;
// abort in-flight requests if, e.g., we've navigated away from this page
on_cleanup(move || {
if let Some(abort_controller) = abort_controller.take() {
abort_controller.abort()
}
});
T::de(&json).ok()
gloo_net::http::Request::get(path)
.abort_signal(abort_signal.as_ref())
.send()
.await
.map_err(|e| logging::error!("{e}"))
.ok()?
.json()
.await
.ok()
})
}
#[cfg(feature = "ssr")]
pub async fn fetch_api<T>(path: &str) -> Option<T>
where
T: Serializable,
T: Serialize + DeserializeOwned,
{
let json = reqwest::get(path)
reqwest::get(path)
.await
.map_err(|e| log::error!("{e}"))
.map_err(|e| logging::error!("{e}"))
.ok()?
.text()
.json()
.await
.ok()?;
T::de(&json).map_err(|e| log::error!("{e}")).ok()
.ok()
}
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone)]

View File

@@ -1,48 +0,0 @@
use crate::error_template::error_template;
use axum::{
body::Body,
extract::State,
http::{Request, Response, StatusCode, Uri},
response::{IntoResponse, Response as AxumResponse},
};
use leptos::LeptosOptions;
use tower::ServiceExt;
use tower_http::services::ServeDir;
pub async fn file_and_error_handler(
uri: Uri,
State(options): State<LeptosOptions>,
req: Request<Body>,
) -> AxumResponse {
let root = options.site_root.clone();
let res = get_static_file(uri.clone(), &root).await.unwrap();
if res.status() == StatusCode::OK {
res.into_response()
} else {
let handler =
leptos_axum::render_app_to_stream(options.to_owned(), || {
error_template(None)
});
handler(req).await.into_response()
}
}
async fn get_static_file(
uri: Uri,
root: &str,
) -> Result<Response<Body>, (StatusCode, String)> {
let req = Request::builder()
.uri(uri.clone())
.body(Body::empty())
.unwrap();
// `ServeDir` implements `tower::Service` so we can call it with `tower::ServiceExt::oneshot`
// This path is relative to the cargo root
match ServeDir::new(root).oneshot(req).await {
Ok(res) => Ok(res.into_response()),
Err(err) => Err((
StatusCode::INTERNAL_SERVER_ERROR,
format!("Something went wrong: {}", err),
)),
}
}

View File

@@ -1,31 +1,55 @@
use leptos::{component, view, IntoView};
use leptos_meta::*;
use leptos_router::*;
use leptos::prelude::*;
mod api;
pub mod error_template;
#[cfg(feature = "ssr")]
pub mod fallback;
#[cfg(feature = "ssr")]
pub mod handlers;
mod routes;
use leptos_meta::{provide_meta_context, Link, Meta, MetaTags, Stylesheet};
use leptos_router::{
components::{FlatRoutes, Route, Router, RoutingProgress},
ParamSegment, StaticSegment,
};
use routes::{nav::*, stories::*, story::*, users::*};
use std::time::Duration;
pub fn shell(options: LeptosOptions) -> impl IntoView {
view! {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<AutoReload options=options.clone() />
<HydrationScripts options/>
<MetaTags/>
</head>
<body>
<App/>
</body>
</html>
}
}
#[component]
pub fn App() -> impl IntoView {
provide_meta_context();
let (is_routing, set_is_routing) = signal(false);
view! {
<Link rel="shortcut icon" type_="image/ico" href="/favicon.ico"/>
<Stylesheet id="leptos" href="/pkg/hackernews_axum.css"/>
<Link rel="shortcut icon" type_="image/ico" href="/favicon.ico"/>
<Meta name="description" content="Leptos implementation of a HackerNews demo."/>
<Router>
<Router set_is_routing>
// shows a progress bar while async data are loading
<div class="routing-progress">
<RoutingProgress is_routing max_time=Duration::from_millis(250)/>
</div>
<Nav />
<main>
<Routes>
<Route path="users/:id" view=User/>
<Route path="stories/:id" view=Story/>
<Route path=":stories?" view=Stories/>
</Routes>
<FlatRoutes fallback=|| "Not found.">
<Route path=(StaticSegment("users"), ParamSegment("id")) view=User/>
<Route path=(StaticSegment("stories"), ParamSegment("id")) view=Story/>
<Route path=ParamSegment("stories") view=Stories/>
// TODO allow optional params without duplication
<Route path=StaticSegment("") view=Stories/>
</FlatRoutes>
</main>
</Router>
}
@@ -34,7 +58,6 @@ pub fn App() -> impl IntoView {
#[cfg(feature = "hydrate")]
#[wasm_bindgen::prelude::wasm_bindgen]
pub fn hydrate() {
_ = console_log::init_with_level(log::Level::Debug);
console_error_panic_hook::set_once();
leptos::mount_to_body(App);
leptos::mount::hydrate_body(App);
}

View File

@@ -1,24 +1,23 @@
#[cfg(feature = "ssr")]
#[tokio::main]
async fn main() {
use axum::{routing::get, Router};
use hackernews_axum::{fallback::file_and_error_handler, *};
use leptos::get_configuration;
use axum::Router;
use hackernews_axum::{shell, App};
use leptos::config::get_configuration;
use leptos_axum::{generate_route_list, LeptosRoutes};
let conf = get_configuration(Some("Cargo.toml")).await.unwrap();
let conf = get_configuration(Some("Cargo.toml")).unwrap();
let leptos_options = conf.leptos_options;
let addr = leptos_options.site_addr;
let routes = generate_route_list(App);
simple_logger::init_with_level(log::Level::Debug)
.expect("couldn't initialize logging");
// build our application with a route
let app = Router::new()
.route("/favicon.ico", get(file_and_error_handler))
.leptos_routes(&leptos_options, routes, App)
.fallback(file_and_error_handler)
.leptos_routes(&leptos_options, routes, {
let leptos_options = leptos_options.clone();
move || shell(leptos_options.clone())
})
.fallback(leptos_axum::file_and_error_handler(shell))
.with_state(leptos_options);
// run our app with hyper

View File

@@ -1,12 +1,12 @@
use leptos::{component, view, IntoView};
use leptos_router::*;
use leptos::prelude::*;
use leptos_router::components::A;
#[component]
pub fn Nav() -> impl IntoView {
view! {
<header class="header">
<nav class="inner">
<A href="/">
<A href="/home">
<strong>"HN"</strong>
</A>
<A href="/new">

View File

@@ -1,6 +1,9 @@
use crate::api;
use leptos::*;
use leptos_router::*;
use leptos::{either::Either, prelude::*};
use leptos_router::{
components::A,
hooks::{use_params_map, use_query_map},
};
fn category(from: &str) -> &'static str {
match from {
@@ -18,62 +21,65 @@ pub fn Stories() -> impl IntoView {
let params = use_params_map();
let page = move || {
query
.with(|q| q.get("page").and_then(|page| page.parse::<usize>().ok()))
.read()
.get("page")
.and_then(|page| page.parse::<usize>().ok())
.unwrap_or(1)
};
let story_type = move || {
params
.with(|p| p.get("stories").cloned())
.read()
.get("stories")
.unwrap_or_else(|| "top".to_string())
};
let stories = create_resource(
let stories = Resource::new(
move || (page(), story_type()),
move |(page, story_type)| async move {
let path = format!("{}?page={}", category(&story_type), page);
api::fetch_api::<Vec<api::Story>>(&api::story(&path)).await
},
);
let (pending, set_pending) = create_signal(false);
let (pending, set_pending) = signal(false);
let hide_more_link = move || {
stories.get().unwrap_or(None).unwrap_or_default().len() < 28
|| pending.get()
};
let hide_more_link = move || match &*stories.read() {
Some(Some(stories)) => stories.len() < 28,
_ => true
} || pending.get();
view! {
<div class="news-view">
<div class="news-list-nav">
<span>
{move || if page() > 1 {
view! {
Either::Left(view! {
<a class="page-link"
href=move || format!("/{}?page={}", story_type(), page() - 1)
attr:aria_label="Previous Page"
aria-label="Previous Page"
>
"< prev"
</a>
}.into_any()
})
} else {
view! {
Either::Right(view! {
<span class="page-link disabled" aria-hidden="true">
"< prev"
</span>
}.into_any()
})
}}
</span>
<span>"page " {page}</span>
<span class="page-link"
class:disabled=hide_more_link
aria-hidden=hide_more_link
>
<a href=move || format!("/{}?page={}", story_type(), page() + 1)
aria-label="Next Page"
<Suspense>
<span class="page-link"
class:disabled=hide_more_link
aria-hidden=hide_more_link
>
"more >"
</a>
</span>
<a href=move || format!("/{}?page={}", story_type(), page() + 1)
aria-label="Next Page"
>
"more >"
</a>
</span>
</Suspense>
</div>
<main class="news-list">
<div>
@@ -81,23 +87,19 @@ pub fn Stories() -> impl IntoView {
fallback=move || view! { <p>"Loading..."</p> }
set_pending
>
{move || match stories.get() {
None => None,
Some(None) => Some(view! { <p>"Error loading stories."</p> }.into_any()),
Some(Some(stories)) => {
Some(view! {
<ul>
<For
each=move || stories.clone()
key=|story| story.id
let:story
>
<Story story/>
</For>
</ul>
}.into_any())
}
}}
<Show when=move || stories.read().as_ref().map(Option::is_none).unwrap_or(false)>
>
<p>"Error loading stories."</p>
</Show>
<ul>
<For
each=move || stories.get().unwrap_or_default().unwrap_or_default()
key=|story| story.id
let:story
>
<Story story/>
</For>
</ul>
</Transition>
</div>
</main>
@@ -112,23 +114,23 @@ fn Story(story: api::Story) -> impl IntoView {
<span class="score">{story.points}</span>
<span class="title">
{if !story.url.starts_with("item?id=") {
view! {
Either::Left(view! {
<span>
<a href=story.url target="_blank" rel="noreferrer">
{story.title.clone()}
</a>
<span class="host">"("{story.domain}")"</span>
</span>
}.into_view()
})
} else {
let title = story.title.clone();
view! { <A href=format!("/stories/{}", story.id)>{title.clone()}</A> }.into_view()
Either::Right(view! { <A href=format!("/stories/{}", story.id)>{title}</A> })
}}
</span>
<br />
<span class="meta">
{if story.story_type != "job" {
view! {
Either::Left(view! {
<span>
{"by "}
{story.user.map(|user| view ! { <A href=format!("/users/{user}")>{user.clone()}</A>})}
@@ -141,10 +143,10 @@ fn Story(story: api::Story) -> impl IntoView {
}}
</A>
</span>
}.into_view()
})
} else {
let title = story.title.clone();
view! { <A href=format!("/item/{}", story.id)>{title.clone()}</A> }.into_view()
Either::Right(view! { <A href=format!("/item/{}", story.id)>{title}</A> })
}}
</span>
{(story.story_type != "link").then(|| view! {

View File

@@ -1,13 +1,15 @@
use crate::api;
use leptos::*;
use leptos_meta::*;
use leptos_router::*;
use leptos::either::Either;
use leptos::prelude::*;
use leptos_meta::Meta;
use leptos_router::components::A;
use leptos_router::hooks::use_params_map;
#[component]
pub fn Story() -> impl IntoView {
let params = use_params_map();
let story = create_resource(
move || params.get().get("id").cloned().unwrap_or_default(),
let story = Resource::new(
move || params.read().get("id").unwrap_or_default(),
move |id| async move {
if id.is_empty() {
None
@@ -17,19 +19,13 @@ pub fn Story() -> impl IntoView {
}
},
);
let meta_description = move || {
story
.get()
.and_then(|story| story.map(|story| story.title))
.unwrap_or_else(|| "Loading story...".to_string())
};
view! {
<Suspense fallback=|| view! { "Loading..." }>
<Meta name="description" content=meta_description/>
{move || story.get().map(|story| match story {
None => view! { <div class="item-view">"Error loading this story."</div> },
Some(story) => view! {
Suspense(SuspenseProps::builder().fallback(|| "Loading...").children(ToChildren::to_children(move || Suspend(async move {
match story.await.clone() {
None => Either::Left("Story not found."),
Some(story) => {
Either::Right(view! {
<Meta name="description" content=story.title.clone()/>
<div class="item-view">
<div class="item-view-header">
<a href=story.url target="_blank">
@@ -38,7 +34,7 @@ pub fn Story() -> impl IntoView {
<span class="host">
"("{story.domain}")"
</span>
{story.user.map(|user| view! { <p class="meta">
{story.user.map(|user| view! { <p class="meta">
{story.points}
" points | by "
<A href=format!("/users/{user}")>{user.clone()}</A>
@@ -46,32 +42,33 @@ pub fn Story() -> impl IntoView {
</p>})}
</div>
<div class="item-view-comments">
<p class="item-view-comments-header">
{if story.comments_count.unwrap_or_default() > 0 {
format!("{} comments", story.comments_count.unwrap_or_default())
} else {
"No comments yet.".into()
}}
</p>
<ul class="comment-children">
<For
each=move || story.comments.clone().unwrap_or_default()
key=|comment| comment.id
let:comment
>
<Comment comment />
</For>
</ul>
<p class="item-view-comments-header">
{if story.comments_count.unwrap_or_default() > 0 {
format!("{} comments", story.comments_count.unwrap_or_default())
} else {
"No comments yet.".into()
}}
</p>
<ul class="comment-children">
<For
each=move || story.comments.clone().unwrap_or_default()
key=|comment| comment.id
let:comment
>
<Comment comment />
</For>
</ul>
</div>
</div>
</div>
}})}
</Suspense>
}
})
}
}
}))).build())
}
#[component]
pub fn Comment(comment: api::Comment) -> impl IntoView {
let (open, set_open) = create_signal(true);
let (open, set_open) = signal(true);
view! {
<li class="comment">
@@ -80,10 +77,10 @@ pub fn Comment(comment: api::Comment) -> impl IntoView {
{format!(" {}", comment.time_ago)}
</div>
<div class="text" inner_html=comment.content></div>
{(!comment.comments.is_empty()).then(move || {
{(!comment.comments.is_empty()).then(|| {
view! {
<div>
<div class="toggle" class:open=move ||open.get()>
<div class="toggle" class:open=open>
<a on:click=move |_| set_open.update(|n| *n = !*n)>
{
let comments_len = comment.comments.len();
@@ -113,7 +110,7 @@ pub fn Comment(comment: api::Comment) -> impl IntoView {
}
})}
</li>
}
}.into_any()
}
fn pluralize(n: usize) -> &'static str {

View File

@@ -1,12 +1,13 @@
use crate::api::{self, User};
use leptos::*;
use leptos_router::*;
use leptos::server::Resource;
use leptos::{either::Either, prelude::*};
use leptos_router::hooks::use_params_map;
#[component]
pub fn User() -> impl IntoView {
let params = use_params_map();
let user = create_resource(
move || params.get().get("id").cloned().unwrap_or_default(),
let user = Resource::new(
move || params.read().get("id").unwrap_or_default(),
move |id| async move {
if id.is_empty() {
None
@@ -17,12 +18,12 @@ pub fn User() -> impl IntoView {
);
view! {
<div class="user-view">
<Suspense fallback=|| view! { "Loading..." }>
{move || user.get().map(|user| match user {
None => view! { <h1>"User not found."</h1> }.into_any(),
Some(user) => view! {
<Suspense fallback=|| view! { "Loading..." }>
{move || Suspend(async move { match user.await.clone() {
None => Either::Left(view! { <h1>"User not found."</h1> }),
Some(user) => Either::Right(view! {
<div>
<h1>"User: " {&user.id}</h1>
<h1>"User: " {user.id.clone()}</h1>
<ul class="meta">
<li>
<span class="label">"Created: "</span> {user.created}
@@ -30,7 +31,7 @@ pub fn User() -> impl IntoView {
<li>
<span class="label">"Karma: "</span> {user.karma}
</li>
{user.about.as_ref().map(|about| view! { <li inner_html=about class="about"></li> })}
<li inner_html={user.about} class="about"></li>
</ul>
<p class="links">
<a href=format!("https://news.ycombinator.com/submitted?id={}", user.id)>"submissions"</a>
@@ -38,8 +39,8 @@ pub fn User() -> impl IntoView {
<a href=format!("https://news.ycombinator.com/threads?id={}", user.id)>"comments"</a>
</p>
</div>
}.into_any()
})}
})
}})}
</Suspense>
</div>
}

View File

@@ -11,18 +11,13 @@ codegen-units = 1
lto = true
[dependencies]
console_log = "1.0"
console_error_panic_hook = "0.1"
leptos = { path = "../../leptos", features = [
"experimental-islands",
] }
leptos_axum = { path = "../../integrations/axum", optional = true, features = [
"experimental-islands",
] }
leptos_axum = { path = "../../integrations/axum", optional = true }
leptos_meta = { path = "../../meta" }
leptos_router = { path = "../../router"}
log = "0.4"
simple_logger = "4.0"
serde = { version = "1.0", features = ["derive"] }
tracing = "0.1"
gloo-net = { version = "0.4", features = ["http"] }
@@ -44,8 +39,8 @@ mime_guess = { version = "2.0.4", optional = true }
[features]
default = []
csr = ["leptos/csr", "leptos_meta/csr", "leptos_router/csr"]
hydrate = ["leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate"]
csr = ["leptos/csr"]
hydrate = ["leptos/hydrate"]
ssr = [
"dep:axum",
"dep:tower",

View File

@@ -1,27 +1,30 @@
#![allow(unused)]
use leptos::Serializable;
#[cfg(feature = "ssr")]
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
#[cfg(feature = "ssr")]
pub fn story(path: &str) -> String {
format!("https://node-hnapi.herokuapp.com/{path}")
}
#[cfg(feature = "ssr")]
pub fn user(path: &str) -> String {
format!("https://hacker-news.firebaseio.com/v0/user/{path}.json")
}
lazy_static::lazy_static! {
static ref CLIENT: reqwest::Client = reqwest::Client::new();
}
#[cfg(feature = "ssr")]
pub async fn fetch_api<T>(path: &str) -> Option<T>
where
T: Serializable,
T: Serialize + DeserializeOwned,
{
let json = CLIENT.get(path).send().await.ok()?.text().await.ok()?;
T::de(&json).map_err(|e| log::error!("{e}")).ok()
use leptos::logging;
reqwest::get(path)
.await
.map_err(|e| logging::error!("{e}"))
.ok()?
.json()
.await
.ok()
}
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone)]

View File

@@ -1,28 +0,0 @@
use leptos::{view, Errors, For, IntoView, RwSignal, SignalGet, View};
// A basic function to display errors served by the error boundaries. Feel free to do more complicated things
// here than just displaying them
pub fn error_template(errors: Option<RwSignal<Errors>>) -> View {
let Some(errors) = errors else {
panic!("No Errors found and we expected errors!");
};
view! {
<h1>"Errors"</h1>
<For
// a function that returns the items we're iterating over; a signal is fine
each=move || errors.get()
// a unique key for each item as a reference
key=|(key, _)| key.clone()
// renders each item to a view
children= move | (_, error)| {
let error_string = error.to_string();
view! {
<p>"Error: " {error_string}</p>
}
}
/>
}
.into_view()
}

View File

@@ -1,11 +1,8 @@
use crate::error_template::error_template;
use axum::{
body::Body,
extract::State,
http::{header, Request, Response, StatusCode, Uri},
response::{IntoResponse, Response as AxumResponse},
};
use leptos::LeptosOptions;
use std::borrow::Cow;
#[cfg(not(debug_assertions))]
@@ -20,7 +17,6 @@ struct Assets;
pub async fn file_and_error_handler(
uri: Uri,
State(options): State<LeptosOptions>,
req: Request<Body>,
) -> AxumResponse {
let accept_encoding = req
@@ -34,11 +30,7 @@ pub async fn file_and_error_handler(
if res.status() == StatusCode::OK {
res.into_response()
} else {
let handler =
leptos_axum::render_app_to_stream(options.to_owned(), || {
error_template(None)
});
handler(req).await.into_response()
(StatusCode::NOT_FOUND, "Not found.").into_response()
}
}

View File

@@ -1,12 +1,32 @@
use leptos::*;
use leptos_meta::*;
use leptos_router::*;
use leptos::prelude::*;
mod api;
pub mod error_template;
mod routes;
use leptos_meta::{provide_meta_context, Link, Meta, MetaTags, Stylesheet};
use leptos_router::{
components::{FlatRoutes, Route, Router},
ParamSegment, StaticSegment,
};
use routes::{nav::*, stories::*, story::*, users::*};
#[cfg(feature = "ssr")]
pub mod fallback;
mod routes;
use routes::{nav::*, stories::*, story::*, users::*};
pub fn shell(options: LeptosOptions) -> impl IntoView {
view! {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<AutoReload options=options.clone() />
<HydrationScripts options islands=true/>
<MetaTags/>
</head>
<body>
<App/>
</body>
</html>
}
}
#[component]
pub fn App() -> impl IntoView {
@@ -19,11 +39,13 @@ pub fn App() -> impl IntoView {
<Router>
<Nav />
<main>
<Routes>
<Route path="users/:id" view=User ssr=SsrMode::InOrder/>
<Route path="stories/:id" view=Story ssr=SsrMode::InOrder/>
<Route path=":stories?" view=Stories ssr=SsrMode::InOrder/>
</Routes>
<FlatRoutes fallback=|| "Not found.">
<Route path=(StaticSegment("users"), ParamSegment("id")) view=User/>
<Route path=(StaticSegment("stories"), ParamSegment("id")) view=Story/>
<Route path=ParamSegment("stories") view=Stories/>
// TODO allow optional params without duplication
<Route path=StaticSegment("") view=Stories/>
</FlatRoutes>
</main>
</Router>
}
@@ -32,7 +54,6 @@ pub fn App() -> impl IntoView {
#[cfg(feature = "hydrate")]
#[wasm_bindgen::prelude::wasm_bindgen]
pub fn hydrate() {
#[cfg(debug_assertions)]
console_error_panic_hook::set_once();
leptos::leptos_dom::HydrationCtx::stop_hydrating();
leptos::mount::hydrate_islands();
}

View File

@@ -1,17 +1,16 @@
#[cfg(feature = "ssr")]
#[tokio::main]
async fn main() {
pub use axum::{routing::get, Router};
pub use hackernews_islands::fallback::file_and_error_handler;
pub use axum::Router;
use hackernews_islands::*;
pub use leptos::get_configuration;
pub use leptos::config::get_configuration;
pub use leptos_axum::{generate_route_list, LeptosRoutes};
use tower_http::compression::{
predicate::{NotForContentType, SizeAbove},
CompressionLayer, CompressionLevel, Predicate,
};
let conf = get_configuration(Some("Cargo.toml")).await.unwrap();
let conf = get_configuration(Some("Cargo.toml")).unwrap();
let leptos_options = conf.leptos_options;
let addr = leptos_options.site_addr;
let routes = generate_route_list(App);
@@ -26,14 +25,16 @@ async fn main() {
// build our application with a route
let app = Router::new()
.route("/favicon.ico", get(file_and_error_handler))
.leptos_routes(&leptos_options, routes, App)
.leptos_routes(&leptos_options, routes, {
let leptos_options = leptos_options.clone();
move || shell(leptos_options.clone())
})
.layer(
CompressionLayer::new()
.quality(CompressionLevel::Fastest)
.compress_when(predicate),
)
.fallback(file_and_error_handler)
.fallback(fallback::file_and_error_handler)
.with_state(leptos_options);
// run our app with hyper
@@ -49,8 +50,7 @@ async fn main() {
#[cfg(not(feature = "ssr"))]
pub fn main() {
use hackernews_islands::*;
use leptos::*;
_ = console_log::init_with_level(log::Level::Debug);
use leptos::prelude::*;
console_error_panic_hook::set_once();
mount_to_body(App);
leptos::mount::mount_to_body(App);
}

View File

@@ -1,5 +1,5 @@
use leptos::{component, view, IntoView};
use leptos_router::*;
use leptos::prelude::*;
use leptos_router::components::A;
#[component]
pub fn Nav() -> impl IntoView {
@@ -21,7 +21,7 @@ pub fn Nav() -> impl IntoView {
<A href="/job">
<strong>"Jobs"</strong>
</A>
<a class="github" href="http://github.com/gbj/leptos" target="_blank" rel="noreferrer">
<a class="github" href="http://github.com/leptos-rs/leptos" target="_blank" rel="noreferrer">
"Built with Leptos"
</a>
</nav>

View File

@@ -1,6 +1,9 @@
use crate::api;
use leptos::*;
use leptos_router::*;
use leptos::{either::Either, prelude::*};
use leptos_router::{
components::A,
hooks::{use_params_map, use_query_map},
};
fn category(from: &str) -> String {
match from {
@@ -13,7 +16,7 @@ fn category(from: &str) -> String {
.to_string()
}
#[server(FetchStories, "/api")]
#[server]
pub async fn fetch_stories(
story_type: String,
page: usize,
@@ -30,80 +33,84 @@ pub fn Stories() -> impl IntoView {
let params = use_params_map();
let page = move || {
query
.with(|q| q.get("page").and_then(|page| page.parse::<usize>().ok()))
.read()
.get("page")
.and_then(|page| page.parse::<usize>().ok())
.unwrap_or(1)
};
let story_type = move || {
params
.with(|p| p.get("stories").cloned())
.read()
.get("stories")
.unwrap_or_else(|| "top".to_string())
};
let stories = create_resource(
let stories = Resource::new(
move || (page(), story_type()),
move |(page, story_type)| fetch_stories(category(&story_type), page),
move |(page, story_type)| async move {
fetch_stories(category(&story_type), page).await.ok()
},
);
let (pending, set_pending) = create_signal(false);
let (pending, set_pending) = signal(false);
let hide_more_link = move || {
pending.get()
|| stories
.map(|stories| {
stories.as_ref().map(|s| s.len() < 28).unwrap_or_default()
})
.unwrap_or_default()
};
let hide_more_link = move || match &*stories.read() {
Some(Some(stories)) => stories.len() < 28,
_ => true
} || pending.get();
view! {
<div class="news-view">
<div class="news-list-nav">
<span>
{move || if page() > 1 {
view! {
Either::Left(view! {
<a class="page-link"
href=move || format!("/{}?page={}", story_type(), page() - 1)
attr:aria_label="Previous Page"
aria-label="Previous Page"
>
"< prev"
</a>
}.into_any()
})
} else {
view! {
Either::Right(view! {
<span class="page-link disabled" aria-hidden="true">
"< prev"
</span>
}.into_any()
})
}}
</span>
<span>"page " {page}</span>
<span class="page-link"
class:disabled=hide_more_link
aria-hidden=hide_more_link
>
<a href=move || format!("/{}?page={}", story_type(), page() + 1)
aria-label="Next Page"
<Suspense>
<span class="page-link"
class:disabled=hide_more_link
aria-hidden=hide_more_link
>
"more >"
</a>
</span>
<a href=move || format!("/{}?page={}", story_type(), page() + 1)
aria-label="Next Page"
>
"more >"
</a>
</span>
</Suspense>
</div>
<main class="news-list">
<div>
<Transition
fallback=|| ()
fallback=move || view! { <p>"Loading..."</p> }
set_pending
>
{move || stories.get().map(|story| story.map(|stories| view! {
<ul>
<For
each=move || stories.clone()
key=|story| story.id
let:story
>
<Story story/>
</For>
</ul>
}))}
<Show when=move || stories.read().as_ref().map(Option::is_none).unwrap_or(false)>
>
<p>"Error loading stories."</p>
</Show>
<ul>
<For
each=move || stories.get().unwrap_or_default().unwrap_or_default()
key=|story| story.id
let:story
>
<Story story/>
</For>
</ul>
</Transition>
</div>
</main>
@@ -118,26 +125,26 @@ fn Story(story: api::Story) -> impl IntoView {
<span class="score">{story.points}</span>
<span class="title">
{if !story.url.starts_with("item?id=") {
view! {
Either::Left(view! {
<span>
<a href=story.url target="_blank" rel="noreferrer">
{story.title.clone()}
</a>
<span class="host">"("{story.domain}")"</span>
</span>
}.into_view()
})
} else {
let title = story.title.clone();
view! { <A href=format!("/stories/{}", story.id)>{title.clone()}</A> }.into_view()
Either::Right(view! { <A href=format!("/stories/{}", story.id)>{title}</A> })
}}
</span>
<br />
<span class="meta">
{if story.story_type != "job" {
view! {
Either::Left(view! {
<span>
{"by "}
{story.user.map(|user| view ! { <A href=format!("/users/{user}")>{user.clone()}</A>})}
{story.user.map(|user| view ! { <A href=format!("/users/{user}")>{user.clone()}</A>})}
{format!(" {} | ", story.time_ago)}
<A href=format!("/stories/{}", story.id)>
{if story.comments_count.unwrap_or_default() > 0 {
@@ -147,10 +154,10 @@ fn Story(story: api::Story) -> impl IntoView {
}}
</A>
</span>
}.into_view()
})
} else {
let title = story.title.clone();
view! { <A href=format!("/item/{}", story.id)>{title.clone()}</A> }.into_view()
Either::Right(view! { <A href=format!("/item/{}", story.id)>{title}</A> })
}}
</span>
{(story.story_type != "link").then(|| view! {

View File

@@ -1,52 +1,37 @@
use crate::api;
use leptos::*;
use leptos_meta::*;
use leptos_router::*;
use std::cell::RefCell;
use leptos::either::Either;
use leptos::prelude::*;
use leptos_meta::Meta;
use leptos_router::components::A;
use leptos_router::hooks::use_params_map;
#[server(FetchStory, "/api")]
#[server]
pub async fn fetch_story(
id: String,
) -> Result<RefCell<Option<api::Story>>, ServerFnError> {
Ok(RefCell::new(
api::fetch_api::<api::Story>(&api::story(&format!("item/{id}"))).await,
))
) -> Result<Option<api::Story>, ServerFnError> {
Ok(api::fetch_api::<api::Story>(&api::story(&format!("item/{id}"))).await)
}
#[component]
pub fn Story() -> impl IntoView {
let params = use_params_map();
let story = create_resource(
move || params.get().get("id").cloned().unwrap_or_default(),
let story = Resource::new(
move || params.read().get("id").unwrap_or_default(),
move |id| async move {
if id.is_empty() {
Ok(RefCell::new(None))
Ok(None)
} else {
fetch_story(id).await
}
},
);
let meta_description = move || {
story
.map(|story| {
story
.as_ref()
.map(|story| {
story.borrow().as_ref().map(|story| story.title.clone())
})
.ok()
})
.flatten()
.flatten()
.unwrap_or_else(|| "Loading story...".to_string())
};
let story_view = move || {
story.map(|story| {
story.as_ref().ok().and_then(|story| {
let story: Option<api::Story> = story.borrow_mut().take();
story.map(|story| {
view! {
Suspense(SuspenseProps::builder().fallback(|| "Loading...").children(ToChildren::to_children(move || Suspend(async move {
match story.await.ok().flatten() {
None => Either::Left("Story not found."),
Some(story) => {
Either::Right(view! {
<Meta name="description" content=story.title.clone()/>
<div class="item-view">
<div class="item-view-header">
<a href=story.url target="_blank">
@@ -55,63 +40,36 @@ pub fn Story() -> impl IntoView {
<span class="host">
"("{story.domain}")"
</span>
{story.user.map(|user| view! { <p class="meta">
{story.user.map(|user| view! { <p class="meta">
{story.points}
" points | by "
<A href=format!("/users/{user}")>{user.clone()}</A>
{format!(" {}", story.time_ago)}
</p>})}
</div>
<div class="item-view-comments">
<p class="item-view-comments-header">
{if story.comments_count.unwrap_or_default() > 0 {
format!("{} comments", story.comments_count.unwrap_or_default())
} else {
"No comments yet.".into()
}}
</p>
<ul class="comment-children">
<For
each=move || story.comments.clone().unwrap_or_default()
key=|comment| comment.id
let:comment
>
<Comment comment />
</For>
</ul>
</div>
</div>
<div class="item-view-comments">
<p class="item-view-comments-header">
{if story.comments_count.unwrap_or_default() > 0 {
format!("{} comments", story.comments_count.unwrap_or_default())
} else {
"No comments yet.".into()
}}
</p>
<ul class="comment-children">
{story.comments.unwrap_or_default().into_iter()
.map(|comment: api::Comment| view! { <Comment comment /> })
.collect_view()}
</ul>
</div>
</div>
}})})})
};
view! {
<Meta name="description" content=meta_description/>
<Suspense fallback=|| ()>
{story_view}
</Suspense>
}
}
#[island]
pub fn Toggle(children: Children) -> impl IntoView {
let (open, set_open) = create_signal(true);
view! {
<div class="toggle" class:open=open>
<a on:click=move |_| set_open.update(|n| *n = !*n)>
{move || if open.get() {
"[-]"
} else {
"[+] comments collapsed"
}}
</a>
</div>
<ul
class="comment-children"
style:display=move || if open.get() {
"block"
} else {
"none"
})
}
>
{children()}
</ul>
}
}
}))).build())
}
#[component]
@@ -135,3 +93,29 @@ pub fn Comment(comment: api::Comment) -> impl IntoView {
</li>
}
}
#[island]
pub fn Toggle(children: Children) -> impl IntoView {
let (open, set_open) = signal(true);
view! {
<div class="toggle" class:open=open>
<a on:click=move |_| set_open.update(|n| *n = !*n)>
{move || if open.get() {
"[-]"
} else {
"[+] comments collapsed"
}}
</a>
</div>
<ul
class="comment-children"
style:display=move || if open.get() {
"block"
} else {
"none"
}
>
{children()}
</ul>
}
}

View File

@@ -1,20 +1,20 @@
#[allow(unused)] // User is unused in WASM build
use crate::api::{self, User};
use leptos::*;
use leptos_router::*;
use crate::api;
use leptos::server::Resource;
use leptos::{either::Either, prelude::*};
use leptos_router::hooks::use_params_map;
#[server(FetchUser, "/api")]
#[server]
pub async fn fetch_user(
id: String,
) -> Result<Option<api::User>, ServerFnError> {
Ok(api::fetch_api::<User>(&api::user(&id)).await)
Ok(api::fetch_api::<api::User>(&api::user(&id)).await)
}
#[component]
pub fn User() -> impl IntoView {
let params = use_params_map();
let user = create_resource(
move || params.get().get("id").cloned().unwrap_or_default(),
let user = Resource::new(
move || params.read().get("id").unwrap_or_default(),
move |id| async move {
if id.is_empty() {
Ok(None)
@@ -25,12 +25,12 @@ pub fn User() -> impl IntoView {
);
view! {
<div class="user-view">
<Suspense fallback=|| ()>
{move || user.get().map(|user| user.map(|user| match user {
None => view! { <h1>"User not found."</h1> }.into_view(),
Some(user) => view! {
<Suspense fallback=|| view! { "Loading..." }>
{move || Suspend(async move { match user.await.ok().flatten() {
None => Either::Left(view! { <h1>"User not found."</h1> }),
Some(user) => Either::Right(view! {
<div>
<h1>"User: " {&user.id}</h1>
<h1>"User: " {user.id.clone()}</h1>
<ul class="meta">
<li>
<span class="label">"Created: "</span> {user.created}
@@ -46,8 +46,8 @@ pub fn User() -> impl IntoView {
<a href=format!("https://news.ycombinator.com/threads?id={}", user.id)>"comments"</a>
</p>
</div>
}.into_view()
}))}
})
}})}
</Suspense>
</div>
}

View File

@@ -1,5 +1,5 @@
[package]
name = "hackernews-js-fetch"
name = "hackernews_js_fetch"
version = "0.1.0"
edition = "2021"
@@ -11,63 +11,63 @@ codegen-units = 1
lto = true
[dependencies]
console_log = "1.0.0"
console_error_panic_hook = "0.1.7"
cfg-if = "1.0.0"
console_error_panic_hook = "0.1"
console_log = "1.0"
leptos = { path = "../../leptos" }
leptos_axum = { path = "../../integrations/axum", default-features = false, optional = true }
leptos_meta = { path = "../../meta" }
leptos_router = { path = "../../router" }
log = "0.4.17"
simple_logger = "4.0.0"
serde = { version = "1.0.148", features = ["derive"] }
leptos_server = { path = "../../leptos_server", optional = true }
serde = { version = "1.0", features = ["derive"] }
tracing = "0.1"
gloo-net = { version = "0.4.0", features = ["http"] }
reqwest = { version = "0.11.13", features = ["json"] }
gloo-net = { version = "0.5", features = ["http"] }
reqwest = { version = "0.12", features = ["json"] }
axum = { version = "0.7", default-features = false, optional = true }
tower = { version = "0.4.13", optional = true }
http = { version = "0.2.11", optional = true }
tower = { version = "0.4", optional = true }
http = { version = "1.0", optional = true }
web-sys = { version = "0.3", features = [
"AbortController",
"AbortSignal",
"Request",
"Response",
] }
getrandom = { version = "0.2.7", features = ["js"] }
wasm-bindgen = "0.2"
wasm-bindgen-futures = { version = "0.4.37", features = [
"futures-core-03-stream",
], optional = true }
axum-js-fetch = { git = "https://github.com/seanaye/axum-js-fetch", optional = true }
lazy_static = "1.4.0"
send_wrapper = { version = "0.6.0", features = ["futures"] }
[features]
hydrate = ["leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate"]
hydrate = ["leptos/hydrate"]
ssr = [
"dep:axum",
"dep:tower",
"dep:http",
"dep:axum",
"dep:wasm-bindgen-futures",
"dep:axum-js-fetch",
"leptos/ssr",
"leptos_axum/wasm",
"leptos_meta/ssr",
"leptos_router/ssr",
"leptos_server/serde-wasm-bindgen",
]
[package.metadata.cargo-all-features]
denylist = ["axum", "tower", "http", "leptos_axum"]
skip_feature_sets = [["ssr", "hydrate"]]
skip_feature_sets = [["csr", "ssr"], ["csr", "hydrate"], ["ssr", "hydrate"]]
[package.metadata.leptos]
# The name used by wasm-bindgen/cargo-leptos for the JS/WASM bundle. Defaults to the crate name
output-name = "hackernews_axum"
output-name = "hackernews_js_fetch"
# The site root folder is where cargo-leptos generate all output. WARNING: all content of this folder will be erased on a rebuild. Use it in your server setup.
site-root = "target/site"
# The site-root relative folder where all compiled output (JS, WASM and CSS) is written
# Defaults to pkg
site-pkg-dir = "pkg"
# [Optional] The source CSS file. If it ends with .sass or .scss then it will be compiled by dart-sass into CSS. The CSS is optimized by Lightning CSS before being written to <site-root>/<site-pkg>/app.css
style-file = "./style.css"
style-file = "style.css"
# [Optional] Files in the asset-dir will be copied to the site-root directory
assets-dir = "public"
# The IP and port (ex: 127.0.0.1:3000) where the server serves the content. Use it in your server setup.

View File

@@ -1,6 +1,6 @@
extend = [
{ path = "../cargo-make/main.toml" },
{ path = "../cargo-make/deno-build.toml" },
{ path = "../cargo-make/main.toml" },
{ path = "../cargo-make/deno-build.toml" },
]
[env]

Some files were not shown because too many files have changed in this diff Show More