|
| 1 | +# @gesturejs/signal |
| 2 | + |
| 3 | +**Signal** is a higher-level abstraction over events that merges and refines disparate inputs into a single, composable stream. |
| 4 | + |
| 5 | +## Why @gesturejs/signal? |
| 6 | + |
| 7 | +- **Unified Interface** - Subscribe to diverse event sources through one consistent API by normalizing them into a unified signal stream. |
| 8 | +- **GC Optimized** - Built-in object pooling prevents garbage collection pauses during high-frequency input (60+ events/sec) |
| 9 | +- **Observable-Based** - Works seamlessly with reactive patterns via `@gesturejs/stream`. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +```bash |
| 14 | +npm install @gesturejs/signal |
| 15 | +``` |
| 16 | + |
| 17 | +## Quick Start |
| 18 | + |
| 19 | +### Single Pointer |
| 20 | + |
| 21 | +```typescript |
| 22 | +import { singlePointer } from "@gesturejs/signal"; |
| 23 | + |
| 24 | +const stream = singlePointer(canvasElement); |
| 25 | +const unsub = stream.subscribe((pointer) => { |
| 26 | + console.log(pointer.x, pointer.y); |
| 27 | + console.log(pointer.phase); // start, move, end, cancel |
| 28 | +}); |
| 29 | +``` |
| 30 | + |
| 31 | +## Recipes |
| 32 | + |
| 33 | +### Use Other Events |
| 34 | + |
| 35 | +```typescript |
| 36 | +import { touchEventsToSinglePointer } from "@gesturejs/signal"; |
| 37 | +import { fromEvent, merge, pipe, filter } from "@gesturejs/stream"; |
| 38 | + |
| 39 | +/** |
| 40 | + * The `singlePointer()` factory is primarily designed for PointerEvents. |
| 41 | + * If you're working with TouchEvents, you can convert a TouchEvent stream via |
| 42 | + * `touchEventsToSinglePointer()` and keep using the same SinglePointer pipeline. |
| 43 | + */ |
| 44 | +const stream = pipe( |
| 45 | + merge( |
| 46 | + fromEvent(el, "touchstart"), |
| 47 | + fromEvent(el, "touchmove"), |
| 48 | + fromEvent(el, "touchend"), |
| 49 | + fromEvent(el, "touchcancel") |
| 50 | + ), |
| 51 | + touchEventsToSinglePointer(), |
| 52 | + filter((p) => p.phase === "move") |
| 53 | +); |
| 54 | +const unsub = stream.subscribe((pointer) => { |
| 55 | + draw(pointer.x, pointer.y); |
| 56 | +}); |
| 57 | +``` |
| 58 | + |
| 59 | +### Object Pooling |
| 60 | + |
| 61 | +```typescript |
| 62 | +/** |
| 63 | + * Pooling reduces allocations/GC pressure for high-frequency input |
| 64 | + * - but emitted objects are reused (mutated/reset). Don't keep references. |
| 65 | + * - If you need to persist data, copy the fields you need. |
| 66 | + */ |
| 67 | +const spStream = singlePointer(canvasElement, { pooling: true }); |
| 68 | +``` |
| 69 | + |
| 70 | +## Documentation |
| 71 | + |
| 72 | +- [API Reference](./docs/api.md) |
| 73 | +- [Signal Structure](./docs/signal.md) |
| 74 | + |
| 75 | +## License |
| 76 | + |
| 77 | +MIT |
0 commit comments