Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add effectful methods for vitest hooks #3686

5 changes: 5 additions & 0 deletions .changeset/chilled-hotels-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

Adds HashMap.HashMap.Entry type helper
5 changes: 5 additions & 0 deletions .changeset/early-poems-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

`Latch` implements `Effect<void>` with `.await` semantic
5 changes: 5 additions & 0 deletions .changeset/fast-experts-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

fix Unify for RcRef & Deferred
5 changes: 5 additions & 0 deletions .changeset/fluffy-insects-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/vitest": minor
---

Add effectful methods for vitest hooks
5 changes: 5 additions & 0 deletions .changeset/lemon-worms-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

`Pool` is now a subtype of `Effect`, equivalent to `Pool.get`
5 changes: 5 additions & 0 deletions .changeset/purple-beans-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

support ManagedRuntime in Effect.provide
5 changes: 5 additions & 0 deletions .changeset/smart-fishes-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

`ManagedRuntime<R, E>` is subtype of `Effect<Runtime<R>, E, never>`
5 changes: 5 additions & 0 deletions .changeset/tricky-oranges-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

Add an `isRegExp` type guard
6 changes: 6 additions & 0 deletions .changeset/wicked-bears-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"effect": minor
---

`Resource<A, E>` is subtype of `Effect<A, E>`.
`ScopedRed<A>` is subtype of `Effect<A>`.
10 changes: 9 additions & 1 deletion packages/effect/dtslint/HashMap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { pipe } from "effect/Function"
import { hole, pipe } from "effect/Function"
import * as HashMap from "effect/HashMap"
import * as Predicate from "effect/Predicate"
import type * as Types from "effect/Types"

declare const hmLiterals: HashMap.HashMap<"k", "v">
declare const numbers: HashMap.HashMap<string, number>
Expand All @@ -23,6 +24,13 @@ export type K = HashMap.HashMap.Key<typeof hmLiterals>
// $ExpectType "v"
export type V = HashMap.HashMap.Value<typeof hmLiterals>

// -------------------------------------------------------------------------------------
// HashMap.Entry
// -------------------------------------------------------------------------------------

// $ExpectType ["k", "v"]
hole<Types.Simplify<HashMap.HashMap.Entry<typeof hmLiterals>>>()

// -------------------------------------------------------------------------------------
// filter
// -------------------------------------------------------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions packages/effect/dtslint/Predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ if (Predicate.isTupleOfAtLeast(unknowns, 3)) {
unknowns
}

// -------------------------------------------------------------------------------------
// isRegExp
// -------------------------------------------------------------------------------------

// $ExpectType RegExp[]
unknowns.filter(Predicate.isRegExp)

// -------------------------------------------------------------------------------------
// compose
// -------------------------------------------------------------------------------------
Expand Down
66 changes: 52 additions & 14 deletions packages/effect/dtslint/Unify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import * as Either from "effect/Either"
import type * as Exit from "effect/Exit"
import type * as Fiber from "effect/Fiber"
import type * as FiberRef from "effect/FiberRef"
import type * as ManagedRuntime from "effect/ManagedRuntime"
import type * as Micro from "effect/Micro"
import type * as Option from "effect/Option"
import type * as Pool from "effect/Pool"
import type * as Queue from "effect/Queue"
import type * as RcRef from "effect/RcRef"
import type * as Ref from "effect/Ref"
import type * as Resource from "effect/Resource"
import type * as ScopedRef from "effect/ScopedRef"
import type * as Stream from "effect/Stream"
import type * as SubscriptionRef from "effect/SubscriptionRef"
import type * as SynchronizedRef from "effect/SynchronizedRef"
Expand Down Expand Up @@ -73,7 +77,7 @@ export type SubscriptionRefUnify = Unify.Unify<
| SubscriptionRef.SubscriptionRef<1>
| SubscriptionRef.SubscriptionRef<"a">
>
// $ExpectType RcRef<"a" | 1, "b" | 2>
// $ExpectType RcRef<1, 2> | RcRef<"a", "b">
export type RcRefUnify = Unify.Unify<
| RcRef.RcRef<1, 2>
| RcRef.RcRef<"a", "b">
Expand All @@ -98,7 +102,11 @@ export type RuntimeFiberUnify = Unify.Unify<
| Fiber.RuntimeFiber<1, 2>
| Fiber.RuntimeFiber<"a", "b">
>

