-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Jan Beinke <[email protected]>
- Loading branch information
Showing
3 changed files
with
50 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module Quasar.Resources.FutureDisposer ( | ||
futureDisposer, | ||
) where | ||
|
||
import Quasar.Prelude | ||
import Quasar.Future | ||
import Quasar.Resources.Disposer | ||
import Quasar.Utils.TOnce | ||
|
||
data FutureDisposer = FutureDisposer Unique (TOnce (Future Disposer) (Future [DisposeDependencies])) | ||
|
||
instance IsDisposerElement FutureDisposer where | ||
disposerElementKey (FutureDisposer key _) = key | ||
|
||
disposeEventually# self = do | ||
beginDispose# self <&> \case | ||
DisposeResultAwait future -> future | ||
DisposeResultDependencies deps -> flattenDisposeDependencies deps | ||
|
||
beginDispose# (FutureDisposer key var) = do | ||
fdeps <- mapFinalizeTOnce var \future -> do | ||
|
||
promise <- newPromise | ||
callOnceCompleted_ future \disposer -> do | ||
fdeps <- beginDisposeDisposer disposer | ||
tryFulfillPromise_ promise fdeps | ||
|
||
pure (join (toFuture promise)) | ||
|
||
pure (DisposeResultDependencies (DisposeDependencies key fdeps)) | ||
|
||
instance ToFuture () FutureDisposer where | ||
toFuture (FutureDisposer _ var) = do | ||
deps <- join (toFuture var) | ||
mapM_ flattenDisposeDependencies deps | ||
|
||
instance Disposable FutureDisposer where | ||
getDisposer x = mkDisposer [x] | ||
|
||
futureDisposer :: Future Disposer -> STMc NoRetry '[] Disposer | ||
futureDisposer future = do | ||
key <- newUniqueSTM | ||
var <- newTOnce future | ||
pure (getDisposer (FutureDisposer key var)) |