@@ -31,32 +31,17 @@ impl<M: Message + 'static> ChannelView<M> {
3131 loop {
3232 let message = channel. get_receiver ( ) . recv ( ) . await . unwrap ( ) ;
3333
34- println ! ( "Got Message!" ) ;
35-
36- async_model
37- . update ( & mut async_ctx, |data, ctx| {
38- data. messages . push ( message) ;
39- ctx. notify ( ) ;
40- } )
41- . unwrap ( ) ;
42- }
43- } )
44- . detach ( ) ;
45-
46- ctx
47- . observe ( & state_model, |this : & mut ChannelView < M > , model, cx| {
48- let items = model. read ( cx) . messages . clone ( ) ;
49-
50- this. list_state = ListState :: new ( items. len ( ) , ListAlignment :: Bottom , Pixels ( 20. ) , move |idx, _cx| {
51- let item = items. get ( idx) . unwrap ( ) . clone ( ) ;
52- div ( ) . child ( message ( item) ) . into_any_element ( )
53- } ) ;
54-
55- cx. notify ( ) ;
56- } )
57- . detach ( ) ;
34+ async_model. update ( & mut async_ctx, |data, ctx| {
35+ data. add_external_message ( message) ;
36+ ctx. notify ( ) ;
37+ } ) . unwrap ( ) ;
38+ }
39+ } ) . detach ( ) ;
5840
59- let items = state_model. read ( ctx) . messages . clone ( ) ;
41+ ctx. observe ( & state_model, |this : & mut ChannelView < M > , model, cx| {
42+ this. list_state = model. read ( cx) . create_list_state ( ) ;
43+ cx. notify ( ) ;
44+ } ) . detach ( ) ;
6045
6146 let message_input = ctx. new_view ( |cx| {
6247 let mut input = components:: input:: TextInput :: new ( cx) ;
@@ -76,21 +61,19 @@ impl<M: Message + 'static> ChannelView<M> {
7661 text_input. set_text ( "" , cx) ;
7762 } ) ;
7863
79- ctx . foreground_executor ( ) . spawn ( async move {
80- let nonce = random_string :: generate ( 20 , random_string :: charsets :: ALPHANUMERIC ) ;
64+ let nonce = random_string :: generate ( 20 , random_string :: charsets :: ALPHANUMERIC ) ;
65+ let pending = channel_sender . send_message ( content , nonce ) ;
8166
82- channel_sender. send_message ( content, nonce) . await ;
83- } ) . detach ( ) ;
67+ channel_view. list_model . update ( ctx, move |v, _| {
68+ v. add_pending_message ( pending) ;
69+ } ) ;
8470 }
8571 _ => { }
8672 }
8773 } ) . detach ( ) ;
8874
8975 ChannelView :: < M > {
90- list_state : ListState :: new ( items. len ( ) , ListAlignment :: Bottom , Pixels ( 20. ) , move |idx, _cx| {
91- let item = items. get ( idx) . unwrap ( ) . clone ( ) ;
92- div ( ) . child ( message ( item) ) . into_any_element ( )
93- } ) ,
76+ list_state : state_model. read ( ctx) . create_list_state ( ) ,
9477 list_model : state_model,
9578 message_input,
9679 }
0 commit comments