All Downloads are FREE. Search and download functionalities are using the official Maven repository.

crystal.react.hooks.EffectWithCleanup.scala Maven / Gradle / Ivy

There is a newer version: 0.45.2
Show newest version
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA)
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause

package crystal.react.hooks

import cats.Applicative
import japgolly.scalajs.react.util.DefaultEffects.Async as DefaultA

// Typeclass for normalizing effects into an effect that returns a cleanup effect.
opaque type EffectWithCleanup[G, F[_]] = G => F[F[Unit]]

object EffectWithCleanup:
  // Effect already returns a cleanup effect, return as is.
  given effectWithNoCleanup: EffectWithCleanup[DefaultA[DefaultA[Unit]], DefaultA] =
    identity

  // Effect doesn't return a cleanup effect, add a no-op cleanup.
  given effectWithCleanup: EffectWithCleanup[DefaultA[Unit], DefaultA] =
    _.as(Applicative[DefaultA].unit)

extension [G, F[_]](effect: G)(using effectWithCleanup: EffectWithCleanup[G, F])
  def normalize: F[F[Unit]] = effectWithCleanup(effect)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy