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

Function makeWith

packages/effect/src/Cache.ts:190–218  ·  view source on GitHub ↗
(lookup: (key: Key) => Effect.Effect<A, E, R>, options: {
  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

188 * @since 2.0.0
189 */
190export const makeWith = <
191 Key,
192 A,
193 E = never,
194 R = never,
195 ServiceMode extends "lookup" | "construction" = never
196>(lookup: (key: Key) => Effect.Effect<A, E, R>, options: {
197 readonly capacity: number
198 readonly timeToLive?: ((exit: Exit.Exit<A, E>, key: Key) => Duration.Input) | undefined
199 readonly requireServicesAt?: ServiceMode | undefined
200}): Effect.Effect<
201 Cache<Key, A, E, "lookup" extends ServiceMode ? R : never>,
202 never,
203 "lookup" extends ServiceMode ? never : R
204> =>
205 effect.contextWith((context: Context.Context<any>) => {
206 const self = Object.create(Proto)
207 self.lookup = (key: Key): Effect.Effect<A, E> =>
208 effect.updateContext(
209 lookup(key),
210 (input) => Context.merge(context, input)
211 )
212 self.map = MutableHashMap.make()
213 self.capacity = options.capacity
214 self.timeToLive = options.timeToLive
215 ? (exit: Exit.Exit<A, E>, key: Key) => Duration.fromInputUnsafe(options.timeToLive!(exit, key))
216 : defaultTimeToLive
217 return effect.succeed(self as Cache<Key, A, E>)
218 })
219
220/**
221 * Creates a cache with a fixed time-to-live for all entries.

Callers 2

makeFunction · 0.70
PgClient.tsFile · 0.50

Calls 4

mergeMethod · 0.80
lookupFunction · 0.70
makeMethod · 0.65
succeedMethod · 0.45

Tested by

no test coverage detected