Skip to content

Commit

Permalink
Simplify isEnabled and add isEnabledWithContext
Browse files Browse the repository at this point in the history
  • Loading branch information
evenbrenden committed Jun 14, 2024
1 parent bf56170 commit 6d67d38
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
3 changes: 1 addition & 2 deletions example/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import qualified Data.Text as T
import Data.Void (Void)
import Servant.Client (BaseUrl (BaseUrl), Scheme (Http))
import System.Exit (die)
import Unleash (emptyContext)
import Unleash.Client (
HasUnleash (..),
UnleashConfig (..),
Expand Down Expand Up @@ -60,7 +59,7 @@ program = do
application :: Program Void
application =
forever do
enabled <- isEnabled featureToggle emptyContext
enabled <- isEnabled featureToggle
liftIO . putStrLn $ T.unpack featureToggle <> " is " <> (if enabled then "enabled" else "disabled")
liftIO . threadDelay $ 2 * 1000 * 1000

Expand Down
24 changes: 22 additions & 2 deletions src/Unleash/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ module Unleash.Client (
pollTogglesWithCustomStrategies,
pushMetrics,
isEnabled,
isEnabledWithContext,
tryIsEnabled,
tryIsEnabledWithContext,
getVariant,
tryGetVariant,
-- Re-exports
Expand Down Expand Up @@ -179,14 +181,23 @@ pushMetrics = do

-- | Check if a feature is enabled or not. Blocks until first feature toggle set is received. Blocks if the mutable metrics variables are empty.
isEnabled ::
(HasUnleash r, MonadReader r m, MonadIO m) =>
-- | Feature toggle name.
Text ->
-- | Client context.
m Bool
isEnabled feature = isEnabledWithContext feature emptyContext

-- | Check if a feature is enabled or not. Blocks until first feature toggle set is received. Blocks if the mutable metrics variables are empty.
isEnabledWithContext ::
(HasUnleash r, MonadReader r m, MonadIO m) =>
-- | Feature toggle name.
Text ->
-- | Client context.
Context ->
-- | Whether or not the feature toggle is enabled.
m Bool
isEnabled feature context = do
isEnabledWithContext feature context = do
config <- asks getUnleashConfig
state <- liftIO . readMVar $ config.state
enabled <- featureIsEnabled state feature context
Expand All @@ -195,14 +206,23 @@ isEnabled feature context = do

-- | Check if a feature is enabled or not. Returns false for all toggles until first toggle set is received. Blocks if the mutable metrics variables are empty.
tryIsEnabled ::
(HasUnleash r, MonadReader r m, MonadIO m) =>
-- | Feature toggle name.
Text ->
-- | Client context.
m Bool
tryIsEnabled feature = tryIsEnabledWithContext feature emptyContext

-- | Check if a feature is enabled or not. Returns false for all toggles until first toggle set is received. Blocks if the mutable metrics variables are empty.
tryIsEnabledWithContext ::
(HasUnleash r, MonadReader r m, MonadIO m) =>
-- | Feature toggle name.
Text ->
-- | Client context.
Context ->
-- | Whether or not the feature toggle is enabled.
m Bool
tryIsEnabled feature context = do
tryIsEnabledWithContext feature context = do
config <- asks getUnleashConfig
maybeState <- liftIO . tryReadMVar $ config.state
case maybeState of
Expand Down
1 change: 0 additions & 1 deletion unleash-client-haskell.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,3 @@ executable example
servant-client,
text,
unleash-client-haskell,
unleash-client-haskell-core,

0 comments on commit 6d67d38

Please sign in to comment.