Skip to content

Commit

Permalink
Avoid new function creation in PanResponder
Browse files Browse the repository at this point in the history
This follows the discussion in facebook#37
  • Loading branch information
saschatimme committed Jun 27, 2017
1 parent 3b8f195 commit b63e3e8
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 63 deletions.
106 changes: 60 additions & 46 deletions src/panResponderRe.re
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
type t;

type gestureState =
{
type gestureState = {
stateID: float,
moveX: float,
moveY: float,
x0: float,
y0: float,
dx: float,
dy: float,
vx: float,
vy: float,
numberActiveTouches: int
};

external _create : 'a => t = "create" [@@bs.scope "PanResponder"] [@@bs.module "react-native"];

type jsGestureState =
Js.t {
.
stateID : float,
moveX : float,
moveY : float,
Expand All @@ -14,9 +30,24 @@ type gestureState =
numberActiveTouches : int
};

type callback 'a = RNEvent.NativeEvent.t => gestureState => 'a;
type callback 'a = Js.t {. nativeEvent : RNEvent.NativeEvent.t} => jsGestureState => 'a;

external _create : 'a => t = "create" [@@bs.scope "PanResponder"] [@@bs.module "react-native"];
let callback (x: RNEvent.NativeEvent.t => gestureState => 'a) :callback 'a =>
fun event state =>
x
event##nativeEvent
{
dx: state##dx,
dy: state##dy,
moveX: state##moveX,
moveY: state##moveY,
numberActiveTouches: state##numberActiveTouches,
stateID: state##stateID,
vx: state##vx,
vy: state##vy,
x0: state##x0,
y0: state##y0
};

external shamelesslyWrapCallback : 'a => callback unit = "%identity";

Expand All @@ -34,27 +65,10 @@ let animatedEvent l => {
(Js.Obj.empty ())
l;
shamelesslyWrapCallback (
AnimatedRe.event (Array.of_list [Js.null, Js.Null.return config]) {"useNativeDriver": Js.false_}
AnimatedRe.event [|Js.null, Js.Null.return config|] {"useNativeDriver": Js.false_}
)
};

let convertCallback x =>
switch x {
| None => Js.undefined;
| Some x => Js.Undefined.return (fun event state => x event##nativeEvent {
dx: state##dx,
dy: state##dy,
moveX: state##moveX,
moveY: state##moveY,
numberActiveTouches: state##numberActiveTouches,
stateID: state##stateID,
vx: state##vx,
vy: state##vy,
x0: state##x0,
y0: state##y0
});
};

let create
onMoveShouldSetPanResponder::(onMoveShouldSetPanResponder: option (callback bool))=?
onMoveShouldSetPanResponderCapture::
Expand All @@ -72,27 +86,27 @@ let create
onPanResponderTerminationRequest::(onPanResponderTerminationRequest: option (callback bool))=?
onShouldBlockNativeResponder::(onShouldBlockNativeResponder: option (callback bool))=?
() =>
_create
{
"onMoveShouldSetPanResponder": convertCallback onMoveShouldSetPanResponder,
"onMoveShouldSetPanResponderCapture": convertCallback onMoveShouldSetPanResponderCapture,
"onStartShouldSetPanResponder": convertCallback onStartShouldSetPanResponder,
"onStartShouldSetPanResponderCapture": convertCallback onStartShouldSetPanResponderCapture,
"onPanResponderReject": convertCallback onPanResponderReject,
"onPanResponderGrant": convertCallback onPanResponderGrant,
"onPanResponderStart": convertCallback onPanResponderStart,
"onPanResponderEnd": convertCallback onPanResponderEnd,
"onPanResponderRelease": convertCallback onPanResponderRelease,
"onPanResponderMove":
switch onPanResponderMove {
| None => Js.undefined
| Some x =>
switch x {
| `update l => Js.Undefined.return (animatedEvent l)
| `callback (x: callback unit) => Js.Undefined.return x
}
},
"onPanResponderTerminate": convertCallback onPanResponderTerminate,
"onPanResponderTerminationRequest": convertCallback onPanResponderTerminationRequest,
"onShouldBlockNativeResponder": convertCallback onShouldBlockNativeResponder
};
_create {
"onMoveShouldSetPanResponder": Js.Undefined.from_opt onMoveShouldSetPanResponder,
"onMoveShouldSetPanResponderCapture": Js.Undefined.from_opt onMoveShouldSetPanResponderCapture,
"onStartShouldSetPanResponder": Js.Undefined.from_opt onStartShouldSetPanResponder,
"onStartShouldSetPanResponderCapture":
Js.Undefined.from_opt onStartShouldSetPanResponderCapture,
"onPanResponderReject": Js.Undefined.from_opt onPanResponderReject,
"onPanResponderGrant": Js.Undefined.from_opt onPanResponderGrant,
"onPanResponderStart": Js.Undefined.from_opt onPanResponderStart,
"onPanResponderEnd": Js.Undefined.from_opt onPanResponderEnd,
"onPanResponderRelease": Js.Undefined.from_opt onPanResponderRelease,
"onPanResponderMove":
switch onPanResponderMove {
| None => Js.undefined
| Some x =>
switch x {
| `update l => Js.Undefined.return (animatedEvent l)
| `callback (x: callback unit) => Js.Undefined.return x
}
},
"onPanResponderTerminate": Js.Undefined.from_opt onPanResponderTerminate,
"onPanResponderTerminationRequest": Js.Undefined.from_opt onPanResponderTerminationRequest,
"onShouldBlockNativeResponder": Js.Undefined.from_opt onShouldBlockNativeResponder
};
34 changes: 17 additions & 17 deletions src/panResponderRe.rei
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
type t;

type gestureState =
{
stateID : float,
moveX : float,
moveY : float,
x0 : float,
y0 : float,
dx : float,
dy : float,
vx : float,
vy : float,
numberActiveTouches : int
};
type gestureState = {
stateID: float,
moveX: float,
moveY: float,
x0: float,
y0: float,
dx: float,
dy: float,
vx: float,
vy: float,
numberActiveTouches: int
};

type callback 'a;

type callback 'a = RNEvent.NativeEvent.t => gestureState => 'a;
let callback: (RNEvent.NativeEvent.t => gestureState => 'a) => callback 'a;

let create:
onMoveShouldSetPanResponder::callback bool? =>
Expand All @@ -28,14 +28,14 @@ let create:
onPanResponderEnd::callback unit? =>
onPanResponderRelease::callback unit? =>
onPanResponderMove::
[<
[
| `callback (callback unit)
| `update (
list [< | `X AnimatedRe.Value.t | `XY AnimatedRe.ValueXY.t | `Y AnimatedRe.Value.t]
list [ | `X AnimatedRe.Value.t | `XY AnimatedRe.ValueXY.t | `Y AnimatedRe.Value.t]
)
]? =>
onPanResponderTerminate::callback unit? =>
onPanResponderTerminationRequest::callback bool? =>
onShouldBlockNativeResponder::callback bool? =>
unit =>
t;
t;

0 comments on commit b63e3e8

Please sign in to comment.