Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any way to cancel the request? #19

Closed
ajselvig opened this issue Aug 26, 2016 · 7 comments
Closed

Any way to cancel the request? #19

ajselvig opened this issue Aug 26, 2016 · 7 comments
Milestone

Comments

@ajselvig
Copy link

Is there any way to cancel the request (like xhr.abort())? We're using this for potentially long running requests and it would be nice to be able to let the user cancel it in the middle.

@jonnyreeves
Copy link
Owner

jonnyreeves commented Aug 26, 2016

@ajselvig; we will be incorperating this as part of #16; I'm thinking we will land #15 and release it ti npm as 0.5 and then follow-up with the stream change in 0.6

@jonnyreeves jonnyreeves modified the milestones: 0.5, 0.6 Aug 26, 2016
@ajselvig
Copy link
Author

Okay, thanks. I'll keep an eye out for that.

@ajselvig
Copy link
Author

Our current user base is chrome only, any chance there's a browser-specific hack we could use as a shim until this in place?

@jonnyreeves
Copy link
Owner

@ajselvig see #20; you should now be able to cancel a request by issuing a cancel on the reader, ie:

chunkedRequest({ ... })
  .then(res => {
    // do something with the response...
    // abort the request.
    res.body.cancel();
  });

@ajselvig
Copy link
Author

@jonnyreeves I downloaded the latest and tried it out. The chunkedRequest function doesn't appear to return anything. From your example it seems like you're expecting it to return a promise?

@jonnyreeves
Copy link
Owner

jonnyreeves commented Sep 21, 2016

@ajselvig - please see #22 for context.

If you can migrate your implementation to use jonnyreeves/fetch-readablestream then you can use the cancel() method on the Response.body Stream object to cancel the response (the tests include an example of this).

Apologies for the churn; hope this works out for you - if you have further issues please let me know and I can help resolve them.

@ajselvig
Copy link
Author

@jonnyreeves - I'm trying to set up my solution to use fetchStream instead of chunkedRequest. I have a working implementation, except that I'm no longer getting the chunks as they come in, just one big response at the end. Any chance you could tell me what's wrong with the implementation? I'm having some trouble interpreting the various parts.

(sorry for the coffeescript)

readAllChunks = (readableStream) ->
	reader = readableStream.getReader()
	chunks = []

	pump = ->
		return reader.read().then((data) ->
			if (data.done)
				return chunks
			chunks.push(data.value)
			return pump()
		)

	pump()

decodeResponse = (chunks) ->
	decoder = new TextDecoder()
	return chunks
		.map((bytes, idx) ->
			isLastChunk = (idx == (chunks.length - 1))
			return decoder.decode(bytes, { stream: !isLastChunk })
		).join('')

decodeUnaryJSON = (chunks) ->
	new Promise((resolve) ->
		json = decodeResponse(chunks)
		resolve(
			_.map(json.split(/}\s*{/), (row) ->
				row = row.trim()
				unless row.startsWith('{')
					row = '{' + row
				unless row.endsWith('}')
					row = row + '}'
				JSON.parse(row)
			)
		)
	)

fetchStream(url, {
			method: 'POST'
			headers: {accept: 'application/json'}
			credentials: 'include'
			body: JSON.stringify(data)
		})
		.then((response) -> readAllChunks(response.body))
		.then(decodeUnaryJSON)
		.then((chunks) =>
			@listener.onChunks chunks
		)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants