wait for initial sync before adding listeners

This commit is contained in:
Sharon Kennedy 2020-07-01 00:50:34 -04:00
parent ad28e4acc5
commit 4f0abfed09

View File

@ -232,12 +232,6 @@ class ChatBox extends React.Component {
this.setState({ ready: false }) this.setState({ ready: false })
const client = await this.createClientWithAccount() const client = await this.createClientWithAccount()
this.setState({
client: client
})
client.setDisplayName(this.props.anonymousDisplayName)
this.setMatrixListeners(client)
try { try {
await client.initCrypto() await client.initCrypto()
} catch(err) { } catch(err) {
@ -245,7 +239,15 @@ class ChatBox extends React.Component {
} }
await client.startClient() await client.startClient()
await this.createRoom(client)
client.once('sync', async (state, prevState, data) => {
if (state === "PREPARED") {
this.setState({ client })
client.setDisplayName(this.props.anonymousDisplayName)
this.setMatrixListeners(client)
await this.createRoom(client)
}
})
} }
restartWithoutCrypto = async () => { restartWithoutCrypto = async () => {
@ -277,35 +279,35 @@ class ChatBox extends React.Component {
let client; let client;
client = matrix.createClient(opts) client = matrix.createClient(opts)
this.setState({ await client.startClient()
client: client,
})
try { client.once('sync', async (state, prevState, data) => {
this.setMatrixListeners(client) if (state === "PREPARED") {
client.setDisplayName(this.props.anonymousDisplayName) try {
await this.createRoom(client) this.setState({ client })
await client.startClient() client.setDisplayName(this.props.anonymousDisplayName)
this.displayBotMessage({ body: UNENCRYPTION_NOTICE }) this.setMatrixListeners(client)
} catch(err) { await this.createRoom(client)
console.log("error", err) this.displayBotMessage({ body: UNENCRYPTION_NOTICE })
this.handleInitError(err) } catch(err) {
} console.log("error", err)
this.handleInitError(err)
}
}
})
} }
initializeUnencryptedChat = async () => { initializeUnencryptedChat = async () => {
this.setState({ ready: false }) this.setState({ ready: false })
const client = await this.createClientWithAccount() const client = await this.createClientWithAccount()
this.setState({
client: client
})
client.setDisplayName(this.props.anonymousDisplayName)
this.setMatrixListeners(client)
await client.startClient() await client.startClient()
await this.createRoom(client)
client.once('sync', async (state, prevState, data) => {
client.setDisplayName(this.props.anonymousDisplayName)
this.setMatrixListeners(client)
await this.createRoom(client)
})
} }
handleInitError = (err) => { handleInitError = (err) => {
@ -504,6 +506,21 @@ class ChatBox extends React.Component {
} }
setMatrixListeners = client => { setMatrixListeners = client => {
client.on("sync", (state, prevState, data) => {
switch (state) {
case "ERROR":
// update UI to say "Connection Lost"
break;
case "SYNCING":
// update UI to remove any "Connection Lost" message
break;
case "PREPARED":
// the client instance is ready to be queried.
this.setState({ client: client })
break;
}
});
client.on("Room.timeline", (event, room) => { client.on("Room.timeline", (event, room) => {
const eventType = event.getType() const eventType = event.getType()
const content = event.getContent() const content = event.getContent()