Skip to content

Commit

Permalink
fix(fio): fix issue with racing with never
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Oct 20, 2019
1 parent b45b284 commit d5becbb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
30 changes: 21 additions & 9 deletions src/main/FIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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

/**
Expand Down Expand Up @@ -56,7 +59,7 @@ export type NodeJSCallback<A> = (
*/
export class FIO<E1 = unknown, A1 = unknown, R1 = NoEnv> {
/**
* Safely converts an interuptable IO to non-interruptable one.
* Safely converts an interuptable IO to non-interuptable one.
*/
public get asEither(): FIO<never, Either<E1, A1>, R1> {
return this.map(Either.right).catch(_ => FIO.of(Either.left(_)))
Expand Down Expand Up @@ -660,15 +663,24 @@ export class FIO<E1 = unknown, A1 = unknown, R1 = NoEnv> {
cb2: (exit: Either<E2, A2>, fiber: Fiber<E1, A1>) => IO<E4, A4>
): FIO<E3 | E4, A3 | A4, R1 & R2> {
return Await.of<E3 | E4, A3 | A4>().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)
})
)
}
Expand Down
4 changes: 2 additions & 2 deletions test/FIO.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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', () => {
Expand Down

0 comments on commit d5becbb

Please sign in to comment.