@@ -168,6 +168,41 @@ where
168168
169169 data
170170 }
171+
172+ /// Synchronously, reactively reads the current value of the resource and applies the function
173+ /// `f` to its value if it is `Some(_)`.
174+ #[ track_caller]
175+ pub fn map < U > ( & self , f : impl FnOnce ( & T ) -> U ) -> Option < U >
176+ where
177+ T : Send + Sync + ' static ,
178+ {
179+ self . try_with ( |n| n. as_ref ( ) . map ( f) ) ?
180+ }
181+ }
182+
183+ impl < T , E , Ser > ArcOnceResource < Result < T , E > , Ser >
184+ where
185+ Ser : Encoder < Result < T , E > > + Decoder < Result < T , E > > ,
186+ <Ser as Encoder < Result < T , E > > >:: Error : Debug ,
187+ <Ser as Decoder < Result < T , E > > >:: Error : Debug ,
188+ <<Ser as Decoder < Result < T , E > > >:: Encoded as FromEncodedStr >:: DecodingError :
189+ Debug ,
190+ <Ser as Encoder < Result < T , E > > >:: Encoded : IntoEncodedString ,
191+ <Ser as Decoder < Result < T , E > > >:: Encoded : FromEncodedStr ,
192+ T : Send + Sync + ' static ,
193+ E : Send + Sync + Clone + ' static ,
194+ {
195+ /// Applies the given function when a resource that returns `Result<T, E>`
196+ /// has resolved and loaded an `Ok(_)`, rather than requiring nested `.map()`
197+ /// calls over the `Option<Result<_, _>>` returned by the resource.
198+ ///
199+ /// This is useful when used with features like server functions, in conjunction
200+ /// with `<ErrorBoundary/>` and `<Suspense/>`, when these other components are
201+ /// left to handle the `None` and `Err(_)` states.
202+ #[ track_caller]
203+ pub fn and_then < U > ( & self , f : impl FnOnce ( & T ) -> U ) -> Option < Result < U , E > > {
204+ self . map ( |data| data. as_ref ( ) . map ( f) . map_err ( |e| e. clone ( ) ) )
205+ }
171206}
172207
173208impl < T , Ser > ArcOnceResource < T , Ser > {
@@ -534,6 +569,37 @@ where
534569 defined_at,
535570 }
536571 }
572+
573+ /// Synchronously, reactively reads the current value of the resource and applies the function
574+ /// `f` to its value if it is `Some(_)`.
575+ pub fn map < U > ( & self , f : impl FnOnce ( & T ) -> U ) -> Option < U > {
576+ self . try_with ( |n| n. as_ref ( ) . map ( |n| Some ( f ( n) ) ) ) ?. flatten ( )
577+ }
578+ }
579+
580+ impl < T , E , Ser > OnceResource < Result < T , E > , Ser >
581+ where
582+ Ser : Encoder < Result < T , E > > + Decoder < Result < T , E > > ,
583+ <Ser as Encoder < Result < T , E > > >:: Error : Debug ,
584+ <Ser as Decoder < Result < T , E > > >:: Error : Debug ,
585+ <<Ser as Decoder < Result < T , E > > >:: Encoded as FromEncodedStr >:: DecodingError :
586+ Debug ,
587+ <Ser as Encoder < Result < T , E > > >:: Encoded : IntoEncodedString ,
588+ <Ser as Decoder < Result < T , E > > >:: Encoded : FromEncodedStr ,
589+ T : Send + Sync + ' static ,
590+ E : Send + Sync + Clone + ' static ,
591+ {
592+ /// Applies the given function when a resource that returns `Result<T, E>`
593+ /// has resolved and loaded an `Ok(_)`, rather than requiring nested `.map()`
594+ /// calls over the `Option<Result<_, _>>` returned by the resource.
595+ ///
596+ /// This is useful when used with features like server functions, in conjunction
597+ /// with `<ErrorBoundary/>` and `<Suspense/>`, when these other components are
598+ /// left to handle the `None` and `Err(_)` states.
599+ #[ track_caller]
600+ pub fn and_then < U > ( & self , f : impl FnOnce ( & T ) -> U ) -> Option < Result < U , E > > {
601+ self . map ( |data| data. as_ref ( ) . map ( f) . map_err ( |e| e. clone ( ) ) )
602+ }
537603}
538604
539605impl < T , Ser > OnceResource < T , Ser >
0 commit comments