Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit 163e5e0

Browse files
FABN-929: Allow function chaining on Transaction
Return this from Transaction.setEventHandlerStrategy() and Transaction.setTransient() to allow chaining of calls, for example: await contract.createTransaction('name').setTransient(data).submit(); Change-Id: I859f452a9c902ce7de63bdd6e647eaf1edb22d08 Signed-off-by: Mark S. Lewis <mark_lewis@uk.ibm.com>
1 parent 0713ad6 commit 163e5e0

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

fabric-network/lib/transaction.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,11 @@ class Transaction {
7373
* Set the event handler strategy to be used for this transaction invocation
7474
* instead of the default handler configured in the gateway options.
7575
* @param {Function} factoryFunction Event handler factory function.
76+
* @returns {Transaction} This object, to allow function chaining.
7677
*/
7778
setEventHandlerStrategy(factoryFunction) {
7879
this._createTxEventHandler = factoryFunction;
80+
return this;
7981
}
8082

8183
/**
@@ -84,9 +86,11 @@ class Transaction {
8486
* private data to a transaction function.
8587
* @param {Object} transientMap Object with String property names and
8688
* Buffer property values.
89+
* @returns {Transaction} This object, to allow function chaining.
8790
*/
8891
setTransient(transientMap) {
8992
this._transientMap = transientMap;
93+
return this;
9094
}
9195

9296
/**

fabric-network/test/transaction.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,26 @@ describe('Transaction', () => {
6060
});
6161
});
6262

63+
describe('#setEventHandlerStrategy', () => {
64+
it('returns this', () => {
65+
const transaction = new Transaction(stubContract, 'name');
66+
const stubEventHandler = sinon.createStubInstance(TransactionEventHandler);
67+
const stubEventHandlerFactoryFn = () => stubEventHandler;
68+
69+
const result = transaction.setEventHandlerStrategy(stubEventHandlerFactoryFn);
70+
71+
expect(result).to.equal(transaction);
72+
});
73+
});
74+
75+
describe('#setTransient', () => {
76+
it('returns this', () => {
77+
const transaction = new Transaction(stubContract, 'name');
78+
const result = transaction.setTransient(new Map());
79+
expect(result).to.equal(transaction);
80+
});
81+
});
82+
6383
describe('#submit', () => {
6484
const transactionName = 'TRANSACTION_NAME';
6585
const expectedResult = Buffer.from('42');
@@ -182,8 +202,7 @@ describe('Transaction', () => {
182202
const stubEventHandlerFactoryFn = sinon.stub();
183203
stubEventHandlerFactoryFn.withArgs(txId, network, options).returns(stubEventHandler);
184204

185-
transaction.setEventHandlerStrategy(stubEventHandlerFactoryFn);
186-
await transaction.submit();
205+
await transaction.setEventHandlerStrategy(stubEventHandlerFactoryFn).submit();
187206

188207
sinon.assert.called(stubEventHandler.startListening);
189208
sinon.assert.called(stubEventHandler.waitForEvents);
@@ -193,8 +212,7 @@ describe('Transaction', () => {
193212
const transientMap = {key1: 'value1', key2: 'value2'};
194213
expectedProposal.transientMap = transientMap;
195214

196-
transaction.setTransient(transientMap);
197-
await transaction.submit();
215+
await transaction.setTransient(transientMap).submit();
198216

199217
sinon.assert.calledWith(channel.sendTransactionProposal, sinon.match(expectedProposal));
200218
});

test/integration/network-e2e/invoke.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,8 +642,7 @@ test('\n\n***** Network End-to-end flow: invoke transaction with transient data
642642
key1: Buffer.from('value1'),
643643
key2: Buffer.from('value2')
644644
};
645-
transaction.setTransient(transientMap);
646-
const response = await transaction.submit();
645+
const response = await transaction.setTransient(transientMap).submit();
647646
t.pass('Got response: ' + response.toString('utf8'));
648647
const result = JSON.parse(response.toString('utf8'));
649648

test/integration/network-e2e/query.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ test('\n\n***** Network End-to-end flow: evaluate transaction with transient dat
146146
key1: Buffer.from('value1'),
147147
key2: Buffer.from('value2')
148148
};
149-
transaction.setTransient(transientMap);
150-
const response = await transaction.evaluate();
149+
const response = await transaction.setTransient(transientMap).evaluate();
151150

152151
t.pass('Got response: ' + response.toString('utf8'));
153152
const result = JSON.parse(response.toString('utf8'));

0 commit comments

Comments
 (0)