// $ExpectType ManagedRuntime<1, 2> | ManagedRuntime<"a", "b">
export type ManagedRuntimeUnify = Unify.Unify<
| ManagedRuntime.ManagedRuntime<1, 2>
| ManagedRuntime.ManagedRuntime<"a", "b">
>
// $ExpectType Queue<1> | Queue<"a">
export type QueueUnify = Unify.Unify<
| Queue.Queue<1>
Expand All @@ -109,34 +117,64 @@ export type DequeueUnify = Unify.Unify<
| Queue.Dequeue<1>
| Queue.Dequeue<"a">
>
// $ExpectType Pool<1, 2> | Pool<"a", "b" | "c">
export type PoolUnify = Unify.Unify<
| Pool.Pool<1, 2>
| Pool.Pool<"a", "b">
| Pool.Pool<"a", "c">
>

// $ExpectType 0 | Option<string | number> | Ref<1> | SynchronizedRef<1> | SubscriptionRef<1> | Deferred<1, 2> | Deferred<"a", "b"> | Fiber<"a" | 1, "b" | 2> | RuntimeFiber<"a" | 1, "b" | 2> | Queue<1> | Queue<"a"> | Dequeue<"a" | 1> | Ref<"A"> | SynchronizedRef<"A"> | SubscriptionRef<"A"> | FiberRef<12> | FiberRef<"a2"> | Either<1 | "A", 0 | "E"> | Effect<1 | "A", 0 | "E", "R" | "R1"> | RcRef<1 | "A", 0 | "E">
// $ExpectType ScopedRef<1> | ScopedRef<"a">
export type ScopedRefUnify = Unify.Unify<
| ScopedRef.ScopedRef<1>
| ScopedRef.ScopedRef<"a">
>
// $ExpectType Resource<1, never> | Resource<never, 2> | Resource<1, 2> | Resource<"a", "b"> | Resource<any, any>
export type ResourceUnify = Unify.Unify<
| Resource.Resource<1>
| Resource.Resource<never, 2>
| Resource.Resource<1, 2>
| Resource.Resource<"a", "b">
| Resource.Resource<any, any>
>

