-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from oslabs-beta/refactor
Refactor
- Loading branch information
Showing
11 changed files
with
281 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,87 @@ | ||
const generateMeta = require("../utils/generateMeta"); | ||
|
||
// (metaObject, interceptorArray, callback) | ||
|
||
module.exports = function clientStreamCall(that, methodName, first, second) { | ||
let metadata = null; | ||
let interceptors = null; | ||
let callback = () => {}; | ||
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 sender = that[methodName](metadata, interceptors, callback); | ||
console.log(metadata); | ||
console.log(interceptors); | ||
let callback = function(err, res) { | ||
if (err) clientStreamObj.$catch(err); | ||
clientStreamObj.$on(res); | ||
}; | ||
let sender = that[methodName](metadata, interceptors, callback); | ||
const clientStreamObj = { | ||
// throw: function(metadata) { | ||
// sender.throw() | ||
// }, | ||
getPeer: function() { | ||
return sender.getPeer(); | ||
}, | ||
write: function(...args) { | ||
return this.send(...args) | ||
}, | ||
send: function(message, flags, flushCallback) { | ||
sender.write(message, flags, flushCallback); | ||
return clientStreamObj; | ||
}, | ||
$catch: function() { | ||
throw new Error( | ||
"Unary Call: .catch and .on must be defined before .send" | ||
); | ||
}, | ||
catch: function(first) { | ||
if (typeof first !== "function") { | ||
throw new Error("Client Stream: catch takes a callback"); | ||
} | ||
clientStreamObj.$catch = first; | ||
return clientStreamObj; | ||
}, | ||
$on: () => { | ||
throw new Error( | ||
"Client Stream: .catch and .on must be invoked to .send" | ||
); | ||
}, | ||
on: function(first, second) { | ||
let listenerCallback; | ||
if (typeof first !== "function" && typeof second !== "function") { | ||
throw new Error("Client Stream: on takes a callback"); | ||
} | ||
if (typeof first === "function") { | ||
listenerCallback = first; | ||
} else { | ||
listenerCallback = second; | ||
} | ||
"Client Stream: .catch and .on must be defined to .send" | ||
); | ||
}, | ||
catch: function(first) { | ||
if (typeof first !== "function") { | ||
throw new Error("Client Stream: catch takes a callback"); | ||
} | ||
clientStreamObj.$catch = first; | ||
return clientStreamObj; | ||
}, | ||
$on: (res) => { | ||
throw new Error( | ||
"Client Stream: .catch and .on must be invoked to .send" | ||
); | ||
}, | ||
on: function(first, second) { | ||
let listenerCallback; | ||
if (typeof first !== "function" && typeof second !== "function") { | ||
throw new Error("Client Stream: on takes a callback"); | ||
} | ||
if (typeof first === "function") { | ||
listenerCallback = first; | ||
} else { | ||
listenerCallback = second; | ||
} | ||
switch (first) { | ||
case 'status': | ||
sender.on('status', second); | ||
break; | ||
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; | ||
}; | ||
|
||
// module.exports = function clientStreamCall(that, methodName, ...args) { | ||
// let interceptors = undefined; | ||
// let metadata = undefined; | ||
// let callback = undefined; | ||
// let hasFunction = false; | ||
// args = args.filter(arg => args !== undefined); | ||
// for (let i = 0; i < args.length; i++) { | ||
// if (typeof args[i] === "function") { | ||
// callback = args[i]; | ||
// hasFunction = true; | ||
// } else { | ||
// if (typeof (args[i] === "object")) { | ||
// if (Array.isArray(args[i])) { | ||
// interceptors = { interceptors: args[i] }; | ||
// } else { | ||
// metadata = generateMeta(args[i]); | ||
// } | ||
// } | ||
// } | ||
// } | ||
// if (!hasFunction) { | ||
// throw new Error("Must include a callback function to client Stream Call"); | ||
// } | ||
// return that[methodName](metadata, interceptors, callback); | ||
// }; | ||
} | ||
}; | ||
return clientStreamObj; | ||
}; |
Oops, something went wrong.