Skip to content

Commit

Permalink
client stream is working
Browse files Browse the repository at this point in the history
  • Loading branch information
Dnaganog committed Aug 7, 2019
1 parent 5927f09 commit feebb89
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 55 deletions.
28 changes: 16 additions & 12 deletions examples/firecomm/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,26 @@ const testUnaryChat = () => {
// .then(data => console.log(data))
// .catch(err => console.error(err));

const testClientStream = () => {
const clientStream = stub.clientStream((err, res) => {
if (err) console.log(err);
console.log({ res });
});
clientStream.write(firstChat);
clientStream.end();
};
// const testClientStream = () => {
// const clientStream = stub.clientStream((err, res) => {
// if (err) console.log(err);
// console.log({ res });
// });
// clientStream.write(firstChat);
// clientStream.end();
// };

stub
.clientStream()
// testClientStream();

const newClient = stub.clientStream({meta: 'data'}, [interceptorProvider])
.send({message: 'yolo'})
.send(firstChat)
.on(data => console.log({ data }))
.catch(err => console.log({ err }))
.send(firstChat);

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

const testServerStream = () => {
const serverStream = stub.serverStream(firstChat);
Expand Down
92 changes: 49 additions & 43 deletions lib/clientCalls/clientStreamCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ const generateMeta = require("../utils/generateMeta");
// (metaObject, interceptorArray, callback)

module.exports = function clientStreamCall(that, methodName, first, second) {
let metadata = undefined;
let interceptors = undefined;
let metadata = null;
let interceptors = null;
let callback = () => {};
if (typeof first === "object") {
if (Array.isArray(first)) {
interceptors = { interceptors: first };
Expand All @@ -19,53 +20,58 @@ module.exports = function clientStreamCall(that, methodName, first, second) {
metadata = generateMeta(second);
}
}
let callback = function(err, res) {
if (err) clientStreamObj.$catch(err);
clientStreamObj.$on(res);
return clientStreamObj;
};
let sender = that[methodName](metadata, interceptors, callback);
const sender = that[methodName](metadata, interceptors, callback);
console.log(metadata);
console.log(interceptors);
const clientStreamObj = {
send: message => {
// console.log(sender);
sender.emit("data", message);
send: function(message) {
const sender = that[methodName](metadata, interceptors, callback);
sender.write(message, null, () => {
// this.sender.write(message)
});
console.log(sender);
return clientStreamObj;
},
$catch: () => {
$catch: function() {
throw new Error(
"Unary Call: .catch and .on must be defined before .send"
);
},
catch: first => {
if (typeof first !== "function") {
throw new Error("Unary Call: catch takes a callback");
}
clientStreamObj.$catch = first;
return clientStreamObj;
},
$on: () => {
throw new Error(
"Unary Call: .catch and .on must be defined before .send"
);
},
on: (first, second) => {
let listenerCallback;
if (typeof first !== "function" && typeof second !== "function") {
throw new Error("Unary Call: on takes a callback");
}
if (typeof first === "function") {
listenerCallback = first;
} else {
listenerCallback = second;
}
clientStreamObj.$on = listenerCallback;
);
},
catch: function(first) {
if (typeof first !== "function") {
throw new Error("Unary Call: catch takes a callback");
}
clientStreamObj.$catch = first;
return clientStreamObj;
},
$on: () => {
throw new Error(
"Unary Call: .catch and .on must be defined before .send"
);
},
on: function(first, second) {
let listenerCallback;
if (typeof first !== "function" && typeof second !== "function") {
throw new Error("Unary Call: on takes a callback");
}
if (typeof first === "function") {
listenerCallback = first;
} else {
listenerCallback = second;
}
clientStreamObj.$on = listenerCallback;
return clientStreamObj;
}
};
callback = function(err, res) {
if (err) clientStreamObj.$catch(err);
clientStreamObj.$on(res);
return clientStreamObj;
};
return clientStreamObj;
}
};
return clientStreamObj;
};

// module.exports = function clientStreamCall(that, methodName, ...args) {
};

// module.exports = function clientStreamCall(that, methodName, ...args) {
// let interceptors = undefined;
// let metadata = undefined;
// let callback = undefined;
Expand Down

0 comments on commit feebb89

Please sign in to comment.