// $ExpectType 0 | Option<string | number> | Ref<1> | Ref<"a"> | SynchronizedRef<1> | SynchronizedRef<"a"> | SubscriptionRef<1> | SubscriptionRef<"a"> | RcRef<"a", "b"> | Deferred<"a", "b"> | FiberRef<1> | FiberRef<"a"> | ManagedRuntime<"a", "b"> | Queue<1> | Queue<"a"> | Dequeue<"a" | 1> | Pool<1, 2> | Pool<"a", "b" | "c"> | ScopedRef<1> | ScopedRef<"a"> | Resource<"a", "b"> | RcRef<1, 0> | Deferred<1, 0> | Resource<1, 0> | Latch | ManagedRuntime<1, 0> | Fiber<"a" | 1, 0 | "b"> | RuntimeFiber<"a" | 1, 0 | "b"> | Either<"a" | 1, 0 | "b"> | Effect<"a" | 1, 0 | "b", "R" | "R1">
export type AllUnify = Unify.Unify<
| Either.Either<1, 0>
| Either.Either<"A", "E">
| Either.Either<"a", "b">
| Option.Option<number>
| Option.Option<string>
| Effect.Effect<"A", "E", "R">
| Effect.Effect<"a", "b", "R">
| Effect.Effect<1, 0, "R1">
| Ref.Ref<1>
| Ref.Ref<"A">
| Ref.Ref<"a">
| SynchronizedRef.SynchronizedRef<1>
| SynchronizedRef.SynchronizedRef<"A">
| SynchronizedRef.SynchronizedRef<"a">
| SubscriptionRef.SubscriptionRef<1>
| SubscriptionRef.SubscriptionRef<"A">
| SubscriptionRef.SubscriptionRef<"a">
| RcRef.RcRef<1, 0>
| RcRef.RcRef<"A", "E">
| Deferred.Deferred<1, 2>
| RcRef.RcRef<"a", "b">
| Deferred.Deferred<1, 0>
| Deferred.Deferred<"a", "b">
| FiberRef.FiberRef<12>
| FiberRef.FiberRef<"a2">
| Fiber.Fiber<1, 2>
| FiberRef.FiberRef<1>
| FiberRef.FiberRef<"a">
| Fiber.Fiber<1, 0>
| Fiber.Fiber<"a", "b">
| Fiber.RuntimeFiber<1, 2>
| Fiber.RuntimeFiber<1, 0>
| Fiber.RuntimeFiber<"a", "b">
| Queue.Queue<1>
| Queue.Queue<"a">
| Queue.Dequeue<1>
| Queue.Dequeue<"a">
| Pool.Pool<1, 2>
| Pool.Pool<"a", "b">
| Pool.Pool<"a", "c">
| ScopedRef.ScopedRef<1>
| ScopedRef.ScopedRef<"a">
| Resource.Resource<1, 0>
| Resource.Resource<"a", "b">
| Effect.Latch
| ManagedRuntime.ManagedRuntime<1, 0>
| ManagedRuntime.ManagedRuntime<"a", "b">
| 0
>
2 changes: 1 addition & 1 deletion packages/effect/src/Deferred.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export interface Deferred<in out A, in out E = never> extends Effect.Effect<A, E
* @since 3.8.0
*/
export interface DeferredUnify<A extends { [Unify.typeSymbol]?: any }> extends Effect.EffectUnify<A> {
Deferred?: () => Extract<A[Unify.typeSymbol], Deferred<any>>
Deferred?: () => Extract<A[Unify.typeSymbol], Deferred<any, any>>
}

