Skip to content

Instantly share code, notes, and snippets.

@Denommus
Last active October 6, 2017 14:16
Show Gist options
  • Save Denommus/b824a3d62cb6dfb06fe4df3221791ce7 to your computer and use it in GitHub Desktop.
Save Denommus/b824a3d62cb6dfb06fe4df3221791ce7 to your computer and use it in GitHub Desktop.
open ReactErratique;
module type ReactModule = {
type t 'a;
let map: eq::('b => 'b => bool)? => ('a => 'b) => t 'a => t 'b;
let create: eq::(unit => unit => bool)? => unit => (t unit, step::step? => unit => unit);
};
module ComponentGenerator (R: ReactModule) => {
type action 'a =
| Tick 'a;
type state = {
vdom: ReasonReact.reactElement,
subscriberId: ref (R.t unit)
};
let componentFrom ::eq=(==) name vdomFRP => {
let component = ReasonReact.reducerComponent name;
{
...component,
initialState: fun () => {vdom: <div />, subscriberId: ref (fst (R.create ()))},
reducer: fun action state =>
switch action {
| Tick x => ReasonReact.Update {...state, vdom: x}
},
didMount: fun {state, reduce} => {
state.subscriberId := R.map ::eq (fun x => reduce (fun _event => Tick x) ()) vdomFRP;
ReasonReact.NoUpdate
},
render: fun {state: {vdom}} => vdom
}
};
};
module SHelper: ReactModule with type t 'a = signal 'a = {
include S;
};
module ComponentFromSignal = ComponentGenerator SHelper;
let componentFromSignal ::eq=(==) name vdomS => ComponentFromSignal.componentFrom ::eq name vdomS;
module EHelper: ReactModule with type t 'a = event 'a = {
include E;
let map eq::(_: 'b => 'b => bool)=(==) f e => E.map f e;
let create eq::(_: unit => unit => bool)=(==) () => E.create ();
};
module ComponentFromEvent = ComponentGenerator EHelper;
let componentFromEvent name vdomE => ComponentFromEvent.componentFrom name vdomE;
module ComponentFromSignal: {type action 'a; type state;};
module ComponentFromEvent: {type action 'a; type state;};
let componentFromSignal:
eq::(unit => unit => bool)? =>
string =>
ReactErratique.signal ReasonReact.reactElement =>
ReasonReact.componentSpec
ComponentFromSignal.state
ComponentFromSignal.state
ReasonReact.noRetainedProps
ReasonReact.noRetainedProps
(ComponentFromSignal.action ReasonReact.reactElement);
let componentFromEvent:
string =>
ReactErratique.event ReasonReact.reactElement =>
ReasonReact.componentSpec
ComponentFromEvent.state
ComponentFromEvent.state
ReasonReact.noRetainedProps
ReasonReact.noRetainedProps
(ComponentFromEvent.action ReasonReact.reactElement);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment