Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support calling save multiple times #249

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jacobq
Copy link
Contributor

@jacobq jacobq commented Nov 11, 2019

Cherry-picked from #246

@jacobq
Copy link
Contributor Author

jacobq commented Nov 11, 2019

FWIW trailing whitespace keeps getting trimmed automatically by my IDE because of the .editorconfig settings associated with this project. I know it's not nice to have styling changes mixed in with other changes, but it's a pain to manually undo this, so if someone really cares about this please let me know and I'll submit a separate whitespace / style clean-up PR first.

@jlami
Copy link
Collaborator

jlami commented Nov 12, 2019

Whitespaces are not that bad in the diffs. But is there a way to let Travis check the failing test commit alone? Would be good to see the failing test there before merging the solution. I think cherry picking and force updating made it skip/forget about commit 9bfde5c

@jacobq
Copy link
Contributor Author

jacobq commented Nov 12, 2019

Would be good to see the failing test there before merging the solution.

It is pretty easy to see by running the test locally. There has also been an open issue about it for nearly a year. But sure, I'll push a revert commit (218c8cd) just so you can see it on CI. Error message appears as:

Error: Attempted to handle event becameError on <(subclass of DS.Model):ember1377:E> while in state root.loaded.saved.

@jlami
Copy link
Collaborator

jlami commented Nov 12, 2019

Thanks, this just makes it easier to review without having to run things locally.

@jacobq
Copy link
Contributor Author

jacobq commented Nov 12, 2019

The part of this fix that I really don't like are these 2 lines:
d7bfcc2#diff-8bef95280660598dc2f4437f447292e2R481-R482

It does seem to work, but I suspect there is a cleaner, if not easier, way to do this.

@jlami
Copy link
Collaborator

jlami commented Nov 12, 2019

You could kind of unwrap updateRecord.

var data = this._recordToData(store, type, snapshot);
data.rev = records[Object.keys(records)[0]][0].rev;
return this.get('db').rel.save(this.getRecordTypeName(type), data);

But that still introduces double code and keeps the Object.keys part. And you would have to handle this._init before that point. Maybe the Object.keys kan be replaced with some serializer code, but I'm not sure. But at least Object.keys should be stable across ember-data versions. The __attributes part I'm more worried about.

You could make a new version of updateRecord that takes the snapshotted data which we can update the rev for to prevent the double code.

@jacobq
Copy link
Contributor Author

jacobq commented Nov 12, 2019

Another way to do it is to use store.peekRecord(...) then .setProperties(...) and .save() though I am not sure if that is any better.

@jlami
Copy link
Collaborator

jlami commented Nov 12, 2019

Hmmm, re-entering the save of ember-data itself feels really dangerous, and might even infinite loop?

@jacobq
Copy link
Contributor Author

jacobq commented Nov 12, 2019

might even infinite loop?

The if that checks if changes need to be applied should prevent that.

@jacobq jacobq changed the title WIP: fix multiple save fix: support calling save multiple times Jan 7, 2020
This commit changes how ember-pouch implements `Adapter.createRecord`,
which is invoked after calling `.save()` on a new record,
so that it does not create multiple records if `.save()` is called
more than once before the first operation has finished.

If `.save()` is only invoked once before the record has finished persisting
to the DB (i.e. the promise that it returns has resolved) then
the behavior is unchanged. However, subsequent calls will wait for
the previously returned promise to resolve and then, if changes have
been made to the record, as indicated by Snapshot#changedAttributes,
it will delegate the task to `updateRecord`.

To avoid a problem caused by ember-data changing the ID associated
with the internalModel/record when the record has finished persisting,
the `Adapter.generateIdForRecord` method has been implemented so
that the ID is available immediately. Previously ember-pouch had still
been generating this id during `createRecord`, but ember-data was
not being made aware of this until its returned promise resolved.

Also, rather than rely on `adapter.db.rel.uuid()` to generate
an RFC4122 v4 UUID (requiring initialization to have completed),
this has been replaced by the equivalent `uuid` module from npm,
and the ember-auto-import addon has been installed to make it
easy to access this from within ember-pouch.

Finally, the `engines` section of package.json has been updated
to align with ember-auto-import's minimum version of 6.x
BREAKING CHANGE: drop node 4.x support (and 6.x/7.x not tested by CI)
@jacobq
Copy link
Contributor Author

jacobq commented Jan 7, 2020

Any objections to merging this? It seems to work, and I'm not planning to make more changes unless requested.

@backspace
Copy link
Collaborator

Thanks for all your work on this, @jacobq, I’m sorry it’s gone stale. I’m updating an old application and have run into some of these errors. For now I’ve been sticking with an old Ember Data but eventually I’ll face updating that and will be checking out your proposed solution, as my initial experiment produced a huge number of inFlight errors in the test suite.

@jacobq
Copy link
Contributor Author

jacobq commented Jan 17, 2023

Thanks for all your work on this, @jacobq, I’m sorry it’s gone stale. I’m updating an old application and have run into some of these errors. For now I’ve been sticking with an old Ember Data but eventually I’ll face updating that and will be checking out your proposed solution, as my initial experiment produced a huge number of inFlight errors in the test suite.

You're welcome & no worries! FWIW, I feel like my FOSS life has entirely gone stale -- been overwhelmed by other aspects of life for the past couple years. Good luck & thanks for picking this up again. I ended up using ember-indexeddb instead, IIRC, and it did the job. Pouch seems like a cool concept though, and if I hadn't run into problems I didn't fully understand I probably would've kept using it.

backspace added a commit that referenced this pull request Feb 2, 2023
This is adapted from #249, I’m just pushing to see if it
fixes the problems in the application I’m updating.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants