gesturx 0.0.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,17 +1,17 @@
1
- import { describe, it, expect, beforeEach } from "vitest";
2
- import { createTapRecognizer } from "./tapRecognizer";
3
- import { buffer, first, lastValueFrom, tap, timer } from "rxjs";
1
+ import { describe, it, expect, beforeEach } from "vitest"
2
+ import { TapRecognizer } from "./TapRecognizer"
3
+ import { buffer, first, lastValueFrom, tap, timer } from "rxjs"
4
4
 
5
5
  const waitFor = (time: number) =>
6
- new Promise((resolve) => setTimeout(resolve, time));
6
+ new Promise((resolve) => setTimeout(resolve, time))
7
7
 
8
8
  describe("TapGestureRecognizer", () => {
9
- let container = document.createElement("div");
9
+ let container = document.createElement("div")
10
10
 
11
11
  beforeEach(() => {
12
- container = document.createElement("div");
13
- document.body.appendChild(container);
14
- });
12
+ container = document.createElement("div")
13
+ document.body.appendChild(container)
14
+ })
15
15
 
16
16
  function createPointerEvent({
17
17
  type,
@@ -19,10 +19,10 @@ describe("TapGestureRecognizer", () => {
19
19
  y,
20
20
  identifier,
21
21
  }: {
22
- type: string;
23
- x: number;
24
- y: number;
25
- identifier: number;
22
+ type: string
23
+ x: number
24
+ y: number
25
+ identifier: number
26
26
  }) {
27
27
  const event = new PointerEvent(type, {
28
28
  clientX: x,
@@ -30,9 +30,14 @@ describe("TapGestureRecognizer", () => {
30
30
  pointerId: identifier,
31
31
  pointerType: "touch",
32
32
  bubbles: true,
33
- });
33
+ })
34
34
 
35
- return event;
35
+ // @ts-ignore
36
+ event.x = x
37
+ // @ts-ignore
38
+ event.y = y
39
+
40
+ return event
36
41
  }
37
42
 
38
43
  function sendPointerEvent({
@@ -42,44 +47,45 @@ describe("TapGestureRecognizer", () => {
42
47
  container,
43
48
  identifier,
44
49
  }: {
45
- container: HTMLElement;
46
- type: string;
47
- x: number;
48
- y: number;
49
- identifier: number;
50
+ container: HTMLElement
51
+ type: string
52
+ x: number
53
+ y: number
54
+ identifier: number
50
55
  }) {
51
56
  const event = createPointerEvent({
52
57
  type,
53
58
  x,
54
59
  y,
55
60
  identifier,
56
- });
61
+ })
57
62
 
58
- sendPointerEventFromEvent({ event, container });
63
+ sendPointerEventFromEvent({ event, container })
59
64
  }
60
65
 
61
66
  function sendPointerEventFromEvent({
62
67
  event,
63
68
  container,
64
69
  }: {
65
- event: PointerEvent;
66
- container: HTMLElement;
70
+ event: PointerEvent
71
+ container: HTMLElement
67
72
  }) {
68
- container.dispatchEvent(event);
73
+ container.dispatchEvent(event)
69
74
  }
70
75
 
71
76
  it("should detect a single tap", async () => {
72
- const multiTapThreshold = 5;
73
- const waitLongEnough$ = timer(multiTapThreshold * 2);
74
- const recognizer = createTapRecognizer({ multiTapThreshold })({
77
+ const multiTapThreshold = 5
78
+ const waitLongEnough$ = timer(100)
79
+ const recognizer = new TapRecognizer({ multiTapThreshold })
80
+ recognizer.initialize({
75
81
  container,
76
- });
82
+ })
77
83
 
78
84
  const values = await lastValueFrom(
79
85
  recognizer.events$.pipe(
80
86
  tap({
81
87
  subscribe: async () => {
82
- await waitFor(1);
88
+ await waitFor(1)
83
89
 
84
90
  sendPointerEvent({
85
91
  container,
@@ -87,36 +93,37 @@ describe("TapGestureRecognizer", () => {
87
93
  type: "pointerdown",
88
94
  x: 0,
89
95
  y: 0,
90
- });
96
+ })
91
97
  sendPointerEvent({
92
98
  container,
93
99
  identifier: 1,
94
100
  type: "pointerup",
95
101
  x: 0,
96
102
  y: 0,
97
- });
103
+ })
98
104
  },
99
105
  }),
100
106
  buffer(waitLongEnough$),
101
107
  first(),
102
108
  ),
103
- );
109
+ )
104
110
 
105
- expect(values).toMatchObject([{ type: "tap", taps: 1 }]);
106
- });
111
+ expect(values).toMatchObject([{ type: "tap", taps: 1 }])
112
+ })
107
113
 
