Skip to content

Commit e25d5a4

Browse files
committed
Some reorganising, and more docs
1 parent cfe7b77 commit e25d5a4

15 files changed

+530
-155
lines changed

README.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Halo is a [Halogen](https://github.com/purescript-halogen/purescript-halogen)-in
44

55
It is implemented as a hook: `useHalo`; and simple component helpers are included: `component` and `component_`.
66

7+
## Documentation
8+
9+
Module documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-react-halo).
10+
711
## Using with [Spago](https://github.com/purescript/spago)
812

913
Update the additions in your `packages.dhall`:
@@ -13,7 +17,7 @@ let additions =
1317
{ react-halo =
1418
{ dependencies = [ "aff", "free", "freeap", "react-basic-hooks", "wire" ]
1519
, repo = "https://github.com/robertdp/purescript-react-halo.git"
16-
, version = "v0.2.2"
20+
, version = "v0.2.3"
1721
}
1822
, wire =
1923
{ dependencies = [ "aff", "filterable", "refs", "unsafe-reference" ]
@@ -27,10 +31,6 @@ Then install with Spago:
2731

2832
`$ spago install react-halo`
2933

30-
## Generated documentation
31-
32-
See [here](https://github.com/robertdp/purescript-react-halo/blob/master/generated-docs/md/React.Halo.md)
33-
3434
## What does Halo provide?
3535

3636
Whether you are using the hook or one of the component helpers, the main feature that Halo provides is the `eval` function. It looks like:
@@ -51,7 +51,7 @@ data Lifecycle props action
5151

5252
`HaloM` is also a monad transformer, and so you can lift any monad `m` logic into `HaloM`. Just be aware that in order to run the logic, Halo requires that you `hoist` (convert) your chosen monad into `Aff` before returning it.
5353

54-
## Hoisting
54+
### Hoisting
5555

5656
```purescript
5757
hoist :: forall props state action m m'. Functor m => (m ~> m') -> HaloM props state action m ~> HaloM props state action m'
@@ -63,7 +63,7 @@ Example:
6363
invertReaderT x = ReaderT \env -> Halo.hoist (flip runReaderT env) x
6464
```
6565

66-
## Working with props
66+
### Working with props
6767

6868
```purescript
6969
props :: forall props action state m. HaloM props state action m props
@@ -77,13 +77,13 @@ fireOnChange value = do
7777
onChange value
7878
```
7979

80-
## Working with state
80+
### Working with state
8181

8282
`HaloM` doesn't have any special interface for reading and modifying state, instead providing an instance of [MonadState](https://pursuit.purescript.org/packages/purescript-transformers/docs/Control.Monad.State.Class) for flexibility.
8383

84-
## Subscriptions
84+
### Subscriptions
8585

86-
`HaloM` also provides functions for subscriptions management:
86+
Subscriptions registered using these functions are automatically tracked by Halo.
8787

8888
```purescript
8989
subscribe :: forall props state action m. Event action -> HaloM props state action m SubscriptionId
@@ -101,9 +101,9 @@ subscribe' :: forall m action state props. (SubscriptionId -> Event action) -> H
101101

102102
Any subscriptions that remain when the component is unmounted are automatically unsubscribed. This prevents requiring manual clean up in the `Finalize` lifecycle event. Also note that new subscriptions will not be created once the `Finalize` event has been fired.
103103

104-
## Parallelism
104+
### Forking
105105

106-
And finally, `HaloM` provides functions for creating and killing forks which run in parallel (or as useful an approximation as we can get in JavaScript):
106+
Also provided are functions for creating and killing forks which launch processes in separate "threads" (or as useful an approximation as we can get in JavaScript):
107107

108108
```purescript
109109
fork :: forall m action state props. HaloM props state action m Unit -> HaloM props state action m ForkId
@@ -112,3 +112,8 @@ kill :: forall m action state props. ForkId -> HaloM props state action m Unit
112112
```
113113

114114
Similarly to subscriptions, when the component unmounts all still-running forks will be killed. However new forks _can_ be created during the `Finalize` phase but there is no way of killing them (as with Halogen).
115+
116+
117+
### Parallelism
118+
119+
Finally `HaloM` provides an instance of `Parallel` for converting back and forth between `HaloAp`, it's applicative counterpart. This allows any logic to be easily converted to run in `parallel` or `sequential`ly.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
## Module React.Halo.Component
2+
3+
#### `HookSpec`
4+
5+
``` purescript
6+
type HookSpec props state action m = { eval :: Lifecycle props action -> HaloM props state action m Unit, initialState :: state, props :: props }
7+
```
8+
9+
#### `UseHalo`
10+
11+
``` purescript
12+
newtype UseHalo props state action hooks
13+
= UseHalo (UseEffect Unit (UseEffect Unit (UseMemo Unit (HaloState props state action) (UseState state hooks))))
14+
```
15+
16+
##### Instances
17+
``` purescript
18+
Newtype (UseHalo props state action hooks) _
19+
```
20+
21+
#### `useHalo`
22+
23+
``` purescript
24+
useHalo :: forall state action props. HookSpec props state action Aff -> Hook (UseHalo props state action) (state /\ (action -> Effect Unit))
25+
```
26+
27+
Run renderless Halo in the current component. This allows Halo to be used with other hooks and other ways of
28+
building components.
29+
30+
#### `ComponentSpec`
31+
32+
``` purescript
33+
type ComponentSpec props state action m = { eval :: Lifecycle props action -> HaloM props state action m Unit, initialState :: state, render :: { props :: props, send :: action -> Effect Unit, state :: state } -> JSX }
34+
```
35+
36+
#### `component`
37+
38+
``` purescript
39+
component :: forall state action props. String -> ComponentSpec props state action Aff -> Effect (props -> JSX)
40+
```
41+
42+
Build a component by providing a name and Halo component spec.
43+
44+
#### `component_`
45+
46+
``` purescript
47+
component_ :: forall state action. String -> ComponentSpec Unit state action Aff -> Effect JSX
48+
```
49+
50+
Build a propless component by providing a name and Halo component spec.
51+
52+

generated-docs/md/React.Halo.Internal.Control.md

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ data HaloF props state action m a
1414
| Kill ForkId a
1515
```
1616

17+
The Halo evaluation algebra
18+
19+
- `props` are the component props
20+
- `state` is the component state
21+
- `action` is the set of actions that the component handles
22+
- `m` is the monad used during evaluation
23+
- `a` is the result type
24+
1725
##### Instances
1826
``` purescript
1927
(Functor m) => Functor (HaloF props state action m)
@@ -26,6 +34,14 @@ newtype HaloM props state action m a
2634
= HaloM (Free (HaloF props state action m) a)
2735
```
2836

37+
The Halo evaluation monad. It lifts the `HaloF` algebra into a free monad.
38+
39+
- `props` are the component props
40+
- `state` is the component state
41+
- `action` is the set of actions that the component handles
42+
- `m` is the monad used during evaluation
43+
- `a` is the result type
44+
2945
##### Instances
3046
``` purescript
3147
Functor (HaloM props state action m)
@@ -53,6 +69,14 @@ newtype HaloAp props state action m a
5369
= HaloAp (FreeAp (HaloM props state action m) a)
5470
```
5571

72+
The Halo parallel evaluation applicative. It lifts `HaloM` into a free applicative.
73+
74+
- `props` are the component props
75+
- `state` is the component state
76+
- `action` is the set of actions that the component handles
77+
- `m` is the monad used during evaluation
78+
- `a` is the result type
79+
5680
##### Instances
5781
``` purescript
5882
Newtype (HaloAp props state action m a) _
@@ -68,40 +92,62 @@ Parallel (HaloAp props state action m) (HaloM props state action m)
6892
hoist :: forall props state action m m'. Functor m => (m ~> m') -> (HaloM props state action m) ~> (HaloM props state action m')
6993
```
7094

95+
Hoist (transform) the base monad of a `HaloM` expression.
96+
7197
#### `props`
7298

7399
``` purescript
74100
props :: forall props m action state. HaloM props state action m props
75101
```
76102

77-
#### `subscribe'`
103+
Read the current props.
104+
105+
#### `subscribe`
78106

79107
``` purescript
80-
subscribe' :: forall m action state props. (SubscriptionId -> Event action) -> HaloM props state action m SubscriptionId
108+
subscribe :: forall props state action m. Event action -> HaloM props state action m SubscriptionId
81109
```
82110

83-
#### `subscribe`
111+
Subscribe to new actions from an `Event`. Subscriptions will be automatically cancelled when the component
112+
unmounts.
113+
114+
Returns a `SubscriptionId` which can be used with `unsubscribe` to manually cancel a subscription.
115+
116+
#### `subscribe'`
84117

85118
``` purescript
86-
subscribe :: forall props state action m. Event action -> HaloM props state action m SubscriptionId
119+
subscribe' :: forall m action state props. (SubscriptionId -> Event action) -> HaloM props state action m SubscriptionId
87120
```
88121

122+
Same as `subscribe` but the event-producing logic is also passed the `SuscriptionId`. This is useful when events
123+
need to unsubscribe themselves.
124+
89125
#### `unsubscribe`
90126

91127
``` purescript
92128
unsubscribe :: forall m action state props. SubscriptionId -> HaloM props state action m Unit
93129
```
94130

131+
Cancels the event subscription belonging to the `SubscriptionId`.
132+
95133
#### `fork`
96134

97135
``` purescript
98136
fork :: forall m action state props. HaloM props state action m Unit -> HaloM props state action m ForkId
99137
```
100138

139+
Start a `HaloM` process running independantly from the current "thread". Forks are tracked automatically and
140+
killed when the `Finalize` event occurs (when the component unmounts). New forks can still be created during the
141+
`Finalize` event, but once evaluation ends there will be no way of killing them.
142+
143+
Returns a `ForkId` for the new process.
144+
101145
#### `kill`
102146

103147
``` purescript
104148
kill :: forall m action state props. ForkId -> HaloM props state action m Unit
105149
```
106150

151+
Kills the process belonging to the `ForkId`.
152+
107153

generated-docs/md/React.Halo.Internal.Eval.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,49 @@
66
evalHaloM :: forall props state action. HaloState props state action -> (HaloM props state action Aff) ~> Aff
77
```
88

9+
Interprets `HaloM` into the base monad `Aff`.
10+
911
#### `evalHaloF`
1012

1113
``` purescript
1214
evalHaloF :: forall props state action. HaloState props state action -> (HaloF props state action Aff) ~> Aff
1315
```
1416

17+
Interprets `HaloF` into the base monad `Aff`, keeping track of state in `HaloState`.
18+
1519
#### `EvalSpec`
1620

1721
``` purescript
18-
type EvalSpec props action state m = { onAction :: action -> HaloM props state action m Unit, onFinalize :: Maybe action, onInitialize :: props -> Maybe action, onUpdate :: props -> props -> Maybe action }
22+
type EvalSpec props state action m = { onAction :: action -> HaloM props state action m Unit, onFinalize :: Maybe action, onInitialize :: props -> Maybe action, onUpdate :: props -> props -> Maybe action }
1923
```
2024

25+
A simpler interface for building the components eval function. The main lifecycle events map directly into
26+
actions, so only the action handling logic needs to be written using `HaloM`.
27+
2128
#### `defaultEval`
2229

2330
``` purescript
24-
defaultEval :: forall props action state m. EvalSpec props action state m
31+
defaultEval :: forall props action state m. EvalSpec props state action m
2532
```
2633

34+
The empty `EvalSpec`.
35+
2736
#### `makeEval`
2837

2938
``` purescript
30-
makeEval :: forall props action state m. EvalSpec props action state m -> Lifecycle props action -> HaloM props state action m Unit
39+
makeEval :: forall m action state props. (EvalSpec props state action m -> EvalSpec props state action m) -> Lifecycle props action -> HaloM props state action m Unit
3140
```
3241

42+
Given an `EvalSpec` builder, it will return an eval function.
43+
3344
#### `runAff`
3445

3546
``` purescript
3647
runAff :: Aff Unit -> Effect Unit
3748
```
3849

50+
Simple way to run Aff logic asynchronously, while bringing errors back into Effect.
51+
3952
#### `runInitialize`
4053

4154
``` purescript

generated-docs/md/React.Halo.Internal.State.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,22 @@ newtype HaloState props state action
77
= HaloState { eval :: Lifecycle props action -> HaloM props state action Aff Unit, forks :: Ref (Map ForkId (Fiber Unit)), fresh :: Ref Int, props :: Ref props, render :: state -> Effect Unit, state :: Ref state, subscriptions :: Ref (Map SubscriptionId (Effect Unit)), unmounted :: Ref Boolean }
88
```
99

10+
HThe alo component state used during evaluation.
11+
1012
#### `createInitialState`
1113

1214
``` purescript
1315
createInitialState :: forall props state action. state -> (Lifecycle props action -> HaloM props state action Aff Unit) -> (state -> Effect Unit) -> props -> Effect (HaloState props state action)
1416
```
1517

18+
Creates a starting `HaloState`, ready for initialization.
19+
1620
#### `fresh`
1721

1822
``` purescript
1923
fresh :: forall props state action a. (Int -> a) -> HaloState props state action -> Effect a
2024
```
2125

26+
Issue a new identifier, unique to this component.
27+
2228

generated-docs/md/React.Halo.Internal.Types.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ data Lifecycle props action
1010
| Finalize
1111
```
1212

13+
The Halo lifecycle events.
14+
15+
- `Initialize` contains the initial props. It occurs when the component mounts, and only once per component.
16+
- `Update` contains the previous and new props. It occurs when the component re-renders and the props have changes.
17+
- `Action` contains the dispatched action. It occurs each time an action is dispatched to be eval'd, up until the
18+
`Finalize` event
19+
- `Finalize` occurs when the component unmounts.
20+
1321
#### `SubscriptionId`
1422

1523
``` purescript

0 commit comments

Comments
 (0)