Skip to content

Commit adc4b95

Browse files
committed
Separate "logged"
1 parent 0b1e580 commit adc4b95

File tree

5 files changed

+217
-175
lines changed

5 files changed

+217
-175
lines changed

src/web/Main.re

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@ let restart_caret_animation = () =>
1919

2020
let apply =
2121
(
22-
model: History.Model.t,
23-
action: History.Update.t,
22+
model: Logged.Model.t,
23+
action: Logged.Update.t,
2424
~schedule_action,
2525
~schedule_autosave,
2626
)
27-
: History.Model.t => {
27+
: Logged.Model.t => {
2828
restart_caret_animation();
2929

3030
/* This function is split into two phases, update and calculate.
3131
The intention is that eventually, the calculate phase will be
3232
done automatically by incremental calculation. */
3333
// ---------- UPDATE PHASE ----------
34-
let updated: Updated.t(History.Model.t) =
34+
let updated: Updated.t(Logged.Model.t) =
3535
try(
36-
History.Update.update(
36+
Logged.Update.update(
3737
~import_log=Log.import,
3838
~get_log_and=Log.get_and,
3939
~schedule_action,
@@ -58,7 +58,7 @@ let apply =
5858
let model' =
5959
try(
6060
updated.model
61-
|> History.Update.calculate(
61+
|> Logged.Update.calculate(
6262
~schedule_action,
6363
~is_edited=updated.is_edit,
6464
~dynamics=true,
@@ -98,8 +98,8 @@ let start = {
9898
let%sub save_scheduler = BonsaiUtil.Alarm.alarm;
9999
let%sub (app_model, app_inject) =
100100
Bonsai.state_machine1(
101-
(module History.Model),
102-
(module History.Update),
101+
(module Logged.Model),
102+
(module Logged.Update),
103103
~apply_action=
104104
(~inject, ~schedule_event, input) => {
105105
let schedule_action = x => schedule_event(inject(x));
@@ -112,8 +112,8 @@ let start = {
112112
apply(~schedule_action, ~schedule_autosave);
113113
},
114114
~default_model=
115-
History.Model.init()
116-
|> History.Update.calculate(
115+
Logged.Model.init()
116+
|> Logged.Update.calculate(
117117
~schedule_action=_ => (),
118118
~is_edited=true,
119119
~dynamics=false,
@@ -220,7 +220,8 @@ let start = {
220220
let _ = Haz3lcore.ProbePerform.FocusEffect.execute();
221221
/* Update floating elements (backpack) to viewport coordinates */
222222
FloatingElement.update_all();
223-
model.current.globals.settings.core.statics ? Animation.go() : ();
223+
model.current.current.globals.settings.core.statics
224+
? Animation.go() : ();
224225
},
225226
(),
226227
);
@@ -231,7 +232,7 @@ let start = {
231232
let%arr app_model = app_model
232233
and app_inject = app_inject;
233234
try(
234-
History.View.view(app_model, ~inject=app_inject, ~get_log_and=Log.get_and)
235+
Logged.View.view(app_model, ~inject=app_inject, ~get_log_and=Log.get_and)
235236
) {
236237
| exc =>
237238
print_endline(

src/web/app/History.re

Lines changed: 3 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ module Model = {
99
current: state,
1010
undo_stack: list(Updated.t(state)),
1111
redo_stack: list(Updated.t(state)),
12-
future_log: list((float, Page.Update.t)),
13-
replay_toggle: bool,
1412
};
1513

1614
let equal = (===);
@@ -19,8 +17,6 @@ module Model = {
1917
current: Page.Store.load(),
2018
undo_stack: [],
2119
redo_stack: [],
22-
future_log: [],
23-
replay_toggle: false,
2420
};
2521
};
2622

@@ -30,15 +26,6 @@ module Update = {
3026
[@deriving (show({with_path: false}), sexp, yojson)]
3127
type t = Page.Update.t;
3228

33-
// let sexp = Page.Update.sexp_of_t(action);
34-
// For now, we don't ignore any actions; add here if needed
35-
// check if str contains "(Select (Term (Id" (ignoring whitespace)
36-
// let str = Sexplib.Sexp.to_string(sexp);
37-
// StringUtil.match(StringUtil.regexp("Select\\s*\\(Term\\s*\\(Id"), str);
38-
let ignore_if_action_fails_in_log_replay = (_action: t): bool => {
39-
false;
40-
};
41-
4229
[@deriving (show({with_path: false}), sexp, yojson)]
4330
let update =
4431
(
@@ -55,13 +42,10 @@ module Update = {
5542
| [] =>
5643
print_endline("Cannot undo");
5744
model |> Updated.raise_invalid_action;
58-
| [x, ...rest] =>
59-
Log.Entry.save(Log.Entry.mk(action));
60-
{
45+
| [x, ...rest] => {
6146
...x,
6247
model: {
6348
current: x.model,
64-
future_log: model.future_log,
6549
undo_stack: rest,
6650
redo_stack: [
6751
{
@@ -70,22 +54,18 @@ module Update = {
7054
},
7155
...model.redo_stack,
7256
],
73-
replay_toggle: model.replay_toggle,
7457
},
75-
};
58+
}
7659
}
7760
| Globals(Redo) =>
7861
switch (model.redo_stack) {
7962
| [] =>
8063
print_endline("Cannot redo");
8164
model |> Updated.raise_invalid_action;
82-
| [x, ...rest] =>
83-
Log.Entry.save(Log.Entry.mk(action));
84-
{
65+
| [x, ...rest] => {
8566
...x,
8667
model: {
8768
current: x.model,
88-
future_log: model.future_log,
8969
undo_stack: [
9070
{
9171
...x,
@@ -94,135 +74,8 @@ module Update = {
9474
...model.undo_stack,
9575
],
9676
redo_stack: rest,
97-
replay_toggle: model.replay_toggle,
9877
},
99-
};
100-
}
101-
| Globals(Log(a)) =>
102-
switch (a) {
103-
| InitImport(f) =>
104-
JsUtil.read_file(f, data =>
105-
schedule_action(Globals(Log(FinishImport(data))))
106-
);
107-
model |> Updated.return_quiet;
108-
| FinishImport(None) =>
109-
LogSidebar.log_error("Log import failed");
110-
model |> Updated.return_quiet;
111-
| FinishImport(Some(data)) =>
112-
let of_data = (data: string): list((float, Page.Update.t)) =>
113-
Export.import_just_log(data)
114-
|> Sexplib.Sexp.of_string
115-
|> Log.Entry.s_of_sexp_opt
116-
|> List.filter_map(x => x);
117-
let actions =
118-
data
119-
|> of_data
120-
|> Log.flatten_imports(~of_data)
121-
|> (
122-
x => {
123-
LogSidebar.log_info(
124-
"Imported log entries: " ++ string_of_int(List.length(x)),
125-
);
126-
x;
127-
}
128-
);
129-
{
130-
...model,
131-
future_log: model.future_log @ actions,
132-
}
133-
|> Updated.return_quiet;
134-
| NextLog =>
135-
switch (model.future_log) {
136-
| [] =>
137-
LogSidebar.log_info("No next log action to perform");
138-
model |> Updated.return_quiet;
139-
| [(t, next), ...rest] =>
140-
LogSidebar.log_action(
141-
"Applying next log action",
142-
Some(JsUtil.print_timestamp(t)),
143-
);
144-
// Keep full action expression in console for detailed debugging
145-
print_endline("Full action: " ++ Page.Update.show(next));
146-
try({
147-
let updated =
148-
Page.Update.update(
149-
~import_log,
150-
~get_log_and,
151-
~schedule_action,
152-
next,
153-
model.current,
154-
);
155-
{
156-
...updated,
157-
model: {
158-
current: updated.model,
159-
undo_stack: [
160-
{
161-
...updated,
162-
model: model.current,
163-
},
164-
...model.undo_stack,
165-
],
166-
redo_stack: model.redo_stack,
167-
future_log: rest,
168-
replay_toggle: model.replay_toggle,
169-
},
170-
};
171-
}) {
172-
| _ =>
173-
LogSidebar.log_error("Failed to apply log action");
174-
Model.{
175-
...model,
176-
future_log:
177-
ignore_if_action_fails_in_log_replay(next)
178-
? rest : model.future_log,
179-
replay_toggle:
180-
ignore_if_action_fails_in_log_replay(next)
181-
? model.replay_toggle : false,
182-
}
183-
|> Updated.return_quiet;
184-
};
185-
}
186-
| SkipLog =>
187-
LogSidebar.log_action("Skipping the next log entry", None);
188-
switch (model.future_log) {
189-
| [] =>
190-
LogSidebar.log_info("No log entry to skip");
191-
model |> return_quiet;
192-
| [(_, _), ...rest] =>
193-
{
194-
...model,
195-
future_log: rest,
196-
}
197-
|> return_quiet
198-
};
199-
| SkipExercise =>
200-
LogSidebar.log_action(
201-
"Skipping to the next exercise in the log",
202-
None,
203-
);
204-
let rec skip_to_next_exercise = (log: list((float, Page.Update.t))) =>
205-
switch (log) {
206-
| [] => []
207-
| [(_, Editors(SwitchMode(_))), ..._] as rest => rest
208-
| [(_, Editors(Exercises(SwitchExercise(_)))), ..._] as rest => rest
209-
| [(_, _), ...rest] => skip_to_next_exercise(rest)
210-
};
211-
{
212-
...model,
213-
future_log: skip_to_next_exercise(model.future_log),
214-
}
215-
|> return_quiet;
216-
| ToggleReplay =>
217-
Model.{
218-
...model,
219-
replay_toggle: !model.replay_toggle,
22078
}
221-
|> return_quiet
222-
| ClearLog =>
223-
Log.DB.clear_and(() => ());
224-
LogSidebar.log_info("Log cleared");
225-
model |> return_quiet;
22679
}
22780
| action =>
22881
let current =
@@ -233,13 +86,11 @@ module Update = {
23386
action,
23487
model.current,
23588
);
236-
let _ = Log.update(action, current);
23789
if (Page.Update.can_undo(action)) {
23890
{
23991
...current,
24092
model: {
24193
current: current.model,
242-
future_log: model.future_log,
24394
undo_stack: [
24495
{
24596
...current,
@@ -248,7 +99,6 @@ module Update = {
24899
...model.undo_stack,
249100
],
250101
redo_stack: [],
251-
replay_toggle: model.replay_toggle,
252102
},
253103
};
254104
} else {
@@ -258,8 +108,6 @@ module Update = {
258108
current: current.model,
259109
undo_stack: model.undo_stack,
260110
redo_stack: model.redo_stack,
261-
future_log: model.future_log,
262-
replay_toggle: model.replay_toggle,
263111
},
264112
};
265113
};
@@ -278,8 +126,6 @@ module Update = {
278126
|> Page.Update.calculate(~schedule_action, ~is_edited, ~dynamics),
279127
undo_stack: model.undo_stack,
280128
redo_stack: model.redo_stack,
281-
future_log: model.future_log,
282-
replay_toggle: model.replay_toggle,
283129
};
284130
};
285131

0 commit comments

Comments
 (0)