/**
Expand Down
30 changes: 29 additions & 1 deletion packages/effect/src/Effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import * as _runtime from "./internal/runtime.js"
import * as _schedule from "./internal/schedule.js"
import type * as Layer from "./Layer.js"
import type { LogLevel } from "./LogLevel.js"
import type * as ManagedRuntime from "./ManagedRuntime.js"
import type * as Metric from "./Metric.js"
import type * as MetricLabel from "./MetricLabel.js"
import type * as Option from "./Option.js"
Expand Down Expand Up @@ -3330,12 +3331,19 @@ export const provide: {
): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E2 | E, RIn | Exclude<R, ROut>>
<R2>(context: Context.Context<R2>): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, Exclude<R, R2>>
<R2>(runtime: Runtime.Runtime<R2>): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, Exclude<R, R2>>
<E2, R2>(
managedRuntime: ManagedRuntime.ManagedRuntime<R2, E2>
): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E | E2, Exclude<R, R2>>
<A, E, R, ROut, E2, RIn>(
self: Effect<A, E, R>,
layer: Layer.Layer<ROut, E2, RIn>
): Effect<A, E | E2, RIn | Exclude<R, ROut>>
<A, E, R, R2>(self: Effect<A, E, R>, context: Context.Context<R2>): Effect<A, E, Exclude<R, R2>>
<A, E, R, R2>(self: Effect<A, E, R>, runtime: Runtime.Runtime<R2>): Effect<A, E, Exclude<R, R2>>
<A, E, E2, R, R2>(
self: Effect<A, E, R>,
runtime: ManagedRuntime.ManagedRuntime<R2, E2>
): Effect<A, E | E2, Exclude<R, R2>>
} = layer.effect_provide

/**
Expand Down Expand Up @@ -5408,7 +5416,7 @@ export const makeSemaphore: (permits: number) => Effect<Semaphore> = circular.ma
* @category latch
* @since 3.8.0
*/
export interface Latch {
export interface Latch extends Effect<void> {
/** open the latch, releasing all fibers waiting on it */
readonly open: Effect<void>
/** release all fibers waiting on the latch, without opening it */
Expand All @@ -5419,6 +5427,26 @@ export interface Latch {
readonly close: Effect<void>
/** only run the given effect when the latch is open */
readonly whenOpen: <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, R>

readonly [Unify.typeSymbol]?: unknown
readonly [Unify.unifySymbol]?: LatchUnify<this>
readonly [Unify.ignoreSymbol]?: LatchUnifyIgnore
}

/**
* @category models
* @since 3.8.0
*/
export interface LatchUnify<A extends { [Unify.typeSymbol]?: any }> extends EffectUnify<A> {
Latch?: () => Latch
}

/**
* @category models
* @since 3.8.0
*/
export interface LatchUnifyIgnore extends EffectUnifyIgnore {
Effect?: true
}

/**
Expand Down
16 changes: 16 additions & 0 deletions packages/effect/src/HashMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ export declare namespace HashMap {
* @category type-level
*/
export type Value<T extends HashMap<any, any>> = [T] extends [HashMap<infer _K, infer _V>] ? _V : never

/**
* This type-level utility extracts the entry type `[K, V]` from a `HashMap<K, V>` type.
*
* @example
* import { HashMap } from "effect"
*
* declare const hm: HashMap.HashMap<string, number>
*
* // $ExpectType [string, number]
* type V = HashMap.HashMap.Entry<typeof hm>
*
* @since 3.9.0
* @category type-level
*/
export type Entry<T extends HashMap<any, any>> = [Key<T>, Value<T>]
}

/**
Expand Down
47 changes: 45 additions & 2 deletions packages/effect/src/ManagedRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,30 @@ import type * as Exit from "./Exit.js"
import type * as Fiber from "./Fiber.js"
import * as internal from "./internal/managedRuntime.js"
import type * as Layer from "./Layer.js"
import type { Pipeable } from "./Pipeable.js"
import type * as Runtime from "./Runtime.js"
import type * as Unify from "./Unify.js"

/**
* @since 3.9.0
* @category symbol
*/
export const TypeId: unique symbol = internal.TypeId as TypeId

/**
* @since 3.9.0
* @category symbol
*/
export type TypeId = typeof TypeId

/**
* Checks if the provided argument is a `ManagedRuntime`.
*
* @param input - The value to be checked if it is a `ManagedRuntime`.
* @since 3.9.0
* @category guards
*/
export const isManagedRuntime: (input: unknown) => input is ManagedRuntime<unknown, unknown> = internal.isManagedRuntime

/**
* @since 3.4.0
Expand All @@ -29,7 +51,8 @@ export declare namespace ManagedRuntime {
* @since 2.0.0
* @category models
*/
export interface ManagedRuntime<in R, out ER> extends Pipeable {
export interface ManagedRuntime<in R, out ER> extends Effect.Effect<Runtime.Runtime<R>, ER> {
readonly [TypeId]: TypeId
readonly memoMap: Layer.MemoMap
readonly runtimeEffect: Effect.Effect<Runtime.Runtime<R>, ER>
readonly runtime: () => Promise<Runtime.Runtime<R>>
Expand Down Expand Up @@ -103,6 +126,26 @@ export interface ManagedRuntime<in R, out ER> extends Pipeable {
* Dispose of the resources associated with the runtime.
*/
readonly disposeEffect: Effect.Effect<void, never, never>

readonly [Unify.typeSymbol]?: unknown
readonly [Unify.unifySymbol]?: ManagedRuntimeUnify<this>
readonly [Unify.ignoreSymbol]?: ManagedRuntimeUnifyIgnore
}

/**
* @category models
* @since 3.9.0
*/
export interface ManagedRuntimeUnify<A extends { [Unify.typeSymbol]?: any }> extends Effect.EffectUnify<A> {
ManagedRuntime?: () => Extract<A[Unify.typeSymbol], ManagedRuntime<any, any>>
}

/**
* @category models
* @since 3.9.0
*/
export interface ManagedRuntimeUnifyIgnore extends Effect.EffectUnifyIgnore {
Effect?: true
}

/**
Expand Down
Loading