Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. module React.RecSpec where
  2.  
  3. import Control.Monad.Eff (Eff, kind Effect)
  4. import Data.Maybe (Maybe(..), fromMaybe)
  5. import Data.Record as R
  6. import Prelude (Unit, bind, pure, show, unit, ($))
  7. import React (Disallowed, ReactElement, ReactProps, ReactRefs, ReactState, ReadOnly, ReadWrite)
  8. import React.DOM as D
  9. import Type.Data.Symbol (class IsSymbol, SProxy(..))
  10. import Type.Row (class RowLacks)
  11.  
  12. newtype This props state (r :: # Type) = This (Record r)
  13.  
  14. set
  15. :: forall l a b p s r r1 r2 eff
  16. . IsSymbol l
  17. => RowCons l a r r1
  18. => RowCons l b r r2
  19. => SProxy l
  20. -> b
  21. -> This p s r1
  22. -> Eff eff (This p s r2)
  23. set l b (This r) = pure (This (R.set l b r))
  24.  
  25. insert
  26. :: forall r1 r2 l a p s eff
  27. . IsSymbol l
  28. => RowLacks l r1
  29. => RowCons l a r1 r2
  30. => SProxy l
  31. -> a
  32. -> This p s r1
  33. -> Eff eff (This p s r2)
  34. insert l a (This r) = pure (This (R.insert l a r))
  35.  
  36. get
  37. :: forall r r' l a p s eff
  38. . IsSymbol l
  39. => RowCons l a r' r
  40. => SProxy l
  41. -> This p s r
  42. -> Eff eff a
  43. get l (This r) = pure $ R.get l r
  44.  
  45. delete
  46. :: forall r1 r2 l a p s eff
  47. . IsSymbol l
  48. => RowLacks l r1
  49. => RowCons l a r1 r2
  50. => SProxy l
  51. -> This p s r2
  52. -> Eff eff (This p s r1)
  53. delete l (This r) = pure (This (R.delete l r))
  54.  
  55. -- | A render function.
  56. type Render props state r eff =
  57. This props state r ->
  58. Eff
  59. ( props :: ReactProps
  60. , refs :: ReactRefs Disallowed
  61. , state :: ReactState ReadOnly
  62. | eff
  63. ) ReactElement
  64.  
  65. -- | A get initial state function.
  66. type GetInitialState props state r eff =
  67. This props state r ->
  68. Eff
  69. ( props :: ReactProps
  70. , state :: ReactState Disallowed
  71. , refs :: ReactRefs Disallowed
  72. | eff
  73. ) state
  74.  
  75. -- | A component will mount function.
  76. type ComponentWillMount props state r eff =
  77. This props state () ->
  78. Eff
  79. ( props :: ReactProps
  80. , state :: ReactState ReadWrite
  81. , refs :: ReactRefs Disallowed
  82. | eff
  83. ) (This props state r)
  84.  
  85. -- | A component did mount function.
  86. type ComponentDidMount props state r eff =
  87. This props state r ->
  88. Eff
  89. ( props :: ReactProps
  90. , state :: ReactState ReadWrite
  91. , refs :: ReactRefs ReadOnly
  92. | eff
  93. ) Unit
  94.  
  95. -- | A component will receive props function.
  96. type ComponentWillReceiveProps props state r eff =
  97. This props state r ->
  98. props ->
  99. Eff
  100. ( props :: ReactProps
  101. , state :: ReactState ReadWrite
  102. , refs :: ReactRefs ReadOnly
  103. | eff
  104. ) Unit
  105.  
  106. -- | A should component update function.
  107. type ShouldComponentUpdate props state r eff =
  108. This props state r ->
  109. props ->
  110. state ->
  111. Eff
  112. ( props :: ReactProps
  113. , state :: ReactState ReadWrite
  114. , refs :: ReactRefs ReadOnly
  115. | eff
  116. ) Boolean
  117.  
  118. -- | A component will update function.
  119. type ComponentWillUpdate props state r eff =
  120. This props state r ->
  121. props ->
  122. state ->
  123. Eff
  124. ( props :: ReactProps
  125. , state :: ReactState ReadWrite
  126. , refs :: ReactRefs ReadOnly
  127. | eff
  128. ) Unit
  129.  
  130. -- | A component did update function.
  131. type ComponentDidUpdate props state r eff =
  132. This props state r ->
  133. props ->
  134. state ->
  135. Eff
  136. ( props :: ReactProps
  137. , state :: ReactState ReadOnly
  138. , refs :: ReactRefs ReadOnly
  139. | eff
  140. ) Unit
  141.  
  142. -- | A component will unmount function.
  143. type ComponentWillUnmount props state r1 eff =
  144. -- Subrow r2 r1 =>
  145. This props state r1 ->
  146. Eff
  147. ( props :: ReactProps
  148. , state :: ReactState ReadOnly
  149. , refs :: ReactRefs ReadOnly
  150. | eff
  151. ) (This props state ())
  152.  
  153. type Spec p s (r :: # Type) (eff :: # Effect) =
  154. { render :: Render p s r eff
  155. , displayName :: String
  156. , getInitialState :: GetInitialState p s r eff
  157. , componentWillMount :: ComponentWillMount p s r eff
  158. , componentDidMount :: ComponentDidMount p s r eff
  159. , componentWillReceiveProps :: ComponentWillReceiveProps p s r eff
  160. , shouldComponentUpdate :: ShouldComponentUpdate p s r eff
  161. , componentWillUpdate :: ComponentWillUpdate p s r eff
  162. , componentDidUpdate :: ComponentDidUpdate p s r eff
  163. , componentWillUnmount :: ComponentWillUnmount p s r eff
  164. }
  165.  
  166. spec'
  167. :: forall p s r eff
  168. . GetInitialState p s r eff
  169. -> ComponentWillMount p s r eff
  170. -> ComponentWillUnmount p s r eff
  171. -> Render p s r eff
  172. -> Spec p s r eff
  173. spec' getInitialState componentWillMount componentWillUnmount renderFn =
  174. { render: renderFn
  175. , displayName: ""
  176. , getInitialState: getInitialState
  177. , componentWillMount: componentWillMount
  178. , componentDidMount: \_ -> pure unit
  179. , componentWillReceiveProps: \_ _ -> pure unit
  180. , shouldComponentUpdate: \_ _ _ -> pure true
  181. , componentWillUpdate: \_ _ _ -> pure unit
  182. , componentDidUpdate: \_ _ _ -> pure unit
  183. , componentWillUnmount: componentWillUnmount
  184. }
  185.  
  186. spec
  187. :: forall p s eff
  188. . s
  189. -> Render p s () eff
  190. -> Spec p s () eff
  191. spec s r = spec' (\_ -> pure s) pure pure r
  192.  
  193. -- test
  194. cSpec :: forall eff. Spec Unit Unit (count :: Maybe Int) eff
  195. cSpec = (spec' (\_ -> pure unit) componentWillMount componentWillUnmount render)
  196. { componentWillMount = componentWillMount
  197. }
  198. where
  199. componentWillMount this = do
  200. insert (SProxy :: SProxy "count") (Just 0) this
  201.  
  202. componentWillUnmount this = do
  203. delete (SProxy :: SProxy "count") this
  204.  
  205. render this = do
  206. c <- get (SProxy :: SProxy "count") this
  207. pure $ D.div' [ D.text (show (fromMaybe 0 c)) ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement