Stream hooks work
All checks were successful
Lint / lint (push) Successful in 14s

This commit is contained in:
Julien Valverdé
2025-05-04 01:18:29 +02:00
parent cf6c84ff8e
commit d6256a7cfd

View File

@@ -483,12 +483,18 @@ export abstract class ReffuseNamespace<R> {
return stream return stream
} }
useSubscribeStream<A, InitialA extends A | undefined, E, R>( useSubscribeStream<A, InitialA extends A | undefined, E, InitialE, R>(
this: ReffuseNamespace<R>, this: ReffuseNamespace<R>,
stream: Stream.Stream<A, E, R>, stream: Stream.Stream<A, E, R>,
initialValue?: InitialA, initialValue?: () => Effect.Effect<InitialA, InitialE, R>,
): InitialA extends A ? Option.Some<A> : Option.Option<A> { ): InitialA extends A ? Option.Some<A> : Option.Option<A> {
const [reactStateValue, setReactStateValue] = React.useState<Option.Option<A>>(Option.fromNullable(initialValue)) const [reactStateValue, setReactStateValue] = React.useState<Option.Option<A>>(this.useMemo(
() => initialValue
? Effect.map(initialValue(), v => Option.some(v as A))
: Effect.succeed(Option.none()),
[],
{ doNotReExecuteOnRuntimeOrContextChange: true },
))
this.useFork(() => Stream.runForEach( this.useFork(() => Stream.runForEach(
Stream.changesWith(stream, (x, y) => x === y), Stream.changesWith(stream, (x, y) => x === y),
@@ -498,17 +504,24 @@ export abstract class ReffuseNamespace<R> {
return reactStateValue as InitialA extends A ? Option.Some<A> : Option.Option<A> return reactStateValue as InitialA extends A ? Option.Some<A> : Option.Option<A>
} }
usePullStream<A, InitialA extends A | undefined, E, R>( usePullStream<A, InitialA extends A | undefined, E, InitialE, R>(
this: ReffuseNamespace<R>, this: ReffuseNamespace<R>,
stream: Stream.Stream<A, E, R>, stream: Stream.Stream<A, E, R>,
initialValue?: InitialA, initialValue?: () => Effect.Effect<InitialA, InitialE, R>,
): [ ): [
latestValue: InitialA extends A ? Option.Some<A> : Option.Option<A>, latestValue: InitialA extends A ? Option.Some<A> : Option.Option<A>,
pull: Effect.Effect<Chunk.Chunk<A>, Option.Option<E>>, pull: Effect.Effect<Chunk.Chunk<A>, Option.Option<E>>,
] { ] {
const scope = this.useScope([stream]) const scope = this.useScope([stream])
const [reactStateValue, setReactStateValue] = React.useState<Option.Option<A>>(Option.fromNullable(initialValue)) const [reactStateValue, setReactStateValue] = React.useState<Option.Option<A>>(this.useMemo(
() => initialValue
? Effect.map(initialValue(), v => Option.some(v as A))
: Effect.succeed(Option.none()),
[],
{ doNotReExecuteOnRuntimeOrContextChange: true },
))
const pull = this.useMemo(() => Effect.context<R>().pipe( const pull = this.useMemo(() => Effect.context<R>().pipe(
Effect.flatMap(context => Stream.toPull(Stream.changesWith(stream, (x, y) => x === y)).pipe( Effect.flatMap(context => Stream.toPull(Stream.changesWith(stream, (x, y) => x === y)).pipe(
Effect.map(effect => effect.pipe( Effect.map(effect => effect.pipe(
@@ -567,11 +580,11 @@ export abstract class ReffuseNamespace<R> {
return props.children(this.useRefState(props.ref)) return props.children(this.useRefState(props.ref))
} }
SubscribeStream<A, InitialA extends A | undefined, E, R>( SubscribeStream<A, InitialA extends A | undefined, E, InitialE, R>(
this: ReffuseNamespace<R>, this: ReffuseNamespace<R>,
props: { props: {
readonly stream: Stream.Stream<A, E, R> readonly stream: Stream.Stream<A, E, R>
readonly initialValue?: InitialA readonly initialValue?: () => Effect.Effect<InitialA, InitialE, R>
readonly children: (latestValue: InitialA extends A ? Option.Some<A> : Option.Option<A>) => React.ReactNode readonly children: (latestValue: InitialA extends A ? Option.Some<A> : Option.Option<A>) => React.ReactNode
}, },
): React.ReactNode { ): React.ReactNode {