char/avril
(wip) why'd you have to go and make things so complicaaaaated?
git clone https://git.t4t.associates/char/avril
d71572b
main
1use std:: collections:: HashMap ; 2use std:: io; 3 4use crate :: v8::{ self , Global , WasmStreaming }; 5 6const MAX_ID : u64 =9_007_199_254_740_991 ; 7 8# [ doc ( hidden )] 9pub struct WasmStreamingStart { 10pub id : u64 , 11pub callback : Option < Global < v8:: Function >>, 12} 13 14# [ doc ( hidden )] 15pub struct WasmStreamingStartError { 16pub message : String , 17pub stream : WasmStreaming < false >, 18} 19 20# [ derive ( Default )] 21pub struct WasmStreamingRegistry { 22next_id : u64 , 23callback : Option < Global < v8:: Function >>, 24streams : HashMap < u64 , WasmStreaming < false >>, 25} 26 27impl WasmStreamingRegistry { 28pub ( crate ) fn is_empty ( & self ) ->bool { 29self . callback . is_none () &&self . streams . is_empty () &&self . next_id ==0 30} 31 32pub fn register ( & mut self , callback : Global < v8:: Function >) -> io:: Result <()> { 33if self . callback . is_some () { 34return Err ( io:: Error :: other ( 35"Wasm streaming callback is already registered" , 36)); 37} 38self . callback =Some ( callback); 39Ok (()) 40} 41 42pub fn start ( 43& mut self , 44stream : WasmStreaming < false >, 45) ->Result < WasmStreamingStart , WasmStreamingStartError > { 46let callback =self . callback . as_ref (). cloned (); 47let Some ( id) =self . next_id . checked_add ( 1 ). filter ( |id|* id <=MAX_ID ) else { 48return Err ( WasmStreamingStartError { 49message : "Wasm streaming resource IDs exhausted" . to_owned (), 50 stream, 51}); 52}; 53self . next_id = id; 54self . streams . insert ( id, stream); 55Ok ( WasmStreamingStart { id, callback}) 56} 57 58pub fn stream_mut ( & mut self , id : u64 ) -> io:: Result < & mut WasmStreaming < false >> { 59self . streams 60. get_mut ( & id) 61. ok_or_else ( || io:: Error :: other ( "Wasm streaming resource is closed" )) 62} 63 64pub fn finish ( & mut self , id : u64 ) -> io:: Result <()> { 65self . streams 66. remove ( & id) 67. ok_or_else ( || io:: Error :: other ( "Wasm streaming resource is closed" )) ? 68. finish (); 69Ok (()) 70} 71 72pub fn abort ( & mut self , id : u64 , exception : v8:: Local < v8:: Value >) -> io:: Result <()> { 73self . streams 74. remove ( & id) 75. ok_or_else ( || io:: Error :: other ( "Wasm streaming resource is closed" )) ? 76. abort ( Some ( exception)); 77Ok (()) 78} 79}