Skip to content

Add autoConnect option and isConnected getter to EventSource - #77

Open
feggaa wants to merge 2 commits into
binaryminds:masterfrom
feggaa:master
Open

Add autoConnect option and isConnected getter to EventSource#77
feggaa wants to merge 2 commits into
binaryminds:masterfrom
feggaa:master

Conversation

@feggaa

Copy link
Copy Markdown

No description provided.

@EmilJunker

Copy link
Copy Markdown
Contributor

Why would you need an autoConnect option? If you want to disable the automatic reconnect, you can just set the pollingInterval to 0.

And instead of adding a new isConnected function, it would be both easier and more useful to expose the status property of the EventSource class.

@feggaa

Copy link
Copy Markdown
Author

Thank you for your question

I need it for create instance of eventSource without connect

then I control connection by call eventSource.open() or eventSource.close()

this small example how I use it

typetEvent={type: 'data'|'error'|'close'|'remove';data: string|null;eventId : string;}consteventSource=newEventSource(global.serverUrl+'serverEvents',{autoConnect: false});functionsubscribeToEvent(api : string,args: string,eventId : string){returnfetch(global.serverUrl+'serverEvents',{method: 'POST',headers: {'Content-Type': 'application/json',},body: JSON.stringify({api,args,eventId}),})}constlisteners=newMap<string,(data :any)=>any>();exportfunctionstartListening<T>(api : string,callback : (data : T)=>void,args : any){if(!eventSource.isConnected){eventSource.open();}consteventHash=crc32.str(api+':'+JSON.stringify(args)).toString()constrandomId=Math.random().toString(36).substring(2,15);consteventId=eventHash+':'+randomId;subscribeToEvent(api,args,eventId)listeners.set(eventId,callback);return()=>{listeners.delete(eventId);if(listeners.size===0){eventSource.close();}}}eventSource.addEventListener('message',(event)=>{constdata : tEvent|null=event.data ? JSON.parse(event.data) : null;if(!data)returnconst{type,data: eventData, eventId}=data;if(type==='data'){constcallback=listeners.get(eventId);constcache=storage.getString(eventId);if(cache){callback?.(JSON.parse(cache));}callback?.(eventData);}elseif(type==='error'){console.error('Error event:',eventData);}elseif(type==='close'){eventSource.close();console.log('Connection closed');}elseif(type==='remove'){listeners.delete(eventId);console.log('Listener removed for eventId:',eventId);}})eventSource.addEventListener('error',(event)=>{console.error('Error event:',event);});AppState.addEventListener('change',(state)=>{if(state==='active'){eventSource.open();}else{eventSource.close();}})exportfunctionuseEventSource({api, args} : {api : string,args : any}){const[_args,setArgs]=useState(args);const[data,setData]=useState<any>(null);const[eventId,setEventId]=useState<string|null>(null);useEffect(()=>{consteventHash=crc32.str(api+':'+JSON.stringify(args)).toString()constrandomId=Math.random().toString(36).substring(2,15);consteventId=eventHash+':'+randomId;subscribeToEvent(api,args,eventId)listeners.set(eventId,setData);setEventId(eventId);return()=>{listeners.delete(eventId);if(listeners.size===0){eventSource.close();}}},[_args])return{
data,
eventId,
setArgs,remove : ()=>{if(!eventId)return;listeners.delete(eventId);if(listeners.size===0){eventSource.close();}},}}

@SteeBono

Copy link
Copy Markdown

This new option looks useful.
I have the same issue — I also need to create the instance and open the connection only when needed.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@feggaa@EmilJunker@SteeBono