Skip to content

Commit ef2700b

Browse files
committed
Thunk.Basic: forceMain: refact
1 parent 761e854 commit ef2700b

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

src/Nix/Thunk/Basic.hs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ data Deferred m v = Computed v | Deferred (m v)
3535

3636
-- ** Utils
3737

38-
-- | @Deferred (Computed|Deferred)@ analog of @either@.
38+
-- | Apply second if @Deferred@, otherwise (@Computed@) - apply first.
39+
-- Analog of @either@ for @Deferred = Computed|Deferred@.
3940
deferred :: (v -> b) -> (m v -> b) -> Deferred m v -> b
4041
deferred f1 f2 def =
4142
case def of
@@ -147,26 +148,30 @@ forceMain
147148
=> NThunkF m v
148149
-> m v
149150
forceMain (Thunk n thunkRef thunkValRef) =
150-
do
151-
deferred
152-
pure
153-
(\ action ->
154-
do
155-
lockedIt <- lockThunk thunkRef
156-
bool
157-
(throwM $ ThunkLoop $ show n)
158-
(do
159-
v <- catch action $ \(e :: SomeException) ->
160-
do
161-
_unlockedIt <- unlockThunk thunkRef
162-
throwM e
163-
writeVar thunkValRef (Computed v)
164-
_unlockedIt <- unlockThunk thunkRef
165-
pure v
166-
)
167-
(not lockedIt)
168-
)
169-
=<< readVar thunkValRef
151+
deferred
152+
pure
153+
(\ action ->
154+
do
155+
lockedIt <- lockThunk thunkRef
156+
bool
157+
lockFailed
158+
(do
159+
v <- action `catch` actionFailed
160+
writeVar thunkValRef (Computed v)
161+
_unlockedIt <- unlockThunk thunkRef
162+
pure v
163+
)
164+
(not lockedIt)
165+
)
166+
=<< readVar thunkValRef
167+
where
168+
lockFailed = throwM $ ThunkLoop $ show n
169+
170+
actionFailed (e :: SomeException) =
171+
do
172+
_unlockedIt <- unlockThunk thunkRef
173+
throwM e
174+
170175
{-# inline forceMain #-} -- it is big function, but internal, and look at its use.
171176

172177

0 commit comments

Comments
 (0)