From 5b3637afd867b9b05d7a269761c4f548efdbe9f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Mon, 5 May 2025 02:39:12 +0200 Subject: [PATCH] useSubscribeStream work --- packages/example/src/VQueryErrorHandler.tsx | 14 +++++++ packages/reffuse/src/ReffuseNamespace.ts | 43 +++++++++++++-------- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/packages/example/src/VQueryErrorHandler.tsx b/packages/example/src/VQueryErrorHandler.tsx index 0b9fa57..d492f4f 100644 --- a/packages/example/src/VQueryErrorHandler.tsx +++ b/packages/example/src/VQueryErrorHandler.tsx @@ -18,6 +18,20 @@ export function VQueryErrorHandler() { ), []) ) + const error2 = R.useSubscribeStream( + R.useMemo(() => AppQueryErrorHandler.pipe( + Effect.flatMap() + + Effect.map(handler => handler.errors.pipe( + Stream.changes, + Stream.tap(Console.error), + Stream.tap(() => Effect.sync(() => setOpen(true))), + )) + ), []), + + () => Effect.fail(new Error()), + ) + if (Option.isNone(error)) return <> diff --git a/packages/reffuse/src/ReffuseNamespace.ts b/packages/reffuse/src/ReffuseNamespace.ts index b7ac754..0a12773 100644 --- a/packages/reffuse/src/ReffuseNamespace.ts +++ b/packages/reffuse/src/ReffuseNamespace.ts @@ -30,7 +30,7 @@ export abstract class ReffuseNamespace { this.SubRef = this.SubRef.bind(this as any) as any this.SubscribeRefs = this.SubscribeRefs.bind(this as any) as any this.RefState = this.RefState.bind(this as any) as any - this.SubscribeStream = this.SubscribeStream.bind(this as any) as any + // this.SubscribeStream = this.SubscribeStream.bind(this as any) as any } @@ -483,14 +483,23 @@ export abstract class ReffuseNamespace { return stream } - useSubscribeStream( + useSubscribeStream( this: ReffuseNamespace, stream: Stream.Stream, - initialValue?: () => Effect.Effect, - ): InitialA extends A ? Option.Some : Option.Option { - const [reactStateValue, setReactStateValue] = React.useState>(this.useMemo( + ): Option.Option + useSubscribeStream( + this: ReffuseNamespace, + stream: Stream.Stream, + initialValue: () => Effect.Effect, + ): Option.Some + useSubscribeStream( + this: ReffuseNamespace, + stream: Stream.Stream, + initialValue?: () => Effect.Effect, + ): Option.Option { + const [reactStateValue, setReactStateValue] = React.useState(this.useMemo( () => initialValue - ? Effect.map(initialValue(), v => Option.some(v as A)) + ? Effect.map(initialValue(), Option.some) : Effect.succeed(Option.none()), [], { doNotReExecuteOnRuntimeOrContextChange: true }, @@ -501,7 +510,7 @@ export abstract class ReffuseNamespace { v => Effect.sync(() => setReactStateValue(Option.some(v))), ), [stream]) - return reactStateValue as InitialA extends A ? Option.Some : Option.Option + return reactStateValue } usePullStream( @@ -580,16 +589,16 @@ export abstract class ReffuseNamespace { return props.children(this.useRefState(props.ref)) } - SubscribeStream( - this: ReffuseNamespace, - props: { - readonly stream: Stream.Stream - readonly initialValue?: () => Effect.Effect - readonly children: (latestValue: InitialA extends A ? Option.Some : Option.Option) => React.ReactNode - }, - ): React.ReactNode { - return props.children(this.useSubscribeStream(props.stream, props.initialValue)) - } + // SubscribeStream( + // this: ReffuseNamespace, + // props: { + // readonly stream: Stream.Stream + // readonly initialValue?: () => Effect.Effect + // readonly children: (latestValue: InitialA extends A ? Option.Some : Option.Option) => React.ReactNode + // }, + // ): React.ReactNode { + // return props.children(this.useSubscribeStream(props.stream, props.initialValue)) + // } }