Skip to content

Commit

Permalink
fix(managed): handle aborts and interruptions thru exit
Browse files Browse the repository at this point in the history
Create a new type called `Exit` which is a `ICancellable`.
  • Loading branch information
tusharmath committed Sep 8, 2019
1 parent 1a13755 commit 250c727
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
19 changes: 19 additions & 0 deletions src/internals/Exit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {ICancellable} from 'ts-scheduler'

import {UIO} from '../main/FIO'
import {IRuntime} from '../runtimes/IRuntime'

/**
* Created by tushar on 08/09/19
*/

export class Exit implements ICancellable {
public constructor(
private readonly uio: UIO<void>,
private readonly runtime: IRuntime
) {}

public cancel(): void {
this.runtime.execute(this.uio)
}
}
7 changes: 7 additions & 0 deletions src/internals/FiberContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {IRuntime} from '../runtimes/IRuntime'
import {CancellationList} from './CancellationList'
import {CB} from './CB'
import {Evaluate} from './Evaluate'
import {Exit} from './Exit'

/**
* @ignore
Expand Down Expand Up @@ -50,6 +51,12 @@ export class FiberContext<E = never, A = never> extends Fiber<E, A>
this.unsafeAbort()
}

public exit(fio: UIO<void>): UIO<void> {
return UIO(() => {
this.cancellationList.push(new Exit(fio, this.runtime))
})
}

public resumeAsync(cb: (exit: Either<E, A>) => UIO<void>): UIO<void> {
const eee = <X>(con: (x: X) => Either<E, A>) => (data: X) => {
// tslint:disable-next-line: no-use-before-declare
Expand Down
1 change: 1 addition & 0 deletions src/main/Fiber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ import {Either} from './Either'
export abstract class Fiber<E, A> {
public abstract abort: UIO<void>
public abstract resume: FIO<E, A>
public abstract exit(p: UIO<void>): UIO<void>
public abstract resumeAsync(cb: (exit: Either<E, A>) => UIO<void>): UIO<void>
}
15 changes: 9 additions & 6 deletions src/main/Managed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,16 @@ export class Managed<E1, A1, R1> {
}

public use<E2, A2, R2>(
cb: (a: A1) => FIO<E2, A2, R2>
fn: (a: A1) => FIO<E2, A2, R2>
): FIO<E1 | E2, A2, R1 & R2> {
return this.reservation.chain(r =>
r.acquire
.chain(cb)
.catch(e12 => r.release.and(FIO.reject(e12)))
.chain(a2 => r.release.const(a2))
return FIO.flatten(
this.reservation.zipWith(FIO.env<R1 & R2>(), (R, ENV) =>
R.acquire
.chain(fn)
.catch(e12 => R.release.and(FIO.reject(e12)))
.chain(a2 => R.release.const(a2))
.fork.chain(F => F.exit(R.release.provide(ENV)).and(F.resume))
)
)
}

Expand Down
2 changes: 1 addition & 1 deletion test/Managed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('Managed', () => {
assert.strictEqual(r.count, 1)
})

it.skip('should release resource on cancellation', () => {
it('should release resource on cancellation', () => {
const r = Resource()
const runtime = testRuntime()

Expand Down

0 comments on commit 250c727

Please sign in to comment.