Skip to content

Commit

Permalink
duplex now can accept metadata request options and write/send
Browse files Browse the repository at this point in the history
  • Loading branch information
Dnaganog committed Aug 10, 2019
1 parent 16d0da3 commit eada4c8
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 49 deletions.
64 changes: 36 additions & 28 deletions examples/firecomm/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,24 @@ const firstChat = {

// testClientStream();

const newClient = stub.clientStream(
{hello: 'world yo',
options: {
idempotentRequest: true,
cacheableRequest: true,
corked: true,
waitForReady: false,
}})
.send({message: 'yolo'})
.send(firstChat)
.on((data) => console.log({ data }))
.on('status', (status) => console.log({ status }))
.on('metadata', (metadata) => console.log(metadata, metadata.getMap()))
.catch(err => console.log({ err }))

setInterval(()=>{
newClient.send({message:'please'})
}, 1000)
// const newClient = stub.clientStream(
// {hello: 'world yo',
// options: {
// idempotentRequest: true,
// cacheableRequest: true,
// corked: true,
// waitForReady: false,
// }})
// .send({message: 'yolo'})
// .send(firstChat)
// .on((data) => console.log({ data }))
// .on('status', (status) => console.log({ status }))
// .on('metadata', (metadata) => console.log(metadata, metadata.getMap()))
// .catch(err => console.log({ err }))

// setInterval(()=>{
// newClient.send({message:'please'})
// }, 1000)

// const testServerStream = () => {
// const serverStream = stub.serverStream(firstChat);
Expand All @@ -99,17 +99,25 @@ const newClient = stub.clientStream(
// };
// testServerStream();

// const testBidiChat = () => {
// const duplexStream = stub.bidiChat({meta: 'data'});
// duplexStream.write({ message: "from client" });
// duplexStream.on("data", ({message}) => {
// console.log(message);
// duplexStream.send({ message: "from client2" });
// });

// duplexStream.catch(err => {
// console.log({ err });
// }));
const duplexStream = stub.bidiChat(
{bidi: 'meta',
options: {
idempotentRequest: true,
cacheableRequest: true,
corked: true,
waitForReady: false,
}}
);
duplexStream.write({ message: "from client" });
duplexStream.on("data", ({message}) => {
// console.log(message);
duplexStream.send({ message: "from client2" });
});

duplexStream.catch(err => {
console.log({ err });
});

// const duplexStream = stub.bidiChat({meta: 'data'}, [interceptorProvider])
// .send({ message: "from client" })
Expand Down
10 changes: 5 additions & 5 deletions examples/firecomm/methodHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ function clientStream(call) {
setTimeout(() => call.send({ message: "world" }), 5000);
}

function bidiChat(context) {
function bidiChat(call) {
// console.log('context keys', Object.keys(context));
// console.log('context proto', context.__proto__)

context.on("data", data => {
console.log("data:", data);
context.send({ message: data.message + " World" });
console.log(call.head);
call.on("data", data => {
// console.log("data:", data);
call.send({ message: data.message + " World" });
});
}

Expand Down
14 changes: 6 additions & 8 deletions lib/clientCalls/clientStreamCall.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const generateMeta = require("../utils/generateMeta");

// (metaObject, interceptorArray, callback)

module.exports = function clientStreamCall(that, methodName, first, second) {
let metadata = null;
let interceptors = null;
Expand Down Expand Up @@ -35,8 +33,10 @@ module.exports = function clientStreamCall(that, methodName, first, second) {
getPeer: function() {
return sender.getPeer();
},
write: function(...args) {
return this.send(...args)
},
send: function(message, flags, flushCallback) {
lastMessage = { message, flags, flushCallback };
sender.write(message, flags, flushCallback);
return clientStreamObj;
},
Expand Down Expand Up @@ -74,16 +74,14 @@ module.exports = function clientStreamCall(that, methodName, first, second) {
case 'metadata':
sender.on('metadata', second);
break;
case 'error':
clientStreamObj.catch(second);
break;
default:
clientStreamObj.$on = listenerCallback;
}
return clientStreamObj;
}
};
// callback = function(err, res) {
// if (err) clientStreamObj.$catch(err);
// clientStreamObj.$on(res);
// return clientStreamObj;
// };
return clientStreamObj;
};
41 changes: 33 additions & 8 deletions lib/clientCalls/duplexCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,42 @@ const generateMeta = require("../utils/generateMeta");
// metaObject, interceptorArray

module.exports = function duplexCall(that, methodName, first, second) {
let metadata;
let interceptors;
let metadata = null;
let interceptors = null;
if (typeof first === "object") {
if (Array.isArray(first)) {
interceptors = { interceptors: first };
} else {
metadata = generateMeta(first);
const {options} = first;
delete first.options;
metadata = generateMeta(first, options);
}
};
if (typeof second === "object") {
if (Array.isArray(second)) {
interceptors = { interceptors: second };
} else {
metadata = generateMeta(second);
const {options} = second;
delete second.options;
metadata = generateMeta(second, options);
}
};
const duplex = that[methodName](metadata, interceptors);
const duplexObj = {
// throw: function(metadata) {
// sender.throw()
// },
getPeer: function() {
return duplex.getPeer();
},
write: function(...args) {
return this.send(...args)
},
send: function(message, flags, flushCallback) {
duplex.write(message, flags, flushCallback);
return duplexObj;
},
catch: (first) => {
catch: function(first) {
if (typeof first !== 'function') {
throw new Error('Unary Call: catch takes a callback')
}
Expand All @@ -41,9 +54,21 @@ module.exports = function duplexCall(that, methodName, first, second) {
listenerCallback = first;
} else {
listenerCallback = second;
}
duplex.on('data', listenerCallback);
return duplexObj;
};
switch (first) {
case 'status':
duplex.on('status', second);
break;
case 'metadata':
duplex.on('metadata', second);
break;
case 'error':
duplex.on('error', second);
break;
default:
duplex.on('data', listenerCallback);
}
return duplexObj;
}
}
return duplexObj;
Expand Down

0 comments on commit eada4c8

Please sign in to comment.