11use std:: collections:: HashSet ;
2+ use std:: rc:: Rc ;
23
4+ use serde:: { Deserialize , Serialize } ;
35use yew:: prelude:: * ;
4- use yew_agent:: { Agent , AgentLink , Bridge , Bridged , HandlerId } ;
6+ use yew_agent:: { Bridge , Bridged , HandlerId , Public , Worker , WorkerLink } ;
57
68/// Modal actions.
79pub enum ModalMsg {
810 Open ,
911 Close ,
10- CloseFromAgent ( ModalCloseMsg ) ,
12+ CloseFromWorker ( ModalCloseMsg ) ,
1113}
1214
1315#[ derive( Clone , Debug , Properties , PartialEq ) ]
@@ -41,8 +43,12 @@ impl Component for Modal {
4143 type Properties = ModalProps ;
4244
4345 fn create ( ctx : & Context < Self > ) -> Self {
44- let callback = ctx. link ( ) . callback ( ModalMsg :: CloseFromAgent ) ;
45- let subscription = ModalCloser :: bridge ( callback) ;
46+ let link = ctx. link ( ) . clone ( ) ;
47+
48+ let subscription = ModalCloser :: bridge ( Rc :: new ( move |msg| {
49+ link. send_message ( ModalMsg :: CloseFromWorker ( msg) ) ;
50+ } ) ) ;
51+
4652 Self { subscription, is_active : false }
4753 }
4854
@@ -54,7 +60,7 @@ impl Component for Modal {
5460 ModalMsg :: Open => {
5561 self . is_active = true ;
5662 }
57- ModalMsg :: CloseFromAgent ( id) => {
63+ ModalMsg :: CloseFromWorker ( id) => {
5864 if id. 0 == ctx. props ( ) . id {
5965 self . is_active = false ;
6066 } else {
@@ -130,8 +136,11 @@ impl Component for ModalCard {
130136 type Properties = ModalCardProps ;
131137
132138 fn create ( ctx : & Context < Self > ) -> Self {
133- let callback = ctx. link ( ) . callback ( ModalMsg :: CloseFromAgent ) ;
134- let subscription = ModalCloser :: bridge ( callback) ;
139+ let link = ctx. link ( ) . clone ( ) ;
140+
141+ let subscription = ModalCloser :: bridge ( Rc :: new ( move |msg| {
142+ link. send_message ( ModalMsg :: CloseFromWorker ( msg) ) ;
143+ } ) ) ;
135144 Self { subscription, is_active : false }
136145 }
137146
@@ -143,7 +152,7 @@ impl Component for ModalCard {
143152 ModalMsg :: Open => {
144153 self . is_active = true ;
145154 }
146- ModalMsg :: CloseFromAgent ( id) => {
155+ ModalMsg :: CloseFromWorker ( id) => {
147156 if id. 0 == ctx. props ( ) . id {
148157 self . is_active = false ;
149158 } else {
@@ -195,7 +204,7 @@ impl Component for ModalCard {
195204///
196205/// The ID provided in this message must match the ID of the modal which is to be closed, else
197206/// the message will be ignored.
198- #[ derive( Clone , Debug ) ]
207+ #[ derive( Clone , Debug , Deserialize , Serialize ) ]
199208pub struct ModalCloseMsg ( pub String ) ;
200209
201210/// An agent used for being able to close `Modal` & `ModalCard` instances by ID.
@@ -238,18 +247,19 @@ pub struct ModalCloseMsg(pub String);
238247///
239248/// This pattern allows you to communicate with a modal by its given ID, allowing
240249/// you to close the modal from anywhere in your application.
250+ // #[derive(Serialize)]
241251pub struct ModalCloser {
242- link : AgentLink < Self > ,
252+ link : WorkerLink < Self > ,
243253 subscribers : HashSet < HandlerId > ,
244254}
245255
246- impl Agent for ModalCloser {
247- type Reach = yew_agent :: Context < Self > ;
256+ impl Worker for ModalCloser {
257+ type Reach = Public < Self > ;
248258 type Message = ( ) ;
249259 type Input = ModalCloseMsg ; // The agent receives requests to close modals by ID.
250260 type Output = ModalCloseMsg ; // The agent forwards the input to all registered modals.
251261
252- fn create ( link : AgentLink < Self > ) -> Self {
262+ fn create ( link : WorkerLink < Self > ) -> Self {
253263 Self { link, subscribers : HashSet :: new ( ) }
254264 }
255265
0 commit comments