Add Subscribable module
Some checks failed
Lint / lint (push) Failing after 11s

This commit is contained in:
Julien Valverdé
2026-03-27 11:21:03 +01:00
parent 15a62459c8
commit 2fde98c367
3 changed files with 63 additions and 8 deletions

View File

@@ -1,5 +1,6 @@
import { Array, Chunk, Effect, Function, Pipeable, Predicate, Readable, Stream, Subscribable, type SubscriptionRef, type SynchronizedRef } from "effect"
import { Array, Chunk, Effect, Function, Pipeable, Predicate, Readable, Stream, type SubscriptionRef, type SynchronizedRef } from "effect"
import type { NoSuchElementException } from "effect/Cause"
import * as Subscribable from "./Subscribable.js"
export const LensTypeId: unique symbol = Symbol.for("@effect-fc/Lens/Lens")
@@ -206,7 +207,7 @@ export const mapStream: {
/**
* Narrows the focus to a field of an object. Replaces the object in an immutable fashion when written to.
*/
export const focusField: {
export const focusObjectField: {
<A extends object, K extends keyof A, ER, EW, RR, RW>(
self: Lens<A, ER, EW, RR, RW>,
key: K,
@@ -223,7 +224,7 @@ export const focusField: {
(a, b) => Object.setPrototypeOf({ ...a, [key]: b }, Object.getPrototypeOf(a)),
))
export declare namespace focusMutableField {
export declare namespace focusObjectMutableField {
export type WritableKeys<T> = {
[K in keyof T]-?: IfEquals<
{ [P in K]: T[K] },
@@ -239,15 +240,15 @@ export declare namespace focusMutableField {
/**
* Narrows the focus to a mutable field of an object. Mutates the object in place when written to.
*/
export const focusMutableField: {
<A extends object, K extends focusMutableField.WritableKeys<A>, ER, EW, RR, RW>(
export const focusObjectMutableField: {
<A extends object, K extends focusObjectMutableField.WritableKeys<A>, ER, EW, RR, RW>(
self: Lens<A, ER, EW, RR, RW>,
key: K,
): Lens<A[K], ER, EW, RR, RW>
<A extends object, K extends focusMutableField.WritableKeys<A>, ER, EW, RR, RW>(
<A extends object, K extends focusObjectMutableField.WritableKeys<A>, ER, EW, RR, RW>(
key: K,
): (self: Lens<A, ER, EW, RR, RW>) => Lens<A[K], ER, EW, RR, RW>
} = Function.dual(2, <A extends object, K extends focusMutableField.WritableKeys<A>, ER, EW, RR, RW>(
} = Function.dual(2, <A extends object, K extends focusObjectMutableField.WritableKeys<A>, ER, EW, RR, RW>(
self: Lens<A, ER, EW, RR, RW>,
key: K,
): Lens<A[K], ER, EW, RR, RW> => map(self, a => a[key], (a, b) => { a[key] = b; return a }))
@@ -259,7 +260,7 @@ export const focusArrayAt: {
<A extends readonly any[], ER, EW, RR, RW>(
self: Lens<A, ER, EW, RR, RW>,
index: number,
): Lens<A[number]>
): Lens<A[number], ER | NoSuchElementException, EW | NoSuchElementException, RR, RW>
<A extends readonly any[], ER, EW, RR, RW>(
index: number
): (self: Lens<A, ER, EW, RR, RW>) => Lens<A[number], ER | NoSuchElementException, EW | NoSuchElementException, RR, RW>

View File

@@ -0,0 +1,53 @@
import { Array, Chunk, Function, Subscribable } from "effect"
import type { NoSuchElementException } from "effect/Cause"
export * from "effect/Subscribable"
/**
* Narrows the focus to a field of an object.
*/
export const focusObjectField: {
<A extends object, K extends keyof A, E, R>(
self: Subscribable.Subscribable<A, E, R>,
key: K,
): Subscribable.Subscribable<A[K], E, R>
<A extends object, K extends keyof A, E, R>(
key: K,
): (self: Subscribable.Subscribable<A, E, R>) => Subscribable.Subscribable<A[K], E, R>
} = Function.dual(2, <A extends object, K extends keyof A, E, R>(
self: Subscribable.Subscribable<A, E, R>,
key: K,
): Subscribable.Subscribable<A[K], E, R> => Subscribable.map(self, a => a[key]))
/**
* Narrows the focus to an indexed element of an array.
*/
export const focusArrayAt: {
<A extends readonly any[], E, R>(
self: Subscribable.Subscribable<A, E, R>,
index: number,
): Subscribable.Subscribable<A[number], E, R>
<A extends readonly any[], E, R>(
index: number
): (self: Subscribable.Subscribable<A, E, R>) => Subscribable.Subscribable<A[number], E | NoSuchElementException, R>
} = Function.dual(2, <A extends readonly any[], E, R>(
self: Subscribable.Subscribable<A, E, R>,
index: number,
): Subscribable.Subscribable<A[number], E | NoSuchElementException, R> => Subscribable.mapEffect(self, Array.get(index)))
/**
* Narrows the focus to an indexed element of `Chunk`.
*/
export const focusChunkAt: {
<A, E, R>(
self: Subscribable.Subscribable<Chunk.Chunk<A>, E, R>,
index: number,
): Subscribable.Subscribable<A, E | NoSuchElementException, R>
<A, E, R>(
index: number
): (self: Subscribable.Subscribable<Chunk.Chunk<A>, E, R>) => Subscribable.Subscribable<A, E | NoSuchElementException, R>
} = Function.dual(2, <A, E, R>(
self: Subscribable.Subscribable<Chunk.Chunk<A>, E, R>,
index: number,
): Subscribable.Subscribable<A, E | NoSuchElementException, R> => Subscribable.mapEffect(self, Chunk.get(index)))

View File

@@ -1,2 +1,3 @@
export * as Lens from "./Lens.js"
export * as PropertyPath from "./PropertyPath.js"
export * as Subscribable from "./Subscribable.js"