Last active
March 3, 2018 21:29
-
-
Save vincentriemer/ef8468e9929bcd6b9c7bc62b1a85e859 to your computer and use it in GitHub Desktop.
zen-observable reason bindings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type t('a, 'e); | |
module Subscription = { | |
type t; | |
[@bs.get] external closed : t => bool = ""; | |
[@bs.send.pipe : t] external unsubscribe : unit => unit = ""; | |
}; | |
module Observer = { | |
type t('a, 'e); | |
[@bs.get] external closed : t('a, 'e) => bool = ""; | |
[@bs.send.pipe : t('a, 'e)] external next : 'a => unit = ""; | |
[@bs.send.pipe : t('a, 'e)] external error : 'e => unit = ""; | |
[@bs.send.pipe : t('a, 'e)] external complete : unit => unit = ""; | |
}; | |
module SubscriberFunction = { | |
type t('a, 'e) = (Observer.t('a, 'e), unit) => unit; | |
}; | |
type valueHandler('a) = 'a => unit; | |
type errorHandler('e) = 'e => unit; | |
type completeHandler = unit => unit; | |
[@bs.new] [@bs.module] | |
external make : SubscriberFunction.t('a, 'e) => t('a, 'e) = "zen-observable"; | |
[@bs.module "zen-observable"] | |
external fromArray : array('a) => t('a, 'e) = "from"; | |
[@bs.module "zen-observable"] | |
external fromObservable : t('a, 'e) => t('a, 'e) = "from"; | |
[@bs.module "zen-observable"] [@bs.splice] | |
external ofValues : array('a) => t('a, 'e) = "of"; | |
[@bs.send.pipe : t('a, 'e)] | |
external subscribe : | |
( | |
~onValue: valueHandler('a), | |
~onError: errorHandler('e)=?, | |
~onComplete: completeHandler=?, | |
unit | |
) => | |
Subscription.t = | |
""; | |
[@bs.send.pipe : t('a, 'e)] | |
external forEach : ('a => unit) => Js.Promise.t(unit) = ""; | |
[@bs.send.pipe : t('a, 'e)] external filter : ('a => bool) => t('a, 'e) = ""; | |
[@bs.send.pipe : t('a, 'e)] external map : ('a => 'b) => t('b, 'e) = ""; | |
[@bs.send.pipe : t('a, 'e)] | |
external reduce : (('b, 'a) => 'b, 'b) => t('b, 'e) = ""; | |
[@bs.send.pipe : t('a, 'e)] [@bs.splice] | |
external concat : array(t('a, 'e)) => t('a, 'e) = ""; | |
let fromPromise = (promise: Js.Promise.t('a)) => | |
make(observer => { | |
open Js.Promise; | |
let _: Js.Promise.t(unit) = | |
promise | |
|> then_((value: 'a) => { | |
{ | |
open Observer; | |
observer |> next(value); | |
observer |> complete(); | |
}; | |
resolve(); | |
}) | |
|> catch((err: 'e) => resolve(observer |> Observer.error(err))); | |
() => (); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment