-
Notifications
You must be signed in to change notification settings - Fork 152
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 #167 from Coding/zhengxinqi/offline
add offline reconnect logic
- Loading branch information
Showing
11 changed files
with
186 additions
and
28 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,72 @@ | ||
import React, { Component, PropTypes } from 'react' | ||
import { inject } from 'mobx-react' | ||
import { emitter, E } from 'utils' | ||
import cx from 'classnames' | ||
import i18n from 'utils/createI18n' | ||
import config from '../../config' | ||
|
||
|
||
// 三个状态 1. connected(两个socket全链接) 2. disconnected可点击 3. connecting | ||
@inject(() => ({ | ||
fsSocketConnected: config.fsSocketConnected, | ||
})) | ||
class Offline extends Component { | ||
state = { | ||
showButton: false, | ||
isConnecting: false, | ||
} | ||
|
||
componentDidMount () { | ||
emitter.on(E.SOCKET_TRIED_FAILED, () => { | ||
this.setState({ showButton: true, isConnecting: false }) | ||
}) | ||
} | ||
componentWillReceiveProps (np) { | ||
if ((np.fsSocketConnected !== this.props.fsSocketConnected) && (np.fsSocketConnected === true)) { | ||
this.setState({ isConnecting: false }, () => { | ||
setTimeout(() => { | ||
this.setState({ showButton: false }) | ||
}, 10000) | ||
}) | ||
} | ||
} | ||
render () { | ||
const isConnecting = this.state.isConnecting | ||
const isConnected = this.props.fsSocketConnected | ||
if (!this.state.showButton) { | ||
return null | ||
} | ||
|
||
let buttonStatusClass | ||
if (isConnecting) { | ||
buttonStatusClass = 'btn-primary' | ||
} else if (isConnected) { | ||
buttonStatusClass = 'btn-success' | ||
} else { | ||
buttonStatusClass = 'btn-danger' | ||
} | ||
|
||
return (<div | ||
className={cx('offline-container btn toggle btn-xs', buttonStatusClass, { | ||
on: isConnecting || isConnected, | ||
off: !isConnecting && !isConnected, | ||
blink: isConnecting, | ||
})} | ||
onClick={() => { | ||
if (isConnected) return | ||
this.setState({ isConnecting: true }) | ||
emitter.emit(E.SOCKET_RETRY) | ||
}} | ||
> | ||
<div className='toggle-group'> | ||
{i18n`global.online`}<div className='toggle-handle' />{i18n`global.offline`} | ||
</div> | ||
</div>) | ||
} | ||
} | ||
|
||
Offline.propTypes = { | ||
fsSocketConnected: PropTypes.bool | ||
} | ||
|
||
export default Offline |
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,61 @@ | ||
.offline-container { | ||
$container-width = 60px; | ||
$toggle-handle-width = 10px; | ||
$button-text-width = $container-width - $toggle-handle-width - 2px; | ||
|
||
padding: 0; | ||
width: $container-width; | ||
height: 16px; | ||
line-height: 1.2; | ||
position: relative; | ||
overflow: hidden; | ||
noSelect() | ||
|
||
.toggle-group { | ||
position: absolute; | ||
display: flex; | ||
align-content: center; | ||
transition: all 0.35s ease; | ||
top: 0; | ||
bottom: 0; | ||
|
||
& > span { | ||
width: $button-text-width; | ||
display: inline-block; | ||
text-align: center; | ||
height: 100% | ||
} | ||
} | ||
|
||
&.on .toggle-group { | ||
left: 0px; | ||
} | ||
|
||
&.off .toggle-group { | ||
left: -1 * $button-text-width; | ||
} | ||
|
||
.toggle-handle { | ||
display: inline-block; | ||
width: $toggle-handle-width; | ||
background-color: #ffffff; | ||
height: 100%; | ||
border-radius: 2px; | ||
} | ||
} | ||
|
||
.blink { | ||
animation: blink 800ms infinite; | ||
} | ||
|
||
@keyframes blink { | ||
0% { | ||
opacity: 1; | ||
} | ||
50% { | ||
opacity: 0.2; | ||
} | ||
100% { | ||
opacity: 1; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -20,3 +20,4 @@ | |
@import "./Accordion"; | ||
@import "./Initialize"; | ||
@import "./Editor"; | ||
@import "./Offline"; |
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,8 @@ | ||
import EventEmitter from 'eventemitter3' | ||
|
||
export default new EventEmitter() | ||
|
||
export const PANEL_RESIZED = 'PANEL_RESIZED' | ||
export const THEME_CHANGED = 'THEME_CHANGED' | ||
export const SOCKET_TRIED_FAILED = 'SOCKET_TRIED_FAILED' | ||
export const SOCKET_RETRY = 'SOCKET_RETRY' |
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