-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from compose-us/legacy-api-replacement-where-a…
…re-you-at Legacy api replacement for where are you at
- Loading branch information
Showing
6 changed files
with
301 additions
and
109 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { FlottformChannelClient } from './flottform-channel-client'; | ||
import { DEFAULT_WEBRTC_CONFIG, EventEmitter, Logger, POLL_TIME_IN_MS } from './internal'; | ||
|
||
type Listeners = { | ||
connected: []; | ||
'webrtc:connection-impossible': []; | ||
sending: []; // Emitted to signal the start of sending the file(s) | ||
done: []; | ||
disconnected: []; | ||
error: [e: string]; | ||
}; | ||
|
||
export class FlottformTextInputClient extends EventEmitter<Listeners> { | ||
private channel: FlottformChannelClient | null = null; | ||
private logger: Logger; | ||
|
||
constructor({ | ||
endpointId, | ||
flottformApi, | ||
rtcConfiguration = DEFAULT_WEBRTC_CONFIG, | ||
pollTimeForIceInMs = POLL_TIME_IN_MS, | ||
logger = console | ||
}: { | ||
endpointId: string; | ||
flottformApi: string; | ||
rtcConfiguration?: RTCConfiguration; | ||
pollTimeForIceInMs?: number; | ||
logger?: Logger; | ||
}) { | ||
super(); | ||
this.channel = new FlottformChannelClient({ | ||
endpointId, | ||
flottformApi, | ||
rtcConfiguration, | ||
pollTimeForIceInMs, | ||
logger | ||
}); | ||
this.logger = logger; | ||
this.registerListeners(); | ||
} | ||
start = () => { | ||
this.channel?.start(); | ||
}; | ||
|
||
close = () => { | ||
// Should be called once all the text has been sent. | ||
this.channel?.close(); | ||
}; | ||
|
||
sendText = (text: string) => { | ||
// For now, I didn't handle very large texts since for most use cases the text won't exceed the size of 1 chunk ( 16KB ) | ||
this.emit('sending'); | ||
this.channel?.sendData(text); | ||
this.emit('done'); | ||
}; | ||
|
||
private registerListeners = () => { | ||
this.channel?.on('init', () => {}); | ||
this.channel?.on('retrieving-info-from-endpoint', () => {}); | ||
this.channel?.on('sending-client-info', () => {}); | ||
this.channel?.on('connecting-to-host', () => {}); | ||
this.channel?.on('connected', () => { | ||
this.emit('connected'); | ||
}); | ||
this.channel?.on('connection-impossible', () => { | ||
this.emit('webrtc:connection-impossible'); | ||
}); | ||
this.channel?.on('done', () => { | ||
this.emit('done'); | ||
}); | ||
this.channel?.on('disconnected', () => { | ||
this.emit('disconnected'); | ||
}); | ||
this.channel?.on('error', (e) => { | ||
this.emit('error', e); | ||
}); | ||
}; | ||
} |
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,116 @@ | ||
import { FlottformChannelHost } from './flottform-channel-host'; | ||
import { Styles } from './flottform-styles'; | ||
import { DEFAULT_WEBRTC_CONFIG, EventEmitter, Logger, POLL_TIME_IN_MS } from './internal'; | ||
|
||
type Listeners = { | ||
new: []; | ||
disconnected: []; | ||
error: [error: any]; | ||
connected: []; | ||
receive: []; // Emitted to signal the start of receiving the file(s) | ||
done: [data: string]; | ||
'endpoint-created': [{ link: string; qrCode: string }]; | ||
'webrtc:waiting-for-client': [ | ||
event: { link: string; qrCode: string; channel: FlottformChannelHost } | ||
]; | ||
'webrtc:waiting-for-ice': []; | ||
'webrtc:waiting-for-file': []; | ||
}; | ||
|
||
const noop = () => {}; | ||
|
||
export class FlottformTextInputHost extends EventEmitter<Listeners> { | ||
private channel: FlottformChannelHost | null = null; | ||
private logger: Logger; | ||
private link: string = ''; | ||
private qrCode: string = ''; | ||
|
||
constructor({ | ||
flottformApi, | ||
createClientUrl, | ||
rtcConfiguration = DEFAULT_WEBRTC_CONFIG, | ||
pollTimeForIceInMs = POLL_TIME_IN_MS, | ||
theme, | ||
logger = console | ||
}: { | ||
flottformApi: string | URL; | ||
createClientUrl: (params: { endpointId: string }) => Promise<string>; | ||
rtcConfiguration?: RTCConfiguration; | ||
pollTimeForIceInMs?: number; | ||
theme?: (myself: FlottformTextInputHost) => void; | ||
logger?: Logger; | ||
}) { | ||
super(); | ||
this.channel = new FlottformChannelHost({ | ||
flottformApi, | ||
createClientUrl, | ||
rtcConfiguration, | ||
pollTimeForIceInMs, | ||
logger | ||
}); | ||
this.logger = logger; | ||
|
||
this.registerListeners(); | ||
theme && theme(this); | ||
} | ||
|
||
start = () => { | ||
this.channel?.start(); | ||
}; | ||
|
||
close = () => { | ||
this.channel?.close(); | ||
}; | ||
|
||
getLink = () => { | ||
if (this.link === '') { | ||
this.logger.error( | ||
'Flottform is currently establishing the connection. Link is unavailable for now!' | ||
); | ||
} | ||
return this.link; | ||
}; | ||
|
||
getQrCode = () => { | ||
if (this.qrCode === '') { | ||
this.logger.error( | ||
'Flottform is currently establishing the connection. qrCode is unavailable for now!' | ||
); | ||
} | ||
return this.qrCode; | ||
}; | ||
|
||
private handleIncomingData = (e: MessageEvent<any>) => { | ||
// We suppose that the data received is small enough to be all included in 1 message | ||
this.emit('done', e.data); | ||
}; | ||
|
||
private registerListeners = () => { | ||
this.channel?.on('new', ({ channel }) => { | ||
this.emit('new'); | ||
}); | ||
this.channel?.on('waiting-for-client', (event) => { | ||
this.emit('webrtc:waiting-for-client', event); | ||
const { qrCode, link } = event; | ||
this.emit('endpoint-created', { link, qrCode }); | ||
this.link = link; | ||
this.qrCode = qrCode; | ||
}); | ||
this.channel?.on('waiting-for-ice', () => { | ||
this.emit('webrtc:waiting-for-ice'); | ||
}); | ||
this.channel?.on('waiting-for-file', () => { | ||
this.emit('webrtc:waiting-for-file'); | ||
this.emit('connected'); | ||
}); | ||
this.channel?.on('receiving-data', (e) => { | ||
this.handleIncomingData(e); | ||
}); | ||
this.channel?.on('disconnected', () => { | ||
this.emit('disconnected'); | ||
}); | ||
this.channel?.on('error', (error) => { | ||
this.emit('error', error); | ||
}); | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
export { FlottformFileInputHost } from './flottform-file-input-host'; | ||
export { FlottformFileInputClient } from './flottform-file-input-client'; | ||
export { FlottformTextInputClient } from './flottform-text-input-client'; | ||
export { FlottformTextInputHost } from './flottform-text-input-host'; | ||
export type { ClientState, SafeEndpointInfo, Logger } from './internal'; | ||
import './theme/style.css'; | ||
export { defaultThemeForFileInput } from './theme/defaultTheme'; |
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
Oops, something went wrong.