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

Commit 65300ef

Browse files
INT-CHORE: Request terminator
1 parent 0ec8bde commit 65300ef

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

src/main/request-terminator.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default class RequestTerminator {
2+
constructor() {
3+
this.requestMap = {};
4+
}
5+
6+
add(requestId, request) {
7+
this.requestMap[requestId] = request;
8+
}
9+
10+
remove(requestId) {
11+
delete this.requestMap[requestId];
12+
}
13+
14+
terminate(requestId) {
15+
if (this.requestMap[requestId]) {
16+
this.requestMap[requestId].abort();
17+
}
18+
}
19+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import sinon from 'sinon';
2+
import RequestTerminator from '../../../../src/main/request-terminator';
3+
4+
describe('RequestTerminator', () => {
5+
let subject;
6+
7+
beforeEach(() => {
8+
subject = new RequestTerminator();
9+
});
10+
11+
describe('#terminate', () => {
12+
it('should call abort on the belonging request', () => {
13+
const request = { abort() {} };
14+
const spy = sinon.spy(request, 'abort');
15+
16+
subject.add('my-unique-request-id', request);
17+
subject.terminate('my-unique-request-id');
18+
19+
assert(spy.calledOnce);
20+
});
21+
22+
context('when the request could not be found', () => {
23+
it('should do nothing', () => {
24+
expect(subject.terminate('unknown-request-id')).to.not.throw; // eslint-disable-line
25+
});
26+
});
27+
});
28+
29+
describe('#remove', () => {
30+
it('should remove the request object from the inventory', () => {
31+
const request = { abort() {} };
32+
const spy = sinon.spy(request, 'abort');
33+
34+
subject.add('my-unique-request-id', request);
35+
subject.remove('my-unique-request-id');
36+
subject.terminate('my-unique-request-id');
37+
38+
assert(spy.notCalled);
39+
});
40+
});
41+
});

0 commit comments

Comments
 (0)