1
1
use crate :: prelude:: * ;
2
2
use std:: cell:: RefCell ;
3
3
use std:: collections:: HashMap ;
4
+ use wasm_bindgen:: prelude:: Closure ;
4
5
use wasm_bindgen:: JsCast ;
5
6
use wasm_bindgen:: JsValue ;
7
+ use web_sys:: window;
6
8
use web_sys:: Event ;
7
9
8
10
pub struct EventRegistry ;
@@ -42,18 +44,24 @@ impl EventRegistry {
42
44
) {
43
45
Self :: register ( key, cx, value) ;
44
46
}
47
+
48
+ pub fn initialize ( ) -> ParseResult < ( ) > {
49
+ let constants = CurrentHydrator :: with ( |h| h. html_constants ( ) . clone ( ) ) ;
50
+ playback_prehydrate_events ( & constants) ?;
51
+ hook_up_event_listeners ( & constants) ?;
52
+ Ok ( ( ) )
53
+ }
45
54
}
46
55
47
- pub fn playback_prehydrate_events ( ) -> ParseResult < ( ) > {
56
+ fn playback_prehydrate_events ( constants : & HtmlConstants ) -> ParseResult < ( ) > {
48
57
sweet_loader_extern:: GLOBAL . with ( |global| {
49
- let constants = CurrentHydrator :: with ( |h| h. html_constants ( ) . clone ( ) ) ;
50
58
// let event_handler =
51
59
// js_sys::Reflect::get(&global, &constants.event_handler.into())
52
60
// .map_err(|_| {
53
61
// ParseError::Hydration("could not find event handler".into())
54
62
// })?;
55
63
let prehydrate_events =
56
- js_sys:: Reflect :: get ( & global, & constants. prehydrate_events . into ( ) )
64
+ js_sys:: Reflect :: get ( & global, & constants. event_store . into ( ) )
57
65
. map_err ( |_| {
58
66
ParseError :: Hydration ( "could not find event handler" . into ( ) )
59
67
} ) ?;
@@ -68,31 +76,47 @@ pub fn playback_prehydrate_events() -> ParseResult<()> {
68
76
EventRegistry :: trigger ( & event_type, id, event. unchecked_into ( ) ) ;
69
77
}
70
78
}
71
- // } else {
72
- // return Err(ParseError::Hydration("bad event".into()));
73
- // }
74
- // }
79
+ js_sys:: Reflect :: delete_property (
80
+ & global. unchecked_ref ( ) ,
81
+ & constants. event_store . into ( ) ,
82
+ )
83
+ . unwrap ( ) ;
84
+ js_sys:: Reflect :: delete_property (
85
+ & global. unchecked_ref ( ) ,
86
+ & constants. event_handler . into ( ) ,
87
+ )
88
+ . unwrap ( ) ;
89
+
75
90
Ok ( ( ) )
76
91
} )
77
92
}
78
93
79
- // let event = event_arr.get(1);
80
- // Self::trigger
81
- // js_sys::Reflect::delete_property(
82
- // &sweet.unchecked_ref(),
83
- // &"uncanny".into(),
84
- // )
85
- // .unwrap_or_default();
86
- // }
87
- // let closure =
88
- // Closure::wrap(Box::new(move |id: usize, evt: web_sys::Event| {
89
- // func(id, evt);
90
- // }) as Box<dyn FnMut(usize, web_sys::Event)>);
91
-
92
- // js_sys::Reflect::set(&sweet, &"event".into(), &closure.as_ref())
93
- // .unwrap_or_default();
94
+ fn hook_up_event_listeners ( constants : & HtmlConstants ) -> ParseResult < ( ) > {
95
+ REGISTERED_EVENTS . with ( |current| -> ParseResult < ( ) > {
96
+ let mut current = current. borrow_mut ( ) ;
97
+ let document = window ( ) . unwrap ( ) . document ( ) . unwrap ( ) ;
98
+ for ( ( el_id, key) , func) in current. drain ( ) {
99
+ let el = document
100
+ . query_selector ( & format ! ( "[{}='{}']" , constants. id_key, el_id) )
101
+ . ok ( )
102
+ . flatten ( )
103
+ . ok_or_else ( || {
104
+ ParseError :: Hydration ( "could not find element" . into ( ) )
105
+ } ) ?;
106
+ el. remove_attribute ( & key) . unwrap ( ) ;
94
107
95
- // closure.forget();
108
+ let closure = Closure :: wrap ( Box :: new ( move |e : JsValue | {
109
+ func ( e) ;
110
+ } ) as Box < dyn Fn ( JsValue ) > ) ;
111
+ el. add_event_listener_with_callback (
112
+ & key,
113
+ closure. as_ref ( ) . unchecked_ref ( ) ,
114
+ )
115
+ . unwrap ( ) ;
116
+ }
117
+ Ok ( ( ) )
118
+ } )
119
+ }
96
120
97
121
pub mod sweet_loader_extern {
98
122
use wasm_bindgen:: prelude:: * ;
0 commit comments