Skip to content

Commit 21292cf

Browse files
author
Michael Trotter
committed
Clean up capture/monitor
1 parent 3560a7d commit 21292cf

4 files changed

Lines changed: 39 additions & 15 deletions

File tree

examples/component/src/ToggleButton.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ render = make component
2828

2929
, render = \self ->
3030
R.button
31-
{ onClick: self.capture identity $ const Toggle
31+
{ onClick: self.capture_ Toggle
3232
, children:
3333
[ R.text self.props.label
3434
, R.text if self.state.on

examples/counter/src/Counter.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ render = make component
2222

2323
, render = \self ->
2424
R.button
25-
{ onClick: self.capture identity $ const Increment
25+
{ onClick: self.capture_ Increment
2626
, children: [ R.text (self.props.label <> ": " <> show self.state.counter) ]
2727
}
2828
}

src/React/Basic.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ exports.createComponent_ = function(
77
noUpdate,
88
buildStateUpdate,
99
handler,
10-
composeCancel,
10+
composeCancelEventFn,
11+
identityEventFn,
1112
displayName
1213
) {
1314
function contextToSelf(instance) {
@@ -52,11 +53,12 @@ exports.createComponent_ = function(
5253
};
5354
},
5455
capture: function(eventFn) {
55-
return function(makeAction) {
56-
return handler(composeCancel(eventFn))(function(event) {
57-
return self.send(makeAction(event));
58-
});
59-
};
56+
return self.monitor(composeCancelEventFn(eventFn));
57+
},
58+
capture_: function(action) {
59+
return self.capture(identityEventFn)(function() {
60+
return action;
61+
});
6062
},
6163
monitor: function(eventFn) {
6264
return function(makeAction) {
@@ -65,6 +67,11 @@ exports.createComponent_ = function(
6567
});
6668
};
6769
},
70+
monitor_: function(action) {
71+
return self.monitor(identityEventFn)(function() {
72+
return action;
73+
});
74+
},
6875
instance_: instance
6976
};
7077
return self;
@@ -114,9 +121,10 @@ exports.createComponent_ = function(
114121
Component.displayName = displayName;
115122

116123
Component.prototype.shouldComponentUpdate = function(nextProps, nextState) {
117-
return this.$$spec.shouldUpdate(contextToSelf(this))(nextProps.$$props)(
118-
nextState === null ? null : nextState.$$state
119-
);
124+
return this.$$spec.shouldUpdate({
125+
props: this.props.$$props,
126+
state: this.state === null ? null : this.state.$$state
127+
})(nextProps.$$props)(nextState === null ? null : nextState.$$state);
120128
};
121129

122130
Component.prototype.componentDidMount = function() {
@@ -129,7 +137,10 @@ exports.createComponent_ = function(
129137

130138
Component.prototype.componentWillUnmount = function() {
131139
this.$$mounted = false;
132-
return this.$$spec.willUnmount(contextToSelf(this))();
140+
return this.$$spec.willUnmount({
141+
props: this.props.$$props,
142+
state: this.state === null ? null : this.state.$$state
143+
})();
133144
};
134145

135146
Component.prototype.render = function() {

src/React/Basic.purs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module React.Basic
2525
import Prelude
2626

2727
import Data.Either (Either(..))
28-
import Data.Function.Uncurried (Fn2, Fn5, runFn2, runFn5)
28+
import Data.Function.Uncurried (Fn2, Fn6, runFn2, runFn6)
2929
import Data.Nullable (Nullable, notNull, null)
3030
import Effect (Effect)
3131
import Effect.Aff (Aff, runAff_)
@@ -79,8 +79,10 @@ type Self props state action =
7979
-- |
8080
-- | *capturing: prevent default and stop propagation
8181
, capture :: forall a. EventFn SyntheticEvent a -> (a -> action) -> EventHandler
82+
, capture_ :: action -> EventHandler
8283
-- | Like `capture`, but does not cancel the event.
8384
, monitor :: forall a. EventFn SyntheticEvent a -> (a -> action) -> EventHandler
85+
, monitor_ :: action -> EventHandler
8486

8587
-- | Unsafe, but still frequently better than rewriting a
8688
-- | whold component in JS
@@ -196,18 +198,29 @@ createComponent
196198
:: forall props state action
197199
. String
198200
-> ComponentSpec props state Unit action
199-
createComponent = runFn5 createComponent_ NoUpdate buildStateUpdate handler ((preventDefault >>> stopPropagation) >>> _)
201+
createComponent =
202+
runFn6
203+
createComponent_
204+
NoUpdate
205+
buildStateUpdate
206+
handler
207+
((preventDefault >>> stopPropagation) >>> _)
208+
identity
200209

201210
foreign import createComponent_
202211
:: forall props state action
203-
. Fn5
212+
. Fn6
204213
(StateUpdate props state action)
205214
(StateUpdate props state action
206215
-> { state :: Nullable state
207216
, effects :: Nullable (Self props state action -> Effect Unit)
208217
})
218+
-- handler
209219
(forall a. EventFn SyntheticEvent a -> (a -> Effect Unit) -> EventHandler)
220+
-- composeCancelEventFn
210221
(forall a. EventFn SyntheticEvent a -> EventFn SyntheticEvent a)
222+
-- identityEventFn
223+
(EventFn SyntheticEvent SyntheticEvent)
211224
String
212225
(ComponentSpec props state Unit action)
213226

0 commit comments

Comments
 (0)