This commit is contained in:
@@ -5,7 +5,8 @@ import * as PropertyPath from "./PropertyPath.js"
|
||||
export const SubscriptionSubRefTypeId: unique symbol = Symbol.for("effect-fc/types/SubscriptionSubRef")
|
||||
export type SubscriptionSubRefTypeId = typeof SubscriptionSubRefTypeId
|
||||
|
||||
export interface SubscriptionSubRef<in out A, in out B> extends SubscriptionSubRef.Variance<A, B>, SubscriptionRef.SubscriptionRef<A> {
|
||||
export interface SubscriptionSubRef<in out A, in out B extends SubscriptionRef.SubscriptionRef<any>>
|
||||
extends SubscriptionSubRef.Variance<A, B>, SubscriptionRef.SubscriptionRef<A> {
|
||||
readonly parent: SubscriptionRef.SubscriptionRef<B>
|
||||
|
||||
readonly [Unify.typeSymbol]?: unknown
|
||||
@@ -36,7 +37,8 @@ const synchronizedRefVariance = { _A: (_: any) => _ }
|
||||
const subscriptionRefVariance = { _A: (_: any) => _ }
|
||||
const subscriptionSubRefVariance = { _A: (_: any) => _, _B: (_: any) => _ }
|
||||
|
||||
class SubscriptionSubRefImpl<in out A, in out B> extends Effectable.Class<A> implements SubscriptionSubRef<A, B> {
|
||||
class SubscriptionSubRefImpl<in out A, in out B extends SubscriptionRef.SubscriptionRef<any>>
|
||||
extends Effectable.Class<A> implements SubscriptionSubRef<A, B> {
|
||||
readonly [Readable.TypeId]: Readable.TypeId = Readable.TypeId
|
||||
readonly [Subscribable.TypeId]: Subscribable.TypeId = Subscribable.TypeId
|
||||
readonly [Ref.RefTypeId] = refVariance
|
||||
@@ -47,9 +49,9 @@ class SubscriptionSubRefImpl<in out A, in out B> extends Effectable.Class<A> imp
|
||||
readonly get: Effect.Effect<A>
|
||||
|
||||
constructor(
|
||||
readonly parent: SubscriptionRef.SubscriptionRef<B>,
|
||||
readonly getter: (parentValue: B) => A,
|
||||
readonly setter: (parentValue: B, value: A) => B,
|
||||
readonly parent: B,
|
||||
readonly getter: (parentValue: Effect.Effect.Success<B>) => A,
|
||||
readonly setter: (parentValue: Effect.Effect.Success<B>, value: A) => Effect.Effect.Success<B>,
|
||||
) {
|
||||
super()
|
||||
this.get = Effect.map(this.parent, this.getter)
|
||||
@@ -60,12 +62,11 @@ class SubscriptionSubRefImpl<in out A, in out B> extends Effectable.Class<A> imp
|
||||
}
|
||||
|
||||
get changes(): Stream.Stream<A> {
|
||||
return this.get.pipe(
|
||||
Effect.map(a => this.parent.changes.pipe(
|
||||
Stream.map(this.getter),
|
||||
s => Stream.concat(Stream.make(a), s),
|
||||
)),
|
||||
Stream.unwrap,
|
||||
return Stream.unwrap(
|
||||
Effect.map(this.get, a => Stream.concat(
|
||||
Stream.make(a),
|
||||
Stream.map(this.parent.changes, this.getter),
|
||||
))
|
||||
)
|
||||
}
|
||||
|
||||
@@ -75,7 +76,7 @@ class SubscriptionSubRefImpl<in out A, in out B> extends Effectable.Class<A> imp
|
||||
|
||||
modifyEffect<C, E, R>(f: (a: A) => Effect.Effect<readonly [C, A], E, R>): Effect.Effect<C, E, R> {
|
||||
return Effect.Do.pipe(
|
||||
Effect.bind("b", () => this.parent),
|
||||
Effect.bind("b", (): Effect.Effect<Effect.Effect.Success<B>> => this.parent),
|
||||
Effect.bind("ca", ({ b }) => f(this.getter(b))),
|
||||
Effect.tap(({ b, ca: [, a] }) => Ref.set(this.parent, this.setter(b, a))),
|
||||
Effect.map(({ ca: [c] }) => c),
|
||||
@@ -84,28 +85,34 @@ class SubscriptionSubRefImpl<in out A, in out B> extends Effectable.Class<A> imp
|
||||
}
|
||||
|
||||
|
||||
export const makeFromGetSet = <A, B>(
|
||||
parent: SubscriptionRef.SubscriptionRef<B>,
|
||||
export const makeFromGetSet = <A, B extends SubscriptionRef.SubscriptionRef<any>>(
|
||||
parent: B,
|
||||
options: {
|
||||
readonly get: (parentValue: B) => A
|
||||
readonly set: (parentValue: B, value: A) => B
|
||||
readonly get: (parentValue: Effect.Effect.Success<B>) => A
|
||||
readonly set: (parentValue: Effect.Effect.Success<B>, value: A) => Effect.Effect.Success<B>
|
||||
},
|
||||
): SubscriptionSubRef<A, B> => new SubscriptionSubRefImpl(parent, options.get, options.set)
|
||||
|
||||
export const makeFromPath = <B, const P extends PropertyPath.Paths<B>>(
|
||||
parent: SubscriptionRef.SubscriptionRef<B>,
|
||||
export const makeFromPath = <
|
||||
B extends SubscriptionRef.SubscriptionRef<any>,
|
||||
const P extends PropertyPath.Paths<Effect.Effect.Success<B>>,
|
||||
>(
|
||||
parent: B,
|
||||
path: P,
|
||||
): SubscriptionSubRef<PropertyPath.ValueFromPath<B, P>, B> => new SubscriptionSubRefImpl(
|
||||
): SubscriptionSubRef<PropertyPath.ValueFromPath<Effect.Effect.Success<B>, P>, B> => new SubscriptionSubRefImpl(
|
||||
parent,
|
||||
parentValue => Option.getOrThrow(PropertyPath.get(parentValue, path)),
|
||||
(parentValue, value) => Option.getOrThrow(PropertyPath.immutableSet(parentValue, path, value)),
|
||||
)
|
||||
|
||||
export const makeFromChunkRef = <A>(
|
||||
parent: SubscriptionRef.SubscriptionRef<Chunk.Chunk<A>>,
|
||||
export const makeFromChunkRef = <A, B extends SubscriptionRef.SubscriptionRef<Chunk.Chunk<any> | Chunk.NonEmptyChunk<any>>>(
|
||||
parent: B,
|
||||
index: number,
|
||||
): SubscriptionSubRef<A, Chunk.Chunk<A>> => new SubscriptionSubRefImpl(
|
||||
parent,
|
||||
): SubscriptionSubRef<
|
||||
Effect.Effect.Success<B> extends Chunk.Chunk<infer A> ? A : never,
|
||||
B
|
||||
> => new SubscriptionSubRefImpl(
|
||||
parent as SubscriptionRef.SubscriptionRef<Chunk.Chunk<any>>,
|
||||
parentValue => Chunk.unsafeGet(parentValue, index),
|
||||
(parentValue, value) => Chunk.replace(parentValue, index, value),
|
||||
)
|
||||
) as any
|
||||
|
||||
@@ -2,13 +2,17 @@ import { runtime } from "@/runtime"
|
||||
import { Flex, Text, TextField } from "@radix-ui/themes"
|
||||
import { createFileRoute } from "@tanstack/react-router"
|
||||
import { GetRandomValues, makeUuid4 } from "@typed/id"
|
||||
import { Effect } from "effect"
|
||||
import { Chunk, Effect, SubscriptionRef } from "effect"
|
||||
import { Component, Memoized } from "effect-fc"
|
||||
import { SubscriptionSubRef } from "effect-fc/types"
|
||||
import * as React from "react"
|
||||
|
||||
|
||||
const RouteComponent = Component.make(function* RouteComponent() {
|
||||
const [value, setValue] = React.useState("")
|
||||
const myRef = yield* SubscriptionRef.make(Chunk.make({ name: "person 1" } as const))
|
||||
// const myRef = yield* SubscriptionRef.make(Chunk.empty<{ readonly name: "person 1" }>())
|
||||
const mySubRef = SubscriptionSubRef.makeFromChunkRef(myRef, 0)
|
||||
|
||||
return (
|
||||
<Flex direction="column" gap="2">
|
||||
|
||||
Reference in New Issue
Block a user