Hyperlinkv0.8.0-beta.28

RcMap

RcMap.touchconsteffect/RcMap.ts:594
<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>

Extends the idle time for a resource in the RcMap. If the RcMap has an idleTimeToLive configured, calling touch will reset the expiration timer for the specified key.

When to use

Use to keep an idle resource alive longer without acquiring a new reference.

Example (Extending resource idle time)

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}`)
      ),
    idleTimeToLive: "10 seconds"
  })

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

  // Touch the resource to extend its idle time
  // This resets the 10-second expiration timer
  yield* RcMap.touch(map, "session")

  // The resource will now live for another 10 seconds
  // from the time it was touched
}).pipe(Effect.scoped)
combinatorsinvalidate
Source effect/RcMap.ts:59419 lines
export const touch: {
  <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,
  <K, A, E>(self: RcMap<K, A, E>, key: K) =>
    Effect.clockWith((clock) => {
      if (self.state._tag === "Closed") {
        return Effect.void
      }
      const o = MutableHashMap.get(self.state.map, key)
      if (o._tag === "None" || Duration.isZero(o.value.idleTimeToLive)) {
        return Effect.void
      }
      const entry = o.value
      entry.expiresAt = clock.currentTimeMillisUnsafe() + Duration.toMillis(entry.idleTimeToLive)
      return Effect.void
    })
)
Referenced by 2 symbols