Hyperlinkv0.8.0-beta.28

RcMap

RcMap.invalidateconsteffect/RcMap.ts:500
<K>(key: K): <A, E>(self: RcMap<K, A, E>) => Effect.Effect<void>
<K, A, E>(self: RcMap<K, A, E>, key: K): Effect.Effect<void>

Invalidates and removes a specific key from the RcMap. If the resource is not currently in use (reference count is 0), it will be immediately released.

When to use

Use to remove a resource by key so the next access performs a fresh lookup.

Example (Invalidating a resource)

import { Effect, RcMap } from "effect"

Effect.gen(function*() {
  const map = yield* RcMap.make({
    lookup: (key: string) =>
      Effect.acquireRelease(
        Effect.succeed(`Resource: ${key}`),
        () => Effect.log(`Released ${key}`)
      )
  })

  // Get a resource
  yield* RcMap.get(map, "cache")

  // Invalidate the resource - it will be removed from the map
  // and released if no longer in use
  yield* RcMap.invalidate(map, "cache")

  // Next access will create a new resource
  yield* RcMap.get(map, "cache")
}).pipe(Effect.scoped)
combinatorsgettouch
Source effect/RcMap.ts:50016 lines
export const invalidate: {
  <K>(key: K): <A, E>(self: RcMap<K, A, E>) => Effect.Effect<void>
  <K, A, E>(self: RcMap<K, A, E>, key: K): Effect.Effect<void>
} = dual(
  2,
  Effect.fnUntraced(function*<K, A, E>(self: RcMap<K, A, E>, key: K) {
    if (self.state._tag === "Closed") return
    const o = MutableHashMap.get(self.state.map, key)
    if (o._tag === "None") return
    const entry = o.value
    MutableHashMap.remove(self.state.map, key)
    if (entry.refCount > 0) return
    if (entry.fiber) yield* Fiber.interrupt(entry.fiber)
    yield* Scope.close(entry.scope, Exit.void)
  }, Effect.uninterruptible)
)
Referenced by 1 symbols