Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring: Remove state management API #445

Merged
merged 2 commits into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 0 additions & 163 deletions examples/Autocomplete.re

This file was deleted.

147 changes: 79 additions & 68 deletions examples/Examples.re
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,85 @@ module ExampleButton = {
type action =
| SelectExample(string);

let reducer = (s: state, a: action) =>
switch (a) {
| SelectExample(name) => {...s, selectedExample: name}
let reducer = (action: action, state: state) =>
switch (action) {
| SelectExample(name) => {...state, selectedExample: name}
};

module ExampleHost = {
let component = React.component("ExampleHost");

let createElement = (~children as _, ~win, ()) =>
component(hooks => {
let (state, dispatch, hooks) =
React.Hooks.reducer(~initialState=state, reducer, hooks);

let renderButton = (x: example) => {
let isActive = String.equal(x.name, state.selectedExample);
<ExampleButton
isActive
name={x.name}
onClick={_ => {
/*
* TEMPORARY WORKAROUND: The animations don't always get stopped when switching examples,
* tracked by briskml/brisk-reconciler#8. We can remove this once it's fixed!
*/
Animated.cancelAll();

let sourceFile = getSourceForSample(state, x.name);
notifyExampleSwitched(sourceFile);
dispatch(SelectExample(x.name));
}}
/>;
};

let buttons = List.map(renderButton, state.examples);

let exampleRender = getRenderFunctionSelector(state);
let example = exampleRender(win);

(
hooks,
<View
onMouseWheel={_evt => ()}
style=Style.[
position(`Absolute),
justifyContent(`Center),
alignItems(`Center),
backgroundColor(bgColor),
bottom(0),
top(0),
left(0),
right(0),
flexDirection(`Row),
]>
<ScrollView
style=Style.[
position(`Absolute),
top(0),
left(0),
width(175),
bottom(0),
backgroundColor(bgColor),
]>
<View> ...buttons </View>
</ScrollView>
<View
style=Style.[
position(`Absolute),
top(0),
left(175),
right(0),
bottom(0),
backgroundColor(activeBackgroundColor),
]>
example
</View>
</View>,
);
});
};

let init = app => {
let maximized = Environment.webGL;

Expand All @@ -206,70 +280,7 @@ let init = app => {
},
);

let render = () => {
let s = App.getState(app);

let renderButton = (x: example) => {
let isActive = String.equal(x.name, s.selectedExample);
<ExampleButton
isActive
name={x.name}
onClick={_ => {
/*
* TEMPORARY WORKAROUND: The animations don't always get stopped when switching examples,
* tracked by briskml/brisk-reconciler#8. We can remove this once it's fixed!
*/
Animated.cancelAll();

let sourceFile = getSourceForSample(s, x.name);
notifyExampleSwitched(sourceFile);
App.dispatch(app, SelectExample(x.name));
}}
/>;
};

let buttons = List.map(renderButton, s.examples);

let exampleRender = getRenderFunctionSelector(s);
let example = exampleRender(win);

<View
onMouseWheel={_evt => ()}
style=Style.[
position(`Absolute),
justifyContent(`Center),
alignItems(`Center),
backgroundColor(bgColor),
bottom(0),
top(0),
left(0),
right(0),
flexDirection(`Row),
]>
<ScrollView
style=Style.[
position(`Absolute),
top(0),
left(0),
width(175),
bottom(0),
backgroundColor(bgColor),
]>
<View> ...buttons </View>
</ScrollView>
<View
style=Style.[
position(`Absolute),
top(0),
left(175),
right(0),
bottom(0),
backgroundColor(activeBackgroundColor),
]>
example
</View>
</View>;
};
let render = () => <ExampleHost win />;

if (Environment.webGL) {
Window.maximize(win);
Expand All @@ -283,4 +294,4 @@ let init = app => {
};

let onIdle = () => print_endline("Example: idle callback triggered");
App.startWithState(~onIdle, state, reducer, init);
App.start(~onIdle, init);
Loading