Skip to content

Commit

Permalink
Merge pull request #37 from brinkmanlab/race-condition
Browse files Browse the repository at this point in the history
Race condition
  • Loading branch information
kiaking authored Jul 8, 2019
2 parents 2ed5dcd + dd2209d commit d793bf4
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vuex-orm/plugin-axios",
"version": "0.5.0",
"version": "0.5.1",
"description": "Vuex-ORM Plugin to sync the data against a RESTful API.",
"main": "dist/index.js",
"scripts": {
Expand Down
10 changes: 6 additions & 4 deletions src/actions/Create.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ export default class Create extends Action {
const request = axios[method](endpoint, params.data);

this.onRequest(commit);
request
.then(data => this.onSuccess(commit, model, data))
.catch(error => this.onError(commit, error))
try {
await this.onSuccess(commit, model, await request);
} catch(error) {
this.onError(commit, error);
}

return request;
}
Expand All @@ -44,7 +46,7 @@ export default class Create extends Action {
*/
static onSuccess(commit, model, data) {
commit('onSuccess')
model.insertOrUpdate({
return model.insertOrUpdate({
data,
});
}
Expand Down
16 changes: 9 additions & 7 deletions src/actions/Delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ export default class Delete extends Action {
const method = Action.getMethod('$delete', model, 'delete');
const request = axios[method](endpoint);

this.onRequest(model, params);
request
.then(data => this.onSuccess(model, params, data))
.catch(error => this.onError(model, params, error))
await this.onRequest(model, params);
try {
await this.onSuccess(model, params, await request);
} catch(error) {
await this.onError(model, params, error);
}

return request;
}
Expand All @@ -30,7 +32,7 @@ export default class Delete extends Action {
* @param {object} params
*/
static onRequest(model, params) {
model.update({
return model.update({
where: params.params.id,
data: {
$isDeleting: true,
Expand All @@ -46,7 +48,7 @@ export default class Delete extends Action {
* @param {object} data
*/
static onSuccess(model, params, data) {
model.delete({
return model.delete({
where: params.params.id || data.id,
})
}
Expand All @@ -58,7 +60,7 @@ export default class Delete extends Action {
* @param {object} error
*/
static onError(model, params, error) {
model.update({
return model.update({
where: params.params.id,
data: {
$isDeleting: false,
Expand Down
10 changes: 6 additions & 4 deletions src/actions/Fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ export default class Fetch extends Action {
const request = axios[method](endpoint);

this.onRequest(commit);
request
.then(data => this.onSuccess(commit, model, data))
.catch(error => this.onError(commit, error))
try {
await this.onSuccess(commit, model, await request);
} catch(error) {
this.onError(commit, error);
}

return request;
}
Expand All @@ -40,7 +42,7 @@ export default class Fetch extends Action {
*/
static onSuccess(commit, model, data) {
commit('onSuccess')
model.insertOrUpdate({
return model.insertOrUpdate({
data,
});
}
Expand Down
10 changes: 6 additions & 4 deletions src/actions/Get.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ export default class Get extends Action {
const request = axios[method](endpoint);

this.onRequest(commit);
request
.then(data => this.onSuccess(commit, model, data))
.catch(error => this.onError(commit, error))
try {
await this.onSuccess(commit, model, await request);
} catch(error) {
this.onError(commit, error);
}

return request;
}
Expand All @@ -40,7 +42,7 @@ export default class Get extends Action {
*/
static onSuccess(commit, model, data) {
commit('onSuccess')
model.insertOrUpdate({
return model.insertOrUpdate({
data,
});
}
Expand Down
16 changes: 9 additions & 7 deletions src/actions/Update.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ export default class Update extends Action {
const method = Action.getMethod('$update', model, 'put');
const request = axios[method](endpoint, params.data);

this.onRequest(model, params);
request
.then(data => this.onSuccess(model, params, data))
.catch(error => this.onError(model, params, error))
await this.onRequest(model, params);
try {
await this.onSuccess(model, params, await request);
} catch(error) {
await this.onError(model, params, error);
}

return request;
}
Expand All @@ -35,7 +37,7 @@ export default class Update extends Action {
* @param {object} params
*/
static onRequest(model, params) {
model.update({
return model.update({
where: params.params.id,
data: {
$isUpdating: true,
Expand All @@ -51,7 +53,7 @@ export default class Update extends Action {
* @param {object} data
*/
static onSuccess(model, params, data) {
model.update({
return model.update({
where: params.params.id || data.id,
data: merge({}, data, {
$isUpdating: false,
Expand All @@ -67,7 +69,7 @@ export default class Update extends Action {
* @param {object} error
*/
static onError(model, params, error) {
model.update({
return model.update({
where: params.params.id,
data: {
$isUpdating: false,
Expand Down

0 comments on commit d793bf4

Please sign in to comment.