(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
})
| 130 | * @since 2.0.0 |
| 131 | */ |
| 132 | export 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. |
no test coverage detected