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
5ea0f706
Commit
5ea0f706
authored
3 years ago
by
Jens Nolte
Browse files
Options
Downloads
Patches
Plain Diff
Implement MonadCatch instance for Observable
parent
f53fd838
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#2729
passed
3 years ago
Changes
1
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Quasar/Observable.hs
+41
-51
41 additions, 51 deletions
src/Quasar/Observable.hs
with
41 additions
and
51 deletions
src/Quasar/Observable.hs
+
41
−
51
View file @
5ea0f706
...
@@ -19,8 +19,8 @@ module Quasar.Observable (
...
@@ -19,8 +19,8 @@ module Quasar.Observable (
--observeWhile_,
--observeWhile_,
--observeBlocking,
--observeBlocking,
--
--
* Helper types
-- * Helper types
--
ObservableCallback,
ObservableCallback
,
)
where
)
where
import
Control.Applicative
import
Control.Applicative
...
@@ -130,17 +130,17 @@ instance MonadThrow Observable where
...
@@ -130,17 +130,17 @@ instance MonadThrow Observable where
throwM
::
forall
e
v
.
Exception
e
=>
e
->
Observable
v
throwM
::
forall
e
v
.
Exception
e
=>
e
->
Observable
v
throwM
=
toObservable
.
ThrowObservable
@
v
.
toException
throwM
=
toObservable
.
ThrowObservable
@
v
.
toException
--
instance MonadCatch Observable where
instance
MonadCatch
Observable
where
--
catch action handler = toObservable $ CatchObservable action handler
catch
action
handler
=
toObservable
$
CatchObservable
action
handler
--
instance
MonadFail
Observable
where
instance
MonadFail
Observable
where
fail
=
throwM
.
userError
fail
=
throwM
.
userError
--
instance Alternative Observable where
instance
Alternative
Observable
where
--
empty = fail "empty"
empty
=
fail
"empty"
--
x <|> y = x `catchAll` const y
x
<|>
y
=
x
`
catchAll
`
const
y
--
--
instance MonadPlus Observable
instance
MonadPlus
Observable
...
@@ -306,47 +306,37 @@ instance IsObservable a (BindObservable a) where
...
@@ -306,47 +306,37 @@ instance IsObservable a (BindObservable a) where
mapObservable
f
(
BindObservable
fx
fn
)
=
toObservable
$
BindObservable
fx
(
f
<<$>>
fn
)
mapObservable
f
(
BindObservable
fx
fn
)
=
toObservable
$
BindObservable
fx
(
f
<<$>>
fn
)
--data CatchObservable e r = Exception e => CatchObservable (Observable r) (e -> Observable r)
data
CatchObservable
e
a
=
Exception
e
=>
CatchObservable
(
Observable
a
)
(
e
->
Observable
a
)
--
--instance IsRetrievable r (CatchObservable e r) where
instance
IsRetrievable
a
(
CatchObservable
e
a
)
where
-- retrieve (CatchObservable fx fn) = retrieve fx `catch` \ex -> retrieve (fn ex)
retrieve
(
CatchObservable
fx
fn
)
=
retrieve
fx
`
catch
`
\
ex
->
retrieve
(
fn
ex
)
--
--instance IsObservable r (CatchObservable e r) where
instance
IsObservable
a
(
CatchObservable
e
a
)
where
-- observe (CatchObservable fx fn) callback = do
observe
(
CatchObservable
fx
fn
)
callback
=
ensureQuasarSTM
do
-- disposableVar <- liftIO $ newTMVarIO noDisposable
callback
ObservableLoading
-- keyVar <- liftIO $ newTMVarIO =<< newUnique
keyVar
<-
newTVar
=<<
newUniqueSTM
--
disposableVar
<-
liftSTM
$
newTVar
trivialDisposer
-- observe fx (leftCallback disposableVar keyVar)
observe
fx
(
leftCallback
keyVar
disposableVar
)
-- where
where
-- leftCallback disposableVar keyVar message = do
leftCallback
keyVar
disposableVar
lmsg
=
do
-- key <- liftIO newUnique
disposeEventually_
=<<
readTVar
disposableVar
--
key
<-
newUniqueSTM
-- oldDisposable <- liftIO $ atomically do
-- Dispose is not instant, so a key is used to disarm the callback derived from the last (now outdated) value
-- -- Blocks while `rightCallback` is running
writeTVar
keyVar
key
-- void $ swapTMVar keyVar key
disposer
<-
captureResources_
--
case
lmsg
of
-- takeTMVar disposableVar
ObservableNotAvailable
(
fromException
->
Just
ex
)
->
observe
(
fn
ex
)
(
rightCallback
key
)
--
_
->
callback
lmsg
-- disposeEventually_ oldDisposable
writeTVar
disposableVar
disposer
--
where
-- disposable <- case message of
rightCallback
::
Unique
->
ObservableCallback
a
-- (ObservableNotAvailable (fromException -> Just ex)) ->
rightCallback
callbackKey
rmsg
=
do
-- captureDisposable_ $ observe (fn ex) (rightCallback keyVar key)
activeKey
<-
readTVar
keyVar
-- msg -> noDisposable <$ callback msg
when
(
callbackKey
==
activeKey
)
(
callback
rmsg
)
--
-- liftIO $ atomically $ putTMVar disposableVar disposable
pingObservable
(
CatchObservable
fx
fn
)
=
do
--
pingObservable
fx
`
catch
`
\
ex
->
pingObservable
(
fn
ex
)
-- rightCallback :: TMVar Unique -> Unique -> ObservableState r -> ResourceManagerIO ()
-- rightCallback keyVar key message =
-- bracket
-- -- Take key var to prevent parallel callbacks
-- (liftIO $ atomically $ takeTMVar keyVar)
-- -- Put key back
-- (liftIO . atomically . putTMVar keyVar)
-- -- Ignore all callbacks that arrive from the old `fn` when a new `fx` has been observed
-- (\currentKey -> when (key == currentKey) $ callback message)
--
--
newtype
ObserverRegistry
a
=
ObserverRegistry
(
TVar
(
HM
.
HashMap
Unique
(
ObservableCallback
a
)))
newtype
ObserverRegistry
a
=
ObserverRegistry
(
TVar
(
HM
.
HashMap
Unique
(
ObservableCallback
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