pub trait Component: Sized + 'static {
    type CommandOutput: Debug + Send + 'static;
    type Input: Debug + 'static;
    type Output: Debug + 'static;
    type Init;
    type Root: Debug + Clone;
    type Widgets: 'static;

    fn init_root() -> Self::Root;
    fn init(
        init: Self::Init,
        root: &Self::Root,
        sender: ComponentSender<Self>
    ) -> ComponentParts<Self>; fn builder() -> ComponentBuilder<Self> { ... } fn update(
        &mut self,
        message: Self::Input,
        sender: ComponentSender<Self>,
        root: &Self::Root
    ) { ... } fn update_cmd(
        &mut self,
        message: Self::CommandOutput,
        sender: ComponentSender<Self>,
        root: &Self::Root
    ) { ... } fn update_cmd_with_view(
        &mut self,
        widgets: &mut Self::Widgets,
        message: Self::CommandOutput,
        sender: ComponentSender<Self>,
        root: &Self::Root
    ) { ... } fn update_view(
        &self,
        widgets: &mut Self::Widgets,
        sender: ComponentSender<Self>
    ) { ... } fn update_with_view(
        &mut self,
        widgets: &mut Self::Widgets,
        message: Self::Input,
        sender: ComponentSender<Self>,
        root: &Self::Root
    ) { ... } fn shutdown(
        &mut self,
        widgets: &mut Self::Widgets,
        output: Sender<Self::Output>
    ) { ... } fn id(&self) -> String { ... } }
Expand description

The fundamental building block of a Relm4 application.

A Component is an element of an application that defines initialization, state, behavior and communication as a modular unit.

Component is powerful and flexible, but for many use-cases the SimpleComponent convenience trait will suffice. SimpleComponent enforces separation between model and view updates, and provides no-op implementations for advanced features that are not relevant for most use-cases.

Required Associated Types§

Messages which are received from commands executing in the background.

The message type that the component accepts as inputs.

The message type that the component provides as outputs.

The parameter used to initialize the component.

The widget that was constructed by the component.

The type that’s used for storing widgets created for this component.

Required Methods§

Initializes the root widget.

Creates the initial model and view, docking it into the component.

Provided Methods§

Create a builder for this component.

Processes inputs received by the component.

Examples found in repository?
src/component/sync/traits.rs (line 115)
108
109
110
111
112
113
114
115
116
117
    fn update_with_view(
        &mut self,
        widgets: &mut Self::Widgets,
        message: Self::Input,
        sender: ComponentSender<Self>,
        root: &Self::Root,
    ) {
        self.update(message, sender.clone(), root);
        self.update_view(widgets, sender);
    }

Defines how the component should respond to command updates.

Examples found in repository?
src/component/sync/traits.rs (line 87)
80
81
82
83
84
85
86
87
88
89
    fn update_cmd_with_view(
        &mut self,
        widgets: &mut Self::Widgets,
        message: Self::CommandOutput,
        sender: ComponentSender<Self>,
        root: &Self::Root,
    ) {
        self.update_cmd(message, sender.clone(), root);
        self.update_view(widgets, sender);
    }

Updates the model and view upon completion of a command.

Overriding this method is helpful if you need access to the widgets while processing a command output.

The default implementation of this method calls update_cmd followed by update_view. If you override this method while using the component macro, you must remember to call update_view in your implementation. Otherwise, the view will not reflect the updated model.

Examples found in repository?
src/component/worker.rs (line 141)
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    pub fn detach_worker(self, payload: C::Init) -> WorkerHandle<C> {
        let Self { root, .. } = self;

        // Used for all events to be processed by this component's internal service.
        let (input_sender, input_receiver) = crate::channel::<C::Input>();

        let RuntimeSenders {
            output_sender,
            output_receiver,
            cmd_sender,
            cmd_receiver,
            shutdown_notifier,
            shutdown_recipient,
            shutdown_on_drop,
            mut shutdown_event,
        } = RuntimeSenders::<C::Output, C::CommandOutput>::new();

        // Encapsulates the senders used by component methods.
        let component_sender = ComponentSender::new(
            input_sender.clone(),
            output_sender.clone(),
            cmd_sender,
            shutdown_recipient,
        );

        let mut state = C::init(payload, &root, component_sender.clone());

        thread::spawn(move || {
            let context =
                glib::MainContext::thread_default().unwrap_or_else(glib::MainContext::new);

            // Spawns the component's service. It will receive both `Self::Input` and
            // `Self::CommandOutput` messages. It will spawn commands as requested by
            // updates, and send `Self::Output` messages externally.
            context.block_on(async move {
                let mut cmd = GuardedReceiver::new(cmd_receiver);
                let mut input = GuardedReceiver::new(input_receiver);

                loop {
                    futures::select!(
                        // Performs the model update, checking if the update requested a command.
                        // Runs that command asynchronously in the background using tokio.
                        message = input => {
                            let ComponentParts {
                                model,
                                widgets,
                            } = &mut state;

                            let span = info_span!(
                                "update_with_view",
                                input=?message,
                                component=any::type_name::<C>(),
                                id=model.id(),
                            );
                            let _enter = span.enter();

                            model.update_with_view(widgets, message, component_sender.clone(), &root);
                        }

                        // Handles responses from a command.
                        message = cmd => {
                            let ComponentParts {
                                model,
                                widgets,
                            } = &mut state;

                            let span = info_span!(
                                "update_cmd_with_view",
                                cmd_output=?message,
                                component=any::type_name::<C>(),
                                id=model.id(),
                            );
                            let _enter = span.enter();

                            model.update_cmd_with_view(widgets, message, component_sender.clone(), &root);
                        },

                        // Triggered when the component is destroyed
                        _ = shutdown_event => {
                            let ComponentParts {
                                model,
                                widgets,
                            } = &mut state;

                            model.shutdown(widgets, output_sender);

                            shutdown_notifier.shutdown();

                            return;
                        }
                    );
                }
            });
        });

        // Give back a type for controlling the component service.
        WorkerHandle {
            sender: input_sender,
            receiver: output_receiver,
            shutdown_on_drop,
        }
    }
More examples
Hide additional examples
src/component/sync/builder.rs (line 251)
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
    fn launch_with_input_channel(
        self,
        payload: C::Init,
        input_sender: Sender<C::Input>,
        input_receiver: Receiver<C::Input>,
    ) -> Connector<C> {
        let Self { root, priority, .. } = self;

        let RuntimeSenders {
            output_sender,
            output_receiver,
            cmd_sender,
            cmd_receiver,
            shutdown_notifier,
            shutdown_recipient,
            shutdown_on_drop,
            mut shutdown_event,
        } = RuntimeSenders::<C::Output, C::CommandOutput>::new();

        // Gets notifications when a component's model and view is updated externally.
        let (notifier, notifier_receiver) = crate::channel();

        let (source_id_sender, source_id_receiver) = oneshot::channel::<gtk::glib::SourceId>();

        // Encapsulates the senders used by component methods.
        let component_sender = ComponentSender::new(
            input_sender.clone(),
            output_sender.clone(),
            cmd_sender,
            shutdown_recipient,
        );

        // Constructs the initial model and view with the initial payload.
        let state = Rc::new(RefCell::new(C::init(
            payload,
            &root,
            component_sender.clone(),
        )));
        let watcher = StateWatcher {
            state,
            notifier,
            shutdown_on_drop,
        };

        let rt_state = watcher.state.clone();
        let rt_root = root.clone();

        // Spawns the component's service. It will receive both `Self::Input` and
        // `Self::CommandOutput` messages. It will spawn commands as requested by
        // updates, and send `Self::Output` messages externally.
        let id = crate::spawn_local_with_priority(priority, async move {
            let id = source_id_receiver.await.unwrap();
            let mut notifier = GuardedReceiver::new(notifier_receiver);
            let mut cmd = GuardedReceiver::new(cmd_receiver);
            let mut input = GuardedReceiver::new(input_receiver);
            loop {
                futures::select!(
                    // Performs the model update, checking if the update requested a command.
                    // Runs that command asynchronously in the background using tokio.
                    message = input => {
                        let ComponentParts {
                            model,
                            widgets,
                        } = &mut *rt_state.borrow_mut();

                        let span = info_span!(
                            "update_with_view",
                            input=?message,
                            component=any::type_name::<C>(),
                            id=model.id(),
                        );
                        let _enter = span.enter();

                        model.update_with_view(widgets, message, component_sender.clone(), &rt_root);
                    }

                    // Handles responses from a command.
                    message = cmd => {
                        let ComponentParts {
                            model,
                            widgets,
                        } = &mut *rt_state.borrow_mut();

                        let span = info_span!(
                            "update_cmd_with_view",
                            cmd_output=?message,
                            component=any::type_name::<C>(),
                            id=model.id(),
                        );
                        let _enter = span.enter();

                        model.update_cmd_with_view(widgets, message, component_sender.clone(), &rt_root);
                    }

                    // Triggered when the model and view have been updated externally.
                    _ = notifier => {
                        let ComponentParts {
                            model,
                            widgets,
                        } = &mut *rt_state.borrow_mut();

                        model.update_view(widgets, component_sender.clone());
                    }

                    // Triggered when the component is destroyed
                    _ = shutdown_event => {
                        let ComponentParts {
                            model,
                            widgets,
                        } = &mut *rt_state.borrow_mut();

                        model.shutdown(widgets, output_sender);

                        shutdown_notifier.shutdown();

                        id.remove();

                        return;
                    }
                );
            }
        });

        source_id_sender.send(id).unwrap();

        // Give back a type for controlling the component service.
        Connector {
            state: watcher,
            widget: root,
            sender: input_sender,
            receiver: output_receiver,
        }
    }

Updates the view after the model has been updated.

Examples found in repository?
src/component/sync/traits.rs (line 88)
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    fn update_cmd_with_view(
        &mut self,
        widgets: &mut Self::Widgets,
        message: Self::CommandOutput,
        sender: ComponentSender<Self>,
        root: &Self::Root,
    ) {
        self.update_cmd(message, sender.clone(), root);
        self.update_view(widgets, sender);
    }

    /// Updates the view after the model has been updated.
    #[allow(unused)]
    fn update_view(&self, widgets: &mut Self::Widgets, sender: ComponentSender<Self>) {}

    /// Updates the model and view when a new input is received.
    ///
    /// Overriding this method is helpful if you need access to the widgets while processing an
    /// input.
    ///
    /// The default implementation of this method calls [`update`] followed by [`update_view`]. If
    /// you override this method while using the [`component`] macro, you must remember to
    /// call [`update_view`] in your implementation. Otherwise, the view will not reflect the
    /// updated model.
    ///
    /// [`update`]: Self::update
    /// [`update_view`]: Self::update_view
    /// [`component`]: relm4_macros::component
    fn update_with_view(
        &mut self,
        widgets: &mut Self::Widgets,
        message: Self::Input,
        sender: ComponentSender<Self>,
        root: &Self::Root,
    ) {
        self.update(message, sender.clone(), root);
        self.update_view(widgets, sender);
    }
More examples
Hide additional examples
src/component/sync/builder.rs (line 261)
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
    fn launch_with_input_channel(
        self,
        payload: C::Init,
        input_sender: Sender<C::Input>,
        input_receiver: Receiver<C::Input>,
    ) -> Connector<C> {
        let Self { root, priority, .. } = self;

        let RuntimeSenders {
            output_sender,
            output_receiver,
            cmd_sender,
            cmd_receiver,
            shutdown_notifier,
            shutdown_recipient,
            shutdown_on_drop,
            mut shutdown_event,
        } = RuntimeSenders::<C::Output, C::CommandOutput>::new();

        // Gets notifications when a component's model and view is updated externally.
        let (notifier, notifier_receiver) = crate::channel();

        let (source_id_sender, source_id_receiver) = oneshot::channel::<gtk::glib::SourceId>();

        // Encapsulates the senders used by component methods.
        let component_sender = ComponentSender::new(
            input_sender.clone(),
            output_sender.clone(),
            cmd_sender,
            shutdown_recipient,
        );

        // Constructs the initial model and view with the initial payload.
        let state = Rc::new(RefCell::new(C::init(
            payload,
            &root,
            component_sender.clone(),
        )));
        let watcher = StateWatcher {
            state,
            notifier,
            shutdown_on_drop,
        };

        let rt_state = watcher.state.clone();
        let rt_root = root.clone();

        // Spawns the component's service. It will receive both `Self::Input` and
        // `Self::CommandOutput` messages. It will spawn commands as requested by
        // updates, and send `Self::Output` messages externally.
        let id = crate::spawn_local_with_priority(priority, async move {
            let id = source_id_receiver.await.unwrap();
            let mut notifier = GuardedReceiver::new(notifier_receiver);
            let mut cmd = GuardedReceiver::new(cmd_receiver);
            let mut input = GuardedReceiver::new(input_receiver);
            loop {
                futures::select!(
                    // Performs the model update, checking if the update requested a command.
                    // Runs that command asynchronously in the background using tokio.
                    message = input => {
                        let ComponentParts {
                            model,
                            widgets,
                        } = &mut *rt_state.borrow_mut();

                        let span = info_span!(
                            "update_with_view",
                            input=?message,
                            component=any::type_name::<C>(),
                            id=model.id(),
                        );
                        let _enter = span.enter();

                        model.update_with_view(widgets, message, component_sender.clone(), &rt_root);
                    }

                    // Handles responses from a command.
                    message = cmd => {
                        let ComponentParts {
                            model,
                            widgets,
                        } = &mut *rt_state.borrow_mut();

                        let span = info_span!(
                            "update_cmd_with_view",
                            cmd_output=?message,
                            component=any::type_name::<C>(),
                            id=model.id(),
                        );
                        let _enter = span.enter();

                        model.update_cmd_with_view(widgets, message, component_sender.clone(), &rt_root);
                    }

                    // Triggered when the model and view have been updated externally.
                    _ = notifier => {
                        let ComponentParts {
                            model,
                            widgets,
                        } = &mut *rt_state.borrow_mut();

                        model.update_view(widgets, component_sender.clone());
                    }

                    // Triggered when the component is destroyed
                    _ = shutdown_event => {
                        let ComponentParts {
                            model,
                            widgets,
                        } = &mut *rt_state.borrow_mut();

                        model.shutdown(widgets, output_sender);

                        shutdown_notifier.shutdown();

                        id.remove();

                        return;
                    }
                );
            }
        });

        source_id_sender.send(id).unwrap();

        // Give back a type for controlling the component service.
        Connector {
            state: watcher,
            widget: root,
            sender: input_sender,
            receiver: output_receiver,
        }
    }

Updates the model and view when a new input is received.

Overriding this method is helpful if you need access to the widgets while processing an input.

The default implementation of this method calls update followed by update_view. If you override this method while using the component macro, you must remember to call update_view in your implementation. Otherwise, the view will not reflect the updated model.

Examples found in repository?
src/component/worker.rs (line 123)
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    pub fn detach_worker(self, payload: C::Init) -> WorkerHandle<C> {
        let Self { root, .. } = self;

        // Used for all events to be processed by this component's internal service.
        let (input_sender, input_receiver) = crate::channel::<C::Input>();

        let RuntimeSenders {
            output_sender,
            output_receiver,
            cmd_sender,
            cmd_receiver,
            shutdown_notifier,
            shutdown_recipient,
            shutdown_on_drop,
            mut shutdown_event,
        } = RuntimeSenders::<C::Output, C::CommandOutput>::new();

        // Encapsulates the senders used by component methods.
        let component_sender = ComponentSender::new(
            input_sender.clone(),
            output_sender.clone(),
            cmd_sender,
            shutdown_recipient,
        );

        let mut state = C::init(payload, &root, component_sender.clone());

        thread::spawn(move || {
            let context =
                glib::MainContext::thread_default().unwrap_or_else(glib::MainContext::new);

            // Spawns the component's service. It will receive both `Self::Input` and
            // `Self::CommandOutput` messages. It will spawn commands as requested by
            // updates, and send `Self::Output` messages externally.
            context.block_on(async move {
                let mut cmd = GuardedReceiver::new(cmd_receiver);
                let mut input = GuardedReceiver::new(input_receiver);

                loop {
                    futures::select!(
                        // Performs the model update, checking if the update requested a command.
                        // Runs that command asynchronously in the background using tokio.
                        message = input => {
                            let ComponentParts {
                                model,
                                widgets,
                            } = &mut state;

                            let span = info_span!(
                                "update_with_view",
                                input=?message,
                                component=any::type_name::<C>(),
                                id=model.id(),
                            );
                            let _enter = span.enter();

                            model.update_with_view(widgets, message, component_sender.clone(), &root);
                        }

                        // Handles responses from a command.
                        message = cmd => {
                            let ComponentParts {
                                model,
                                widgets,
                            } = &mut state;

                            let span = info_span!(
                                "update_cmd_with_view",
                                cmd_output=?message,
                                component=any::type_name::<C>(),
                                id=model.id(),
                            );
                            let _enter = span.enter();

                            model.update_cmd_with_view(widgets, message, component_sender.clone(), &root);
                        },

                        // Triggered when the component is destroyed
                        _ = shutdown_event => {
                            let ComponentParts {
                                model,
                                widgets,
                            } = &mut state;

                            model.shutdown(widgets, output_sender);

                            shutdown_notifier.shutdown();

                            return;
                        }
                    );
                }
            });
        });

        // Give back a type for controlling the component service.
        WorkerHandle {
            sender: input_sender,
            receiver: output_receiver,
            shutdown_on_drop,
        }
    }
More examples
Hide additional examples
src/component/sync/builder.rs (line 233)
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
    fn launch_with_input_channel(
        self,
        payload: C::Init,
        input_sender: Sender<C::Input>,
        input_receiver: Receiver<C::Input>,
    ) -> Connector<C> {
        let Self { root, priority, .. } = self;

        let RuntimeSenders {
            output_sender,
            output_receiver,
            cmd_sender,
            cmd_receiver,
            shutdown_notifier,
            shutdown_recipient,
            shutdown_on_drop,
            mut shutdown_event,
        } = RuntimeSenders::<C::Output, C::CommandOutput>::new();

        // Gets notifications when a component's model and view is updated externally.
        let (notifier, notifier_receiver) = crate::channel();

        let (source_id_sender, source_id_receiver) = oneshot::channel::<gtk::glib::SourceId>();

        // Encapsulates the senders used by component methods.
        let component_sender = ComponentSender::new(
            input_sender.clone(),
            output_sender.clone(),
            cmd_sender,
            shutdown_recipient,
        );

        // Constructs the initial model and view with the initial payload.
        let state = Rc::new(RefCell::new(C::init(
            payload,
            &root,
            component_sender.clone(),
        )));
        let watcher = StateWatcher {
            state,
            notifier,
            shutdown_on_drop,
        };

        let rt_state = watcher.state.clone();
        let rt_root = root.clone();

        // Spawns the component's service. It will receive both `Self::Input` and
        // `Self::CommandOutput` messages. It will spawn commands as requested by
        // updates, and send `Self::Output` messages externally.
        let id = crate::spawn_local_with_priority(priority, async move {
            let id = source_id_receiver.await.unwrap();
            let mut notifier = GuardedReceiver::new(notifier_receiver);
            let mut cmd = GuardedReceiver::new(cmd_receiver);
            let mut input = GuardedReceiver::new(input_receiver);
            loop {
                futures::select!(
                    // Performs the model update, checking if the update requested a command.
                    // Runs that command asynchronously in the background using tokio.
                    message = input => {
                        let ComponentParts {
                            model,
                            widgets,
                        } = &mut *rt_state.borrow_mut();

                        let span = info_span!(
                            "update_with_view",
                            input=?message,
                            component=any::type_name::<C>(),
                            id=model.id(),
                        );
                        let _enter = span.enter();

                        model.update_with_view(widgets, message, component_sender.clone(), &rt_root);
                    }

                    // Handles responses from a command.
                    message = cmd => {
                        let ComponentParts {
                            model,
                            widgets,
                        } = &mut *rt_state.borrow_mut();

                        let span = info_span!(
                            "update_cmd_with_view",
                            cmd_output=?message,
                            component=any::type_name::<C>(),
                            id=model.id(),
                        );
                        let _enter = span.enter();

                        model.update_cmd_with_view(widgets, message, component_sender.clone(), &rt_root);
                    }

                    // Triggered when the model and view have been updated externally.
                    _ = notifier => {
                        let ComponentParts {
                            model,
                            widgets,
                        } = &mut *rt_state.borrow_mut();

                        model.update_view(widgets, component_sender.clone());
                    }

                    // Triggered when the component is destroyed
                    _ = shutdown_event => {
                        let ComponentParts {
                            model,
                            widgets,
                        } = &mut *rt_state.borrow_mut();

                        model.shutdown(widgets, output_sender);

                        shutdown_notifier.shutdown();

                        id.remove();

                        return;
                    }
                );
            }
        });

        source_id_sender.send(id).unwrap();

        // Give back a type for controlling the component service.
        Connector {
            state: watcher,
            widget: root,
            sender: input_sender,
            receiver: output_receiver,
        }
    }

Last method called before a component is shut down.

