Skip to content

Commit 6b63b7d

Browse files
committed
fixup! fixup! Offer better protections against solution overwriting
Signed-off-by: Yann Regis-Gianas <[email protected]>
1 parent d90944f commit 6b63b7d

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

src/ace-lib/ace.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ let set_synchronized_status editor status =
8686
List.iter (fun obs -> obs status) editor.sync_observers;
8787
editor.synchronized <- status
8888

89+
let focus { editor } = editor##focus
90+
8991
let create_editor editor_div check_valid_state =
9092
let editor = edit editor_div in
9193
Js.Unsafe.set editor "$blockScrolling" (Js.Unsafe.variable "Infinity");
@@ -100,7 +102,7 @@ let create_editor editor_div check_valid_state =
100102
editor##.customData := (data, None);
101103
editor##setOption (Js.string "displayIndentGuides") (Js.bool false);
102104
editor##on (Js.string "change") (fun () ->
103-
check_valid_state (set_contents data) ;
105+
check_valid_state (set_contents data) (fun () -> focus data);
104106
set_synchronized_status data false);
105107
data
106108

@@ -192,7 +194,6 @@ let clear_marks editor =
192194
let record_event_handler editor event handler =
193195
editor.editor##(on (Js.string event) handler)
194196

195-
let focus { editor } = editor##focus
196197
let resize { editor } force = editor##(resize (Js.bool force))
197198

198199
let get_keybinding_menu e =

src/ace-lib/ace.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ type loc = {
1717
loc_end: int * int;
1818
}
1919

20-
val create_editor: Dom_html.divElement Js.t -> ((string -> unit) -> unit) -> 'a editor
20+
val create_editor: Dom_html.divElement Js.t
21+
-> ((string -> unit) -> (unit -> unit) -> unit) -> 'a editor
2122

2223
val is_synchronized : 'a editor -> bool
2324

src/ace-lib/ocaml_mode.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ type error = msg list
2424

2525
type warning = error
2626

27-
val create_ocaml_editor: Dom_html.divElement Js.t -> ((string -> unit) -> unit) -> editor
27+
val create_ocaml_editor:
28+
Dom_html.divElement Js.t -> ((string -> unit) -> (unit -> unit) -> unit) -> editor
2829
val get_editor: editor -> editor Ace.editor
2930

3031
val report_error: editor -> ?set_class: bool -> error option -> warning list -> unit Lwt.t

src/app/learnocaml_common.ml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ let is_synchronized_with_server () = !is_synchronized_with_server_callback ()
730730
731731
let check_valid_editor_state id =
732732
let last_changed = ref (Unix.gettimeofday ()) in
733-
fun update_content ->
733+
fun update_content focus_back ->
734734
let update_local_copy checking_time () =
735735
match Learnocaml_local_storage.(retrieve (exercise_state id)) with
736736
| { Answer.mtime; solution; _ } ->
@@ -739,16 +739,17 @@ let check_valid_editor_state id =
739739
if is_synchronized_with_server () then
740740
[
741741
[%i "Fetch from server"],
742-
(fun () -> Lwt.return (update_content solution));
742+
(fun () -> Lwt.return (focus_back (); update_content solution));
743743
[%i "Ignore & keep editing"],
744-
(fun () -> Lwt.return_unit)
744+
(fun () -> Lwt.return (focus_back ()));
745745
]
746746
else
747747
[
748748
[%i "Ignore & keep editing"],
749-
(fun () -> Lwt.return_unit);
749+
(fun () -> Lwt.return (focus_back ()));
750750
[%i "Fetch from server & overwrite"],
751-
(fun () -> Lwt.return (update_content solution));
751+
(fun () ->
752+
Lwt.return (focus_back (); update_content solution));
752753
]
753754
in
754755
lwt_alert ~title:"Question"
@@ -759,7 +760,7 @@ let check_valid_editor_state id =
759760
| exception Not_found -> Lwt.return ()
760761
in
761762
let now = Unix.gettimeofday () in
762-
if now -. !last_changed > 30. then (
763+
if now -. !last_changed > 180. then (
763764
let checking_time = !last_changed in
764765
last_changed := now;
765766
Lwt.async (update_local_copy checking_time)
@@ -771,7 +772,7 @@ let ace_display tab =
771772
let answer =
772773
Ocaml_mode.create_ocaml_editor
773774
(Tyxml_js.To_dom.of_div tab)
774-
ignore
775+
(fun _ _ -> ())
775776
in
776777
let ace = Ocaml_mode.get_editor answer in
777778
Ace.set_font_size ace 16;

0 commit comments

Comments
 (0)