@@ -1192,6 +1192,21 @@ export const keepAlive = <A extends Rx<any>>(self: A): A =>
11921192 keepAlive : true
11931193 } )
11941194
1195+ /**
1196+ * Reverts the `keepAlive` behavior of a reactive value, allowing it to be
1197+ * disposed of when not in use.
1198+ *
1199+ * Note that Rx's have this behavior by default.
1200+ *
1201+ * @since 1.0.0
1202+ * @category combinators
1203+ */
1204+ export const autoDispose = < A extends Rx < any > > ( self : A ) : A =>
1205+ Object . assign ( Object . create ( Object . getPrototypeOf ( self ) ) , {
1206+ ...self ,
1207+ keepAlive : false
1208+ } )
1209+
11951210/**
11961211 * @since 1.0.0
11971212 * @category combinators
@@ -1433,7 +1448,9 @@ export const optimisticFn: {
14331448 < A , W , XA , XE , OW = W > (
14341449 options : {
14351450 readonly reducer : ( current : NoInfer < A > , update : OW ) => NoInfer < W >
1436- readonly fn : RxResultFn < NoInfer < OW > , XA , XE >
1451+ readonly fn :
1452+ | RxResultFn < NoInfer < OW > , XA , XE >
1453+ | ( ( set : ( result : NoInfer < W > ) => void ) => RxResultFn < NoInfer < OW > , XA , XE > )
14371454 }
14381455 ) : (
14391456 self : Writable < A , Rx < Result . Result < W , unknown > > >
@@ -1442,14 +1459,18 @@ export const optimisticFn: {
14421459 self : Writable < A , Rx < Result . Result < W , unknown > > > ,
14431460 options : {
14441461 readonly reducer : ( current : NoInfer < A > , update : OW ) => NoInfer < W >
1445- readonly fn : RxResultFn < NoInfer < OW > , XA , XE >
1462+ readonly fn :
1463+ | RxResultFn < NoInfer < OW > , XA , XE >
1464+ | ( ( set : ( result : NoInfer < W > ) => void ) => RxResultFn < NoInfer < OW > , XA , XE > )
14461465 }
14471466 ) : RxResultFn < OW , XA , XE >
14481467} = dual ( 2 , < A , W , XA , XE , OW = W > (
14491468 self : Writable < A , Rx < Result . Result < W , unknown > > > ,
14501469 options : {
14511470 readonly reducer : ( current : NoInfer < A > , update : OW ) => NoInfer < W >
1452- readonly fn : RxResultFn < OW , XA , XE >
1471+ readonly fn :
1472+ | RxResultFn < NoInfer < OW > , XA , XE >
1473+ | ( ( set : ( result : NoInfer < W > ) => void ) => RxResultFn < NoInfer < OW > , XA , XE > )
14531474 }
14541475) : RxResultFn < OW , XA , XE > => {
14551476 const transition = state < Result . Result < W , unknown > > ( Result . initial ( ) )
@@ -1460,8 +1481,16 @@ export const optimisticFn: {
14601481 }
14611482 get . set ( transition , Result . success ( value , { waiting : true } ) )
14621483 get . set ( self , transition )
1463- get . set ( options . fn , arg )
1464- return Effect . onExit ( get . result ( options . fn , { suspendOnWaiting : true } ) , ( exit ) => {
1484+ const fn = typeof options . fn === "function"
1485+ ? autoDispose ( options . fn ( ( value ) =>
1486+ get . set (
1487+ transition ,
1488+ Result . success ( Result . isResult ( value ) ? Result . waiting ( value ) : value , { waiting : true } )
1489+ )
1490+ ) )
1491+ : options . fn
1492+ get . set ( fn , arg )
1493+ return Effect . onExit ( get . result ( fn , { suspendOnWaiting : true } ) , ( exit ) => {
14651494 get . set ( transition , Result . fromExit ( Exit . as ( exit , value ) ) )
14661495 return Effect . void
14671496 } )
0 commit comments