0.1.0 #1
@@ -1,32 +1,39 @@
|
|||||||
import { Effect, Fiber, Layer, ManagedRuntime, Ref, Runtime, Scope, Stream, SubscriptionRef } from "effect"
|
import { Context, Effect, Fiber, FiberRefs, Layer, ManagedRuntime, Ref, Runtime, RuntimeFlags, Scope, Stream, SubscriptionRef } from "effect"
|
||||||
import React from "react"
|
import React from "react"
|
||||||
|
|
||||||
|
|
||||||
export class Reffuse<R, ER> {
|
export interface ProviderProps<R, E> {
|
||||||
|
readonly layer: Layer.Layer<R, E>
|
||||||
|
readonly children?: React.ReactNode
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export class Reffuse<R> {
|
||||||
|
|
||||||
|
readonly Context = React.createContext<Runtime.Runtime<R>>(null!)
|
||||||
|
readonly Provider: React.FC<ProviderProps<R, any>>
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
runtime: ManagedRuntime.ManagedRuntime<R, ER>
|
runtime: Runtime.Runtime<R>
|
||||||
) {
|
) {
|
||||||
this.Context = React.createContext<ManagedRuntime.ManagedRuntime<R, ER>>(null!)
|
this.Provider = (props: ProviderProps<R, any>) => {
|
||||||
|
const runtime = React.useMemo(() => Runtime.make({
|
||||||
|
context: Context.empty(),
|
||||||
|
runtimeFlags: RuntimeFlags.make(),
|
||||||
|
fiberRefs: FiberRefs.empty(),
|
||||||
|
}), [])
|
||||||
|
|
||||||
this.Provider = (props: { readonly children?: React.ReactNode }) => (
|
return (
|
||||||
<this.Context
|
<this.Context
|
||||||
{...props}
|
{...props}
|
||||||
value={runtime}
|
value={runtime}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
|
}
|
||||||
const context = runtime.runtimeEffect.pipe(
|
|
||||||
Effect.map(r => Layer.succeedContext(r.context)),
|
|
||||||
runtime.runSync,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
readonly Context: React.Context<ManagedRuntime.ManagedRuntime<R, ER>>
|
|
||||||
readonly Provider: React.FC<{ readonly children?: React.ReactNode }>
|
|
||||||
|
|
||||||
|
useRuntime(): Runtime.Runtime<R> {
|
||||||
useRuntime(): ManagedRuntime.ManagedRuntime<R, ER> {
|
|
||||||
return React.useContext(this.Context)
|
return React.useContext(this.Context)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
31
packages/reffuse/src/tests.ts
Normal file
31
packages/reffuse/src/tests.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import { Context, Effect, FiberRefs, Layer, Ref, Runtime, RuntimeFlags } from "effect"
|
||||||
|
|
||||||
|
|
||||||
|
const runtime = Runtime.make({
|
||||||
|
context: Context.empty(),
|
||||||
|
runtimeFlags: RuntimeFlags.make(),
|
||||||
|
fiberRefs: FiberRefs.empty(),
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
class MyService extends Effect.Service<MyService>()("MyServer", {
|
||||||
|
effect: Effect.gen(function*() {
|
||||||
|
return {
|
||||||
|
ref: yield* Ref.make("initial value")
|
||||||
|
} as const
|
||||||
|
})
|
||||||
|
}) {}
|
||||||
|
|
||||||
|
const MyLayer = Layer.empty.pipe(
|
||||||
|
Layer.provideMerge(MyService.Default)
|
||||||
|
)
|
||||||
|
|
||||||
|
const setMyServiceValue = (value: string) => Effect.gen(function*() {
|
||||||
|
console.log("previous value: ", yield* (yield* MyService).ref)
|
||||||
|
yield* Ref.set((yield* MyService).ref, value)
|
||||||
|
console.log("new value: ", yield* (yield* MyService).ref)
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
Runtime.runSync(runtime)(setMyServiceValue("1").pipe(Effect.provide(MyLayer)))
|
||||||
|
Runtime.runSync(runtime)(setMyServiceValue("2").pipe(Effect.provide(MyLayer)))
|
||||||
Reference in New Issue
Block a user