Skip to content

Commit 3391938

Browse files
committed
Use runFiber in sequential
1 parent 1b7a692 commit 3391938

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

src/Control/Monad/Aff.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ function runFiber(util, suspended, aff, completeCb) {
542542
break;
543543

544544
case COMPLETED:
545-
completeCb();
545+
completeCb(step);
546546
tmp = false;
547547
for (var k in joins) {
548548
if ({}.hasOwnProperty.call(joins, k)) {
@@ -664,7 +664,7 @@ exports._launchAff = function (util, suspended, aff) {
664664
};
665665
};
666666

667-
exports._sequential = function (util, runAff, par) {
667+
exports._sequential = function (util, par) {
668668
function runParAff(cb) {
669669
// Table of all forked fibers.
670670
var fiberId = 0;
@@ -708,18 +708,20 @@ exports._sequential = function (util, runAff, par) {
708708
} else {
709709
// Again, we prime the effect but don't run it yet, so that we can
710710
// collect all the fibers first.
711-
kills[count++] = runAff(function (result) {
711+
kills[count++] = function (aff) {
712712
return function () {
713-
count--;
714-
if (fail === null && util.isLeft(result)) {
715-
fail = result;
716-
}
717-
// We can resolve the callback when all fibers have died.
718-
if (count === 0) {
719-
cb(fail || util.right(void 0))();
720-
}
713+
return runFiber(util, false, aff, function (result) {
714+
count--;
715+
if (fail === null && util.isLeft(result)) {
716+
fail = result;
717+
}
718+
// We can resolve the callback when all fibers have died.
719+
if (count === 0) {
720+
cb(fail || util.right(void 0))();
721+
}
722+
});
721723
};
722-
})(tmp._1.kill(error));
724+
}(tmp._1.kill(error));
723725
}
724726
// Terminal case.
725727
if (head === null) {
@@ -872,11 +874,9 @@ exports._sequential = function (util, runAff, par) {
872874

873875
function resolve(fiber) {
874876
return function (result) {
875-
return function () {
876-
delete fibers[fiber._1];
877-
fiber._3 = result;
878-
join(result, fiber._2._1, fiber._2._2);
879-
};
877+
delete fibers[fiber._1];
878+
fiber._3 = result;
879+
join(result, fiber._2._1, fiber._2._2);
880880
};
881881
}
882882

@@ -933,7 +933,11 @@ exports._sequential = function (util, runAff, par) {
933933
// because they may all be synchronous and resolve immediately, at
934934
// which point it would attempt to resolve against an incomplete
935935
// tree.
936-
fibers[fid] = new Aff(THUNK, runAff(resolve(step))(tmp));
936+
fibers[fid] = function (aff, completeCb) {
937+
return new Aff(THUNK, function () {
938+
return runFiber(util, false, aff, completeCb);
939+
});
940+
}(tmp, resolve(step));
937941
}
938942
break;
939943
case RETURN:
@@ -974,10 +978,6 @@ exports._sequential = function (util, runAff, par) {
974978
}
975979
}
976980

977-
function ignore () {
978-
return function () {};
979-
}
980-
981981
// Cancels the entire tree. If there are already subtrees being canceled,
982982
// we need to first cancel those joins. This is important so that errors
983983
// don't accidentally get swallowed by irrelevant join callbacks.
@@ -987,7 +987,7 @@ exports._sequential = function (util, runAff, par) {
987987
// We can drop the fibers here because we are only canceling join
988988
// attempts, which are synchronous anyway.
989989
for (var kid = 0, n = killId; kid < n; kid++) {
990-
runAff(ignore, kills[kid].kill(error))();
990+
runFiber(util, false, kills[kid].kill(error), function () {});
991991
}
992992

993993
var newKills = kill(error, root, cb);
@@ -997,7 +997,7 @@ exports._sequential = function (util, runAff, par) {
997997
return function () {
998998
for (var kid in newKills) {
999999
if (newKills.hasOwnProperty(kid)) {
1000-
runAff(ignore, newKills[kid].kill(killError))();
1000+
runFiber(util, false, newKills[kid].kill(killError), function () {});
10011001
}
10021002
}
10031003
return nonCanceler;

src/Control/Monad/Aff.purs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ instance alternativeParAff ∷ Alternative (ParAff e)
132132

133133
instance parallelAffParallel (ParAff eff) (Aff eff) where
134134
parallel = (unsafeCoerce a. Aff eff a ParAff eff a)
135-
sequential a = Fn.runFn3 _sequential ffiUtil runAff a
135+
sequential a = Fn.runFn2 _sequential ffiUtil a
136136

137137
-- | Represents a forked computation by way of `forkAff`. `Fiber`s are
138138
-- | memoized, so their results are only computed once.
@@ -294,9 +294,8 @@ foreign import _launchAff
294294

295295
foreign import _sequential
296296
eff a
297-
. Fn.Fn3
297+
. Fn.Fn2
298298
FFIUtil
299-
((Either Error a Eff eff Unit) Aff eff a Eff eff (Fiber eff Unit))
300299
(ParAff eff a)
301300
(Aff eff a)
302301

0 commit comments

Comments
 (0)