Hyperlinkv0.8.0-beta.28

ScopedCache

ScopedCache.invalidateWhenconsteffect/ScopedCache.ts:574
<Key, A>(key: Key, f: Predicate.Predicate<A>): <E, R>(
  self: ScopedCache<Key, A, E, R>
) => Effect.Effect<boolean>
<Key, A, E, R>(
  self: ScopedCache<Key, A, E, R>,
  key: Key,
  f: Predicate.Predicate<A>
): Effect.Effect<boolean>

Invalidates the entry associated with the specified key in the cache when the predicate returns true for the cached value.

When to use

Use to remove an already-cached scoped value only when the successful cached value satisfies a predicate.

Details

Returns true only when a successful cached value matches and is removed. It returns false for absent, expired, failed, or non-matching entries.

Gotchas

A matching invalidation closes the entry scope and releases its resources.

combinatorsinvalidate
export const invalidateWhen: {
  <Key, A>(key: Key, f: Predicate.Predicate<A>): <E, R>(self: ScopedCache<Key, A, E, R>) => Effect.Effect<boolean>
  <Key, A, E, R>(self: ScopedCache<Key, A, E, R>, key: Key, f: Predicate.Predicate<A>): Effect.Effect<boolean>
} = dual(
  3,
  <Key, A, E, R>(self: ScopedCache<Key, A, E, R>, key: Key, f: Predicate.Predicate<A>): Effect.Effect<boolean> =>
    effect.uninterruptibleMask((restore) =>
      core.withFiber((fiber) =>
        effect.flatMap(getImpl(self, key, fiber, false), (entry) => {
          if (entry === undefined) {
            return effect.succeed(false)
          }
          return restore(Deferred.await(entry.deferred)).pipe(
            effect.flatMap((value) => {
              if (self.state._tag === "Closed") {
                return effect.succeed(false)
              } else if (f(value)) {
                MutableHashMap.remove(self.state.map, key)
                return effect.as(Scope.close(entry.scope, effect.exitVoid), true)
              }
              return effect.succeed(false)
            }),
            effect.catch_(() => effect.succeed(false))
          )
        })
      )
    )
)