Skip to content

Commit

Permalink
fix yielding sync flows before async ones
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaevsky committed Mar 12, 2021
1 parent 0d34ca0 commit e2e29e8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
13 changes: 8 additions & 5 deletions conclude.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ export function conclude(it, callback) {
}

const subscribers = new Set();
let cancel;

const onConclude = (error, result) => {
finalize(it, { error, result });
Expand All @@ -117,7 +116,7 @@ export function conclude(it, callback) {

const unsubscribe = subscribe(callback);

cancel = runners.get(flowType)(it, onConclude);
const cancel = runners.get(flowType)(it, onConclude);

return unsubscribe;
}
Expand Down Expand Up @@ -159,20 +158,24 @@ function runEffect({ [TYPE]: type, context, fn, args }, callback) {
}

function runIterator(it, callback) {
let cancel;
let cancel, step = 0;

const setCancel = (j, fn) => {
if (j >= step) cancel = fn;
}

function iterate(error, result) {
try {
let cancelled = false;
cancel = () => cancelled = true;
setCancel(++step, () => cancelled = true);

const { value, done } = error
? it.throw(error)
: it.next(result);

if (cancelled) return;

cancel = conclude(value, done ? callback : iterate);
setCancel(step, conclude(value, done ? callback : iterate));
}
catch (err) {
callback(err);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "conclure",
"version": "1.1.2",
"version": "1.1.3",
"description": "Generator runner",
"main": "conclude.js",
"module": "conclude.js",
Expand Down
7 changes: 6 additions & 1 deletion tests/combinators.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ test.cb('all, cancelling before completion', t => {
Promise.reject('boom')
];

const cancel = conclude(Conclude.all(promises), t.fail);
function* g() {
yield 5;
yield Conclude.all(promises);
}

const cancel = conclude(g(), t.fail);

let count = 2;

Expand Down

0 comments on commit e2e29e8

Please sign in to comment.