Skip to content

Commit 19fc554

Browse files
committed
* improved Scratch lookupAll example a little
1 parent 57beeef commit 19fc554

1 file changed

Lines changed: 38 additions & 6 deletions

File tree

src/Demo/Scratch/LookupAll.fs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,22 +209,40 @@ let example() =
209209

210210
// finally throw away the ids and just keep the relevant data
211211
|> AList.map (fun ((_,_,date), va, vb) -> va, vb, date)
212-
212+
213213
let reader = result.GetReader()
214214

215215
let datestr (d : DateTime) = sprintf "%04d-%02d-%02d" d.Year d.Month d.Day
216216

217217
let print() =
218-
let oldState = reader.State
218+
219+
let oldState = reader.State // fetch the state before updating
219220
let changes = reader.GetChanges AdaptiveToken.Top
221+
let _newState = reader.State // fetch the state after updating
220222

223+
// print the changes in a human-readable way
221224
for index, op in changes do
222225
match op with
223226
| Set (person, country, date) ->
224227
match IndexList.tryGet index oldState with
225228
| Some(op, oc, od) ->
226-
printfn " changed %s %s's visit to %s on %s" op.Name op.Surname oc.Name (datestr od)
227-
printfn " => %s %s visited %s on %s" person.Name person.Surname country.Name (datestr date)
229+
let personChanged = op <> person
230+
let countryChanged = oc <> country
231+
let dateChanged = od <> date
232+
233+
if personChanged then
234+
if countryChanged then
235+
printfn " deleted %s %s's visit to %s on %s" op.Name op.Surname oc.Name (datestr od)
236+
printfn " %s %s added a visit to %s on %s" person.Name person.Surname country.Name (datestr date)
237+
else
238+
printfn " %s %s's name changed to %s %s for the visit to %s on %s" op.Name op.Surname person.Name person.Surname country.Name (datestr od)
239+
elif dateChanged && countryChanged then
240+
printfn " %s %s's visit to %s on %s changed to %s on %s" person.Name person.Surname oc.Name (datestr od) country.Name (datestr date)
241+
elif dateChanged then
242+
printfn " %s %s's visit to %s changed from %s to %s" person.Name person.Surname country.Name (datestr od) (datestr date)
243+
elif countryChanged then
244+
printfn " %s %s's visit on %s changed from %s to %s" person.Name person.Surname (datestr date) oc.Name country.Name
245+
228246
| None ->
229247
printfn " %s %s added a visit to %s on %s" person.Name person.Surname country.Name (datestr date)
230248
| Remove ->
@@ -234,15 +252,29 @@ let example() =
234252
| None ->
235253
()
236254

255+
256+
237257
printfn "initial"
238258
print()
239259

240-
transact (fun () -> visited.Append (2,11, DateTime(2022, 1, 1)) |> ignore)
241260
printfn "links.Append (2,11,2022-01-01)"
261+
transact (fun () -> visited.Append (2,11, DateTime(2022, 1, 1)) |> ignore)
242262
print()
243263

244-
transact (fun () -> persons.[2] <- { Name = "Johanna"; Surname = "Huber" })
245264
printfn "Johanna Maier -> Johanna Huber"
265+
transact (fun () -> persons.[2] <- { Name = "Johanna"; Surname = "Huber" })
266+
print()
267+
268+
printfn "India -> Brazil"
269+
transact (fun () -> visited.[2] <- (1, 6, DateTime(2018, 7, 2)))
270+
print()
271+
272+
printfn "2020-03-04 -> 2012-03-04"
273+
transact (fun () -> visited.[3] <- (2, 9, DateTime(2012, 3, 4)))
274+
print()
275+
276+
printfn "delete(Johanna, Argentina)"
277+
transact (fun () -> visited.RemoveAt 4 |> ignore)
246278
print()
247279

248280
printfn "final"

0 commit comments

Comments
 (0)