Skip to content

Commit 6338fc2

Browse files
committed
Clean-up transaction begin
Move it into a dedicated function instead of being part of the `Transaction` constructor.
1 parent eec9e45 commit 6338fc2

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/v1/session.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,13 @@ class Session {
110110
connectionHolder.initializeConnection();
111111
this._hasTx = true;
112112

113-
const onTxClose = () => this._hasTx = false;
114-
return new Transaction(connectionHolder, onTxClose.bind(this), this._lastBookmark, this._updateBookmark.bind(this));
113+
const tx = new Transaction(connectionHolder, this._transactionClosed.bind(this), this._updateBookmark.bind(this));
114+
tx._begin(this._lastBookmark);
115+
return tx;
116+
}
117+
118+
_transactionClosed() {
119+
this._hasTx = false;
115120
}
116121

117122
/**

src/v1/transaction.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,21 @@ class Transaction {
3232
* @constructor
3333
* @param {ConnectionHolder} connectionHolder - the connection holder to get connection from.
3434
* @param {function()} onClose - Function to be called when transaction is committed or rolled back.
35-
* @param {Bookmark} bookmark bookmark for transaction begin.
3635
* @param {function(bookmark: Bookmark)} onBookmark callback invoked when new bookmark is produced.
3736
*/
38-
constructor(connectionHolder, onClose, bookmark, onBookmark) {
37+
constructor(connectionHolder, onClose, onBookmark) {
3938
this._connectionHolder = connectionHolder;
39+
this._state = _states.ACTIVE;
40+
this._onClose = onClose;
41+
this._onBookmark = onBookmark;
42+
}
43+
44+
_begin(bookmark) {
4045
const streamObserver = new _TransactionStreamObserver(this);
4146

4247
this._connectionHolder.getConnection(streamObserver)
4348
.then(conn => conn.protocol().beginTransaction(bookmark, streamObserver))
4449
.catch(error => streamObserver.onError(error));
45-
46-
this._state = _states.ACTIVE;
47-
this._onClose = onClose;
48-
this._onBookmark = onBookmark;
4950
}
5051

5152
/**

0 commit comments

Comments
 (0)