Skip to content

Commit fd6d109

Browse files
authored
Merge pull request #17 from iceddev/transfer-notifications
support transferables with notifications
2 parents 24f81a1 + f167039 commit fd6d109

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

index.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const transports = require('./transports');
44
function rawr({ transport, timeout = 0, handlers = {}, methods, idGenerator }) {
55
let callId = 0;
66
// eslint-disable-next-line no-param-reassign
7-
methods = methods || handlers; // backwards compat
7+
methods = methods || handlers || {}; // backwards compat
88
const pendingCalls = {};
99
const methodHandlers = {};
1010
const notificationEvents = new EventEmitter();
@@ -111,6 +111,8 @@ function rawr({ transport, timeout = 0, handlers = {}, methods, idGenerator }) {
111111
const testArg = args.pop();
112112
if (testArg && typeof testArg === 'object' && !Array.isArray(testArg)) {
113113
config = testArg;
114+
} else {
115+
args.push(testArg);
114116
}
115117
}
116118
return sendMessage(name, args, config || {});
@@ -132,6 +134,28 @@ function rawr({ transport, timeout = 0, handlers = {}, methods, idGenerator }) {
132134
}
133135
});
134136

137+
const configurableNotifiersProxy = new Proxy({}, {
138+
get: (target, name) => {
139+
return (...args) => {
140+
let config;
141+
if (args.length) {
142+
const testArg = args.pop();
143+
if (testArg && typeof testArg === 'object' && !Array.isArray(testArg)) {
144+
config = testArg;
145+
} else {
146+
args.push(testArg);
147+
}
148+
}
149+
const msg = {
150+
jsonrpc: '2.0',
151+
method: name,
152+
params: args
153+
};
154+
transport.send(msg, config || {});
155+
};
156+
}
157+
});
158+
135159
const notifications = new Proxy({}, {
136160
get: (target, name) => {
137161
return (callback) => {
@@ -148,6 +172,7 @@ function rawr({ transport, timeout = 0, handlers = {}, methods, idGenerator }) {
148172
addHandler,
149173
notifications,
150174
notifiers,
175+
notifiersExt: configurableNotifiersProxy,
151176
transport,
152177
};
153178
}

test/index.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@ function mockTransports() {
1212
a.on('message', (msg) => {
1313
a.emit('rpc', msg);
1414
});
15-
a.send = (msg) => {
15+
a.send = (msg, config) => {
1616
b.emit('message', msg);
17+
if (config) {
18+
b.emit('config', config);
19+
}
1720
};
1821

1922
b.on('message', (msg) => {
2023
b.emit('rpc', msg);
2124
});
22-
b.send = (msg) => {
25+
b.send = (msg, config) => {
26+
if (config) {
27+
a.emit('config', config);
28+
}
2329
a.emit('message', msg);
2430
};
2531

@@ -139,6 +145,46 @@ describe('rawr', () => {
139145
clientB.notifiers.doSomething('testing_notification');
140146
});
141147

148+
it('client should have notifiersExt method', () => {
149+
const { a } = mockTransports();
150+
const client = rawr({ transport: a });
151+
client.should.have.property('notifiersExt');
152+
(typeof client.notifiersExt).should.equal('object');
153+
});
154+
155+
it('client should be able to send a notification with notifiersExt', (done) => {
156+
const { a, b } = mockTransports();
157+
const clientA = rawr({ transport: a });
158+
const clientB = rawr({ transport: b });
159+
160+
clientA.notifications.ondoSomething((someData) => {
161+
someData.should.equal('testing_notification_ext');
162+
done();
163+
});
164+
165+
clientB.notifiersExt.doSomething('testing_notification_ext');
166+
});
167+
168+
it('client should pass config to transport when using notifiersExt', (done) => {
169+
const { a, b } = mockTransports();
170+
const clientA = rawr({ transport: a });
171+
const clientB = rawr({ transport: b });
172+
173+
let receivedConfig = false;
174+
a.on('config', (config) => {
175+
config.should.deep.equal({ postMessageOptions: { transfer: ['test'] } });
176+
receivedConfig = true;
177+
});
178+
179+
clientA.notifications.ondoConfigTest((someData) => {
180+
someData.should.equal('config_test');
181+
receivedConfig.should.equal(true);
182+
done();
183+
});
184+
185+
clientB.notifiersExt.doConfigTest('config_test', { postMessageOptions: { transfer: ['test'] } });
186+
});
187+
142188
it('client should fail on a configured timeout', async () => {
143189
const { a, b } = mockTransports();
144190
const clientA = rawr({ transport: a, handlers: { slowFunction, hi } });

0 commit comments

Comments
 (0)