Last active
October 7, 2016 22:30
-
-
Save franciscotln/f1f567156b8a2f16e0268a568b0a0abb to your computer and use it in GitHub Desktop.
using storageDriver for Cycle.js
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
function main({ DOM, HTTP, storage }) { | |
const loginRequest$ = Observable.of({ | |
category: 'tokens', | |
url: 'someUrl', | |
method: 'POST', | |
send: { | |
data: { | |
type: 'tokens', | |
attributes: { | |
email: 'some@email.io', | |
password: '3.1415' | |
} | |
} | |
} | |
}); | |
const response$ = HTTP.select('tokens').switch() | |
const data$ = response$.map(response => response.body.data); | |
const user$ = data$.map(data => ({ | |
email: data.attributes.email, | |
isAuthenticated: !!data.meta.token | |
})) | |
.share(); | |
const storage$ = user$.combineLatest(storage, (user, ls) => ls.set('user', user)); | |
const userFromStorage$ = storage.map(ls => ls.get('user')).filter(user => user); | |
const vTree$ = user$.combineLatest(userFromStorage$, (user1, user2) => | |
div([ | |
h4('User from HTTP: ' + user1.email), | |
h4('User from local storage: ' + user2.email) | |
]) | |
); | |
return { | |
DOM: vTree$, | |
HTTP: loginRequest$, | |
storage: storage$ | |
}; | |
} | |
const drivers = { | |
DOM: makeDOMDriver('body'), | |
HTTP: makeHTTPDriver(), | |
storage: makeStorageDriver('local') | |
}; | |
const disposeApp = run(main, drivers); |
Author
franciscotln
commented
Oct 7, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment