-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: React Native SSE Support (#1012)
- Loading branch information
1 parent
bdab3fa
commit 201279b
Showing
12 changed files
with
123 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { DVCLogger } from '../logger' | ||
|
||
export interface SSEConnectionInterface { | ||
updateURL(url: string): void | ||
isConnected(): boolean | ||
reopen(): void | ||
close(): void | ||
} | ||
|
||
export interface SSEConnectionConstructor { | ||
new ( | ||
url: string, | ||
onMessage: (message: unknown) => void, | ||
logger: DVCLogger, | ||
): SSEConnectionInterface | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import EventSource from 'react-native-sse' | ||
import type { DVCLogger, SSEConnectionInterface } from '@devcycle/types' | ||
|
||
export class ReactNativeSSEConnection implements SSEConnectionInterface { | ||
private connection?: EventSource | ||
private isConnectionOpen = false | ||
|
||
constructor( | ||
private url: string, | ||
private onMessage: (message: unknown) => void, | ||
private logger: DVCLogger, | ||
) { | ||
this.openConnection() | ||
} | ||
|
||
public updateURL(url: string): void { | ||
this.close() | ||
this.url = url | ||
this.openConnection() | ||
} | ||
|
||
private openConnection() { | ||
this.connection = new EventSource(this.url, { | ||
debug: false, | ||
// start connection immediately | ||
timeoutBeforeConnection: 0, | ||
// disable request timeout so connections are kept open | ||
timeout: 0, | ||
// enable withCredentials so we can send cookies | ||
withCredentials: true, | ||
}) | ||
|
||
this.connection.addEventListener('message', (event) => { | ||
this.logger.debug(`ReactNativeSSEConnection message. ${event.data}`) | ||
this.onMessage(event.data) | ||
}) | ||
|
||
this.connection.addEventListener('error', (error) => { | ||
this.logger.error( | ||
`ReactNativeSSEConnection error. ${ | ||
(error as any)?.message || JSON.stringify(error) | ||
}`, | ||
) | ||
}) | ||
|
||
this.connection.addEventListener('open', () => { | ||
this.logger.debug('ReactNativeSSEConnection opened') | ||
this.isConnectionOpen = true | ||
}) | ||
this.connection.addEventListener('close', () => { | ||
this.logger.debug('ReactNativeSSEConnection closed') | ||
this.isConnectionOpen = false | ||
}) | ||
} | ||
|
||
isConnected(): boolean { | ||
return this.isConnectionOpen | ||
} | ||
|
||
reopen(): void { | ||
if (!this.isConnected()) { | ||
this.close() | ||
this.openConnection() | ||
} | ||
} | ||
|
||
close(): void { | ||
this.connection?.close() | ||
} | ||
} |
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