Skip to content

Instantly share code, notes, and snippets.

@franciscotln
Last active October 7, 2016 22:30
Show Gist options
  • Save franciscotln/f1f567156b8a2f16e0268a568b0a0abb to your computer and use it in GitHub Desktop.
Save franciscotln/f1f567156b8a2f16e0268a568b0a0abb to your computer and use it in GitHub Desktop.
using storageDriver for Cycle.js
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);
@franciscotln
Copy link
Author

example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment