Skip to content

Commit

Permalink
Experiment with switching to Bonsai
Browse files Browse the repository at this point in the history
  • Loading branch information
Negabinary committed Sep 19, 2024
1 parent 4037935 commit 9171acb
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 67 deletions.
141 changes: 79 additions & 62 deletions src/haz3lweb/Main.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
open Util;
open Js_of_ocaml;
open Incr_dom;
open Haz3lweb;

let scroll_to_caret = ref(true);
Expand Down Expand Up @@ -36,7 +35,7 @@ let restart_caret_animation = () =>
| _ => ()
};

let apply = (model, action, state, ~schedule_action): Model.t => {
let apply = (model, action, ~schedule_action): Model.t => {
restart_caret_animation();
if (UpdateAction.is_edit(action)) {
last_edit_action := JsUtil.timestamp();
Expand All @@ -48,7 +47,7 @@ let apply = (model, action, state, ~schedule_action): Model.t => {
last_edit_action := JsUtil.timestamp();
switch (
try({
let new_model = Update.apply(model, action, state, ~schedule_action);
let new_model = Update.apply(model, action, (), ~schedule_action);
Log.update(action);
new_model;
}) {
Expand All @@ -70,74 +69,92 @@ let apply = (model, action, state, ~schedule_action): Model.t => {
};
};

let on_startup =
(
~inject: UpdateAction.t => Ui_effect.t(unit),
~schedule_event: Ui_effect.t(unit) => unit,
m: Model.t,
)
: Model.t => {
let schedule_action = action => schedule_event(inject(action));
let _ =
observe_font_specimen("font-specimen", fm =>
schedule_action(Haz3lweb.Update.SetMeta(FontMetrics(fm)))
);
NinjaKeys.initialize(NinjaKeys.options(schedule_action));
JsUtil.focus_clipboard_shim();
/* initialize state. */
/* Initial evaluation on a worker */
Update.schedule_evaluation(~schedule_action, m);
Os.is_mac :=
Dom_html.window##.navigator##.platform##toUpperCase##indexOf(
Js.string("MAC"),
)
>= 0;
m;
};

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 on_startup = (~schedule_action, m: Model.t) => {
let _ =
observe_font_specimen("font-specimen", fm =>
schedule_action(Haz3lweb.Update.SetMeta(FontMetrics(fm)))
);

NinjaKeys.initialize(NinjaKeys.options(schedule_action));
JsUtil.focus_clipboard_shim();

/* initialize state. */
let state = State.init();

/* Initial evaluation on a worker */
Update.schedule_evaluation(~schedule_action, m);
let app =
Bonsai.state_machine0(
(module Model),
(module Update),
~apply_action=
(~inject, ~schedule_event) =>
apply(~schedule_action=x => schedule_event(inject(x))),
~default_model=Model.load(Model.blank),
~reset=on_startup,
);

Os.is_mac :=
Dom_html.window##.navigator##.platform##toUpperCase##indexOf(
Js.string("MAC"),
)
>= 0;
Async_kernel.Deferred.return(state);
};
open Bonsai.Let_syntax;

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 view = {
let%sub app = app;
let%arr (model, inject) = app;
Haz3lweb.Page.view(~inject, model);
// Bonsai.Computation.map(app, ~f=((model, inject)) =>
// Haz3lweb.Page.view(~inject, model)
// );
};

switch (JsUtil.Fragment.get_current()) {
| Some("debug") => DebugMode.go()
| _ =>
Incr_dom.Start_app.start(
(module App),
~debug=false,
~bind_to_element_with_id="container",
~initial_model=Model.load(Model.blank),
)
| _ => Bonsai_web.Start.start(view, ~bind_to_element_with_id="container")
};
3 changes: 2 additions & 1 deletion src/haz3lweb/Model.re
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ let ui_state_init = {
mousedown: false,
};

[@deriving sexp]
type t = {
editors: Editors.t,
settings: Settings.t,
Expand All @@ -41,7 +42,7 @@ type t = {
ui_state,
};

let cutoff = (===);
let equal = (===);

let mk = (editors, results) => {
editors,
Expand Down
9 changes: 6 additions & 3 deletions src/haz3lweb/dune
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
(name workerServer)
(modules WorkerServer)
(libraries
incr_dom
bonsai
bonsai.web
virtual_dom.input_widgets
util
ppx_yojson_conv.expander
Expand Down Expand Up @@ -38,7 +39,8 @@
ezjs_idb
workerServer
str
incr_dom
bonsai
bonsai.web
virtual_dom.input_widgets
util
ppx_yojson_conv.expander
Expand Down Expand Up @@ -66,7 +68,8 @@
js_of_ocaml-ppx
ppx_let
ppx_sexp_conv
ppx_deriving.show)))
ppx_deriving.show
bonsai.ppx_bonsai)))

(executable
(name worker)
Expand Down
2 changes: 1 addition & 1 deletion src/util/dune
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(library
(name util)
(libraries re base ptmap incr_dom virtual_dom yojson)
(libraries re base ptmap bonsai bonsai.web virtual_dom yojson)
(js_of_ocaml)
(preprocess
(pps
Expand Down

0 comments on commit 9171acb

Please sign in to comment.