Replies: 2 comments 3 replies
-
I'm unsure if exposing those is something we should do - our API surface is getting bigger. I also have a feeling Wdyt @ronag? |
Beta Was this translation helpful? Give feedback.
3 replies
-
if someone is looking for a solution, I ended up with this class RequestHandlerWrap {
constructor (originalHandler, initialTransferCallback) {
this.originalHandler = originalHandler
this.initialTransferCallback = initialTransferCallback
}
onConnect (abort, context) {
return this.originalHandler.onConnect(abort, context)
}
onBodySent (chunk) {
this.initialTransferCallback(chunk)
if (typeof this.originalHandler.onBodySent === 'function') {
return this.originalHandler.onBodySent(chunk)
}
}
onHeaders (statusCode, headers, resume) {
return this.originalHandler.onHeaders(statusCode, headers, resume)
}
onData (chunk) {
return this.originalHandler.onData(chunk)
}
onComplete (trailers) {
return this.originalHandler.onComplete(trailers)
}
onError (err) {
return this.originalHandler.onError(err)
}
}
class AgentWrap {
constructor (originalDispatcher) {
this.originalDispatcher = originalDispatcher
}
dispatch (opts, handler) {
const { initialTransferCallback, ...originalOptions } = opts
return this.originalDispatcher.dispatch(originalOptions, new RequestHandlerWrap(handler, initialTransferCallback))
}
get closed () {
return this.originalDispatcher.closed
}
get destroyed () {
return this.originalDispatcher.destroyed
}
close (callback) {
return this.originalDispatcher.close(callback)
}
destroy (err, callback) {
return this.originalDispatcher.close(err, callback)
}
}
function _call(url, cb) {
let isTransferStarted = false
return new Promise((resolve, reject) => {
request(url, {
method,
body,
dispatcher: new AgentWrap(getGlobalDispatcher())
initialTransferCallback: (data) => {
if (!isTransferStarted) {
isTransferStarted = true
cb(null, data)
}
}
}, (err, data) => {
if (!isTransferStarted) {
isTransferStarted = true
cb(err, data)
}
if (err) {
reject(err)
return
}
handleResponse(data, path).catch(reject).then(resolve)
})
})
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey, I want to implement something like "fire and forget", this is some sort of POC
maybe there is a simple way to implement, it, but in my example I need the RequestHandler to be exported, so I could extend it
ps. my idea is to resolve the promise once data was sent, but still wait for the response (to have a fallback if needed )
Beta Was this translation helpful? Give feedback.
All reactions