Skip to content
This repository was archived by the owner on Jul 19, 2020. It is now read-only.

Commit 563216a

Browse files
jstarrygitbook-bot
authored andcommitted
GitBook: [master] 29 pages modified
1 parent 2bdbafc commit 563216a

24 files changed

+200
-345
lines changed

‎src/README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ Rust also helps developers write safer code with its rich type system and owners
4040

4141
#### Alternatives?
4242

43-
We love to share ideas with other projects and believe we can all help each other reach the full potential of this exciting new technology. If you're not into Yew, you may like the following projects \(listed alphabetically\)
43+
We love to share ideas with other projects and believe we can all help each other reach the full potential of this exciting new technology. If you're not into Yew, you may like the following projects:
4444

45-
* [Draco](https://github.com/utkarshkukreti/draco) - _"A Rust library for building client side web applications with Web Assembly"_
4645
* [Percy](https://github.com/chinedufn/percy) - _"A modular toolkit for building isomorphic web apps with Rust + WebAssembly"_
4746
* [Seed](https://github.com/seed-rs/seed) - _"A Rust framework for creating web apps"_
48-
* [Smithy](https://github.com/rbalicki2/smithy) - _"A framework for building WebAssembly apps in Rust"_
47+
48+
4949

‎src/advanced-topics/optimizations.md‎

Lines changed: 17 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@ description: Make your app faster
66

77
## neq\_assign
88

9-
When a component receives props from its parent component, the `change` method is called. This, in
10-
addition to allowing you to update the component's state, also allows you to return a `ShouldRender`
11-
boolean value that indicates if the component should re-render itself in response to the prop changes.
9+
When a component receives props from its parent component, the `change` method is called. This, in addition to allowing you to update the component's state, also allows you to return a `ShouldRender` boolean value that indicates if the component should re-render itself in response to the prop changes.
1210

13-
Re-rendering is expensive, and if you can avoid it, you should. As a general rule, you only want to
14-
re-render when the props actually changed. The following block of code represents this rule,
15-
returning `true` if the props differed from the previous props:
11+
Re-rendering is expensive, and if you can avoid it, you should. As a general rule, you only want to re-render when the props actually changed. The following block of code represents this rule, returning `true` if the props differed from the previous props:
1612

1713
```rust
1814
use yew::ShouldRender;
@@ -36,68 +32,37 @@ impl Example {
3632
}
3733
```
3834

39-
But we can go further! This is six lines of boilerplate can be reduced down to one by using a trait
40-
and a blanket implementation for anything that implements `PartialEq`. Check out the `yewtil` crate's
41-
`NeqAssign` trait [here](https://docs.rs/yewtil/*/yewtil/trait.NeqAssign.html).
35+
But we can go further! This is six lines of boilerplate can be reduced down to one by using a trait and a blanket implementation for anything that implements `PartialEq`. Check out the `yewtil` crate's `NeqAssign` trait [here](https://docs.rs/yewtil/*/yewtil/trait.NeqAssign.html).
4236

4337
## RC
4438

45-
In an effort to avoid cloning large chunks of data to create props when re-rendering, we can use
46-
smart pointers to only clone the pointer instead. If you use `Rc<_>`s in your props and child
47-
components instead of plain unboxed values, you can delay cloning until you need to modify the data
48-
in the child component, where you use `Rc::make_mut` to clone and get a mutable reference to the data
49-
you want to alter. By not cloning until mutation, child components can reject props identical to
50-
their state-owned props in `Component::change` for almost no performance cost, versus the case where
51-
the data itself needs to be copied into the props struct in the parent before it is compared and
52-
rejected in the child.
39+
In an effort to avoid cloning large chunks of data to create props when re-rendering, we can use smart pointers to only clone the pointer instead. If you use `Rc<_>`s in your props and child components instead of plain unboxed values, you can delay cloning until you need to modify the data in the child component, where you use `Rc::make_mut` to clone and get a mutable reference to the data you want to alter. By not cloning until mutation, child components can reject props identical to their state-owned props in `Component::change` for almost no performance cost, versus the case where the data itself needs to be copied into the props struct in the parent before it is compared and rejected in the child.
5340

54-
This optimization is most useful for data types that aren't `Copy`. If you can copy your data easily,
55-
then it probably isn't worth putting it behind a smart pointer. For structures that can contain lots
56-
of data like `Vec`, `HashMap`, and `String`, this optimization should be worthwhile.
41+
This optimization is most useful for data types that aren't `Copy`. If you can copy your data easily, then it probably isn't worth putting it behind a smart pointer. For structures that can contain lots of data like `Vec`, `HashMap`, and `String`, this optimization should be worthwhile.
5742

58-
This optimization works best if the values are never updated by the children, and even better, if
59-
they are rarely updated by parents. This makes `Rc<_>s` a good choice for wrapping property values in
60-
for pure components.
43+
This optimization works best if the values are never updated by the children, and even better, if they are rarely updated by parents. This makes `Rc<_>s` a good choice for wrapping property values in for pure components.
6144

6245
## View Functions
6346

64-
For code readability reasons, it often makes sense to migrate sections of `html!` to their own
65-
functions so you can avoid the rightward drift present in deeply nested HTML.
47+
For code readability reasons, it often makes sense to migrate sections of `html!` to their own functions so you can avoid the rightward drift present in deeply nested HTML.
6648

6749
## Pure Components/Function Components
6850

69-
Pure components are components that don't mutate their state, only displaying content and propagating
70-
messages up to normal, mutable components. They differ from view functions in that they can be used
71-
from within the `html!` macro using the component syntax \(`<SomePureComponent />`\) instead of
72-
expression syntax \(`{some_view_function()}`\), and that depending on their implementation, they can
73-
be memoized - preventing re-renders for identical props using the aforementioned `neq_assign` logic.
51+
Pure components are components that don't mutate their state, only displaying content and propagating messages up to normal, mutable components. They differ from view functions in that they can be used from within the `html!` macro using the component syntax \(`<SomePureComponent />`\) instead of expression syntax \(`{some_view_function()}`\), and that depending on their implementation, they can be memoized - preventing re-renders for identical props using the aforementioned `neq_assign` logic.
7452

7553
Yew doesn't natively support pure or function components, but they are available via external crates.
7654

77-
Function components don't exist yet, but in theory, pure components could be generated by using proc
78-
macros and annotating functions.
55+
Function components don't exist yet, but in theory, pure components could be generated by using proc macros and annotating functions.
7956

8057
## Keyed DOM nodes when they arrive
8158

8259
## Compile speed optimizations using Cargo Workspaces
8360

84-
Arguably, the largest drawback to using Yew is the long time it takes to compile. Compile time seems
85-
to correlate with the quantity of code found within `html!` macro blocks. This tends to not be a
86-
significant problem for smaller projects, but for web apps that span multiple pages, it makes sense
87-
to break apart your code across multiple crates to minimize the amount of work the compiler has to do
88-
for each change made.
61+
Arguably, the largest drawback to using Yew is the long time it takes to compile. Compile time seems to correlate with the quantity of code found within `html!` macro blocks. This tends to not be a significant problem for smaller projects, but for web apps that span multiple pages, it makes sense to break apart your code across multiple crates to minimize the amount of work the compiler has to do for each change made.
8962

90-
You should try to make your main crate handle routing/page selection, move all commonly shared code
91-
to another crate, and then make a different crate for each page, where each page could be a different
92-
component, or just a big function that produces `Html`. In the best case scenario, you go from
93-
rebuilding all of your code on each compile to rebuilding only the main crate, and one of your page
94-
crates. In the worst case, where you edit something in the "common" crate, you will be right back to
95-
where you started: compiling all code that depends on that commonly shared crate, which is
96-
probably everything else.
63+
You should try to make your main crate handle routing/page selection, move all commonly shared code to another crate, and then make a different crate for each page, where each page could be a different component, or just a big function that produces `Html`. In the best case scenario, you go from rebuilding all of your code on each compile to rebuilding only the main crate, and one of your page crates. In the worst case, where you edit something in the "common" crate, you will be right back to where you started: compiling all code that depends on that commonly shared crate, which is probably everything else.
9764

98-
If your main crate is too heavyweight, or you want to rapidly iterate on a deeply nested page \(eg. a
99-
page that renders on top of another page\), you can use an example crate to create a more simple
100-
implementation of the main page and render your work-in-progress component on top of that.
65+
If your main crate is too heavyweight, or you want to rapidly iterate on a deeply nested page \(eg. a page that renders on top of another page\), you can use an example crate to create a more simple implementation of the main page and render your work-in-progress component on top of that.
10166

10267
## Build size optimization
10368

@@ -106,19 +71,13 @@ implementation of the main page and render your work-in-progress component on to
10671
* `cargo.toml` \( defining release profile \)
10772
* optimize wasm code using `wasm-opt`
10873

109-
More information about code size profiling:
110-
[rustwasm book](https://rustwasm.github.io/book/reference/code-size.html#optimizing-builds-for-code-size)
74+
More information about code size profiling: [rustwasm book](https://rustwasm.github.io/book/reference/code-size.html#optimizing-builds-for-code-size)
11175

11276
### wee\_alloc
11377

114-
[wee\_alloc](https://github.com/rustwasm/wee_alloc) is a tiny allocator that is much smaller than the
115-
allocator that is normally used in Rust binaries. Replacing the default allocator with this one will
116-
result in smaller WASM file sizes, at the expense of speed and memory overhead.
78+
[wee\_alloc](https://github.com/rustwasm/wee_alloc) is a tiny allocator that is much smaller than the allocator that is normally used in Rust binaries. Replacing the default allocator with this one will result in smaller WASM file sizes, at the expense of speed and memory overhead.
11779

118-
The slower speed and memory overhead are minor in comparison to the size gains made by not including
119-
the default allocator. This smaller file size means that your page will load faster, and so it is
120-
generally recommended that you use this allocator over the default, unless your app is doing some
121-
allocation-heavy work.
80+
The slower speed and memory overhead are minor in comparison to the size gains made by not including the default allocator. This smaller file size means that your page will load faster, and so it is generally recommended that you use this allocator over the default, unless your app is doing some allocation-heavy work.
12281

12382
```rust
12483
// Use `wee_alloc` as the global allocator.
@@ -152,8 +111,7 @@ Further more it is possible to optimize size of `wasm` code.
152111

153112
wasm-opt info: [binaryen project](https://github.com/WebAssembly/binaryen)
154113

155-
The Rust Wasm book features a section about reducing the size of WASM binaries:
156-
[Shrinking .wasm size](https://rustwasm.github.io/book/game-of-life/code-size.html)
114+
The Rust Wasm book features a section about reducing the size of WASM binaries: [Shrinking .wasm size](https://rustwasm.github.io/book/game-of-life/code-size.html)
157115

158116
* using `wasm-pack` which by default optimizes `wasm` code in release builds
159117
* using `wasm-opt` directly on `wasm` files.
@@ -164,8 +122,7 @@ wasm-opt wasm_bg.wasm -Os -o wasm_bg_opt.wasm
164122

165123
#### Build size of 'minimal' example in yew/examples/
166124

167-
Note: `wasm-pack` combines optimization for Rust and Wasm code. `wasm-bindgen` is used in this
168-
example without any Rust size optimization.
125+
Note: `wasm-pack` combines optimization for Rust and Wasm code. `wasm-bindgen` is used in this example without any Rust size optimization.
169126

170127
| used tool | size |
171128
| :--- | :--- |

‎src/concepts/agents.md‎

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,9 @@ description: Yew's Actor System
44

55
# Agents
66

7-
Agents are similar to Angular's [Services](https://angular.io/guide/architecture-services) \(but
8-
without dependency injection\), and provide a Yew with an
9-
[Actor Model](https://en.wikipedia.org/wiki/Actor_model). Agents can be used to route messages
10-
between components independently of where they sit in the component hierarchy, or they can be used to
11-
create a shared state, and they can also be used to offload computationally expensive tasks from the
12-
main thread which renders the UI. There is also planned support for using agents to allow Yew
13-
applications to communicate across tabs \(in the future\).
7+
Agents are similar to Angular's [Services](https://angular.io/guide/architecture-services) \(but without dependency injection\), and provide a Yew with an [Actor Model](https://en.wikipedia.org/wiki/Actor_model). Agents can be used to route messages between components independently of where they sit in the component hierarchy, or they can be used to create a shared state, and they can also be used to offload computationally expensive tasks from the main thread which renders the UI. There is also planned support for using agents to allow Yew applications to communicate across tabs \(in the future\).
148

15-
In order for agents to run concurrently, Yew uses
16-
[web-workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers).
9+
In order for agents to run concurrently, Yew uses [web-workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers).
1710

1811
## Lifecycle
1912

@@ -24,12 +17,19 @@ In order for agents to run concurrently, Yew uses
2417
#### Reaches
2518

2619
* Context - There will exist at most one instance of a Context Agent at any given time. Bridges will
27-
spawn or connect to an already spawned agent on the UI thread. This can be used to coordinate state
28-
between components or other agents. When no bridges are connected to this agent, the agent will
29-
disappear.
20+
21+
spawn or connect to an already spawned agent on the UI thread. This can be used to coordinate state
22+
23+
between components or other agents. When no bridges are connected to this agent, the agent will
24+
25+
disappear.
26+
3027
* Job - Spawn a new agent on the UI thread for every new bridge. This is good for moving shared but
31-
independent behavior that communicates with the browser out of components. \(TODO verify\) When the
32-
task is done, the agent will disappear.
28+
29+
independent behavior that communicates with the browser out of components. \(TODO verify\) When the
30+
31+
task is done, the agent will disappear.
32+
3333
* Public - Same as Context, but runs on its own web worker.
3434
* Private - Same as Job, but runs on its own web worker.
3535
* Global \(WIP\)
@@ -38,24 +38,19 @@ task is done, the agent will disappear.
3838

3939
### Bridges
4040

41-
A bridge allows bi-directional communication between an agent and a component. Bridges also allow
42-
agents to communicate with one another.
41+
A bridge allows bi-directional communication between an agent and a component. Bridges also allow agents to communicate with one another.
4342

4443
### Dispatchers
4544

46-
A dispatcher allows uni-directional communication between a component and an agent. A dispatcher
47-
allows a component to send messages to an agent.
45+
A dispatcher allows uni-directional communication between a component and an agent. A dispatcher allows a component to send messages to an agent.
4846

4947
## Overhead
5048

51-
Agents that live in their own separate web worker (Private and Public) incur serialization overhead
52-
on the messages they send and receive. They use [bincode](https://github.com/servo/bincode) to
53-
communicate with other threads, so the cost is substantially higher than just calling a function.
54-
Unless the cost of computation will outweigh the cost of message passing, you should contain your
55-
logic in the UI thread agents (Job or Context).
49+
Agents that live in their own separate web worker \(Private and Public\) incur serialization overhead on the messages they send and receive. They use [bincode](https://github.com/servo/bincode) to communicate with other threads, so the cost is substantially higher than just calling a function. Unless the cost of computation will outweigh the cost of message passing, you should contain your logic in the UI thread agents \(Job or Context\).
5650

5751
## Further reading
5852

5953
* The [pub\_sub](https://github.com/yewstack/yew/tree/master/examples/pub_sub) example shows how
60-
components can use agents to communicate with each other.
54+
55+
components can use agents to communicate with each other.
6156

0 commit comments

Comments
 (0)