Skip to content

Commit

Permalink
Finish converting to Bonsai
Browse files Browse the repository at this point in the history
  • Loading branch information
Negabinary committed Sep 27, 2024
1 parent 6440099 commit 31b690a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 76 deletions.
81 changes: 34 additions & 47 deletions src/haz3lweb/Main.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
open Util;
open Js_of_ocaml;
open Haz3lweb;
open Bonsai.Let_syntax;

let scroll_to_caret = ref(true);
let edit_action_applied = ref(true);
Expand Down Expand Up @@ -52,44 +53,6 @@ let apply = (model, action, ~schedule_action): Model.t => {
};
};

module App = {
module Model = Model;
module Action = Update;
module State = State;
// let create =
// (
// model: Incr.t(Haz3lweb.Model.t),
// ~old_model as _: Incr.t(Haz3lweb.Model.t),
// ~inject,
// ) => {
// open Incr.Let_syntax;
// let%map model = model;
// /* Note: mapping over the old_model here may
// trigger an additional redraw */
// Component.create(
// ~apply_action=apply(model),
// model,
// Haz3lweb.Page.view(~inject, model),
// ~on_display=(_, ~schedule_action) => {
// if (edit_action_applied^
// && JsUtil.timestamp()
// -. last_edit_action^ > 1000.0) {
// /* If an edit action has been applied, but no other edit action
// has been applied for 1 second, save the model. */
// edit_action_applied := false;
// print_endline("Saving...");
// schedule_action(Update.Save);
// };
// if (scroll_to_caret.contents) {
// scroll_to_caret := false;
// JsUtil.scroll_cursor_into_view_if_needed();
// };
// },
// );
// };
// };
};

let app =
Bonsai.state_machine0(
(module Model),
Expand All @@ -100,22 +63,46 @@ let app =
~default_model=Model.load(Model.blank),
);

open Bonsai.Let_syntax;

let view = {
let startup_completed = Bonsai.toggle'(~default_model=false);
let%sub startup_completed = startup_completed;
let%sub app = app;
let on_startup = effect => {
let%sub startup_completed = Bonsai.toggle'(~default_model=false);
let%sub after_display = {
switch%sub (startup_completed) {
| {state: false, set_state, _} =>
let%arr (_model, inject) = app
let%arr effect = effect
and set_state = set_state;
Bonsai.Effect.Many([set_state(true), inject(Update.Startup)]);
Bonsai.Effect.Many([set_state(true), effect]);
| {state: true, _} => Bonsai.Computation.return(Ui_effect.Ignore)
};
};
let%sub () = Bonsai.Edge.lifecycle(~after_display, ());
Bonsai.Edge.lifecycle(~after_display, ());
};

let view = {
let%sub app = app;
let%sub () = {
on_startup(
Bonsai.Value.map(~f=((_model, inject)) => inject(Startup), app),
);
};
let%sub after_display = {
let%arr (_model, inject) = app;
if (scroll_to_caret.contents) {
scroll_to_caret := false;
JsUtil.scroll_cursor_into_view_if_needed();
};
if (edit_action_applied^
&& JsUtil.timestamp()
-. last_edit_action^ > 1000.0) {
/* If an edit action has been applied, but no other edit action
has been applied for 1 second, save the model. */
edit_action_applied := false;
print_endline("Saving...");
inject(Update.Save);
} else {
Ui_effect.Ignore;
};
};
let%sub () = Bonsai.Edge.after_display(after_display);
let%arr (model, inject) = app;
Haz3lweb.Page.view(~inject, model);
};
Expand Down
31 changes: 2 additions & 29 deletions src/haz3lweb/view/DebugMode.re
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,8 @@ let view = {
);
};

module App = {
module Model = {
type t = unit;
let cutoff = (_, _) => false;
};
module Action = {
type t = unit;
let sexp_of_t = _ => Sexplib.Sexp.unit;
};
module State = {
type t = unit;
};
let on_startup = (~schedule_action as _, _) =>
Async_kernel.Deferred.return();
let create = (_, ~old_model as _, ~inject as _) =>
Incr_dom.Incr.return()
|> Incr_dom.Incr.map(~f=_ =>
Incr_dom.Component.create(
~apply_action=(_, _, ~schedule_action as _) => (),
(),
view,
)
);
};

let go = () =>
Incr_dom.Start_app.start(
(module App),
~debug=false,
Bonsai_web.Start.start(
Bonsai.Computation.return(view),
~bind_to_element_with_id="container",
~initial_model=(),
);

0 comments on commit 31b690a

Please sign in to comment.