@@ -11,18 +11,25 @@ import type { Touch, TouchEvent } from './ResponderEventTypes';
11
11
import { isStartish , isMoveish , isEndish } from './ResponderEventTypes' ;
12
12
13
13
type TouchRecord = { |
14
- touchActive : boolean ,
15
- startPageX : number ,
16
- startPageY : number ,
17
- startTimeStamp : number ,
18
14
currentPageX : number ,
19
15
currentPageY : number ,
20
16
currentTimeStamp : number ,
21
17
previousPageX : number ,
22
18
previousPageY : number ,
23
- previousTimeStamp : number
19
+ previousTimeStamp : number ,
20
+ startPageX : number ,
21
+ startPageY : number ,
22
+ startTimeStamp : number ,
23
+ touchActive : boolean
24
24
| } ;
25
25
26
+ export type TouchHistory = $ReadOnly < { |
27
+ indexOfSingleActiveTouch : number ,
28
+ mostRecentTimeStamp : number ,
29
+ numberActiveTouches : number ,
30
+ touchBank : Array < TouchRecord >
31
+ | } > ;
32
+
26
33
/**
27
34
* Tracks the position and time of each active touch by `touch.identifier`. We
28
35
* should typically only see IDs in the range of 1-20 because IDs get recycled
@@ -31,16 +38,6 @@ type TouchRecord = {|
31
38
32
39
const __DEV__ = process . env . NODE_ENV !== 'production' ;
33
40
const MAX_TOUCH_BANK = 20 ;
34
- const touchBank : Array < TouchRecord > = [];
35
- const touchHistory = {
36
- touchBank ,
37
- numberActiveTouches : 0 ,
38
- // If there is only one active touch, we remember its location. This prevents
39
- // us having to loop through all of the touches all the time in the most
40
- // common case.
41
- indexOfSingleActiveTouch : - 1 ,
42
- mostRecentTimeStamp : 0
43
- } ;
44
41
45
42
function timestampForTouch ( touch : Touch ) : number {
46
43
// The legacy internal implementation provides "timeStamp", which has been
@@ -97,19 +94,19 @@ function getTouchIdentifier({ identifier }: Touch): number {
97
94
return identifier ;
98
95
}
99
96
100
- function recordTouchStart ( touch : Touch ) : void {
97
+ function recordTouchStart ( touch : Touch , touchHistory ) : void {
101
98
const identifier = getTouchIdentifier ( touch ) ;
102
- const touchRecord = touchBank [ identifier ] ;
99
+ const touchRecord = touchHistory . touchBank [ identifier ] ;
103
100
if ( touchRecord ) {
104
101
resetTouchRecord ( touchRecord , touch ) ;
105
102
} else {
106
- touchBank [ identifier ] = createTouchRecord ( touch ) ;
103
+ touchHistory . touchBank [ identifier ] = createTouchRecord ( touch ) ;
107
104
}
108
105
touchHistory . mostRecentTimeStamp = timestampForTouch ( touch ) ;
109
106
}
110
107
111
- function recordTouchMove ( touch : Touch ) : void {
112
- const touchRecord = touchBank [ getTouchIdentifier ( touch ) ] ;
108
+ function recordTouchMove ( touch : Touch , touchHistory ) : void {
109
+ const touchRecord = touchHistory . touchBank [ getTouchIdentifier ( touch ) ] ;
113
110
if ( touchRecord ) {
114
111
touchRecord . touchActive = true ;
115
112
touchRecord . previousPageX = touchRecord . currentPageX ;
@@ -123,13 +120,13 @@ function recordTouchMove(touch: Touch): void {
123
120
console . warn (
124
121
'Cannot record touch move without a touch start.\n' ,
125
122
`Touch Move: ${ printTouch ( touch ) } \n` ,
126
- `Touch Bank: ${ printTouchBank ( ) } `
123
+ `Touch Bank: ${ printTouchBank ( touchHistory ) } `
127
124
) ;
128
125
}
129
126
}
130
127
131
- function recordTouchEnd ( touch : Touch ) : void {
132
- const touchRecord = touchBank [ getTouchIdentifier ( touch ) ] ;
128
+ function recordTouchEnd ( touch : Touch , touchHistory ) : void {
129
+ const touchRecord = touchHistory . touchBank [ getTouchIdentifier ( touch ) ] ;
133
130
if ( touchRecord ) {
134
131
touchRecord . touchActive = false ;
135
132
touchRecord . previousPageX = touchRecord . currentPageX ;
@@ -143,7 +140,7 @@ function recordTouchEnd(touch: Touch): void {
143
140
console . warn (
144
141
'Cannot record touch end without a touch start.\n' ,
145
142
`Touch End: ${ printTouch ( touch ) } \n` ,
146
- `Touch Bank: ${ printTouchBank ( ) } `
143
+ `Touch Bank: ${ printTouchBank ( touchHistory ) } `
147
144
) ;
148
145
}
149
146
}
@@ -157,29 +154,48 @@ function printTouch(touch: Touch): string {
157
154
} ) ;
158
155
}
159
156
160
- function printTouchBank(): string {
157
+ function printTouchBank ( touchHistory ) : string {
158
+ const { touchBank } = touchHistory ;
161
159
let printed = JSON . stringify ( touchBank . slice ( 0 , MAX_TOUCH_BANK ) ) ;
162
160
if ( touchBank . length > MAX_TOUCH_BANK ) {
163
161
printed += ' (original size: ' + touchBank . length + ')' ;
164
162
}
165
163
return printed ;
166
164
}
167
165
168
- const ResponderTouchHistoryStore = {
166
+ export class ResponderTouchHistoryStore {
167
+ _touchHistory = {
168
+ touchBank : [ ] , //Array<TouchRecord>
169
+ numberActiveTouches : 0 ,
170
+ // If there is only one active touch, we remember its location. This prevents
171
+ // us having to loop through all of the touches all the time in the most
172
+ // common case.
173
+ indexOfSingleActiveTouch : - 1 ,
174
+ mostRecentTimeStamp : 0
175
+ } ;
176
+
169
177
recordTouchTrack ( topLevelType : string , nativeEvent : TouchEvent ) : void {
178
+ const touchHistory = this . _touchHistory ;
170
179
if ( isMoveish ( topLevelType ) ) {
171
- nativeEvent . changedTouches . forEach ( recordTouchMove ) ;
180
+ nativeEvent . changedTouches . forEach ( ( touch ) =>
181
+ recordTouchMove ( touch , touchHistory )
182
+ ) ;
172
183
} else if ( isStartish ( topLevelType ) ) {
173
- nativeEvent . changedTouches . forEach ( recordTouchStart ) ;
184
+ nativeEvent . changedTouches . forEach ( ( touch ) =>
185
+ recordTouchStart ( touch , touchHistory )
186
+ ) ;
174
187
touchHistory . numberActiveTouches = nativeEvent . touches . length ;
175
188
if ( touchHistory . numberActiveTouches === 1 ) {
176
189
touchHistory . indexOfSingleActiveTouch =
177
190
nativeEvent . touches [ 0 ] . identifier ;
178
191
}
179
192
} else if ( isEndish ( topLevelType ) ) {
180
- nativeEvent . changedTouches . forEach ( recordTouchEnd ) ;
193
+ nativeEvent . changedTouches . forEach ( ( touch ) =>
194
+ recordTouchEnd ( touch , touchHistory )
195
+ ) ;
181
196
touchHistory . numberActiveTouches = nativeEvent . touches . length ;
182
197
if ( touchHistory . numberActiveTouches === 1 ) {
198
+ const { touchBank } = touchHistory ;
183
199
for ( let i = 0 ; i < touchBank . length ; i ++ ) {
184
200
const touchTrackToCheck = touchBank [ i ] ;
185
201
if ( touchTrackToCheck != null && touchTrackToCheck . touchActive ) {
@@ -195,9 +211,9 @@ const ResponderTouchHistoryStore = {
195
211
}
196
212
}
197
213
}
198
- } ,
199
-
200
- touchHistory
201
- } ;
214
+ }
202
215
203
- export default ResponderTouchHistoryStore ;
216
+ get touchHistory ( ) : TouchHistory {
217
+ return this . _touchHistory ;
218
+ }
219
+ }
0 commit comments