Skip to content
This repository was archived by the owner on Nov 9, 2023. It is now read-only.

Commit 3bccffa

Browse files
author
iantanwx
committed
fix: clean up engine listener
1 parent 320d31f commit 3bccffa

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

src/createEngineStream.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ export default function createEngineStream(opts: EngineStreamOptions): Duplex {
2121
const stream = new Duplex({ objectMode: true, read, write });
2222
// forward notifications
2323
if (engine.on) {
24-
engine.on('notification', (message) => {
25-
stream.push(message);
26-
});
24+
const onNotification = (message: unknown) => stream.push(message);
25+
engine.on('notification', onNotification);
26+
stream.on('end', () => engine.off('notification', onNotification));
2727
}
2828
return stream;
2929

test/index.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const { JsonRpcEngine } = require('json-rpc-engine');
33
const { createStreamMiddleware, createEngineStream } = require('../dist');
44

55
test('middleware - raw test', (t) => {
6-
76
const jsonRpcConnection = createStreamMiddleware();
87
const req = { id: 1, jsonrpc: '2.0', method: 'test' };
98
const initRes = { id: 1, jsonrpc: '2.0' };
@@ -16,17 +15,21 @@ test('middleware - raw test', (t) => {
1615
});
1716

1817
// run middleware, expect end fn to be called
19-
jsonRpcConnection.middleware(req, initRes, () => {
20-
t.fail('should not call next');
21-
}, (err) => {
22-
t.notOk(err, 'should not error');
23-
t.deepEqual(initRes, res, 'got the expected response');
24-
t.end();
25-
});
18+
jsonRpcConnection.middleware(
19+
req,
20+
initRes,
21+
() => {
22+
t.fail('should not call next');
23+
},
24+
(err) => {
25+
t.notOk(err, 'should not error');
26+
t.deepEqual(initRes, res, 'got the expected response');
27+
t.end();
28+
},
29+
);
2630
});
2731

2832
test('engine to stream - raw test', (t) => {
29-
3033
const engine = new JsonRpcEngine();
3134
engine.push((_req, res, _next, end) => {
3235
res.result = 'test';
@@ -51,7 +54,6 @@ test('engine to stream - raw test', (t) => {
5154
});
5255

5356
test('middleware and engine to stream', (t) => {
54-
5557
// create guest
5658
const engineA = new JsonRpcEngine();
5759
const jsonRpcConnection = createStreamMiddleware();
@@ -67,9 +69,7 @@ test('middleware and engine to stream', (t) => {
6769
// connect both
6870
const clientSideStream = jsonRpcConnection.stream;
6971
const hostSideStream = createEngineStream({ engine: engineB });
70-
clientSideStream
71-
.pipe(hostSideStream)
72-
.pipe(clientSideStream);
72+
clientSideStream.pipe(hostSideStream).pipe(clientSideStream);
7373

7474
// request and expected result
7575
const req = { id: 1, jsonrpc: '2.0', method: 'test' };
@@ -115,3 +115,14 @@ test('server notification in stream', (t) => {
115115

116116
engine.emit('notification', notif);
117117
});
118+
119+
test('clean up listener', (t) => {
120+
const n = 10;
121+
const engine = new JsonRpcEngine();
122+
for (let i = 0; i < n; i++) {
123+
const stream = createEngineStream({ engine });
124+
stream.end();
125+
}
126+
t.equal(engine.listenerCount(), 0);
127+
t.end();
128+
});

0 commit comments

Comments
 (0)