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
c143b320
Commit
c143b320
authored
3 years ago
by
Jens Nolte
Browse files
Options
Downloads
Patches
Plain Diff
Move Task to Quasar.Disposable module
parent
3b110430
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Quasar/Async.hs
+0
-62
0 additions, 62 deletions
src/Quasar/Async.hs
src/Quasar/Disposable.hs
+67
-0
67 additions, 0 deletions
src/Quasar/Disposable.hs
with
67 additions
and
62 deletions
src/Quasar/Async.hs
+
0
−
62
View file @
c143b320
...
@@ -5,19 +5,6 @@ module Quasar.Async (
...
@@ -5,19 +5,6 @@ module Quasar.Async (
async_
,
async_
,
asyncWithUnmask_
,
asyncWithUnmask_
,
-- * Task
Task
,
cancelTask
,
cancelTaskIO
,
toTask
,
completedTask
,
successfulTask
,
failedTask
,
-- ** Task exceptions
CancelTask
(
..
),
TaskDisposed
(
..
),
-- * Unmanaged forking
-- * Unmanaged forking
forkTask
,
forkTask
,
forkTask_
,
forkTask_
,
...
@@ -155,52 +142,3 @@ forkTaskWithUnmask_ action = toDisposable <$> forkTaskWithUnmask action
...
@@ -155,52 +142,3 @@ forkTaskWithUnmask_ action = toDisposable <$> forkTaskWithUnmask action
-- | A task that is running asynchronously. It has a result and can fail.
-- The result (or exception) can be aquired by using the `IsAwaitable` class (e.g. by calling `await` or `awaitIO`).
-- It might be possible to cancel the task by using the `IsDisposable` class if the operation has not been completed.
-- If the result is no longer required the task should be cancelled, to avoid leaking memory.
data
Task
r
=
Task
Disposable
(
Awaitable
r
)
instance
IsAwaitable
r
(
Task
r
)
where
toAwaitable
(
Task
_
awaitable
)
=
awaitable
instance
IsDisposable
(
Task
r
)
where
toDisposable
(
Task
disposable
_
)
=
disposable
instance
Functor
Task
where
fmap
fn
(
Task
disposable
awaitable
)
=
Task
disposable
(
fn
<$>
awaitable
)
instance
Applicative
Task
where
pure
value
=
Task
noDisposable
(
pure
value
)
liftA2
fn
(
Task
dx
fx
)
(
Task
dy
fy
)
=
Task
(
dx
<>
dy
)
$
liftA2
fn
fx
fy
cancelTask
::
Task
r
->
IO
(
Awaitable
()
)
cancelTask
=
dispose
cancelTaskIO
::
Task
r
->
IO
()
cancelTaskIO
=
await
<=<
dispose
-- | Creates an `Task` from an `Awaitable`.
-- The resulting task only depends on an external resource, so disposing it has no effect.
toTask
::
IsAwaitable
r
a
=>
a
->
Task
r
toTask
result
=
Task
noDisposable
(
toAwaitable
result
)
completedTask
::
Either
SomeException
r
->
Task
r
completedTask
result
=
Task
noDisposable
(
completedAwaitable
result
)
-- | Alias for `pure`
successfulTask
::
r
->
Task
r
successfulTask
=
pure
failedTask
::
SomeException
->
Task
r
failedTask
ex
=
Task
noDisposable
(
failedAwaitable
ex
)
data
CancelTask
=
CancelTask
deriving
stock
Show
instance
Exception
CancelTask
where
data
TaskDisposed
=
TaskDisposed
deriving
stock
Show
instance
Exception
TaskDisposed
where
This diff is collapsed.
Click to expand it.
src/Quasar/Disposable.hs
+
67
−
0
View file @
c143b320
...
@@ -21,6 +21,19 @@ module Quasar.Disposable (
...
@@ -21,6 +21,19 @@ module Quasar.Disposable (
attachDisposeAction
,
attachDisposeAction
,
attachDisposeAction_
,
attachDisposeAction_
,
disposeEventually
,
disposeEventually
,
-- * Task
Task
(
..
),
cancelTask
,
toTask
,
completedTask
,
successfulTask
,
failedTask
,
-- ** Task exceptions
CancelTask
(
..
),
TaskDisposed
(
..
),
)
where
)
where
import
Control.Concurrent
(
forkIOWithUnmask
)
import
Control.Concurrent
(
forkIOWithUnmask
)
...
@@ -402,3 +415,57 @@ disposeEventually resourceManager disposable = liftIO $ do
...
@@ -402,3 +415,57 @@ disposeEventually resourceManager disposable = liftIO $ do
peekAwaitable
disposeCompleted
>>=
\
case
peekAwaitable
disposeCompleted
>>=
\
case
Just
()
->
pure
()
Just
()
->
pure
()
Nothing
->
attachDisposable
resourceManager
disposable
Nothing
->
attachDisposable
resourceManager
disposable
-- | A task is an operation (e.g. a thread or a network request) that is running asynchronously and can be cancelled.
-- It has a result and can fail.
--
-- The result (or exception) can be aquired by using the `IsAwaitable` class (e.g. by calling `await` or `awaitIO`).
-- It is possible to cancel the task by using `dispose` or `cancelTask` if the operation has not been completed.
data
Task
r
=
Task
Disposable
(
Awaitable
r
)
instance
IsAwaitable
r
(
Task
r
)
where
toAwaitable
(
Task
_
awaitable
)
=
awaitable
instance
IsDisposable
(
Task
r
)
where
toDisposable
(
Task
disposable
_
)
=
disposable
instance
Functor
Task
where
fmap
fn
(
Task
disposable
awaitable
)
=
Task
disposable
(
fn
<$>
awaitable
)
instance
Applicative
Task
where
pure
value
=
Task
noDisposable
(
pure
value
)
liftA2
fn
(
Task
dx
fx
)
(
Task
dy
fy
)
=
Task
(
dx
<>
dy
)
$
liftA2
fn
fx
fy
-- | Alias for `dispose`.
cancelTask
::
Task
r
->
IO
(
Awaitable
()
)
cancelTask
=
dispose
-- | Creates an `Task` from an `Awaitable`.
-- The resulting task only depends on an external resource, so disposing it has no effect.
toTask
::
IsAwaitable
r
a
=>
a
->
Task
r
toTask
result
=
Task
noDisposable
(
toAwaitable
result
)
completedTask
::
Either
SomeException
r
->
Task
r
completedTask
result
=
Task
noDisposable
(
completedAwaitable
result
)
-- | Alias for `pure`
successfulTask
::
r
->
Task
r
successfulTask
=
pure
failedTask
::
SomeException
->
Task
r
failedTask
ex
=
Task
noDisposable
(
failedAwaitable
ex
)
data
CancelTask
=
CancelTask
deriving
stock
Show
instance
Exception
CancelTask
where
data
TaskDisposed
=
TaskDisposed
deriving
stock
Show
instance
Exception
TaskDisposed
where
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