mirror of
https://github.com/appy-one/acebase.git
synced 2026-05-25 06:02:14 -06:00
[GH-ISSUE #23] Mixing update() etc. with a live data proxy? #21
Labels
No labels
IndexedDB
browser
bug
dependencies
documentation
duplicate
enhancement
feature request
indexes
indexes
invalid
pull-request
query
question
transaction logging
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference: github-starred/acebase#21
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @clibu on GitHub (Mar 21, 2021).
Original GitHub issue: https://github.com/appy-one/acebase/issues/23
Originally assigned to: @appy-one on GitHub.
From what I'm seeing if you call
update()on a path that has a Proxy then the proxy isn't aware of the update changes.Assuming this isn't a bug and is too difficult to resolve that's fine, however the doc's need to boldly point out that you can't mix the two approaches.
@appy-one commented on GitHub (Mar 22, 2021):
They should mix perfectly fine, all db changes must update the proxy value
@clibu commented on GitHub (Mar 22, 2021):
Good to hear. I'll try and replicate what I was seeing in a small sample app. Might be a day or so.
@clibu commented on GitHub (Mar 23, 2021):
Ok my description in the first post wasn't right. The issue is to do with
onMutation()callback calls.I wrote small Node program and tried various things. The issue I am able to reproduce in the Browser, but not in the Node code below is this.
Sample 1 - doesn't show the problem, just describes it
Sample 2 - delete db if present and run this once. When run again you see behaviour as described.
@clibu commented on GitHub (Mar 23, 2021):
Small update. In the second example with
test1 = falseand:logs both Jaguar and Porsce.
@appy-one commented on GitHub (Mar 23, 2021):
Thanks, I'll check!
@appy-one commented on GitHub (Mar 23, 2021):
It took some time to investigate, but this is a classic race condition going on here!
Simplifying the code, making it easier to reproduce and analyze the sequence of execution:
When the code runs the first time:
test/carin database isnull(doesn't exist)obj.car === undefinedref.update: database is updated, mutation events are prepared and scheduled to fire in next tick.obj.car = 'Porsche': proxy schedules a database update in next tick."test/car was updated to Jaguar through ref, previous value was: null"onMutationcallback fires and prints"test/car was updated to Porsche through proxy, previous value was: Jaguar"Then, when running the 2nd time:
test/carin database is'Porsche'obj.car === 'Porsche'ref.update: database is updated, mutation events are prepared and scheduled to fire in next tickobj.car = 'Porsche': proxy sees this is the current value (it has not received mutation notification yet) so does not schedule database update.onMutationcallback fires and prints"test/car was updated to Jaguar through ref, previous value was: Porsche"Running 3rd time:
test/carin database is'Jaguar'obj.car === 'Jaguar'ref.update: database value does not change so no mutation events are prepared.obj.car = 'Porsche': proxy schedules a database update in next tick.onMutationcallback fires and prints"test/car was updated to Porsche through proxy, previous value was: Jaguar"Running again after this will repeat 2nd and 3rd time runs.
In real-world situations this is vitrually impossible to occur, because both updates will have to execute in nearly the same tick, and they both have to 'undo' their respective values. Preventing this will not be possible because of the asynchronous execution of the mutation events. Especially when running over a network connection between an
acebase-clientandacebase-server.Long answer short: mystery solved, it's not a bug!
@clibu commented on GitHub (Mar 23, 2021):
OK, that all seems ok. I appreciate how complex all of this is.
Does this also explain the
test1 = falsecase without thesetTimeout()where only one mutation callback fires?@appy-one commented on GitHub (Mar 23, 2021):
Yes, doing the following will update the db only once because the target value is marked as changed, and the db is updated in the next tick:
@clibu commented on GitHub (Mar 24, 2021):
Ok, thanks. Maybe a mention in the docs would be prudent. You could reference this issue for more info.😀