|
1 | | -module React.Basic where |
| 1 | +module React.Basic |
| 2 | + ( Component |
| 3 | + , StatelessComponent |
| 4 | + , ComponentSpec |
| 5 | + , JSX |
| 6 | + , Update |
| 7 | + , StateUpdate(..) |
| 8 | + , Self |
| 9 | + , LimitedSelf |
| 10 | + , ReactComponent |
| 11 | + , ReactComponentInstance |
| 12 | + , make |
| 13 | + , makeStateless |
| 14 | + , asyncEffects |
| 15 | + , createComponent |
| 16 | + , createStatelessComponent |
| 17 | + , fragment |
| 18 | + , fragmentKeyed |
| 19 | + , element |
| 20 | + , elementKeyed |
| 21 | + ) where |
2 | 22 |
|
3 | 23 | import Prelude |
4 | 24 |
|
| 25 | +import Data.Either (Either(..)) |
5 | 26 | import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3) |
6 | 27 | import Data.Nullable (Nullable, notNull, null) |
7 | 28 | import Effect (Effect) |
| 29 | +import Effect.Aff (Aff, runAff_) |
| 30 | +import Effect.Console (error) |
8 | 31 | import Unsafe.Coerce (unsafeCoerce) |
9 | 32 |
|
10 | 33 | -- | A virtual DOM element. |
@@ -42,6 +65,23 @@ data StateUpdate props state action |
42 | 65 | | SideEffects (Self props state action -> Effect Unit) |
43 | 66 | | UpdateAndSideEffects state (Self props state action -> Effect Unit) |
44 | 67 |
|
| 68 | +-- | Convenience function for sending an action asynchronously. |
| 69 | +-- | |
| 70 | +-- | Note: potential failure should be handled and converted to an |
| 71 | +-- | action, as the default error handler will simply log the |
| 72 | +-- | error to the console. |
| 73 | +asyncEffects |
| 74 | + :: forall props state action |
| 75 | + . (Self props state action -> Aff action) |
| 76 | + -> Self props state action |
| 77 | + -> Effect Unit |
| 78 | +asyncEffects work self = runAff_ handle (work self) |
| 79 | + where |
| 80 | + handle (Right action) = self.send action |
| 81 | + handle (Left err) = do |
| 82 | + error "An async action failed." |
| 83 | + error (unsafeCoerce err) |
| 84 | + |
45 | 85 | buildStateUpdate |
46 | 86 | :: forall props state action |
47 | 87 | . StateUpdate props state action |
@@ -125,6 +165,10 @@ type Self props state action = |
125 | 165 | , readProps :: Effect props |
126 | 166 | , readState :: Effect state |
127 | 167 | , send :: action -> Effect Unit |
| 168 | + |
| 169 | + -- | Unsafe, but still frequently better than rewriting a |
| 170 | + -- | whold component in JS |
| 171 | + , instance_ :: ReactComponentInstance |
128 | 172 | } |
129 | 173 |
|
130 | 174 | type LimitedSelf props state = |
|
0 commit comments