-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
An opinionated way of doing async with alt
- Loading branch information
1 parent
75667a7
commit 33ca0c1
Showing
4 changed files
with
112 additions
and
0 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,67 @@ | ||
import Alt from './' | ||
import { createStore, datasource } from './utils/decorators' | ||
import axios from 'axios' | ||
|
||
const alt = new Alt() | ||
|
||
const StargazerActions = alt.generateActions( | ||
'fetchingUsers', | ||
'usersReceived', | ||
'failed' | ||
) | ||
|
||
const StargazerSource = { | ||
fetchUsers: { | ||
fetch(state) { | ||
const url = `https://api.github.com/repos/${state.user}/${state.repo}/stargazers` | ||
return axios({ url }).then(response => response.data) | ||
}, | ||
|
||
cache(state) { | ||
return state.users.length ? state.users : null | ||
}, | ||
|
||
loading: StargazerActions.fetchingUsers, | ||
|
||
success: StargazerActions.usersReceived, | ||
|
||
error: StargazerActions.failed | ||
} | ||
} | ||
|
||
@createStore(alt) | ||
@datasource(StargazerSource) | ||
class StargazerStore { | ||
constructor() { | ||
this.user = 'goatslacker' | ||
this.repo = 'alt' | ||
this.users = [] | ||
this.errorMessage = null | ||
this.isLoading = false | ||
|
||
this.bindListeners({ | ||
loading: StargazerActions.fetchingUsers, | ||
receivedUsers: StargazerActions.usersReceived, | ||
failed: StargazerActions.failed | ||
}) | ||
} | ||
|
||
loading() { | ||
console.log('STORE: starting to load users') | ||
this.isLoading = true | ||
} | ||
|
||
failed(e) { | ||
console.log('STORE: Uh oh') | ||
this.errorMessage = e.statusText || String(e) | ||
} | ||
|
||
receivedUsers(users) { | ||
console.log('STORE: got users') | ||
this.users = users | ||
this.errorMessage = null | ||
} | ||
} | ||
|
||
StargazerStore.listen((state) => console.log('CHANGED', state.users.length)) | ||
StargazerStore.fetchUsers() |