Compare commits
3 Commits
85ed37b591
...
610c1bf759
| Author | SHA1 | Date | |
|---|---|---|---|
| 610c1bf759 | |||
|
|
25d5defdca | ||
|
|
f78b9f318a |
14
packages/effect-fc/src/PubSub.ts
Normal file
14
packages/effect-fc/src/PubSub.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { Effect, PubSub, type Scope } from "effect"
|
||||
import type * as React from "react"
|
||||
import * as Component from "./Component.js"
|
||||
|
||||
|
||||
export const usePubSubFromReactiveValues = Effect.fnUntraced(function* <const A extends React.DependencyList>(
|
||||
values: A
|
||||
): Effect.fn.Return<PubSub.PubSub<A>, never, Scope.Scope> {
|
||||
const pubsub = yield* Component.useOnMount(() => Effect.acquireRelease(PubSub.unbounded<A>(), PubSub.shutdown))
|
||||
yield* Component.useReactEffect(() => Effect.unlessEffect(PubSub.publish(pubsub, values), PubSub.isShutdown(pubsub)), values)
|
||||
return pubsub
|
||||
})
|
||||
|
||||
export * from "effect/PubSub"
|
||||
29
packages/effect-fc/src/Query.ts
Normal file
29
packages/effect-fc/src/Query.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { type Effect, Pipeable, Predicate, type Stream } from "effect"
|
||||
|
||||
|
||||
export const QueryTypeId: unique symbol = Symbol.for("@effect-fc/Query/Query")
|
||||
export type QueryTypeId = typeof QueryTypeId
|
||||
|
||||
export interface Query<in out K extends readonly any[], in out A, in out E = never, out R = never, in out P = never>
|
||||
extends Pipeable.Pipeable {
|
||||
readonly [QueryTypeId]: QueryTypeId
|
||||
|
||||
readonly key: Stream.Stream<K>
|
||||
readonly query: (key: K) => Effect.Effect<A, E, R>
|
||||
readonly initialProgress: P
|
||||
}
|
||||
|
||||
class QueryImpl<in out K extends readonly any[], in out A, in out E = never, out R = never, in out P = never>
|
||||
extends Pipeable.Class() implements Query<K, A, E, R, P> {
|
||||
readonly [QueryTypeId]: QueryTypeId = QueryTypeId
|
||||
|
||||
constructor(
|
||||
readonly key: Stream.Stream<K>,
|
||||
readonly query: (key: K) => Effect.Effect<A, E, R>,
|
||||
readonly initialProgress: P,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
}
|
||||
|
||||
export const isQuery = (u: unknown): u is Query<unknown[], unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, QueryTypeId)
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Effect, Equivalence, Option, PubSub, Ref, type Scope, Stream } from "effect"
|
||||
import { Effect, Equivalence, Option, Stream } from "effect"
|
||||
import * as React from "react"
|
||||
import * as Component from "./Component.js"
|
||||
|
||||
@@ -30,27 +30,4 @@ export const useStream: {
|
||||
return reactStateValue as Option.Some<A>
|
||||
})
|
||||
|
||||
export const useStreamFromReactiveValues = Effect.fnUntraced(function* <const A extends React.DependencyList>(
|
||||
values: A
|
||||
): Effect.fn.Return<Stream.Stream<A>, never, Scope.Scope> {
|
||||
const { latest, pubsub, stream } = yield* Component.useOnMount(() => Effect.Do.pipe(
|
||||
Effect.bind("latest", () => Ref.make(values)),
|
||||
Effect.bind("pubsub", () => Effect.acquireRelease(PubSub.unbounded<A>(), PubSub.shutdown)),
|
||||
Effect.let("stream", ({ latest, pubsub }) => latest.pipe(
|
||||
Effect.flatMap(a => Effect.map(
|
||||
Stream.fromPubSub(pubsub, { scoped: true }),
|
||||
s => Stream.concat(Stream.make(a), s),
|
||||
)),
|
||||
Stream.unwrapScoped,
|
||||
)),
|
||||
))
|
||||
|
||||
yield* Component.useReactEffect(() => Ref.set(latest, values).pipe(
|
||||
Effect.andThen(PubSub.publish(pubsub, values)),
|
||||
Effect.unlessEffect(PubSub.isShutdown(pubsub)),
|
||||
), values)
|
||||
|
||||
return stream
|
||||
})
|
||||
|
||||
export * from "effect/Stream"
|
||||
|
||||
@@ -4,6 +4,8 @@ export * as ErrorObserver from "./ErrorObserver.js"
|
||||
export * as Form from "./Form.js"
|
||||
export * as Memoized from "./Memoized.js"
|
||||
export * as PropertyPath from "./PropertyPath.js"
|
||||
export * as PubSub from "./PubSub.js"
|
||||
export * as Query from "./Query.js"
|
||||
export * as ReactRuntime from "./ReactRuntime.js"
|
||||
export * as Result from "./Result.js"
|
||||
export * as SetStateAction from "./SetStateAction.js"
|
||||
|
||||
Reference in New Issue
Block a user