Skip to content

Commit

Permalink
Invert if in hard rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
Dawidpol committed Oct 10, 2023
1 parent b8b7a1f commit 16794f9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
2 changes: 1 addition & 1 deletion docs/api/sharedb-error.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Representation of an error, with a machine-parsable [code](#error-codes).
> This error might be used as part of standard control flow. For example, consumers may define a middleware that validates document structure, and rejects operations that do not conform to this schema using this error code to reset the client to a valid state.
### `ERR_OP_PENDING_OP_SUBMIT_REJECTED`
### `ERR_PENDING_OP_REMOVED_BY_OP_SUBMIT_REJECTED`

> This may happen if server rejected op with ERR_OP_SUBMIT_REJECTED and the type is not invertible or there are some pending ops after the create op was rejected with ERR_OP_SUBMIT_REJECTED
Expand Down
45 changes: 19 additions & 26 deletions lib/client/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1051,39 +1051,32 @@ Doc.prototype._hardRollback = function(err) {
doc.emit('error', fetchError);
}

if (err.code !== ERROR_CODE.ERR_OP_SUBMIT_REJECTED) {
if (inflightOp) pendingOps.push(inflightOp);
var allOpsHadCallbacks = !!pendingOps.length;
for (var i = 0; i < pendingOps.length; i++) {
allOpsHadCallbacks = util.callEach(pendingOps[i].callbacks, err) && allOpsHadCallbacks;
if (err.code === ERROR_CODE.ERR_OP_SUBMIT_REJECTED) {
/**
* Handle special case of ERR_OP_SUBMIT_REJECTED
* This ensures that we resolve the main op callback and reject
* all the pending ops. This is hard rollback so all the pending ops will be
* discarded. This will ensure that the user is at least informed about it.
* more info: https://github.com/share/sharedb/pull/626
*/
if (inflightOp) {
util.callEach(inflightOp.callbacks);
inflightOp = null;
}
if (err && !allOpsHadCallbacks) doc.emit('error', err);
return;
}

/**
* Handle special case of ERR_OP_SUBMIT_REJECTED
* This ensures that we resolve the main op callback and reject
* all the pending ops. This is hard rollback so all the pending ops will be
* discarded. This will ensure that the user is at least informed about it.
* more info: https://github.com/share/sharedb/pull/626
*/
if (inflightOp) {
util.callEach(inflightOp.callbacks);
if (!pendingOps.length) return;
err = new ShareDBError(
ERROR_CODE.ERR_PENDING_OP_REMOVED_BY_OP_SUBMIT_REJECTED,
'Pending op are present when doing rollback of invertible operation'
);
}

if (!pendingOps.length) return;

var hardRollbackError = new ShareDBError(
ERROR_CODE.ERR_PENDING_OP_REMOVED_BY_OP_SUBMIT_REJECTED,
'ending op removed by hard rollback caused by ERR_OP_SUBMIT_REJECTED'
);

if (inflightOp) pendingOps.push(inflightOp);
var allOpsHadCallbacks = !!pendingOps.length;
for (var i = 0; i < pendingOps.length; i++) {
allOpsHadCallbacks = util.callEach(pendingOps[i].callbacks, hardRollbackError) && allOpsHadCallbacks;
allOpsHadCallbacks = util.callEach(pendingOps[i].callbacks, err) && allOpsHadCallbacks;
}
if (!allOpsHadCallbacks) return doc.emit('error', hardRollbackError);
if (err && !allOpsHadCallbacks) doc.emit('error', err);
});
};

Expand Down

0 comments on commit 16794f9

Please sign in to comment.