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
e6369769
Commit
e6369769
authored
4 years ago
by
Jens Nolte
Browse files
Options
Downloads
Patches
Plain Diff
Implement joinObservable
parent
32501a55
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/lib/Qd/Observable.hs
+34
-0
34 additions, 0 deletions
src/lib/Qd/Observable.hs
with
34 additions
and
0 deletions
src/lib/Qd/Observable.hs
+
34
−
0
View file @
e6369769
{-# LANGUAGE UndecidableInstances #-}
module
Qd.Observable
(
SomeObservable
(
..
),
Observable
(
..
),
...
...
@@ -11,6 +13,7 @@ module Qd.Observable (
mkBasicObservable
,
setBasicObservable
,
updateBasicObservable
,
joinObservable
,
)
where
import
Control.Concurrent.MVar
...
...
@@ -95,3 +98,34 @@ updateBasicObservable (BasicObservable mvar) f =
let
newState
=
(
\
v
->
f
v
)
<$>
oldState
mapM_
(
\
callback
->
callback
(
Update
,
newState
))
subscribers
return
(
newState
,
subscribers
)
newtype
JoinedObservable
o
=
JoinedObservable
o
instance
forall
o
i
v
.
(
Observable
i
o
,
Observable
v
i
)
=>
Observable
v
(
JoinedObservable
o
)
where
getValue
::
JoinedObservable
o
->
IO
(
ObservableState
v
)
getValue
(
JoinedObservable
outer
)
=
do
state
<-
getValue
outer
case
state
of
Just
inner
->
getValue
inner
Nothing
->
return
Nothing
subscribe
::
(
JoinedObservable
o
)
->
(
ObservableMessage
v
->
IO
()
)
->
IO
SubscriptionHandle
subscribe
(
JoinedObservable
outer
)
handler
=
do
innerSubscriptionMVar
<-
newMVar
dummySubscription
outerSubscription
<-
subscribe
outer
(
outerHandler
innerSubscriptionMVar
)
return
$
SubscriptionHandle
{
unsubscribe
=
unsubscribe'
outerSubscription
}
where
dummySubscription
=
SubscriptionHandle
{
unsubscribe
=
return
()
}
outerHandler
innerSubscriptionMVar
=
outerHandler'
where
outerHandler'
(
_
,
Just
inner
)
=
do
unsubscribe
=<<
takeMVar
innerSubscriptionMVar
innerSubscription
<-
subscribe
inner
handler
putMVar
innerSubscriptionMVar
innerSubscription
outerHandler'
(
reason
,
Nothing
)
=
do
unsubscribe
=<<
takeMVar
innerSubscriptionMVar
handler
(
reason
,
Nothing
)
putMVar
innerSubscriptionMVar
dummySubscription
unsubscribe'
outerSubscription
=
do
unsubscribe
outerSubscription
joinObservable
::
(
Observable
i
o
,
Observable
v
i
)
=>
o
->
SomeObservable
v
joinObservable
outer
=
SomeObservable
$
JoinedObservable
outer
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