aboutsummaryrefslogtreecommitdiff
path: root/editor/state
diff options
context:
space:
mode:
Diffstat (limited to 'editor/state')
-rwxr-xr-xeditor/state/storage.ml29
-rwxr-xr-xeditor/state/storage.mli4
2 files changed, 21 insertions, 12 deletions
diff --git a/editor/state/storage.ml b/editor/state/storage.ml
index 1bb8b81..4688881 100755
--- a/editor/state/storage.ml
+++ b/editor/state/storage.ml
@@ -58,20 +58,29 @@ let load'
(** Save the view *)
let save'
- : check:(content Js.t -> bool) -> content Js.t -> Jstr.t -> (bool, Jv.Error.t) result
+ : check:(previous:content Js.t -> update:content Js.t -> bool) -> content Js.t -> Jstr.t -> (bool, Jv.Error.t) result
= fun ~check object_content key ->
- (* First load the content from the storage *)
- match check (load' key) with
- | false ->
- Ok false
- | true ->
+ (* First load the content from the storage. If there is already a note
+ with the same id, send the two notes to the checker to ensure we
+ really need to save it. *)
+ let stored = load' key in
+
+ let process () =
let storage = Brr_io.Storage.local G.window in
let operation = Brr_io.Storage.set_item
storage
key
(Json.encode @@ Jv.Id.to_jv @@ object_content) in
Result.map (fun () -> true) operation
+ in
+
+ Js.Opt.case stored##.date
+ (process)
+ (fun _ -> match check ~previous:stored ~update:object_content with
+ | false -> Ok false
+ | true -> process ()
+ )
(** [load pm schema content f] will try load the content stored in the local
@@ -89,7 +98,7 @@ let load
load' key
let save
- : check:(content Js.t -> bool) -> content Js.t -> Jstr.t option -> (bool, Jv.Error.t) result
+ : check:(previous:content Js.t -> update:content Js.t -> bool) -> content Js.t -> Jstr.t option -> (bool, Jv.Error.t) result
= fun ~check object_content key ->
match key with
| None ->
@@ -159,8 +168,8 @@ let to_json
Brr.Json.encode (Jv.Id.to_jv pages)
let of_json
- : Jstr.t -> (unit, Jv.Error.t) result
- = fun json ->
+ : check:(previous:content Js.t -> update:content Js.t -> bool) -> Jstr.t -> (unit, Jv.Error.t) result
+ = fun ~check json ->
let result = Json.decode json in
Result.map
(fun v ->
@@ -174,7 +183,7 @@ let of_json
end in
ignore @@
save
- ~check:(fun _ -> true)
+ ~check
content
key
))
diff --git a/editor/state/storage.mli b/editor/state/storage.mli
index 4d022d1..cad2982 100755
--- a/editor/state/storage.mli
+++ b/editor/state/storage.mli
@@ -25,7 +25,7 @@ val load
: Jstr.t option -> content Js.t
val save
- : check:(content Js.t -> bool) -> content Js.t -> Jstr.t option -> (bool, Jv.Error.t) result
+ : check:(previous:content Js.t -> update:content Js.t -> bool) -> content Js.t -> Jstr.t option -> (bool, Jv.Error.t) result
(** Remove the page from the storage. *)
val delete
@@ -39,4 +39,4 @@ val to_json
: unit -> Jstr.t
val of_json
- : Jstr.t -> (unit, Jv.Error.t) result
+ : check:(previous:content Js.t -> update:content Js.t -> bool) -> Jstr.t -> (unit, Jv.Error.t) result