0.1.0 #1
3
bun.lock
3
bun.lock
@@ -13,6 +13,9 @@
|
||||
"packages/effect-fc": {
|
||||
"name": "effect-fc",
|
||||
"version": "0.1.0",
|
||||
"dependencies": {
|
||||
"@typed/async-data": "^0.13.1",
|
||||
},
|
||||
"devDependencies": {
|
||||
"@effect/language-service": "^0.23.3",
|
||||
},
|
||||
|
||||
@@ -40,5 +40,8 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@effect/language-service": "^0.23.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"@typed/async-data": "^0.13.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import * as AsyncData from "@typed/async-data"
|
||||
import { type Context, Effect, ExecutionStrategy, Exit, type Layer, Option, pipe, PubSub, Ref, Runtime, Scope, Stream, SubscriptionRef } from "effect"
|
||||
import * as React from "react"
|
||||
import { SetStateAction } from "./types/index.js"
|
||||
@@ -196,6 +197,29 @@ export const useContextSync: {
|
||||
), [scope])
|
||||
})
|
||||
|
||||
export const useContextAsync: {
|
||||
<ROut, E, RIn>(
|
||||
layer: Layer.Layer<ROut, E, RIn>,
|
||||
options?: ScopeOptions,
|
||||
): Effect.Effect<AsyncData.AsyncData<Context.Context<ROut>, E>, never, Exclude<RIn, Scope.Scope>>
|
||||
} = Effect.fn("useContextAsync")(function* <ROut, E, RIn>(
|
||||
layer: Layer.Layer<ROut, E, RIn>,
|
||||
options?: ScopeOptions,
|
||||
) {
|
||||
const [context, setContext] = React.useState<AsyncData.AsyncData<Context.Context<ROut>, E>>(AsyncData.noData())
|
||||
|
||||
yield* useFork(() => Effect.sync(() => setContext(AsyncData.loading())).pipe(
|
||||
Effect.andThen(Effect.provide(Effect.context<ROut>(), layer)),
|
||||
Effect.match({
|
||||
onSuccess: v => AsyncData.success(v),
|
||||
onFailure: e => AsyncData.fail(e),
|
||||
}),
|
||||
Effect.andThen(setContext),
|
||||
), [layer], options)
|
||||
|
||||
return context
|
||||
})
|
||||
|
||||
|
||||
export const useRefFromReactiveValue: {
|
||||
<A>(value: A): Effect.Effect<SubscriptionRef.SubscriptionRef<A>>
|
||||
|
||||
Reference in New Issue
Block a user