Skip to content

Commit

Permalink
expose Layer MemoMap apis (#1735)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored Dec 5, 2023
1 parent 3c77e12 commit cf4c044
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/dirty-ducks-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

expose Layer MemoMap apis
5 changes: 5 additions & 0 deletions .changeset/tricky-timers-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

fix memoization of Layer.effect/scoped
81 changes: 81 additions & 0 deletions docs/modules/Layer.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,12 @@ Added in v2.0.0
- [discard](#discard)
- [map](#map)
- [mapError](#maperror)
- [memo map](#memo-map)
- [buildWithMemoMap](#buildwithmemomap)
- [makeMemoMap](#makememomap)
- [models](#models)
- [Layer (interface)](#layer-interface)
- [MemoMap (interface)](#memomap-interface)
- [requests & batching](#requests--batching)
- [setRequestBatching](#setrequestbatching)
- [setRequestCache](#setrequestcache)
Expand All @@ -96,6 +100,8 @@ Added in v2.0.0
- [symbols](#symbols)
- [LayerTypeId](#layertypeid)
- [LayerTypeId (type alias)](#layertypeid-type-alias)
- [MemoMapTypeId](#memomaptypeid)
- [MemoMapTypeId (type alias)](#memomaptypeid-type-alias)
- [tracing](#tracing)
- [parentSpan](#parentspan)
- [setTracer](#settracer)
Expand Down Expand Up @@ -729,6 +735,43 @@ export declare const mapError: {
Added in v2.0.0
# memo map
## buildWithMemoMap
Builds a layer into an `Effect` value, using the specified `MemoMap` to memoize
the layer construction.
**Signature**
```ts
export declare const buildWithMemoMap: {
(
memoMap: MemoMap,
scope: Scope.Scope
): <RIn, E, ROut>(self: Layer<RIn, E, ROut>) => Effect.Effect<RIn, E, Context.Context<ROut>>
<RIn, E, ROut>(
self: Layer<RIn, E, ROut>,
memoMap: MemoMap,
scope: Scope.Scope
): Effect.Effect<RIn, E, Context.Context<ROut>>
}
```
Added in v2.0.0
## makeMemoMap
Constructs a `MemoMap` that can be used to build additional layers.
**Signature**
```ts
export declare const makeMemoMap: Effect.Effect<never, never, MemoMap>
```
Added in v2.0.0
# models
## Layer (interface)
Expand All @@ -741,6 +784,24 @@ export interface Layer<out RIn, out E, in ROut> extends Layer.Variance<RIn, E, R

Added in v2.0.0

## MemoMap (interface)

**Signature**

```ts
export interface MemoMap {
readonly [MemoMapTypeId]: MemoMapTypeId

/** @internal */
readonly getOrElseMemoize: <RIn, E, ROut>(
layer: Layer<RIn, E, ROut>,
scope: Scope.Scope
) => Effect.Effect<RIn, E, Context.Context<ROut>>
}
```

Added in v2.0.0

# requests & batching

## setRequestBatching
Expand Down Expand Up @@ -929,6 +990,26 @@ export type LayerTypeId = typeof LayerTypeId
Added in v2.0.0
## MemoMapTypeId
**Signature**
```ts
export declare const MemoMapTypeId: typeof MemoMapTypeId
```
Added in v2.0.0
## MemoMapTypeId (type alias)
**Signature**
```ts
export type MemoMapTypeId = typeof MemoMapTypeId
```
Added in v2.0.0
# tracing
## parentSpan
Expand Down
57 changes: 57 additions & 0 deletions src/Layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,32 @@ export declare namespace Layer {
export type Success<T extends Layer<any, any, never>> = [T] extends [Layer<infer _R, infer _E, infer _A>] ? _A : never
}

/**
* @since 2.0.0
* @category symbols
*/
export const MemoMapTypeId: unique symbol = internal.MemoMapTypeId

/**
* @since 2.0.0
* @category symbols
*/
export type MemoMapTypeId = typeof MemoMapTypeId

/**
* @since 2.0.0
* @category models
*/
export interface MemoMap {
readonly [MemoMapTypeId]: MemoMapTypeId

/** @internal */
readonly getOrElseMemoize: <RIn, E, ROut>(
layer: Layer<RIn, E, ROut>,
scope: Scope.Scope
) => Effect.Effect<RIn, E, Context.Context<ROut>>
}

/**
* Returns `true` if the specified value is a `Layer`, `false` otherwise.
*
Expand Down Expand Up @@ -984,3 +1010,34 @@ export const withParentSpan: {
(span: Tracer.ParentSpan): <R, E, A>(self: Layer<R, E, A>) => Layer<Exclude<R, Tracer.ParentSpan>, E, A>
<R, E, A>(self: Layer<R, E, A>, span: Tracer.ParentSpan): Layer<Exclude<R, Tracer.ParentSpan>, E, A>
} = internal.withParentSpan

// -----------------------------------------------------------------------------
// memo map
// -----------------------------------------------------------------------------

/**
* Constructs a `MemoMap` that can be used to build additional layers.
*
* @since 2.0.0
* @category memo map
*/
export const makeMemoMap: Effect.Effect<never, never, MemoMap> = internal.makeMemoMap

/**
* Builds a layer into an `Effect` value, using the specified `MemoMap` to memoize
* the layer construction.
*
* @since 2.0.0
* @category memo map
*/
export const buildWithMemoMap: {
(
memoMap: MemoMap,
scope: Scope.Scope
): <RIn, E, ROut>(self: Layer<RIn, E, ROut>) => Effect.Effect<RIn, E, Context.Context<ROut>>
<RIn, E, ROut>(
self: Layer<RIn, E, ROut>,
memoMap: MemoMap,
scope: Scope.Scope
): Effect.Effect<RIn, E, Context.Context<ROut>>
} = internal.buildWithMemoMap
Loading

0 comments on commit cf4c044

Please sign in to comment.