108
114
  it("should detect a double tap", async () => {
109
- const multiTapThreshold = 5;
110
- const waitLongEnough$ = timer(multiTapThreshold * 2);
111
- const recognizer = createTapRecognizer({ multiTapThreshold, maxTaps: 2 })({
115
+ const multiTapThreshold = 5
116
+ const waitLongEnough$ = timer(multiTapThreshold * 2)
117
+ const recognizer = new TapRecognizer({ multiTapThreshold, maxTaps: 2 })
118
+ recognizer.initialize({
112
119
  container,
113
- });
120
+ })
114
121
 
115
122
  const values = await lastValueFrom(
116
123
  recognizer.events$.pipe(
117
124
  tap({
118
125
  subscribe: async () => {
119
- await waitFor(1);
126
+ await waitFor(1)
120
127
 
121
128
  sendPointerEvent({
122
129
  container,
@@ -124,44 +131,45 @@ describe("TapGestureRecognizer", () => {
124
131
  type: "pointerdown",
125
132
  x: 0,
126
133
  y: 0,
127
- });
134
+ })
128
135
  sendPointerEvent({
129
136
  container,
130
137
  identifier: 1,
131
138
  type: "pointerup",
132
139
  x: 0,
133
140
  y: 0,
134
- });
141
+ })
135
142
  sendPointerEvent({
136
143
  container,
137
144
  identifier: 2,
138
145
  type: "pointerdown",
139
146
  x: 0,
140
147
  y: 0,
141
- });
148
+ })
142
149
  sendPointerEvent({
143
150
  container,
144
151
  identifier: 2,
145
152
  type: "pointerup",
146
153
  x: 0,
147
154
  y: 0,
148
- });
155
+ })
149
156
  },
150
157
  }),
151
158
  buffer(waitLongEnough$),
152
159
  first(),
153
160
  ),
154
- );
161
+ )
155
162
 
156
- expect(values).toMatchObject([{ type: "tap", taps: 2 }]);
157
- });
163
+ expect(values).toMatchObject([{ type: "tap", taps: 2 }])
164
+ })
158
165
 
159
166
  it("should detect a triple tap", async () => {
160
- const multiTapThreshold = 5;
161
- const recognizer = createTapRecognizer({ multiTapThreshold, maxTaps: 3 })({
167
+ const multiTapThreshold = 5
168
+ const recognizer = new TapRecognizer({ multiTapThreshold, maxTaps: 3 })
169
+ recognizer.initialize({
162
170
  container,
163
- });
164
- const waitLongEnough$ = timer(100);
171
+ })
172
+ const waitLongEnough$ = timer(100)
165
173
 
166
174
  const values = await lastValueFrom(
167
175
  recognizer.events$.pipe(
@@ -174,65 +182,66 @@ describe("TapGestureRecognizer", () => {
174
182
  type: "pointerdown",
175
183
  x: 0,
176
184
  y: 0,
177
- });
185
+ })
178
186
  sendPointerEvent({
179
187
  container,
180
188
  identifier: 1,
181
189
  type: "pointerup",
182
190
  x: 0,
183
191
  y: 0,
184
- });
192
+ })
185
193
  sendPointerEvent({
186
194
  container,
187
195
  identifier: 1,
188
196
  type: "pointerdown",
189
197
  x: 0,
190
198
  y: 0,
191
- });
199
+ })
192
200
  sendPointerEvent({
193
201
  container,
194
202
  identifier: 1,
195
203
  type: "pointerup",
196
204
  x: 0,
197
205
  y: 0,
198
- });
206
+ })
199
207
  sendPointerEvent({
200
208
  container,
201
209
  identifier: 1,
202
210
  type: "pointerdown",
203
211
  x: 0,
204
212
  y: 0,
205
- });
213
+ })
206
214
  sendPointerEvent({
207
215
  container,
208
216
  identifier: 1,
209
217
  type: "pointerup",
210
218
  x: 0,
211
219
  y: 0,
212
- });
213
- });
220
+ })
221
+ })
214
222
  },
215
223
  }),
216
224
  buffer(waitLongEnough$),
217
225
  first(),
218
226
  ),
219
- );
227
+ )
220
228
 
221
- expect(values).toMatchObject([{ type: "tap", taps: 3 }]);
222
- });
229
+ expect(values).toMatchObject([{ type: "tap", taps: 3 }])
230
+ })
223
231
 
224
232
  it("should not detect a double tap if the second tap is after threshold", async () => {
225
- const multiTapThreshold = 5;
226
- const waitLongEnough$ = timer(100);
227
- const recognizer = createTapRecognizer({ multiTapThreshold, maxTaps: 2 })({
233
+ const multiTapThreshold = 5
234
+ const waitLongEnough$ = timer(100)
235
+ const recognizer = new TapRecognizer({ multiTapThreshold, maxTaps: 2 })
236
+ recognizer.initialize({
228
237
  container,
229
- });
238
+ })
230
239
 
231
240
  const values = await lastValueFrom(
232
241
  recognizer.events$.pipe(
233
242
  tap({
234
243
  subscribe: async () => {
235
- await waitFor(1);
244
+ await waitFor(1)
236
245
 
237
246
  sendPointerEvent({
238
247
  container,
@@ -240,16 +249,16 @@ describe("TapGestureRecognizer", () => {
240
249
  type: "pointerdown",
241
250
  x: 0,
242
251
  y: 0,
243
- });
252
+ })
244
253
  sendPointerEvent({
245
254
  container,
246
255
  identifier: 1,
247
256
  type: "pointerup",
248
257
  x: 0,
249
258
  y: 0,
250
- });
259
+ })
251
260
 
252
- await waitFor(multiTapThreshold);
261
+ await waitFor(multiTapThreshold)
253
262
 
254
263
  sendPointerEvent({
255
264
  container,
@@ -257,41 +266,44 @@ describe("TapGestureRecognizer", () => {
257
266
  type: "pointerdown",
258
267
  x: 0,
259
268
  y: 0,
260
- });
269
+ })
261
270
  sendPointerEvent({
262
271
  container,
263
272
  identifier: 2,
264
273
  type: "pointerup",
265
274
  x: 0,
266
275
  y: 0,
267
- });
276
+ })
268
277
  },
269
278
  }),
270
279
  buffer(waitLongEnough$),
271
280
  first(),
272
281
  ),
273
- );
282
+ )
274
283
 
275
- expect(values.length).toBe(2);
284
+ expect(values.length).toBe(2)
276
285
  expect(values).toMatchObject([
277
- { type: "tap", taps: 1, srcEvent: { pointerId: 1 } },
278
- { type: "tap", taps: 1, srcEvent: { pointerId: 2 } },
279
- ]);
280
- });
286
+ { type: "tap", taps: 1, pointers: [{ pointerId: 1 }] },
287
+ { type: "tap", taps: 1, pointers: [{ pointerId: 2 }] },
288
+ ])
289
+ })
281
290
 
282
291
  it("should not trigger tap if moved too much between down and up", async () => {
283
- const multiTapThreshold = 5;
284
- const waitLongEnough$ = timer(100);
285
- const recognizer = createTapRecognizer({
292
+ const multiTapThreshold = 5
293
+ const waitLongEnough$ = timer(100)
294
+ const recognizer = new TapRecognizer({
286
295
  multiTapThreshold,
287
296
  posThreshold: 5,
288
- })({ container });
297
+ })
298
+ recognizer.initialize({
299
+ container,
300
+ })
289
301
 
290
302
  const values = await lastValueFrom(
291
303
  recognizer.events$.pipe(
292
304
  tap({
293
305
  subscribe: async () => {
294
- await waitFor(1);
306
+ await waitFor(1)
295
307
 
296
308
  sendPointerEvent({
297
309
  container,
@@ -299,39 +311,43 @@ describe("TapGestureRecognizer", () => {
299
311
  type: "pointerdown",
300
312
  x: 0,
301
313
  y: 0,
302
- });
314
+ })
315
+
303
316
  sendPointerEvent({
304
317
  container,
305
318
  identifier: 1,
306
319
  type: "pointerup",
307
320
  x: 50,
308
321
  y: 50,
309
- });
322
+ })
310
323
  },
311
324
  }),
312
325
  buffer(waitLongEnough$),
313
326
  first(),
314
327
  ),
315
- );
328
+ )
316
329
 
317
- expect(values.length).toBe(0);
318
- });
330
+ expect(values.length).toBe(0)
331
+ })
319
332
 
320
333
  it("should not detect a tap if the pointer is held down too long", async () => {
321
- const multiTapThreshold = 5;
322
- const maximumPressTime = 10;
323
- const waitLongEnough$ = timer(100);
324
- const recognizer = createTapRecognizer({
334
+ const multiTapThreshold = 5
335
+ const maximumPressTime = 10
336
+ const waitLongEnough$ = timer(100)
337
+ const recognizer = new TapRecognizer({
325
338
  multiTapThreshold,
326
339
  posThreshold: 5,
327
340
  maximumPressTime,
328
- })({ container });
341
+ })
342
+ recognizer.initialize({
343
+ container,
344
+ })
329
345
 
330
346
  const values = await lastValueFrom(
331
347
  recognizer.events$.pipe(
332
348
  tap({
333
349
  subscribe: async () => {
334
- await waitFor(1);
350
+ await waitFor(1)
335
351
 
336
352
  sendPointerEvent({
337
353
  container,
@@ -339,9 +355,9 @@ describe("TapGestureRecognizer", () => {
339
355
  type: "pointerdown",
340
356
  x: 0,
341
357
  y: 0,
342
- });
358
+ })
343
359
 
344
- await waitFor(maximumPressTime - 5);
360
+ await waitFor(maximumPressTime - 5)
345
361
 
346
362
  sendPointerEvent({
347
363
  container,
@@ -349,9 +365,9 @@ describe("TapGestureRecognizer", () => {
349
365
  type: "pointerup",
350
366
  x: 0,
351
367
  y: 0,
352
- });
368
+ })
353
369
 
354
- await waitFor(multiTapThreshold * 2);
370
+ await waitFor(multiTapThreshold * 2)
355
371
 
356
372
  sendPointerEvent({
357
373
  container,
@@ -359,9 +375,9 @@ describe("TapGestureRecognizer", () => {
359
375
  type: "pointerdown",
360
376
  x: 0,
361
377
  y: 0,
362
- });
378
+ })
363
379
 
364
- await waitFor(maximumPressTime);
380
+ await waitFor(maximumPressTime)
365
381
 
366
382
  sendPointerEvent({
367
383
  container,
@@ -369,23 +385,26 @@ describe("TapGestureRecognizer", () => {
369
385
  type: "pointerup",
370
386
  x: 0,
371
387
  y: 0,
372
- });
388
+ })
373
389
  },
374
390
  }),
375
391
  buffer(waitLongEnough$),
376
392
  first(),
377
393
  ),
378
- );
394
+ )
379
395
 
380
- expect(values.length).toBe(1);
396
+ expect(values.length).toBe(1)
381
397
  expect(values).toMatchObject([
382
- { type: "tap", taps: 1, srcEvent: { pointerId: 1 } },
383
- ]);
384
- });
398
+ { type: "tap", taps: 1, pointers: [{ pointerId: 1 }] },
399
+ ])
400
+ })
385
401
 
386
402
  it("should ignore taps when there are simultaneous pointers active", async () => {
387
- const recognizer = createTapRecognizer({})({ container });
388
- const waitLongEnough$ = timer(100);
403
+ const recognizer = new TapRecognizer({})
404
+ recognizer.initialize({
405
+ container,
406
+ })
407
+ const waitLongEnough$ = timer(100)
389
408
 
390
409
  const values = await lastValueFrom(
391
410
  recognizer.events$.pipe(
@@ -398,51 +417,54 @@ describe("TapGestureRecognizer", () => {
398
417
  type: "pointerdown",
399
418
  x: 0,
400
419
  y: 0,
401
- });
420
+ })
402
421
  sendPointerEvent({
403
422
  container,
404
423
  identifier: 2,
405
424
  type: "pointerdown",
406
425
  x: 10,
407
426
  y: 10,
408
- });
427
+ })
409
428
  sendPointerEvent({
410
429
  container,
411
430
  identifier: 1,
412
431
  type: "pointerup",
413
432
  x: 0,
414
433
  y: 0,
415
- });
434
+ })
416
435
  sendPointerEvent({
417
436
  container,
418
437
  identifier: 2,
419
438
  type: "pointerup",
420
439
  x: 10,
421
440
  y: 10,
422
- });
423
- });
441
+ })
442
+ })
424
443
  },
425
444
  }),
426
445
  buffer(waitLongEnough$),
427
446
  first(),
428
447
  ),
429
- );
448
+ )
430
449
 
431
- expect(values.length).toBe(0); // Assuming the recognizer should ignore simultaneous taps
432
- });
450
+ expect(values.length).toBe(0) // Assuming the recognizer should ignore simultaneous taps
451
+ })
433
452
 
434
453
  it("should ignore one tap if there is more than one tap", async () => {
435
- const recognizer = createTapRecognizer({
454
+ const recognizer = new TapRecognizer({
436
455
  multiTapThreshold: 10,
437
456
  maxTaps: 1,
438
- })({ container });
439
- const waitLongEnough$ = timer(100);
457
+ })
458
+ recognizer.initialize({
459
+ container,
460
+ })
461
+ const waitLongEnough$ = timer(100)
440
462
 
441
463
  const values = await lastValueFrom(
442
464
  recognizer.events$.pipe(
443
465
  tap({
444
466
  subscribe: async () => {
445
- await waitFor(1);
467
+ await waitFor(1)
446
468
 
447
469
  sendPointerEvent({
448
470
  container,
@@ -450,16 +472,16 @@ describe("TapGestureRecognizer", () => {
450
472
  type: "pointerdown",
451
473
  x: 0,
452
474
  y: 0,
453
- });
475
+ })
454
476
  sendPointerEvent({
455
477
  container,
456
478
  identifier: 1,
457
479
  type: "pointerup",
458
480
  x: 0,
459
481
  y: 0,
460
- });
482
+ })
461
483
 
462
- await waitFor(1);
484
+ await waitFor(1)
463
485
 
464
486
  sendPointerEvent({
465
487
  container,
@@ -467,31 +489,34 @@ describe("TapGestureRecognizer", () => {
467
489
  type: "pointerdown",
468
490
  x: 0,
469
491
  y: 0,
470
- });
492
+ })
471
493
  sendPointerEvent({
472
494
  container,
473
495
  identifier: 2,
474
496
  type: "pointerup",
475
497
  x: 0,
476
498
  y: 0,
477
- });
499
+ })
478
500
  },
479
501
  }),
480
502
  buffer(waitLongEnough$),
481
503
  first(),
482
504
  ),
483
- );
505
+ )
484
506
 
485
- expect(values.length).toBe(0);
486
- });
507
+ expect(values.length).toBe(0)
508
+ })
487
509
 
488
510
  it("should ignore taps when a second tap is outside of position threshold", async () => {
489
- const recognizer = createTapRecognizer({
511
+ const recognizer = new TapRecognizer({
490
512
  posThreshold: 5,
491
513
  multiTapThreshold: 10,
492
514
  maxTaps: Infinity,
493
- })({ container });
494
- const waitLongEnough$ = timer(100);
515
+ })
516
+ recognizer.initialize({
517
+ container,
518
+ })
519
+ const waitLongEnough$ = timer(100)
495
520
 
496
521
  const values = await lastValueFrom(
497
522
  recognizer.events$.pipe(
@@ -505,14 +530,14 @@ describe("TapGestureRecognizer", () => {
505
530
  type: "pointerdown",
506
531
  x: 0,
507
532
  y: 0,
508
- });
533
+ })
509
534
  sendPointerEvent({
510
535
  container,
511
536
  identifier: 1,
512
537
  type: "pointerup",
513
538
  x: 0,
514
539
  y: 0,
515
- });
540
+ })
516
541
 
517
542
  // await waitFor(1)
518
543
 
@@ -522,21 +547,21 @@ describe("TapGestureRecognizer", () => {
522
547
  type: "pointerdown",
523
548
  x: 10,
524
549
  y: 10,
525
- });
550
+ })
526
551
  sendPointerEvent({
527
552
  container,
528
553
  identifier: 2,
529
554
  type: "pointerup",
530
555
  x: 10,
531
556
  y: 10,
532
- });
557
+ })
533
558
  },
534
559
  }),
535
560
  buffer(waitLongEnough$),
536
561
  first(),
537
562
  ),
538
- );
563
+ )
539
564
 
540
- expect(values.length).toBe(0);
541
- });
542
- });
565
+ expect(values.length).toBe(0)
566
+ })
567
+ })