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
34be7a96
Commit
34be7a96
authored
4 years ago
by
Jens Nolte
Browse files
Options
Downloads
Patches
Plain Diff
Improve code quality
parent
35524730
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/ObservableMap.hs
+13
-17
13 additions, 17 deletions
src/lib/Qd/Observable/ObservableMap.hs
with
13 additions
and
17 deletions
src/lib/Qd/Observable/ObservableMap.hs
+
13
−
17
View file @
34be7a96
...
...
@@ -12,7 +12,7 @@ module Qd.Observable.ObservableMap (
import
Qd.Observable
import
Control.Concurrent.MVar
import
Control.Monad.State.Lazy
import
Data.Bifunctor
import
Data.Hashable
(
Hashable
)
import
qualified
Data.HashMap.Strict
as
HM
import
Data.Unique
...
...
@@ -25,15 +25,19 @@ data ObservableValue v = ObservableValue {
subscribers
::
(
HM
.
HashMap
Unique
(
ObservableMessage
v
->
IO
()
))
}
newtype
GetT
s
m
r
=
GetT
{
runGetT
::
m
(
r
,
s
)
}
instance
Functor
m
=>
Functor
(
GetT
s
m
)
where
fmap
::
(
a
->
b
)
->
(
GetT
s
m
)
a
->
(
GetT
s
m
)
b
fmap
fn
=
GetT
.
fmap
(
first
fn
)
.
runGetT
modifyValue
::
forall
k
v
a
.
(
Eq
k
,
Hashable
k
)
=>
(
ObservableValue
v
->
IO
(
ObservableValue
v
,
a
))
->
k
->
ObservableMap
k
v
->
IO
a
modifyValue
f
k
(
ObservableMap
mvar
)
=
modifyMVar
mvar
$
\
hashmap
->
run
State
T
(
HM
.
alterF
update
k
hashmap
)
impossibleCodePath
modifyValue
f
k
(
ObservableMap
mvar
)
=
modifyMVar
mvar
$
\
hashmap
->
run
Get
T
(
HM
.
alterF
update
k
hashmap
)
where
update
::
Maybe
(
ObservableValue
v
)
->
StateT
a
IO
(
Maybe
(
ObservableValue
v
))
update
mov
=
do
let
ov
=
fromMaybe
emptyObservableValue
mov
(
ov'
,
ret
)
<-
liftIO
$
f
ov
put
ret
return
$
toMaybe
ov'
update
::
Maybe
(
ObservableValue
v
)
->
GetT
a
IO
(
Maybe
(
ObservableValue
v
))
update
=
fmap
toMaybe
.
(
GetT
.
f
)
.
fromMaybe
emptyObservableValue
emptyObservableValue
::
ObservableValue
v
emptyObservableValue
=
ObservableValue
Nothing
HM
.
empty
toMaybe
::
ObservableValue
v
->
Maybe
(
ObservableValue
v
)
...
...
@@ -41,15 +45,7 @@ modifyValue f k (ObservableMap mvar) = modifyMVar mvar $ \hashmap -> runStateT (
toMaybe
ov
=
Just
ov
modifyValue_
::
forall
k
v
.
(
Eq
k
,
Hashable
k
)
=>
(
ObservableValue
v
->
IO
(
ObservableValue
v
))
->
k
->
ObservableMap
k
v
->
IO
()
modifyValue_
f
k
(
ObservableMap
mvar
)
=
modifyMVar_
mvar
$
HM
.
alterF
update
k
where
update
::
Maybe
(
ObservableValue
v
)
->
IO
(
Maybe
(
ObservableValue
v
))
update
=
fmap
toMaybe
.
f
.
fromMaybe
emptyObservableValue
emptyObservableValue
::
ObservableValue
v
emptyObservableValue
=
ObservableValue
Nothing
HM
.
empty
toMaybe
::
ObservableValue
v
->
Maybe
(
ObservableValue
v
)
toMaybe
(
ObservableValue
Nothing
(
HM
.
null
->
True
))
=
Nothing
toMaybe
ov
=
Just
ov
modifyValue_
f
=
modifyValue
(
fmap
(,
()
)
.
f
)
modifySubscribers
::
(
HM
.
HashMap
Unique
(
ObservableMessage
v
->
IO
()
)
->
HM
.
HashMap
Unique
(
ObservableMessage
v
->
IO
()
))
->
ObservableValue
v
->
ObservableValue
v
modifySubscribers
f
ov
@
ObservableValue
{
subscribers
}
=
ov
{
subscribers
=
f
subscribers
}
...
...
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