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