Race in common usage of Document.save, document.encodeChangesSince, and Document.heads?
jessegrosjean opened this issue · comments
Often I have a pattern like this:
let changes = try document.encodeChangesSince(heads: lastSavedHeads)
lastSavedHeads = document.heads()
But I think it's possible that the document will be modified between those two calls, so the heads that I'm saving might be newer than the ones used to encode the changes. And data will be lost.
To solve I think both document.save and document.encodeChangesSince should return both the data and the heads used to create that data. I guess something like this:
struct Changes {
from: Set<ChangeHash>?
to: Set<ChangeHash>
data: Data
}
I'm not sure if from
is strictly needed in API, but maybe convenient to include? Anyway to
is what is needed I think.
Hum... I guess a simple fix is to just store the heads first. Then there is still the potential that you'll be saving extra data, but that won't cause any errors or inconsistencies. I think that's probably good enough for my use.