MCPcopy Create free account
hub / github.com/Effect-TS/effect / makeWith

Function makeWith

packages/effect/src/ScopedCache.ts:132–172  ·  view source on GitHub ↗
(options: {
  readonly lookup: (key: Key) => Effect.Effect<A, E, R | Scope.Scope>
  readonly capacity: number
  readonly timeToLive?: ((exit: Exit.Exit<A, E>, key: Key) => Duration.Input) | undefined
  readonly requireServicesAt?: ServiceMode | undefined
})

Source from the content-addressed store, hash-verified

130 * @since 2.0.0
131 */
132export const makeWith = <
133 Key,
134 A,
135 E = never,
136 R = never,
137 ServiceMode extends "lookup" | "construction" = never
138>(options: {
139 readonly lookup: (key: Key) => Effect.Effect<A, E, R | Scope.Scope>
140 readonly capacity: number
141 readonly timeToLive?: ((exit: Exit.Exit<A, E>, key: Key) => Duration.Input) | undefined
142 readonly requireServicesAt?: ServiceMode | undefined
143}): Effect.Effect<
144 ScopedCache<Key, A, E, "lookup" extends ServiceMode ? Exclude<R, Scope.Scope> : never>,
145 never,
146 ("lookup" extends ServiceMode ? never : R) | Scope.Scope
147> =>
148 effect.contextWith((context: Context.Context<any>) => {
149 const scope = Context.get(context, Scope.Scope)
150 const self = Object.create(Proto)
151 self.lookup = (key: Key): Effect.Effect<A, E> =>
152 effect.updateContext(
153 options.lookup(key),
154 (input) => Context.merge(context, input)
155 )
156 const map = MutableHashMap.empty<Key, Entry<A, E>>()
157 self.state = { _tag: "Open", map }
158 self.capacity = options.capacity
159 self.timeToLive = options.timeToLive
160 ? (exit: Exit.Exit<A, E>, key: Key) => Duration.fromInputUnsafe(options.timeToLive!(exit, key))
161 : defaultTimeToLive
162 return effect.as(
163 Scope.addFinalizer(
164 scope,
165 core.withFiber((fiber) => {
166 self.state = { _tag: "Closed" }
167 return invalidateAllImpl(fiber, map)
168 })
169 ),
170 self
171 )
172 })
173
174/**
175 * Creates a `ScopedCache` with a fixed time-to-live for every lookup result.

Callers 1

makeFunction · 0.70

Calls 4

invalidateAllImplFunction · 0.85
mergeMethod · 0.80
addFinalizerMethod · 0.80
getMethod · 0.65

Tested by

no test coverage detected