Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Remove context & job agent #2295

Merged
merged 11 commits into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ members = [
"examples/js_callback",
"examples/keyed_list",
"examples/mount_point",
"examples/multi_thread",
"examples/agents",
"examples/nested_list",
"examples/node_refs",
"examples/password_strength",
"examples/portals",
"examples/pub_sub",
"examples/contexts",
"examples/router",
Copy link
Member

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

"examples/store",
"examples/timer",
"examples/todomvc",
"examples/two_apps",
Expand Down
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"
Expand Down
18 changes: 18 additions & 0 deletions examples/agents/README.md
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.
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>();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use multi_thread::native_worker::Worker;
use agents::native_worker::Worker;
use yew_agent::Threaded;

fn main() {
Expand Down
49 changes: 49 additions & 0 deletions examples/agents/src/lib.rs
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>
}
}
}
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"
Expand Down
10 changes: 10 additions & 0 deletions examples/contexts/README.md
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.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Yew • Pub Sub</title>
<title>Yew • Context</title>
</head>

<body></body>
Expand Down
23 changes: 23 additions & 0 deletions examples/contexts/src/main.rs
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would name this App and keep it in a separate app.rs file, but up to you m8 :D

html! {
<MessageProvider>
<Producer />
<Subscriber />
</MessageProvider>
}
}

fn main() {
yew::start_app::<Model>();
}
37 changes: 37 additions & 0 deletions examples/contexts/src/msg_ctx.rs
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>>
}
}
16 changes: 16 additions & 0 deletions examples/contexts/src/producer.rs
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>
}
}
14 changes: 14 additions & 0 deletions examples/contexts/src/subscriber.rs
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>
}
}
18 changes: 0 additions & 18 deletions examples/multi_thread/README.md

This file was deleted.

61 changes: 0 additions & 61 deletions examples/multi_thread/src/context.rs

This file was deleted.

71 changes: 0 additions & 71 deletions examples/multi_thread/src/job.rs

This file was deleted.

Loading