Examples found in repository?
src/component/worker.rs (line 151)
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    pub fn detach_worker(self, payload: C::Init) -> WorkerHandle<C> {
        let Self { root, .. } = self;

        // Used for all events to be processed by this component's internal service.
        let (input_sender, input_receiver) = crate::channel::<C::Input>();

        let RuntimeSenders {
            output_sender,
            output_receiver,
            cmd_sender,
            cmd_receiver,
            shutdown_notifier,
            shutdown_recipient,
            shutdown_on_drop,
            mut shutdown_event,
        } = RuntimeSenders::<C::Output, C::CommandOutput>::new();

        // Encapsulates the senders used by component methods.
        let component_sender = ComponentSender::new(
            input_sender.clone(),
            output_sender.clone(),
            cmd_sender,
            shutdown_recipient,
        );

        let mut state = C::init(payload, &root, component_sender.clone());

        thread::spawn(move || {
            let context =
                glib::MainContext::thread_default().unwrap_or_else(glib::MainContext::new);

            // Spawns the component's service. It will receive both `Self::Input` and
            // `Self::CommandOutput` messages. It will spawn commands as requested by
            // updates, and send `Self::Output` messages externally.
            context.block_on(async move {
                let mut cmd = GuardedReceiver::new(cmd_receiver);
                let mut input = GuardedReceiver::new(input_receiver);

                loop {
                    futures::select!(
                        // Performs the model update, checking if the update requested a command.
                        // Runs that command asynchronously in the background using tokio.
                        message = input => {
                            let ComponentParts {
                                model,
                                widgets,
                            } = &mut state;

                            let span = info_span!(
                                "update_with_view",
                                input=?message,
                                component=any::type_name::<C>(),
                                id=model.id(),
                            );
                            let _enter = span.enter();

                            model.update_with_view(widgets, message, component_sender.clone(), &root);
                        }

                        // Handles responses from a command.
                        message = cmd => {
                            let ComponentParts {
                                model,
                                widgets,
                            } = &mut state;

                            let span = info_span!(
                                "update_cmd_with_view",
                                cmd_output=?message,
                                component=any::type_name::<C>(),
                                id=model.id(),
                            );
                            let _enter = span.enter();

                            model.update_cmd_with_view(widgets, message, component_sender.clone(), &root);
                        },

                        // Triggered when the component is destroyed
                        _ = shutdown_event => {
                            let ComponentParts {
                                model,
                                widgets,
                            } = &mut state;

                            model.shutdown(widgets, output_sender);

                            shutdown_notifier.shutdown();

                            return;
                        }
                    );
                }
            });
        });

        // Give back a type for controlling the component service.
        WorkerHandle {
            sender: input_sender,
            receiver: output_receiver,
            shutdown_on_drop,
        }
    }
