-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove context & job agent #2295
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
408f6fc
Remove context and job agent.
futursolo 44a42df
Revert "Remove context and job agent."
futursolo 301f742
Revert "Revert "Remove context and job agent.""
futursolo c462ba1
Update example.
futursolo 7cf49d1
Fix docs.
futursolo 31619a6
Fix test.
futursolo 42f9c40
Merge remote-tracking branch 'origin/master' into remove-context-job-…
futursolo 33792c4
Rename examples.
futursolo 4edc069
Fix examples & docs.
futursolo 054b57f
Update website/docs/concepts/function-components/custom-hooks.mdx
futursolo 06b2b55
Examples in alphabetical order.
futursolo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[package] | ||
name = "multi_thread" | ||
name = "agents" | ||
version = "0.1.0" | ||
authors = ["Denis Kolodin <[email protected]>"] | ||
edition = "2018" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Warning | ||
|
||
The agents example is a conceptual WIP and is currently blocked on [a future Trunk feature](https://github.com/thedodd/trunk/issues/46) | ||
|
||
There is an alternate agent example [here](https://github.com/yewstack/yew/tree/master/examples/web_worker_fib). | ||
|
||
|
||
# Multi-Thread Example | ||
|
||
[![Demo](https://img.shields.io/website?label=demo&url=https%3A%2F%2Fexamples.yew.rs%2Fagents)](https://examples.yew.rs/agents) | ||
|
||
|
||
## Concepts | ||
|
||
Uses an [Agent] that runs in a [Web Worker]. | ||
|
||
[agent]: https://yew.rs/docs/concepts/agents/ | ||
[web worker]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers |
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
examples/multi_thread/src/bin/app.rs → examples/agents/src/bin/app.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
fn main() { | ||
wasm_logger::init(wasm_logger::Config::default()); | ||
yew::start_app::<multi_thread::Model>(); | ||
yew::start_app::<agents::Model>(); | ||
} |
2 changes: 1 addition & 1 deletion
2
examples/multi_thread/src/bin/worker.rs → examples/agents/src/bin/worker.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
pub mod native_worker; | ||
|
||
use yew::{html, Component, Context, Html}; | ||
use yew_agent::{Bridge, Bridged}; | ||
|
||
pub enum Msg { | ||
SendToWorker, | ||
DataReceived, | ||
} | ||
|
||
pub struct Model { | ||
worker: Box<dyn Bridge<native_worker::Worker>>, | ||
} | ||
|
||
impl Component for Model { | ||
type Message = Msg; | ||
type Properties = (); | ||
|
||
fn create(ctx: &Context<Self>) -> Self { | ||
let link = ctx.link(); | ||
let callback = link.callback(|_| Msg::DataReceived); | ||
let worker = native_worker::Worker::bridge(callback); | ||
|
||
Self { worker } | ||
} | ||
|
||
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool { | ||
match msg { | ||
Msg::SendToWorker => { | ||
self.worker.send(native_worker::Request::GetDataFromServer); | ||
false | ||
} | ||
Msg::DataReceived => { | ||
log::info!("DataReceived"); | ||
false | ||
} | ||
} | ||
} | ||
|
||
fn view(&self, ctx: &Context<Self>) -> Html { | ||
html! { | ||
<div> | ||
<nav class="menu"> | ||
<button onclick={ctx.link().callback(|_| Msg::SendToWorker)}>{ "Send to Thread" }</button> | ||
</nav> | ||
</div> | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[package] | ||
name = "pub_sub" | ||
name = "contexts" | ||
version = "0.1.0" | ||
edition = "2018" | ||
license = "MIT OR Apache-2.0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Context Example | ||
|
||
[![Demo](https://img.shields.io/website?label=demo&url=https%3A%2F%2Fexamples.yew.rs%2Fcontexts)](https://examples.yew.rs/contexts) | ||
|
||
This is currently a technical demonstration of Context API. | ||
|
||
## Concepts | ||
|
||
The example has two components, which communicates through a context | ||
as opposed to the traditional method using component links. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
mod msg_ctx; | ||
mod producer; | ||
mod subscriber; | ||
|
||
use producer::Producer; | ||
use subscriber::Subscriber; | ||
use yew::prelude::*; | ||
|
||
use msg_ctx::MessageProvider; | ||
|
||
#[function_component] | ||
pub fn Model() -> Html { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would name this |
||
html! { | ||
<MessageProvider> | ||
<Producer /> | ||
<Subscriber /> | ||
</MessageProvider> | ||
} | ||
} | ||
|
||
fn main() { | ||
yew::start_app::<Model>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use std::rc::Rc; | ||
|
||
use yew::prelude::*; | ||
|
||
#[derive(Debug, PartialEq, Clone)] | ||
pub struct Message { | ||
pub inner: String, | ||
} | ||
|
||
impl Reducible for Message { | ||
type Action = String; | ||
|
||
fn reduce(self: Rc<Self>, action: Self::Action) -> Rc<Self> { | ||
Message { inner: action }.into() | ||
} | ||
} | ||
|
||
pub type MessageContext = UseReducerHandle<Message>; | ||
|
||
#[derive(Properties, Debug, PartialEq)] | ||
pub struct MessageProviderProps { | ||
#[prop_or_default] | ||
pub children: Children, | ||
} | ||
|
||
#[function_component] | ||
pub fn MessageProvider(props: &MessageProviderProps) -> Html { | ||
let msg = use_reducer(|| Message { | ||
inner: "No message yet.".to_string(), | ||
}); | ||
|
||
html! { | ||
<ContextProvider<MessageContext> context={msg}> | ||
{props.children.clone()} | ||
</ContextProvider<MessageContext>> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use yew::prelude::*; | ||
|
||
use super::msg_ctx::MessageContext; | ||
|
||
#[function_component] | ||
pub fn Producer() -> Html { | ||
let msg_ctx = use_context::<MessageContext>().unwrap(); | ||
|
||
let onclick = Callback::from(move |_| msg_ctx.dispatch("Message Received.".to_string())); | ||
|
||
html! { | ||
<button {onclick}> | ||
{"PRESS ME"} | ||
</button> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use super::msg_ctx::MessageContext; | ||
|
||
use yew::prelude::*; | ||
|
||
#[function_component] | ||
pub fn Subscriber() -> Html { | ||
let msg_ctx = use_context::<MessageContext>().unwrap(); | ||
|
||
let message = msg_ctx.inner.to_owned(); | ||
|
||
html! { | ||
<h1>{ message }</h1> | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please sort this alphabetically so it's easier to find members based on the directory structure