Skip to content

Commit ffbb9e0

Browse files
authored
Rename methods and loosen mutability for component link and agent (yewstack#780)
* Rename methods and loosen mutability for component link and agent * Update send_back to callback * mut link -> link
1 parent 9e8ccac commit ffbb9e0

File tree

19 files changed

+56
-60
lines changed

19 files changed

+56
-60
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl Agent for Worker {
164164
Worker { link }
165165
}
166166

167-
// Handle inner messages (of services of `send_back` callbacks)
167+
// Handle inner messages (from callbacks)
168168
fn update(&mut self, msg: Self::Message) { /* ... */ }
169169

170170
// Handle incoming messages from components of other agents.
@@ -195,7 +195,7 @@ impl Component for Model {
195195
type Properties = ();
196196

197197
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
198-
let callback = link.send_back(|_| Msg::ContextMsg);
198+
let callback = link.callback(|_| Msg::ContextMsg);
199199
// `Worker::bridge` spawns an instance if no one is available
200200
let context = context::Worker::bridge(callback); // Connected! :tada:
201201
Model { context }
@@ -362,7 +362,7 @@ impl Component for Model {
362362
fn update(&mut self, msg: Self::Message) -> ShouldRender {
363363
match msg {
364364
Msg::Fire => {
365-
let send_msg = self.link.send_back(|_| Msg::Timeout);
365+
let send_msg = self.link.callback(|_| Msg::Timeout);
366366
self.timeout.spawn(Duration::from_secs(5), send_msg);
367367
}
368368
Msg::Timeout => {

examples/dashboard/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl Component for Model {
8787
self.fetching = true;
8888
let task = match format {
8989
Format::Json => {
90-
let callback = self.link.send_back(
90+
let callback = self.link.callback(
9191
move |response: Response<Json<Result<DataFromFile, Error>>>| {
9292
let (meta, Json(data)) = response.into_parts();
9393
println!("META: {:?}, {:?}", meta, data);
@@ -106,7 +106,7 @@ impl Component for Model {
106106
}
107107
}
108108
Format::Toml => {
109-
let callback = self.link.send_back(
109+
let callback = self.link.callback(
110110
move |response: Response<Toml<Result<DataFromFile, Error>>>| {
111111
let (meta, Toml(data)) = response.into_parts();
112112
println!("META: {:?}, {:?}", meta, data);
@@ -129,8 +129,8 @@ impl Component for Model {
129129
}
130130
Msg::WsAction(action) => match action {
131131
WsAction::Connect => {
132-
let callback = self.link.send_back(|Json(data)| Msg::WsReady(data));
133-
let notification = self.link.send_back(|status| match status {
132+
let callback = self.link.callback(|Json(data)| Msg::WsReady(data));
133+
let notification = self.link.callback(|status| match status {
134134
WebSocketStatus::Opened => Msg::Ignore,
135135
WebSocketStatus::Closed | WebSocketStatus::Error => WsAction::Lost.into(),
136136
});

examples/file_upload/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ impl Component for Model {
4848
for file in files.into_iter() {
4949
let task = {
5050
if chunks {
51-
let callback = self.link.send_back(Msg::Chunk);
51+
let callback = self.link.callback(Msg::Chunk);
5252
self.reader.read_file_by_chunks(file, callback, 10)
5353
} else {
54-
let callback = self.link.send_back(Msg::Loaded);
54+
let callback = self.link.callback(Msg::Loaded);
5555
self.reader.read_file(file, callback)
5656
}
5757
};

examples/futures/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl Component for Model {
9494
};
9595
self.link.send_future(future);
9696
self.link
97-
.send_self(SetMarkdownFetchState(FetchState::Fetching));
97+
.send_message(SetMarkdownFetchState(FetchState::Fetching));
9898
false
9999
}
100100
}

examples/game_of_life/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ impl Component for Model {
155155
type Message = Msg;
156156
type Properties = ();
157157

158-
fn create(_: Self::Properties, mut link: ComponentLink<Self>) -> Self {
159-
let callback = link.send_back(|_| Msg::Tick);
158+
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
159+
let callback = link.callback(|_| Msg::Tick);
160160
let mut interval = IntervalService::new();
161161
let handle = interval.spawn(Duration::from_millis(200), callback);
162162
Model {

examples/js_callback/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl Component for Model {
4343
}
4444
}
4545
AsyncPayload => {
46-
get_payload_later(self.link.send_back(Msg::Payload));
46+
get_payload_later(self.link.callback(Msg::Payload));
4747
false
4848
}
4949
}

examples/multi_thread/src/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl Agent for Worker {
3737
fn create(link: AgentLink<Self>) -> Self {
3838
let mut interval = IntervalService::new();
3939
let duration = Duration::from_secs(3);
40-
let callback = link.send_back(|_| Msg::Updating);
40+
let callback = link.callback(|_| Msg::Updating);
4141
let task = interval.spawn(duration, callback);
4242
Worker {
4343
link,

examples/multi_thread/src/job.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl Agent for Worker {
3737
fn create(link: AgentLink<Self>) -> Self {
3838
let mut interval = IntervalService::new();
3939
let duration = Duration::from_secs(3);
40-
let callback = link.send_back(|_| Msg::Updating);
40+
let callback = link.callback(|_| Msg::Updating);
4141
let task = interval.spawn(duration, callback);
4242
Worker {
4343
link,

examples/multi_thread/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ impl Component for Model {
2626
type Message = Msg;
2727
type Properties = ();
2828

29-
fn create(_: Self::Properties, mut link: ComponentLink<Self>) -> Self {
30-
let callback = link.send_back(|_| Msg::DataReceived);
29+
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
30+
let callback = link.callback(|_| Msg::DataReceived);
3131
let worker = native_worker::Worker::bridge(callback);
3232

33-
let callback = link.send_back(|_| Msg::DataReceived);
33+
let callback = link.callback(|_| Msg::DataReceived);
3434
let job = job::Worker::bridge(callback);
3535

36-
let callback = link.send_back(|_| Msg::DataReceived);
36+
let callback = link.callback(|_| Msg::DataReceived);
3737
let context = context::Worker::bridge(callback);
3838

39-
let callback = link.send_back(|_| Msg::DataReceived);
39+
let callback = link.callback(|_| Msg::DataReceived);
4040
let context_2 = context::Worker::bridge(callback);
4141

4242
Model {

examples/multi_thread/src/native_worker.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl Agent for Worker {
3737
fn create(link: AgentLink<Self>) -> Self {
3838
let mut interval = IntervalService::new();
3939
let duration = Duration::from_secs(3);
40-
let callback = link.send_back(|_| Msg::Updating);
40+
let callback = link.callback(|_| Msg::Updating);
4141
let task = interval.spawn(duration, callback);
4242
Worker {
4343
link,

examples/npm_and_rest/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ impl Component for Model {
3333
type Message = Msg;
3434
type Properties = ();
3535

36-
fn create(_: Self::Properties, mut link: ComponentLink<Self>) -> Self {
36+
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
3737
Model {
3838
gravatar: GravatarService::new(),
3939
ccxt: CcxtService::new(),
40-
callback: link.send_back(Msg::GravatarReady),
40+
callback: link.callback(Msg::GravatarReady),
4141
profile: None,
4242
exchanges: Vec::new(),
4343
task: None,

examples/routing/src/b_component.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ impl Component for BModel {
2121
type Message = Msg;
2222
type Properties = ();
2323

24-
fn create(_: Self::Properties, mut link: ComponentLink<Self>) -> Self {
25-
let callback = link.send_back(|route: Route<()>| Msg::HandleRoute(route));
24+
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
25+
let callback = link.callback(|route: Route<()>| Msg::HandleRoute(route));
2626
let mut router = Router::bridge(callback);
2727

2828
router.send(Request::GetCurrentRoute);

examples/routing/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ impl Component for Model {
3232
type Message = Msg;
3333
type Properties = ();
3434

35-
fn create(_: Self::Properties, mut link: ComponentLink<Self>) -> Self {
36-
let callback = link.send_back(|route: Route<()>| Msg::HandleRoute(route));
35+
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
36+
let callback = link.callback(|route: Route<()>| Msg::HandleRoute(route));
3737
let router = router::Router::bridge(callback);
3838
Model {
3939
child: Child::Loading, // This should be quickly overwritten by the actual route.

examples/routing/src/router.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ where
117117
type Output = Route<T>;
118118

119119
fn create(link: AgentLink<Self>) -> Self {
120-
let callback = link.send_back(|route_changed: (String, T)| {
120+
let callback = link.callback(|route_changed: (String, T)| {
121121
Msg::BrowserNavigationRouteChanged(route_changed)
122122
});
123123
let mut route_service = RouteService::new();

examples/timer/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl Component for Model {
2727
type Message = Msg;
2828
type Properties = ();
2929

30-
fn create(_: Self::Properties, mut link: ComponentLink<Self>) -> Self {
30+
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
3131
// This callback doesn't send any message to a scope
3232
let callback = |_| {
3333
println!("Example of a standalone callback.");
@@ -39,8 +39,8 @@ impl Component for Model {
3939
timeout: TimeoutService::new(),
4040
interval,
4141
console: ConsoleService::new(),
42-
callback_tick: link.send_back(|_| Msg::Tick),
43-
callback_done: link.send_back(|_| Msg::Done),
42+
callback_tick: link.callback(|_| Msg::Tick),
43+
callback_done: link.callback(|_| Msg::Done),
4444
job: None,
4545
messages: Vec::new(),
4646
_standalone: Box::new(handle),

src/agent.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -786,21 +786,20 @@ impl<AGN: Agent> AgentLink<AGN> {
786786
}
787787
}
788788

789-
/// Send response to an actor.
789+
/// Send response to an agent.
790790
pub fn response(&self, id: HandlerId, output: AGN::Output) {
791791
self.responder.response(id, output);
792792
}
793793

794-
/// This method sends messages back to the component's loop.
795-
pub fn send_back<F, IN>(&self, function: F) -> Callback<IN>
794+
/// Create a callback which will send a message to the agent when invoked.
795+
pub fn callback<F, IN>(&self, function: F) -> Callback<IN>
796796
where
797797
F: Fn(IN) -> AGN::Message + 'static,
798798
{
799799
let scope = self.scope.clone();
800800
let closure = move |input| {
801801
let output = function(input);
802-
let msg = AgentUpdate::Message(output);
803-
scope.clone().send(msg);
802+
scope.send(AgentUpdate::Message(output));
804803
};
805804
closure.into()
806805
}

src/html/mod.rs

+12-15
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,9 @@ where
385385
}
386386
}
387387

388-
/// This method sends batch of messages back to the component's loop when the
389-
/// returned callback is called.
390-
pub fn send_back_batch<F, IN>(&mut self, function: F) -> Callback<IN>
388+
/// This method creates a `Callback` which will send a batch of messages back to the linked
389+
/// component's update method when called.
390+
pub fn batch_callback<F, IN>(&self, function: F) -> Callback<IN>
391391
where
392392
F: Fn(IN) -> Vec<COMP::Message> + 'static,
393393
{
@@ -399,15 +399,16 @@ where
399399
closure.into()
400400
}
401401

402-
/// This method sends messages back to the component's loop when the returned callback is called.
403-
pub fn send_back<F, IN>(&mut self, function: F) -> Callback<IN>
402+
/// This method creates a `Callback` which will send a message to the linked component's
403+
/// update method when invoked.
404+
pub fn callback<F, IN>(&self, function: F) -> Callback<IN>
404405
where
405406
F: Fn(IN) -> COMP::Message + 'static,
406407
{
407408
let scope = self.scope.clone();
408409
let closure = move |input| {
409410
let output = function(input);
410-
scope.clone().send_message(output);
411+
scope.send_message(output);
411412
};
412413
closure.into()
413414
}
@@ -425,21 +426,17 @@ where
425426
use wasm_bindgen::JsValue;
426427
use wasm_bindgen_futures::future_to_promise;
427428

428-
let mut scope = self.scope.clone();
429-
430-
let js_future = async {
431-
let message: COMP::Message = future.await;
432-
// Force movement of the cloned scope into the async block.
433-
let scope_send = move || scope.send_message(message);
434-
scope_send();
429+
let scope = self.scope.clone();
430+
let js_future = async move {
431+
scope.send_message(future.await);
435432
Ok(JsValue::NULL)
436433
};
437434
future_to_promise(js_future);
438435
}
439436

440437
/// This method sends a message to this component to be processed immediately after the
441438
/// component has been updated and/or rendered.
442-
pub fn send_self(&mut self, msg: COMP::Message) {
439+
pub fn send_message(&mut self, msg: COMP::Message) {
443440
self.scope.send_message(msg);
444441
}
445442

@@ -448,7 +445,7 @@ where
448445
///
449446
/// All messages will first be processed by `update`, and if _any_ of them return `true`,
450447
/// then re-render will occur.
451-
pub fn send_self_batch(&mut self, msgs: Vec<COMP::Message>) {
448+
pub fn send_message_batch(&mut self, msgs: Vec<COMP::Message>) {
452449
self.scope.send_message_batch(msgs)
453450
}
454451
}

src/html/scope.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<COMP: Component> Scope<COMP> {
9090
}
9191

9292
/// Schedules a task to send a message or new props to a component
93-
pub(crate) fn update(&mut self, update: ComponentUpdate<COMP>) {
93+
pub(crate) fn update(&self, update: ComponentUpdate<COMP>) {
9494
let update = UpdateComponent {
9595
shared_state: self.shared_state.clone(),
9696
update,
@@ -106,12 +106,12 @@ impl<COMP: Component> Scope<COMP> {
106106
}
107107

108108
/// Send a message to the component
109-
pub fn send_message(&mut self, msg: COMP::Message) {
109+
pub fn send_message(&self, msg: COMP::Message) {
110110
self.update(ComponentUpdate::Message(msg));
111111
}
112112

113113
/// Send a batch of messages to the component
114-
pub fn send_message_batch(&mut self, messages: Vec<COMP::Message>) {
114+
pub fn send_message_batch(&self, messages: Vec<COMP::Message>) {
115115
self.update(ComponentUpdate::MessageBatch(messages));
116116
}
117117
}

src/services/fetch.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,12 @@ impl FetchService {
155155
///# Error
156156
///# }
157157
///# fn dont_execute() {
158-
///# let mut link: ComponentLink<Comp> = unimplemented!();
158+
///# let link: ComponentLink<Comp> = unimplemented!();
159159
///# let mut fetch_service: FetchService = FetchService::new();
160160
///# let post_request: Request<Result<String, failure::Error>> = unimplemented!();
161161
/// let task = fetch_service.fetch(
162162
/// post_request,
163-
/// link.send_back(|response: Response<Result<String, failure::Error>>| {
163+
/// link.callback(|response: Response<Result<String, failure::Error>>| {
164164
/// if response.status().is_success() {
165165
/// Msg::Noop
166166
/// } else {
@@ -199,9 +199,9 @@ impl FetchService {
199199
/// }
200200
///
201201
///# fn dont_execute() {
202-
///# let mut link: ComponentLink<Comp> = unimplemented!();
202+
///# let link: ComponentLink<Comp> = unimplemented!();
203203
/// let get_request = Request::get("/thing").body(Nothing).unwrap();
204-
/// let callback = link.send_back(|response: Response<Json<Result<Data, failure::Error>>>| {
204+
/// let callback = link.callback(|response: Response<Json<Result<Data, failure::Error>>>| {
205205
/// if let (meta, Json(Ok(body))) = response.into_parts() {
206206
/// if meta.status.is_success() {
207207
/// return Msg::FetchResourceComplete(body);
@@ -244,8 +244,8 @@ impl FetchService {
244244
///# }
245245
///# pub enum Msg {}
246246
///# fn dont_execute() {
247-
///# let mut link: ComponentLink<Comp> = unimplemented!();
248-
///# let callback = link.send_back(|response: Response<Result<String, failure::Error>>| unimplemented!());
247+
///# let link: ComponentLink<Comp> = unimplemented!();
248+
///# let callback = link.callback(|response: Response<Result<String, failure::Error>>| unimplemented!());
249249
/// let request = fetch::Request::get("/path/")
250250
/// .body(Nothing)
251251
/// .unwrap();

0 commit comments

Comments
 (0)