Skip to content

Commit

Permalink
feat(disposable): curry API (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
briancavalier authored Aug 24, 2017
1 parent 134c825 commit 1f9d788
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
6 changes: 3 additions & 3 deletions packages/disposable/src/disposeAll.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/** @license MIT License (c) copyright 2010-2017 original author or authors */
import { reduce } from '@most/prelude'
import { reduce, curry2 } from '@most/prelude'

// Aggregate a list of disposables into a DisposeAll
export const disposeAll = ds =>
new DisposeAll(ds)

// Convenience to aggregate 2 disposables
export const disposeBoth = (d1, d2) =>
disposeAll([d1, d2])
export const disposeBoth = curry2((d1, d2) =>
disposeAll([d1, d2]))

export class DisposeAll {
constructor (disposables) {
Expand Down
5 changes: 3 additions & 2 deletions packages/disposable/src/disposeWith.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/** @license MIT License (c) copyright 2010-2017 original author or authors */
import { disposeOnce } from './disposeOnce'
import { curry2 } from '@most/prelude'

// Create a Disposable that will use the provided
// dispose function to dispose the resource
export const disposeWith = (dispose, resource) =>
disposeOnce(new Disposable(dispose, resource))
export const disposeWith = curry2((dispose, resource) =>
disposeOnce(new Disposable(dispose, resource)))

// Disposable represents a resource that must be
// disposed/released. It aggregates a function to dispose
Expand Down
9 changes: 9 additions & 0 deletions packages/disposable/src/index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@
import type { Time, Disposable, Sink } from '@most/types'

declare export function disposeNone (): Disposable

declare export function disposeWith <R> (dispose: (R) => void, resource: R): Disposable
declare export function disposeWith <R> (dispose: (R) => void): (resource: R) => Disposable

declare export function disposeOnce (d: Disposable): Disposable

declare export function disposeBoth (d1: Disposable, d2: Disposable): Disposable
declare export function disposeBoth (d1: Disposable): (d2: Disposable) => Disposable

declare export function disposeAll (ds: Array<Disposable>): Disposable

declare export function tryDispose (t: Time, disposable: Disposable, sink: Sink<any>): void
declare export function tryDispose (t: Time): (disposable: Disposable, sink: Sink<any>) => void
declare export function tryDispose (t: Time, disposable: Disposable): (sink: Sink<any>) => void
declare export function tryDispose (t: Time): (disposable: Disposable) => (sink: Sink<any>) => void
5 changes: 3 additions & 2 deletions packages/disposable/src/tryDispose.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/** @license MIT License (c) copyright 2010-2017 original author or authors */
import { curry3 } from '@most/prelude'

// Try to dispose the disposable. If it throws, send
// the error to sink.error with the provided Time value
export function tryDispose (t, disposable, sink) {
export const tryDispose = curry3((t, disposable, sink) => {
try {
disposable.dispose()
} catch (e) {
sink.error(t, e)
}
}
})
11 changes: 10 additions & 1 deletion packages/disposable/type-definitions/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { Time, Disposable, Sink } from '@most/types';

declare function disposeNone (): Disposable;

declare function disposeWith <R> (dispose: (resource: R) => void, resource: R): Disposable;
declare function disposeWith <R> (dispose: (resource: R) => void): (resource: R) => Disposable

declare function disposeOnce (d: Disposable): Disposable;

declare function disposeBoth (d1: Disposable, d2: Disposable): Disposable;
declare function disposeBoth (d1: Disposable): (d2: Disposable) => Disposable

declare function disposeAll (ds: Array<Disposable>): Disposable;

declare function tryDispose (t: Time, disposable: Disposable, sink: Sink<any>): void;
declare function tryDispose (t: Time, disposable: Disposable, sink: Sink<any>): void
declare function tryDispose (t: Time): (disposable: Disposable, sink: Sink<any>) => void
declare function tryDispose (t: Time, disposable: Disposable): (sink: Sink<any>) => void
declare function tryDispose (t: Time): (disposable: Disposable) => (sink: Sink<any>) => void

0 comments on commit 1f9d788

Please sign in to comment.