More examples
Hide additional examples
src/component/sync/builder.rs (line 271)
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
    fn launch_with_input_channel(
        self,
        payload: C::Init,
        input_sender: Sender<C::Input>,
        input_receiver: Receiver<C::Input>,
    ) -> Connector<C> {
        let Self { root, priority, .. } = self;

        let RuntimeSenders {
            output_sender,
            output_receiver,
            cmd_sender,
            cmd_receiver,
            shutdown_notifier,
            shutdown_recipient,
            shutdown_on_drop,
            mut shutdown_event,
        } = RuntimeSenders::<C::Output, C::CommandOutput>::new();

        // Gets notifications when a component's model and view is updated externally.
        let (notifier, notifier_receiver) = crate::channel();

        let (source_id_sender, source_id_receiver) = oneshot::channel::<gtk::glib::SourceId>();

        // Encapsulates the senders used by component methods.
        let component_sender = ComponentSender::new(
            input_sender.clone(),
            output_sender.clone(),
            cmd_sender,
            shutdown_recipient,
        );

        // Constructs the initial model and view with the initial payload.
        let state = Rc::new(RefCell::new(C::init(
            payload,
            &root,
            component_sender.clone(),
        )));
        let watcher = StateWatcher {
            state,
            notifier,
            shutdown_on_drop,
        };

        let rt_state = watcher.state.clone();
        let rt_root = root.clone();

        // Spawns the component's service. It will receive both `Self::Input` and
        // `Self::CommandOutput` messages. It will spawn commands as requested by
        // updates, and send `Self::Output` messages externally.
        let id = crate::spawn_local_with_priority(priority, async move {
            let id = source_id_receiver.await.unwrap();
            let mut notifier = GuardedReceiver::new(notifier_receiver);
            let mut cmd = GuardedReceiver::new(cmd_receiver);
            let mut input = GuardedReceiver::new(input_receiver);
            loop {
                futures::select!(
                    // Performs the model update, checking if the update requested a command.
                    // Runs that command asynchronously in the background using tokio.
                    message = input => {
                        let ComponentParts {
                            model,
                            widgets,
                        } = &mut *rt_state.borrow_mut();

                        let span = info_span!(
                            "update_with_view",
                            input=?message,
                            component=any::type_name::<C>(),
                            id=model.id(),
                        );
                        let _enter = span.enter();

                        model.update_with_view(widgets, message, component_sender.clone(), &rt_root);
                    }

                    // Handles responses from a command.
                    message = cmd => {
                        let ComponentParts {
                            model,
                            widgets,
                        } = &mut *rt_state.borrow_mut();

                        let span = info_span!(
                            "update_cmd_with_view",
                            cmd_output=?message,
                            component=any::type_name::<C>(),
                            id=model.id(),
                        );
                        let _enter = span.enter();

                        model.update_cmd_with_view(widgets, message, component_sender.clone(), &rt_root);
                    }

                    // Triggered when the model and view have been updated externally.
                    _ = notifier => {
                        let ComponentParts {
                            model,
                            widgets,
                        } = &mut *rt_state.borrow_mut();

                        model.update_view(widgets, component_sender.clone());
                    }

                    // Triggered when the component is destroyed
                    _ = shutdown_event => {
                        let ComponentParts {
                            model,
                            widgets,
                        } = &mut *rt_state.borrow_mut();

                        model.shutdown(widgets, output_sender);

                        shutdown_notifier.shutdown();

                        id.remove();

                        return;
                    }
                );
            }
        });

        source_id_sender.send(id).unwrap();

        // Give back a type for controlling the component service.
        Connector {
            state: watcher,
            widget: root,
            sender: input_sender,
            receiver: output_receiver,
        }
    }

An identifier for the component used for debug logging.

The default implementation of this method uses the address of the component, but implementations are free to provide more meaningful identifiers.

Examples found in repository?
src/component/worker.rs (line 119)
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    pub fn detach_worker(self, payload: C::Init) -> WorkerHandle<C> {
        let Self { root, .. } = self;

        // Used for all events to be processed by this component's internal service.
        let (input_sender, input_receiver) = crate::channel::<C::Input>();

        let RuntimeSenders {
            output_sender,
            output_receiver,
            cmd_sender,
            cmd_receiver,
            shutdown_notifier,
            shutdown_recipient,
            shutdown_on_drop,
            mut shutdown_event,
        } = RuntimeSenders::<C::Output, C::CommandOutput>::new();

        // Encapsulates the senders used by component methods.
        let component_sender = ComponentSender::new(
            input_sender.clone(),
            output_sender.clone(),
            cmd_sender,
            shutdown_recipient,
        );

        let mut state = C::init(payload, &root, component_sender.clone());

        thread::spawn(move || {
            let context =
                glib::MainContext::thread_default().unwrap_or_else(glib::MainContext::new);

            // Spawns the component's service. It will receive both `Self::Input` and
            // `Self::CommandOutput` messages. It will spawn commands as requested by
            // updates, and send `Self::Output` messages externally.
            context.block_on(async move {
                let mut cmd = GuardedReceiver::new(cmd_receiver);
                let mut input = GuardedReceiver::new(input_receiver);

                loop {
                    futures::select!(
                        // Performs the model update, checking if the update requested a command.
                        // Runs that command asynchronously in the background using tokio.
                        message = input => {
                            let ComponentParts {
                                model,
                                widgets,
                            } = &mut state;

                            let span = info_span!(
                                "update_with_view",
                                input=?message,
                                component=any::type_name::<C>(),
                                id=model.id(),
                            );
                            let _enter = span.enter();

                            model.update_with_view(widgets, message, component_sender.clone(), &root);
                        }

                        // Handles responses from a command.
                        message = cmd => {
                            let ComponentParts {
                                model,
                                widgets,
                            } = &mut state;

                            let span = info_span!(
                                "update_cmd_with_view",
                                cmd_output=?message,
                                component=any::type_name::<C>(),
                                id=model.id(),
                            );
                            let _enter = span.enter();

                            model.update_cmd_with_view(widgets, message, component_sender.clone(), &root);
                        },

                        // Triggered when the component is destroyed
                        _ = shutdown_event => {
                            let ComponentParts {
                                model,
                                widgets,
                            } = &mut state;

                            model.shutdown(widgets, output_sender);

                            shutdown_notifier.shutdown();

                            return;
                        }
                    );
                }
            });
        });

        // Give back a type for controlling the component service.
        WorkerHandle {
            sender: input_sender,
            receiver: output_receiver,
            shutdown_on_drop,
        }
    }
