Skip to content

Commit

Permalink
made server calls chainable
Browse files Browse the repository at this point in the history
  • Loading branch information
iangeckeler committed Aug 13, 2019
1 parent 71afd6c commit 45ccc12
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 48 deletions.
85 changes: 43 additions & 42 deletions examples/firecomm/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ const firstChat = {
// .send({ message: "from client" })
// .on(data => console.log(data));

stub.unaryChat({ hello: "metadata" })
.send({firstChat})
.on(data => console.log(data))
.on('metadata', data => console.log(data))
.on('status', data => console.log(data))
.catch(err => console.error(err))
// stub
// .unaryChat({ hello: "metadata" })
// .send({ firstChat })
// .on(data => console.log(data))
// .on("metadata", data => console.log(data))
// .on("status", data => console.log(data))
// .catch(err => console.error(err));

// const testClientStream = () => {
// const clientStream = stub.clientStream((err, res) => {
Expand All @@ -74,7 +75,7 @@ stub.unaryChat({ hello: "metadata" })
// testClientStream();

// const newClient = stub.clientStream(
// {hello: 'world yo',
// {hello: 'world yo',
// options: {
// idempotentRequest: true,
// cacheableRequest: true,
Expand All @@ -93,33 +94,32 @@ stub.unaryChat({ hello: "metadata" })
// }, 1000)

// const testServerStream = () => {
// const serverStream = stub.serverStream(firstChat);
// const serverStream = stub.serverStream(
// {
// stream: 'server',
// options: {
// idempotentRequest: true,
// cacheableRequest: true,
// corked: true,
// waitForReady: false,
// }
// }
// );
// serverStream.write(firstChat);
// serverStream.on("data", data => {
// console.log(data);
// })
// serverStream.on("status", status => {
// console.log(status);
// })
// serverStream.on("metadata", metadata => {
// console.log(metadata);
// })
// .catch((err) => console.error(err));
// const serverStream = stub.serverStream(firstChat);
// const serverStream = stub.serverStream(
// {
// stream: 'server',
// options: {
// idempotentRequest: true,
// cacheableRequest: true,
// corked: true,
// waitForReady: false,
// }
// }
// );
// serverStream.write(firstChat);
// serverStream.on("data", data => {
// console.log(data);
// })
// serverStream.on("status", status => {
// console.log(status);
// })
// serverStream.on("metadata", metadata => {
// console.log(metadata);
// })
// .catch((err) => console.error(err));
// };
// testServerStream();


// const duplexStream = stub.bidiChat(
// {bidi: 'meta',
// options: {
Expand All @@ -139,17 +139,18 @@ stub.unaryChat({ hello: "metadata" })
// console.log({ err });
// });

// const duplexStream = stub.bidiChat({meta: 'data'}, [interceptorProvider])
// .send({ message: "from client" })
// .on((data) => console.log(data))

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

// duplexStream.catch((err => {
// console.log({ err });
// }));
duplexStream.on(({ message }) => {
console.log(message);
duplexStream.send({ message: "from client2" });
});

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

// testBidiChat();
12 changes: 7 additions & 5 deletions examples/firecomm/methodHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function unaryChat(call) {

function serverStream(call) {
// console.log(context);
call.set({server: 'streamed metadata'})
call.set({ server: "streamed metadata" });
let count = 0;
setInterval(() => {
count += 1;
Expand All @@ -27,9 +27,7 @@ function clientStream(call) {
// console.log('serverStream context: ', context);
// console.log(context.metadata, context.metaData);
console.log(call.head);
call.set(
{please: 'work', dear: 'dog'}
)
call.set({ please: "work", dear: "dog" });
// console.log(call.metadata);
call.on("data", data => {
console.log(data);
Expand All @@ -42,9 +40,13 @@ function bidiChat(call) {
// console.log('context proto', context.__proto__)
console.log(call.head);
call.on("data", data => {
// console.log("data:", data);
console.log("data:", data);
call.send({ message: data.message + " World" });
call
.set({ metadatatest: "test value" })
.send({ message: data.message + " World" });
});
// .send({ message: "hello" + " World" });
}

module.exports = {
Expand Down
2 changes: 2 additions & 0 deletions lib/callFactories/callDecorators/clientStreamDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ module.exports = function(customCall, originalCall) {
"type of first parameter must be string or callback function."
);
}
return this;
};

customCall.catch = function(callback) {
this.call.on("error", callback);
return this;
};
};
3 changes: 2 additions & 1 deletion lib/callFactories/callDecorators/clientUnaryDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ module.exports = function clientUnaryDecorator(customCall, originalCall) {
);
} else {
if (string === "data") {
return callback(this.body);
callback(this.body);
} else {
throw new Error('Only the "data" event is support by unary .on');
}
}
}
return this;
};
};
3 changes: 3 additions & 0 deletions lib/callFactories/callDecorators/serverStreamDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = function serverStreamDecorator(customCall, originalCall) {
);
}
this.call.emit("error", err);
return this;
};

customCall.set = function(object) {
Expand All @@ -37,6 +38,7 @@ module.exports = function serverStreamDecorator(customCall, originalCall) {
} else {
this.metadata = object;
}
return this;
};

customCall.send = function(message) {
Expand All @@ -46,4 +48,5 @@ module.exports = function serverStreamDecorator(customCall, originalCall) {
}
this.call.write(message);
};
return this;
};
3 changes: 3 additions & 0 deletions lib/callFactories/callDecorators/serverUnaryDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = function serverUnaryDecorator(clone, sendCallback) {
this.call.sendMetadata(generateMeta(this.metadata));
}
this.sendCallback(null, message, null, flags);
return this;
};

clone.set = function(object, options) {
Expand All @@ -30,6 +31,7 @@ module.exports = function serverUnaryDecorator(clone, sendCallback) {
} else {
this.metadata = { ...object };
}
return this;
};

clone.throw = function(err, trailers) {
Expand All @@ -56,6 +58,7 @@ module.exports = function serverUnaryDecorator(clone, sendCallback) {
}

this.sendCallback(err, {}, this.trailers);
return this;
};
// ServerUnaryCallClone.send = function(message = {}) {
// if (this.metaData) {
Expand Down

0 comments on commit 45ccc12

Please sign in to comment.