Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Q
quasar
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jens Nolte
quasar
Commits
9f667c79
Commit
9f667c79
authored
3 years ago
by
Jens Nolte
Browse files
Options
Downloads
Patches
Plain Diff
Add blocking `observe` helper functions
parent
5ea0f706
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#2731
passed
3 years ago
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Quasar/Observable.hs
+65
-64
65 additions, 64 deletions
src/Quasar/Observable.hs
with
65 additions
and
64 deletions
src/Quasar/Observable.hs
+
65
−
64
View file @
9f667c79
...
...
@@ -15,9 +15,9 @@ module Quasar.Observable (
stateObservableVar
,
---- * Helper functions
--
observe
While
,
--
observe
While_
,
--
observe
Blocking
,
observe
Blocking
,
observe
Until
,
observe
Until_
,
-- * Helper types
ObservableCallback
,
...
...
@@ -28,6 +28,7 @@ import Control.Monad.Catch
import
Control.Monad.Except
import
Control.Monad.Trans.Maybe
import
Data.HashMap.Strict
qualified
as
HM
import
Data.IORef
import
Data.Unique
import
Quasar.Async
import
Quasar.Future
...
...
@@ -144,69 +145,69 @@ instance MonadPlus Observable
---- | Observe an observable by handling updates on the current thread.
----
---- `observeBlocking` will run the handler whenever the observable changes (forever / until an exception is encountered).
----
---- The handler is allowed to block. When the value changes while the handler is running the handler will be run again
---- after it completes; when the value changes multiple times it will only be executed once (with the latest value).
--observeBlocking
-- :: (IsObservable v o, MonadResourceManager m, MonadIO m, MonadMask m)
-- => o
-- -> (ObservableState v -> m ())
-- -> m a
--observeBlocking observable handler = do
-- -- `withScopedResourceManager` removes the `observe` callback when the `handler` fails.
-- withScopedResourceManager do
-- var <- liftIO newEmptyTMVarIO
-- observe observable \msg -> liftIO $ atomically do
-- void $ tryTakeTMVar var
-- putTMVar var msg
--
-- forever do
-- msg <- liftIO $ atomically $ takeTMVar var
-- handler msg
--
--
---- | Internal control flow exception for `observeWhile` and `observeWhile_`.
--data ObserveWhileCompleted = ObserveWhileCompleted
-- deriving stock (Eq, Show)
--
--instance Exception ObserveWhileCompleted
--
---- | Observe until the callback returns `Just`.
--observeWhile
-- :: (IsObservable v o, MonadResourceManager m, MonadIO m, MonadMask m)
-- => o
-- -> (ObservableState v -> m (Maybe a))
-- -> m a
--observeWhile observable callback = do
-- resultVar <- liftIO $ newIORef unreachableCodePath
-- observeWhile_ observable \msg -> do
-- callback msg >>= \case
-- Just result -> do
-- liftIO $ writeIORef resultVar result
-- pure False
-- Nothing -> pure True
--
-- liftIO $ readIORef resultVar
--
--
---- | Observe until the callback returns `False`.
--observeWhile_
-- :: (IsObservable v o, MonadResourceManager m, MonadIO m, MonadMask m)
-- => o
-- -> (ObservableState v -> m Bool)
-- -> m ()
--observeWhile_ observable callback =
-- catch
-- do
-- observeBlocking observable \msg -> do
-- continue <- callback msg
-- unless continue $ throwM ObserveWhileCompleted
-- \ObserveWhileCompleted -> pure ()
-- | Observe an observable by handling updates on the current thread.
--
-- `observeBlocking` will run the handler whenever the observable changes (forever / until an exception is encountered).
--
-- The handler is allowed to block. When the value changes while the handler is running the handler will be run again
-- after it completes; when the value changes multiple times it will only be executed once (with the latest value).
observeBlocking
::
(
IsObservable
r
a
,
MonadQuasar
m
,
MonadIO
m
,
MonadMask
m
)
=>
a
->
(
ObservableState
r
->
m
()
)
->
m
b
observeBlocking
observable
handler
=
do
-- `withResourceScope` removes the `observe` callback when the `handler` fails.
-- TODO this also releases all resources when the handler fails - is that correct? if so it should be documented
withResourceScope
do
var
<-
liftIO
newEmptyTMVarIO
observe
observable
\
msg
->
liftSTM
do
void
$
tryTakeTMVar
var
putTMVar
var
msg
forever
do
msg
<-
liftIO
$
atomically
$
takeTMVar
var
handler
msg
-- | Internal control flow exception for `observeWhile` and `observeWhile_`.
data
ObserveWhileCompleted
=
ObserveWhileCompleted
deriving
stock
(
Eq
,
Show
)
instance
Exception
ObserveWhileCompleted
-- | Observe until the callback returns `Just`.
observeUntil
::
(
IsObservable
r
a
,
MonadQuasar
m
,
MonadIO
m
,
MonadMask
m
)
=>
a
->
(
ObservableState
r
->
m
(
Maybe
b
))
->
m
b
observeUntil
observable
callback
=
do
resultVar
<-
liftIO
$
newIORef
unreachableCodePath
observeUntil_
observable
\
msg
->
do
callback
msg
>>=
\
case
Just
result
->
do
liftIO
$
writeIORef
resultVar
result
pure
False
Nothing
->
pure
True
liftIO
$
readIORef
resultVar
-- | Observe until the callback returns `False`.
observeUntil_
::
(
IsObservable
r
a
,
MonadQuasar
m
,
MonadIO
m
,
MonadMask
m
)
=>
a
->
(
ObservableState
r
->
m
Bool
)
->
m
()
observeUntil_
observable
callback
=
catch
do
observeBlocking
observable
\
msg
->
do
continue
<-
callback
msg
unless
continue
$
throwM
ObserveWhileCompleted
\
ObserveWhileCompleted
->
pure
()
newtype
ConstObservable
a
=
ConstObservable
a
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment