/
LifeCycle.purs
109 lines (97 loc) · 4.47 KB
/
LifeCycle.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
module React.Queue.LifeCycle where
import Prelude (bind, pure, ($), discard)
import React (ReactSpecAll, ReactClassConstructor)
import Effect.Exception (Error)
import Queue.Types (WRITE)
import Queue (Queue)
import Queue (put) as Queue
import Queue.One (Queue, put) as One
import IxQueue (IxQueue)
import IxQueue (put, broadcastExcept) as IxQueue
data ReactLifeCycle props state snapshot
= DidMount
| WillUnmount
| DidUpdate {props :: props, state :: state, snapshot :: snapshot}
| DidCatch {error :: Error, componentStack :: String}
withLifeCycle :: forall props state snapshot rw
. Queue (write :: WRITE | rw) (ReactLifeCycle props state snapshot)
-> ReactClassConstructor props state (ReactSpecAll props state snapshot)
-> ReactClassConstructor props state (ReactSpecAll props state snapshot)
withLifeCycle q constructor = \this -> do
reactSpec <- constructor this
pure $ reactSpec
{ componentDidMount = do
Queue.put q DidMount
reactSpec.componentDidMount
, componentWillUnmount = do
Queue.put q WillUnmount
reactSpec.componentWillUnmount
, componentDidUpdate = \props state snapshot -> do
Queue.put q $ DidUpdate {props,state,snapshot}
reactSpec.componentDidUpdate props state snapshot
, componentDidCatch = \error params -> do
Queue.put q $ DidCatch {error, componentStack: params.componentStack}
reactSpec.componentDidCatch error params
}
withLifeCycleOne :: forall props state snapshot rw
. One.Queue (write :: WRITE | rw) (ReactLifeCycle props state snapshot)
-> ReactClassConstructor props state (ReactSpecAll props state snapshot)
-> ReactClassConstructor props state (ReactSpecAll props state snapshot)
withLifeCycleOne q constructor = \this -> do
reactSpec <- constructor this
pure $ reactSpec
{ componentDidMount = do
One.put q DidMount
reactSpec.componentDidMount
, componentWillUnmount = do
One.put q WillUnmount
reactSpec.componentWillUnmount
, componentDidUpdate = \props state snapshot -> do
One.put q $ DidUpdate {props,state,snapshot}
reactSpec.componentDidUpdate props state snapshot
, componentDidCatch = \error params -> do
One.put q $ DidCatch {error, componentStack: params.componentStack}
reactSpec.componentDidCatch error params
}
withLifeCycleIx :: forall props state snapshot rw
. String
-> IxQueue (write :: WRITE | rw) (ReactLifeCycle props state snapshot)
-> ReactClassConstructor props state (ReactSpecAll props state snapshot)
-> ReactClassConstructor props state (ReactSpecAll props state snapshot)
withLifeCycleIx k q constructor = \this -> do
reactSpec <- constructor this
pure $ reactSpec
{ componentDidMount = do
IxQueue.put q k DidMount
reactSpec.componentDidMount
, componentWillUnmount = do
IxQueue.put q k WillUnmount
reactSpec.componentWillUnmount
, componentDidUpdate = \props state snapshot -> do
IxQueue.put q k $ DidUpdate {props,state,snapshot}
reactSpec.componentDidUpdate props state snapshot
, componentDidCatch = \error params -> do
IxQueue.put q k $ DidCatch {error, componentStack: params.componentStack}
reactSpec.componentDidCatch error params
}
withLifeCycleBroadcastIx :: forall props state snapshot rw
. Array String -- exception keys
-> IxQueue (write :: WRITE | rw) (ReactLifeCycle props state snapshot)
-> ReactClassConstructor props state (ReactSpecAll props state snapshot)
-> ReactClassConstructor props state (ReactSpecAll props state snapshot)
withLifeCycleBroadcastIx ks q constructor = \this -> do
reactSpec <- constructor this
pure $ reactSpec
{ componentDidMount = do
IxQueue.broadcastExcept q ks DidMount
reactSpec.componentDidMount
, componentWillUnmount = do
IxQueue.broadcastExcept q ks WillUnmount
reactSpec.componentWillUnmount
, componentDidUpdate = \props state snapshot -> do
IxQueue.broadcastExcept q ks $ DidUpdate {props,state,snapshot}
reactSpec.componentDidUpdate props state snapshot
, componentDidCatch = \error params -> do
IxQueue.broadcastExcept q ks $ DidCatch {error, componentStack: params.componentStack}
reactSpec.componentDidCatch error params
}