Comments (11)
@ChepteaCatalin this doesnt work. Im not sure yet what the solution is but I checked above and after controller.abort() call messages are still there. onmessage
callback is still firing.
from fetch-event-source.
Calling controller.abort()
in onerror
can't cancel retries with this library.
onerror
is called here, after which the retry timer will be reset when it doesn't throw, without checking for aborted
again:
fetch-event-source/src/fetch.ts
Lines 129 to 136 in a052949
The retry timer will call create
where a new AbortController
is created that starts non-aborted:
fetch-event-source/src/fetch.ts
Lines 102 to 104 in a052949
Retries will be stopped if controller.abort()
was called in onopen
, onmessage
or onclose
, but calls in onerror
won't affect it.
Throwing in onerror
is absolutely the correct thing to do to stop the request, which is also shown in the readme:
Lines 83 to 86 in a052949
from fetch-event-source.
For those having the issue in React, this works:
const [ abortController, setAbortController ] = useState(new AbortController());
fetch(
signal: abortController.signal
// ...
)
function stopResponseSSE() {
abortController.abort();
setAbortController(new AbortController());
}
As June mentioned, "once an instance of AbortController is used, we need to create a new instance to reset the value."
In React, you most likely will need to set the state with a new Abort Controller.
Hope this helps!
from fetch-event-source.
const controller = new AbortController()
const signal = controller.signal
pass signal
to the object from the second parameter of fetchEventSource()
When you want to close the connection, just call controller.abort()
https://stackoverflow.com/questions/31061838/how-do-i-cancel-an-http-fetch-request
from fetch-event-source.
Once an instance of AbortController is used, we need to create a new instance to reset the value.
eg:
function stopResponseSSE() {
ctrl.abort()
ctrl = new AbortController();
}
from fetch-event-source.
Curious about this too. Aborting the fetch request didn't seem to work for me. Workaround I use is to throw an error in the onerror
and onclose
functions
onclose() {
// do something
throw new Error(); // hack to close the connection
},
onerror() {
// do something
throw new Error(); // hack to close the connection
from fetch-event-source.
@ChepteaCatalin this doesnt work. Im not sure yet what the solution is but I checked above and after controller.abort() call messages are still there.
onmessage
callback is still firing.
Make sure your controller
or signal
are the same. Especially in React.
from fetch-event-source.
its easy to do in modern browsers follow this.
Yes, it's easy and it's just as documented, except it doesn't close the connection, at least not in my case.
from fetch-event-source.
its easy to do in modern browsers follow this.
const controller = new AbortController()
const { signal } = controller
await fetchEventSource('your API', {
onmessage(ev) {
console.log('data', ev.data)
},
onclose() {
controller.abort()
// do something
},
onerror() {
// do something
},
signal,
})
and When you want to close the connection, just call controller.abort()
from fetch-event-source.
For those having the issue in React, this works:
const [ abortController, setAbortController ] = useState(new AbortController()); fetch( signal: abortController.signal // ... ) function stopResponseSSE() { abortController.abort(); setAbortController(new AbortController()); }
As June mentioned, "once an instance of AbortController is used, we need to create a new instance to reset the value."
In React, you most likely will need to set the state with a new Abort Controller.
Hope this helps!
Better to use a useRef
to store the abortController
instead of useState
as you'll end up triggering a (likely undesired) re-render when you call setAbortController(new AbortController());
from fetch-event-source.
As @Systemcluster said, the only thing that works to stop the retry functionality is to throw an error in the onerror
callback.
If this is the correct thing to do, it would probably help to add a small section to the readme about this (I would create a PR however I am not sure if this is the expected way to handle this cause or if throwing an error just happens to do what I want).
from fetch-event-source.
Related Issues (20)
- Support `Headers` in init
- onmessage is not working on a Azure deployed site.
- Error: document is not defined HOT 7
- Defining curRequestController inside the create function can lead to a bug.
- how to get the message with background HOT 2
- If there is a space in front of the data, it will be truncated HOT 2
- 如果接口:Failed to fetch错误,如何关闭连接?使用curRequestController.abort();未生效,请指教 HOT 8
- fetch响应body不是的ReadableStream:苹果端qq浏览器中 HOT 5
- TypeError: failed to execute 'getreader' on 'readablestream': readablestreamdefaultreader constructor can only accept readable streams that are not yet locked to a reader
- The error you provided does not contain a stack trace. HOT 1
- Empty string was return when the event data is a `'\n'` HOT 4
- In the process of onmessage, switch to another pages, it will resend the request. HOT 3
- Cannot find name 'ReadableStreamDefaultReadResult' HOT 1
- How to retry from within onmessage handler?
- In the process of obtaining information, switching to another tab at this time, and then returning, will cause the previous link to be disconnected and reconnected. How to prevent the request from being disconnected when returning from other tabs? HOT 2
- Properties Documentation HOT 3
- Exponential reconnect backoff HOT 2
- @microsoft/fetchEventSource dispose method always curRequestController.abort with console error log HOT 2
- Does React Native app supported? HOT 1
- Can it be achieved to interrupt or cancel the response during the response process HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fetch-event-source.