Last active
October 7, 2018 02:08
-
-
Save greim/54ac5e4b33ef711038ae8c6f974c96b2 to your computer and use it in GitHub Desktop.
Motel Setup Example
This file contains hidden or 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
/* | |
* Using `motel` to implement a vacancy observer. | |
* This happens once at app startup time. | |
*/ | |
const motel = require('motel'); | |
const vacancies = motel(); | |
// start observing the DOM for vacancies | |
vacancies.connect(document.getElementById('#app')); | |
// pump vacancy observer output into Redux | |
vacancies.subscribe(action => reduxStore.dispatch(action)); | |
// handle specific vacancy patterns | |
vacancies.listen('users/:id', async({ id }, dispatch) => { | |
dispatch({ type: 'REQUEST_USER', id }); | |
const resp = await fetch(`/users/${id}`); | |
try { | |
const user = await resp.json(); | |
dispatch({ type: 'RECEIVE_USER_DATA', user, id }); | |
} catch(ex) { | |
dispatch({ type: 'RECEIVE_USER_ERROR', ex, id }); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Vacancy observer pattern described here: https://medium.com/@greim/a-plan-for-data-fetching-a68d171af38