react-detect-offline-api
Advanced tools
Comparing version
@@ -11,4 +11,8 @@ /** | ||
onOffline?: Function; | ||
initialStatusCallback?: (status: boolean) => void; | ||
}; | ||
export default class ReactDetectOfflineAPI extends React.Component<Props> { | ||
declare type State = { | ||
online: boolean | null; | ||
}; | ||
export default class ReactDetectOfflineAPI extends React.Component<Props, State> { | ||
private interval; | ||
@@ -24,1 +28,2 @@ state: { | ||
} | ||
export {}; |
@@ -53,12 +53,22 @@ import { createElement, Fragment, Component } from 'react'; | ||
timeout(3000, fetch(apiUrl).then(function () { | ||
_this.setState({ online: true }, function () { | ||
if (_this.props.onOnline) { | ||
_this.setState(function (_a) { | ||
var online = _a.online; | ||
if (_this.props.onOnline && online === false) { | ||
_this.props.onOnline(); | ||
} | ||
if (_this.props.initialStatusCallback && online === null) { | ||
_this.props.initialStatusCallback(true); | ||
} | ||
return { online: true }; | ||
}); | ||
})).catch(function () { | ||
_this.setState({ online: false }, function () { | ||
if (_this.props.onOffline) { | ||
_this.setState(function (_a) { | ||
var online = _a.online; | ||
if (_this.props.onOffline && online === true) { | ||
_this.props.onOffline(); | ||
} | ||
if (_this.props.initialStatusCallback && online === null) { | ||
_this.props.initialStatusCallback(false); | ||
} | ||
return { online: false }; | ||
}); | ||
@@ -65,0 +75,0 @@ }); |
@@ -57,12 +57,22 @@ 'use strict'; | ||
timeout(3000, fetch(apiUrl).then(function () { | ||
_this.setState({ online: true }, function () { | ||
if (_this.props.onOnline) { | ||
_this.setState(function (_a) { | ||
var online = _a.online; | ||
if (_this.props.onOnline && online === false) { | ||
_this.props.onOnline(); | ||
} | ||
if (_this.props.initialStatusCallback && online === null) { | ||
_this.props.initialStatusCallback(true); | ||
} | ||
return { online: true }; | ||
}); | ||
})).catch(function () { | ||
_this.setState({ online: false }, function () { | ||
if (_this.props.onOffline) { | ||
_this.setState(function (_a) { | ||
var online = _a.online; | ||
if (_this.props.onOffline && online === true) { | ||
_this.props.onOffline(); | ||
} | ||
if (_this.props.initialStatusCallback && online === null) { | ||
_this.props.initialStatusCallback(false); | ||
} | ||
return { online: false }; | ||
}); | ||
@@ -69,0 +79,0 @@ }); |
{ | ||
"name": "react-detect-offline-api", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Detect whether your API is online of offline", | ||
@@ -5,0 +5,0 @@ "author": "jvorcak", |
@@ -30,2 +30,5 @@ # react-detect-offline-api | ||
}} | ||
initialStatusCallback={status => { | ||
console.log('Initial status ', status ? 'online' : 'offline') | ||
}} | ||
render={({ online }) => | ||
@@ -50,4 +53,5 @@ online | ||
| render | false | Function | We're using [Render Props](https://reactjs.org/docs/render-props.html) to render online of offline component. See an example for more info. | | ||
| onOnline | false | Function | Called once an online API is detected | | ||
| onOffline | false | Function | Called once an offline API is detected | | ||
| onOnline | false | Function | Called once an API becomes online and was offline | | ||
| onOffline | false | Function | Called once an API becomes offline and was online | | ||
| initialStatusCallback | false | (status: boolean) => void | Calledback that returnes an initial status | | ||
@@ -54,0 +58,0 @@ ## License |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
42036
7.93%234
11.96%59
7.27%