diff --git a/src/main/FIO.ts b/src/main/FIO.ts index 2d702e4a..1cd70502 100644 --- a/src/main/FIO.ts +++ b/src/main/FIO.ts @@ -2,6 +2,7 @@ * Created by tushar on 2019-05-20 */ +import {debug} from 'debug' import {Either, List, Option} from 'standard-data-structures' import {ICancellable} from 'ts-scheduler' @@ -13,6 +14,8 @@ import {IRuntime} from '../runtimes/IRuntime' import {Await} from './Await' import {Instruction, Tag} from './Instructions' +const D = debug('fio:core') + export type NoEnv = unknown /** @@ -56,7 +59,7 @@ export type NodeJSCallback = ( */ export class FIO { /** - * Safely converts an interuptable IO to non-interruptable one. + * Safely converts an interuptable IO to non-interuptable one. */ public get asEither(): FIO, R1> { return this.map(Either.right).catch(_ => FIO.of(Either.left(_))) @@ -660,15 +663,24 @@ export class FIO { cb2: (exit: Either, fiber: Fiber) => IO ): FIO { return Await.of().chain(done => - this.fork.zip(that.fork).chain(({0: f1, 1: f2}) => { - const resume1 = f1.await.chain(exit => - Option.isSome(exit) ? done.set(cb1(exit.value, f2)) : FIO.of(true) - ) - const resume2 = f2.await.chain(exit => - Option.isSome(exit) ? done.set(cb2(exit.value, f1)) : FIO.of(true) - ) + this.fork.zip(that.fork).chain(([L, R]) => { + D('zip', 'fiber L', L.id, '& fiber R', R.id) + const resume1 = L.await.chain(exit => { + D('zip', 'L cb') + + return Option.isSome(exit) + ? done.set(cb1(exit.value, R)) + : FIO.of(true) + }) + const resume2 = R.await.chain(exit => { + D('zip', 'R cb') + + return Option.isSome(exit) + ? done.set(cb2(exit.value, L)) + : FIO.of(true) + }) - return resume1.and(resume2).and(done.get) + return resume1.fork.and(resume2.fork).and(done.get) }) ) } diff --git a/test/FIO.test.ts b/test/FIO.test.ts index 485d0159..b43884b0 100644 --- a/test/FIO.test.ts +++ b/test/FIO.test.ts @@ -622,7 +622,7 @@ describe('FIO', () => { assert.deepEqual(snapshot.timeline, ['A@1001', 'B@2001']) }) - it('should complete when both complete', () => { + it('should complete when either complete', () => { const snapshot = new Snapshot() const F1 = snapshot.mark('A').delay(1000) @@ -633,7 +633,7 @@ describe('FIO', () => { F1.raceWith(F2, FIO.void, FIO.void).and(snapshot.mark('C')) ) - assert.deepEqual(snapshot.timeline, ['A@1001', 'B@2001', 'C@2001']) + assert.sameDeepMembers(snapshot.timeline, ['A@1001', 'B@2001', 'C@1001']) }) context('when slower is cancelled', () => {