module Polysemy.Conc.Async where
import qualified Control.Concurrent.Async as Base
import Polysemy.Async (Async, async, cancel)
import Polysemy.Resource (Resource, bracket)
import Polysemy.Time (MilliSeconds (MilliSeconds), TimeUnit)
import Polysemy.Conc.Effect.Race (Race)
import qualified Polysemy.Conc.Effect.Sync as Sync
import Polysemy.Conc.Effect.Sync (ScopedSync, Sync)
import Polysemy.Conc.Interpreter.Sync (interpretSync)
import qualified Polysemy.Conc.Race as Race
import Polysemy.Conc.Sync (withSync)
withAsyncBlock ::
Members [Resource, Async] r =>
Sem r b ->
(Base.Async (Maybe b) -> Sem r a) ->
Sem r a
withAsyncBlock :: Sem r b -> (Async (Maybe b) -> Sem r a) -> Sem r a
withAsyncBlock Sem r b
mb =
Sem r (Async (Maybe b))
-> (Async (Maybe b) -> Sem r ())
-> (Async (Maybe b) -> Sem r a)
-> Sem r a
forall (r :: [Effect]) a c b.
MemberWithError Resource r =>
Sem r a -> (a -> Sem r c) -> (a -> Sem r b) -> Sem r b
bracket (Sem r b -> Sem r (Async (Maybe b))
forall (r :: [Effect]) a.
MemberWithError Async r =>
Sem r a -> Sem r (Async (Maybe a))
async Sem r b
mb) Async (Maybe b) -> Sem r ()
forall (r :: [Effect]) a.
MemberWithError Async r =>
Async a -> Sem r ()
cancel
withAsyncWait ::
TimeUnit u =>
Members [Resource, Race, Async] r =>
u ->
Sem r b ->
(Base.Async (Maybe b) -> Sem r a) ->
Sem r a
withAsyncWait :: u -> Sem r b -> (Async (Maybe b) -> Sem r a) -> Sem r a
withAsyncWait u
interval Sem r b
mb =
Sem r (Async (Maybe b))
-> (Async (Maybe b) -> Sem r ())
-> (Async (Maybe b) -> Sem r a)
-> Sem r a
forall (r :: [Effect]) a c b.
MemberWithError Resource r =>
Sem r a -> (a -> Sem r c) -> (a -> Sem r b) -> Sem r b
bracket (Sem r b -> Sem r (Async (Maybe b))
forall (r :: [Effect]) a.
MemberWithError Async r =>
Sem r a -> Sem r (Async (Maybe a))
async Sem r b
mb) (u -> Sem r () -> Sem r ()
forall u (r :: [Effect]).
(TimeUnit u, Member Race r) =>
u -> Sem r () -> Sem r ()
Race.timeoutU u
interval (Sem r () -> Sem r ())
-> (Async (Maybe b) -> Sem r ()) -> Async (Maybe b) -> Sem r ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Async (Maybe b) -> Sem r ()
forall (r :: [Effect]) a.
MemberWithError Async r =>
Async a -> Sem r ()
cancel)
withAsync ::
Members [Resource, Race, Async] r =>
Sem r b ->
(Base.Async (Maybe b) -> Sem r a) ->
Sem r a
withAsync :: Sem r b -> (Async (Maybe b) -> Sem r a) -> Sem r a
withAsync =
MilliSeconds -> Sem r b -> (Async (Maybe b) -> Sem r a) -> Sem r a
forall u (r :: [Effect]) b a.
(TimeUnit u, Members '[Resource, Race, Async] r) =>
u -> Sem r b -> (Async (Maybe b) -> Sem r a) -> Sem r a
withAsyncWait (Int64 -> MilliSeconds
MilliSeconds Int64
500)
withAsync_ ::
Members [Resource, Race, Async] r =>
Sem r b ->
Sem r a ->
Sem r a
withAsync_ :: Sem r b -> Sem r a -> Sem r a
withAsync_ Sem r b
mb =
Sem r b -> (Async (Maybe b) -> Sem r a) -> Sem r a
forall (r :: [Effect]) b a.
Members '[Resource, Race, Async] r =>
Sem r b -> (Async (Maybe b) -> Sem r a) -> Sem r a
withAsync Sem r b
mb ((Async (Maybe b) -> Sem r a) -> Sem r a)
-> (Sem r a -> Async (Maybe b) -> Sem r a) -> Sem r a -> Sem r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sem r a -> Async (Maybe b) -> Sem r a
forall a b. a -> b -> a
const
scheduleAsync ::
∀ res b r a .
Members [ScopedSync res (), Async, Race] r =>
Sem r b ->
(Base.Async (Maybe b) -> Sem (Sync () : r) () -> Sem (Sync () : r) a) ->
Sem r a
scheduleAsync :: Sem r b
-> (Async (Maybe b) -> Sem (Sync () : r) () -> Sem (Sync () : r) a)
-> Sem r a
scheduleAsync Sem r b
mb Async (Maybe b) -> Sem (Sync () : r) () -> Sem (Sync () : r) a
f =
Sem (Sync () : r) a -> Sem r a
forall d res (r :: [Effect]).
Member (ScopedSync res d) r =>
InterpreterFor (Sync d) r
withSync @() @res do
Async (Maybe b)
h <- Sem (Sync () : r) b -> Sem (Sync () : r) (Async (Maybe b))
forall (r :: [Effect]) a.
MemberWithError Async r =>
Sem r a -> Sem r (Async (Maybe a))
async do
forall (r :: [Effect]). MemberWithError (Sync ()) r => Sem r ()
forall d (r :: [Effect]). MemberWithError (Sync d) r => Sem r d
Sync.readBlock @()
Sem r b -> Sem (Sync () : r) b
forall (e :: Effect) (r :: [Effect]) a. Sem r a -> Sem (e : r) a
raise Sem r b
mb
Async (Maybe b) -> Sem (Sync () : r) () -> Sem (Sync () : r) a
f Async (Maybe b)
h (() -> Sem (Sync () : r) ()
forall d (r :: [Effect]).
MemberWithError (Sync d) r =>
d -> Sem r ()
Sync.putBlock ())
scheduleAsyncIO ::
∀ b r a .
Members [Resource, Async, Race, Embed IO] r =>
Sem r b ->
(Base.Async (Maybe b) -> Sem (Sync () : r) () -> Sem (Sync () : r) a) ->
Sem r a
scheduleAsyncIO :: Sem r b
-> (Async (Maybe b) -> Sem (Sync () : r) () -> Sem (Sync () : r) a)
-> Sem r a
scheduleAsyncIO Sem r b
mb Async (Maybe b) -> Sem (Sync () : r) () -> Sem (Sync () : r) a
f =
Sem (Sync () : r) a -> Sem r a
forall d (r :: [Effect]).
Members '[Race, Embed IO] r =>
InterpreterFor (Sync d) r
interpretSync @() do
Async (Maybe b)
h <- Sem (Sync () : r) b -> Sem (Sync () : r) (Async (Maybe b))
forall (r :: [Effect]) a.
MemberWithError Async r =>
Sem r a -> Sem r (Async (Maybe a))
async do
forall (r :: [Effect]). MemberWithError (Sync ()) r => Sem r ()
forall d (r :: [Effect]). MemberWithError (Sync d) r => Sem r d
Sync.readBlock @()
Sem r b -> Sem (Sync () : r) b
forall (e :: Effect) (r :: [Effect]) a. Sem r a -> Sem (e : r) a
raise Sem r b
mb
Async (Maybe b) -> Sem (Sync () : r) () -> Sem (Sync () : r) a
f Async (Maybe b)
h (() -> Sem (Sync () : r) ()
forall d (r :: [Effect]).
MemberWithError (Sync d) r =>
d -> Sem r ()
Sync.putBlock ())