More examples
Hide additional examples
src/component/sync/builder.rs (line 229)
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
    fn launch_with_input_channel(
        self,
        payload: C::Init,
        input_sender: Sender<C::Input>,
        input_receiver: Receiver<C::Input>,
    ) -> Connector<C> {
        let Self { root, priority, .. } = self;

        let RuntimeSenders {
            output_sender,
            output_receiver,
            cmd_sender,
            cmd_receiver,
            shutdown_notifier,
            shutdown_recipient,
            shutdown_on_drop,
            mut shutdown_event,
        } = RuntimeSenders::<C::Output, C::CommandOutput>::new();

        // Gets notifications when a component's model and view is updated externally.
        let (notifier, notifier_receiver) = crate::channel();

        let (source_id_sender, source_id_receiver) = oneshot::channel::<gtk::glib::SourceId>();

        // Encapsulates the senders used by component methods.
        let component_sender = ComponentSender::new(
            input_sender.clone(),
            output_sender.clone(),
            cmd_sender,
            shutdown_recipient,
        );

        // Constructs the initial model and view with the initial payload.
        let state = Rc::new(RefCell::new(C::init(
            payload,
            &root,
            component_sender.clone(),
        )));
        let watcher = StateWatcher {
            state,
            notifier,
            shutdown_on_drop,
        };

        let rt_state = watcher.state.clone();
        let rt_root = root.clone();

        // Spawns the component's service. It will receive both `Self::Input` and
        // `Self::CommandOutput` messages. It will spawn commands as requested by
        // updates, and send `Self::Output` messages externally.
        let id = crate::spawn_local_with_priority(priority, async move {
            let id = source_id_receiver.await.unwrap();
            let mut notifier = GuardedReceiver::new(notifier_receiver);
            let mut cmd = GuardedReceiver::new(cmd_receiver);
            let mut input = GuardedReceiver::new(input_receiver);
            loop {
                futures::select!(
                    // Performs the model update, checking if the update requested a command.
                    // Runs that command asynchronously in the background using tokio.
                    message = input => {
                        let ComponentParts {
                            model,
                            widgets,
                        } = &mut *rt_state.borrow_mut();

                        let span = info_span!(
                            "update_with_view",
                            input=?message,
                            component=any::type_name::<C>(),
                            id=model.id(),
                        );
                        let _enter = span.enter();

                        model.update_with_view(widgets, message, component_sender.clone(), &rt_root);
                    }

                    // Handles responses from a command.
                    message = cmd => {
                        let ComponentParts {
                            model,
                            widgets,
                        } = &mut *rt_state.borrow_mut();

                        let span = info_span!(
                            "update_cmd_with_view",
                            cmd_output=?message,
                            component=any::type_name::<C>(),
                            id=model.id(),
                        );
                        let _enter = span.enter();

                        model.update_cmd_with_view(widgets, message, component_sender.clone(), &rt_root);
                    }

                    // Triggered when the model and view have been updated externally.
                    _ = notifier => {
                        let ComponentParts {
                            model,
                            widgets,
                        } = &mut *rt_state.borrow_mut();

                        model.update_view(widgets, component_sender.clone());
                    }

                    // Triggered when the component is destroyed
                    _ = shutdown_event => {
                        let ComponentParts {
                            model,
                            widgets,
                        } = &mut *rt_state.borrow_mut();

                        model.shutdown(widgets, output_sender);

                        shutdown_notifier.shutdown();

                        id.remove();

                        return;
                    }
                );
            }
        });

        source_id_sender.send(id).unwrap();

        // Give back a type for controlling the component service.
        Connector {
            state: watcher,
            widget: root,
            sender: input_sender,
            receiver: output_receiver,
        }
    }

Implementors§