-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b52d9ee
commit 694411a
Showing
7 changed files
with
243 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import React from 'react' | ||
import { connect } from 'react-redux' | ||
|
||
import { counterSubscribeWs, getNotificationsWs } from 'app/utils/NotifyApiClient' | ||
|
||
const delay = async (msec) => await new Promise(resolve => setTimeout(resolve, msec)) | ||
|
||
class NotifyPolling extends React.Component { | ||
componentDidMount() { | ||
const { username } = this.props | ||
if (username) { | ||
this.poll(username) | ||
} | ||
} | ||
|
||
componentDidUpdate(prevProps) { | ||
const { username } = this.props | ||
if (username && username !== prevProps.username) { | ||
this.poll(username) | ||
} | ||
} | ||
|
||
async poll(username) { | ||
let firstFilled = false | ||
|
||
const { update } = this.props | ||
|
||
await delay(500) | ||
|
||
let subscribeRes | ||
while (true) { | ||
subscribeRes = await counterSubscribeWs(username, async function (err, res) { | ||
if (err) { | ||
console.error(err) | ||
return | ||
} | ||
|
||
if (firstFilled) { // TODO: it is more reliably to use timestamps to check order | ||
await update(res.counters) | ||
} | ||
}) | ||
|
||
if (subscribeRes.err) { | ||
console.warning('counterSubscribeWs:', subscribeRes.err, ', retry...') | ||
await delay(2000) | ||
} else { | ||
break | ||
} | ||
} | ||
|
||
while (true) { | ||
if (this.props.username !== username) { | ||
return | ||
} | ||
|
||
let counters | ||
try { | ||
counters = await getNotificationsWs(username) | ||
} catch (error) { | ||
console.error('getNotificationsWs', error) | ||
} | ||
|
||
if (counters) { | ||
firstFilled = true | ||
await update(counters) | ||
break | ||
} | ||
await delay(2000) | ||
} | ||
} | ||
|
||
render() { | ||
return null | ||
} | ||
} | ||
|
||
export default connect( | ||
(state, ownProps) => { | ||
const current = state.user.get('current') | ||
const username = current && current.get('username') | ||
return { username } | ||
}, | ||
dispatch => ({ | ||
update: (payload) => { | ||
dispatch({type: 'UPDATE_NOTIFICOUNTERS', payload}) | ||
}, | ||
}) | ||
)(NotifyPolling) |
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 was deleted.
Oops, something went wrong.
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