Skip to content

Commit 1eb0228

Browse files
committed
#42 add possibility to call removeTimeout after getTimeoutSagas and getOlderSagas
1 parent 00ecb84 commit 1eb0228

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ $ cat .gitignore
1212
node_modules
1313
node_modules/**/*
1414

15+
package-lock.json
16+
1517
.idea
1618

1719
.DS_Store
1820

1921
*.tingo
2022
*.db
2123

22-
dump.rdb
24+
dump.rdb

lib/pm.js

+18
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,12 @@ _.extend(ProcessManagement.prototype, {
479479
calledAddCommandToSend = true;
480480
sagaModel.addUnsentCommand(cmd);
481481
};
482+
var calledRemoveTimeout = false;
483+
var orgRemoveTimeout = sagaModel.removeTimeout;
484+
sagaModel.removeTimeout = function () {
485+
calledRemoveTimeout = true;
486+
orgRemoveTimeout.bind(sagaModel)();
487+
};
482488
sagaModel.commit = function (clb) {
483489
var cmds = calledAddCommandToSend ? sagaModel.getUndispatchedCommands() : [];
484490

@@ -537,6 +543,9 @@ _.extend(ProcessManagement.prototype, {
537543
return { id: dotty.get(c, self.definitions.command.id), payload: c };
538544
});
539545
self.sagaStore.save(sagaModel.toJSON(), undispCmds, onAfterCommit);
546+
} else if (calledRemoveTimeout) {
547+
sagaModel.setCommitStamp(new Date());
548+
self.sagaStore.save(sagaModel.toJSON(), [], onAfterCommit);
540549
} else {
541550
var err = new Error('Use commit only to remove a saga or to addCommandToSend!');
542551
debug(err);
@@ -571,9 +580,18 @@ _.extend(ProcessManagement.prototype, {
571580
var sagaModel = new SagaModel(s.id);
572581
sagaModel.set(s);
573582
sagaModel.actionOnCommit = 'update';
583+
var calledRemoveTimeout = false;
584+
var orgRemoveTimeout = sagaModel.removeTimeout;
585+
sagaModel.removeTimeout = function () {
586+
calledRemoveTimeout = true;
587+
orgRemoveTimeout.bind(sagaModel)();
588+
};
574589
sagaModel.commit = function (clb) {
575590
if (sagaModel.isDestroyed()) {
576591
self.removeSaga(sagaModel, clb);
592+
} else if (calledRemoveTimeout) {
593+
sagaModel.setCommitStamp(new Date());
594+
self.sagaStore.save(sagaModel.toJSON(), [], clb);
577595
} else {
578596
var err = new Error('Use commit only to remove a saga!');
579597
debug(err);

releasenotes.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## [v1.9.1](https://github.com/adrai/node-cqrs-saga/compare/v1.9.0...v1.9.1)
2+
- add possibility to call removeTimeout after getTimeoutSagas and getOlderSagas [#42](https://github.com/adrai/node-cqrs-saga/issues/#42) thanks to [edro](https://github.com/edro)
3+
14
## [v1.9.0](https://github.com/adrai/node-cqrs-saga/compare/v1.8.0...v1.9.0)
25
- optional pagination for getTimeoutedSagas and getUndispatchedCommands
36

test/unit/pmTest.js

+30-2
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,24 @@ describe('power management', function () {
11321132
expect(cmd.commitStamp).to.be.ok();
11331133
expect(cmd.command.id).to.eql('newId');
11341134
expect(cmd.command.name).to.eql('newCommand');
1135-
done();
1135+
1136+
pm.getTimeoutedSagas(function (err, sagas) {
1137+
expect(err).not.to.be.ok();
1138+
expect(sagas).to.be.an('array');
1139+
expect(sagas.length).to.eql(1);
1140+
1141+
sagas[0].removeTimeout();
1142+
sagas[0].commit(function (err) {
1143+
expect(err).not.to.be.ok();
1144+
pm.getTimeoutedSagas(function (err, sagas) {
1145+
expect(err).not.to.be.ok();
1146+
expect(sagas).to.be.an('array');
1147+
expect(sagas.length).to.eql(0);
1148+
done();
1149+
});
1150+
});
1151+
});
1152+
11361153
});
11371154
});
11381155
});
@@ -1296,7 +1313,18 @@ describe('power management', function () {
12961313
expect(sagas[0].getTimeoutAt().getTime()).to.eql(saga2._timeoutAt.getTime());
12971314
expect(sagas[0].get('data')).to.eql(saga2.data);
12981315

1299-
done();
1316+
sagas[0].removeTimeout();
1317+
sagas[0].commit(function (err) {
1318+
expect(err).not.to.be.ok();
1319+
1320+
pm.getOlderSagas(new Date(2014, 3, 3), function (err, sagas) {
1321+
expect(err).not.to.be.ok();
1322+
expect(sagas).to.be.an('array');
1323+
expect(sagas.length).to.eql(0);
1324+
1325+
done();
1326+
});
1327+
});
13001328
});
13011329
});
13021330
});

0 commit comments

Comments
 (0)