Skip to content

Commit

Permalink
add timeout lastfm
Browse files Browse the repository at this point in the history
  • Loading branch information
sneljo1 committed Mar 16, 2019
1 parent 89fea7e commit 3f69655
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
19 changes: 15 additions & 4 deletions src/main/features/core/lastFm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class LastFm extends Feature {
// Authorize
this.on(EVENTS.APP.LASTFM.AUTH, async () => {
try {
await this.getLastFMSession();
await this.getLastFMSession(true);
} catch (err) {
this.logger.error(err);
throw err;
Expand Down Expand Up @@ -215,7 +215,7 @@ export default class LastFm extends Feature {
shell.openExternal(`http://www.last.fm/api/auth/?api_key=${CONFIG.LASTFM_API_KEY}&token=${token}`);

return new Promise((resolve, reject) => {
this.lastfm
const newSession = this.lastfm
.session({
token
} as any, undefined)
Expand Down Expand Up @@ -244,21 +244,32 @@ export default class LastFm extends Feature {

reject(err);
});

setTimeout(
() => {
if (!newSession.isAuthorised()) {
newSession.cancel();
this.store.dispatch(setLastfmLoading(false));
}
},
30000
);
});
} catch (err) {
throw err;
}
}

getLastFMSession = async () => {
getLastFMSession = async (newSession = false) => {
try {

const { config: { lastfm: lastfmConfig } } = this.store.getState();

if (lastfmConfig && lastfmConfig.user && lastfmConfig.key) {
return this.lastfm.session(lastfmConfig.user, lastfmConfig.key);
} else {
} else if (newSession) {
return this.newSession();

}

} catch (err) {
Expand Down
12 changes: 4 additions & 8 deletions src/renderer/pages/settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ interface Props {
}

interface State {
lastfmLoading: boolean;
restartMsg: boolean;
validDir: boolean;
advancedOpen: boolean;
Expand All @@ -41,7 +40,6 @@ class Settings extends React.PureComponent<AllProps, State> {
};

public readonly state: State = {
lastfmLoading: false,
restartMsg: false,
validDir: true,
advancedOpen: false,
Expand Down Expand Up @@ -336,7 +334,7 @@ class Settings extends React.PureComponent<AllProps, State> {
</div>
) : (
<Button
loading={this.state.lastfmLoading}
loading={this.props.lastfmLoading}
onClick={this.authorizeLastFm}
>
Authorize
Expand Down Expand Up @@ -369,16 +367,14 @@ class Settings extends React.PureComponent<AllProps, State> {
}

private authorizeLastFm = () => {
this.setState({
lastfmLoading: true
});
ipcRenderer.send(EVENTS.APP.LASTFM.AUTH);
}
}

const mapStateToProps = ({ config, auth }: StoreState) => ({
const mapStateToProps = ({ config, auth, app }: StoreState) => ({
config,
authenticated: !!config.token && !auth.authentication.loading
authenticated: !!config.token && !auth.authentication.loading,
lastfmLoading: app.lastfmLoading
});

const mapDispatchToProps = (dispatch: Dispatch) => bindActionCreators({
Expand Down

0 comments on commit 3f69655

Please sign in to comment.