Skip to content

Commit

Permalink
Add makeFromRuntime and makeFromRuntimeContext
Browse files Browse the repository at this point in the history
Move RuntimeProvider.tsx to RuntimeProvider.ts because the .tsx extension broke linting.
  • Loading branch information
evelant authored and datner committed Sep 7, 2023
1 parent 024ce51 commit d896dfd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/six-dogs-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect-react": patch
---

Add makeFromRuntime and makeFromRuntimeContext constructors to RuntimeProvider
24 changes: 23 additions & 1 deletion src/RuntimeProvider.tsx → src/RuntimeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import * as Effect from "@effect/io/Effect"
import * as Layer from "@effect/io/Layer"
import type * as Runtime from "@effect/io/Runtime"
import * as Scope from "@effect/io/Scope"
import { createContext } from "react"
import type { UseResult } from "effect-react/hooks/useResult"
import { makeUseResult } from "effect-react/hooks/useResult"
import type { UseResultCallback } from "effect-react/hooks/useResultCallback"
import { makeUseResultCallback } from "effect-react/hooks/useResultCallback"
import { createContext } from "react"

export { RuntimeContext } from "effect-react/internal/runtimeContext"

Expand Down Expand Up @@ -37,3 +37,25 @@ export const makeFromLayer = <R, E>(
useResult: makeUseResult(RuntimeContext)
}
}

export const makeFromRuntime = <R>(
runtime: Runtime.Runtime<R>
): ReactEffectBag<R> => {
const RuntimeContext = createContext(runtime)

return {
RuntimeContext,
useResultCallback: makeUseResultCallback(RuntimeContext),
useResult: makeUseResult(RuntimeContext)
}
}

export const makeFromRuntimeContext = <R>(
RuntimeContext: React.Context<Runtime.Runtime<R>>
): ReactEffectBag<R> => {
return {
RuntimeContext,
useResultCallback: makeUseResultCallback(RuntimeContext),
useResult: makeUseResult(RuntimeContext)
}
}

0 comments on commit d896dfd

Please sign in to comment.