Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. import xs, {Stream} from 'xstream';
  2. import {run} from '@cycle/xstream-run';
  3. import isolate from '@cycle/isolate';
  4. import TextInput, {TextInputComponent} from './ui/TextInput';
  5. import {makeDOMDriver, DOMSource, VNode, div} from '@cycle/dom';
  6.  
  7. interface MainSources{
  8. DOM: DOMSource
  9. }
  10.  
  11. interface ModelShape{
  12. username: string
  13. password: string
  14. }
  15.  
  16. interface MainIntents{
  17. change$: Stream<ModelShape>
  18. }
  19.  
  20. interface MainModel{
  21. value$: Stream<ModelShape>
  22. }
  23.  
  24. interface MainSinks{
  25. DOM: Stream<VNode>
  26. }
  27.  
  28. interface MainProps{
  29.  
  30. }
  31.  
  32. interface MainChilds{
  33. username: TextInputComponent
  34. password: TextInputComponent
  35. }
  36.  
  37. function intent(sources: MainSources, props: MainProps, childrens: MainChilds): MainIntents{
  38. return {
  39. change$: xs.combine(
  40. (username, password) => ({username, password}),
  41. childrens.username.change$,
  42. childrens.password.change$
  43. )
  44. };
  45. }
  46.  
  47. function model(intents: MainIntents, childrens: MainChilds): MainModel{
  48. return {
  49. value$: xs.combine(
  50. (username, password) => ({username, password}),
  51. childrens.username.value$,
  52. childrens.password.value$
  53. )
  54. };
  55. }
  56.  
  57. function view(model: MainModel, childrens: MainChilds): MainSinks{
  58. return {
  59. DOM: xs.combine(
  60. (username, password, value) => div([
  61. username,
  62. password,
  63. JSON.stringify(value)
  64. ]),
  65. childrens.username.DOM,
  66. childrens.password.DOM,
  67. model.value$
  68. )
  69. };
  70. }
  71.  
  72. function main(sources: MainSources, props?: MainProps): MainSinks{
  73. const username = isolate(TextInput, 'username')(sources);
  74. const password = isolate(TextInput, 'password')(sources, {value$: username.change$.mapTo('')});
  75.  
  76. const childrens = {username, password};
  77.  
  78. const componentIntents = intent(sources, props, childrens);
  79. const componentModel = model(componentIntents, childrens);
  80.  
  81. return view(componentModel, childrens);
  82. }
  83.  
  84. run(main, {
  85. DOM: makeDOMDriver('#app')
  86. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement