40 lines
865 B
TypeScript
40 lines
865 B
TypeScript
"use client"
|
|
|
|
type Point = { x: number; y: number }
|
|
|
|
const listeners = new Set<() => void>()
|
|
let latest: Point = { x: 0, y: 0 }
|
|
let rafId: number | null = null
|
|
|
|
function emit() {
|
|
rafId = null
|
|
for (const cb of listeners) cb()
|
|
}
|
|
|
|
/** Throttle pointer notifications to at most once per animation frame. */
|
|
export function notifyDragPointerMove(x: number, y: number) {
|
|
latest = { x, y }
|
|
if (rafId !== null) return
|
|
rafId = globalThis.requestAnimationFrame(emit)
|
|
}
|
|
|
|
export function getDragPointerSnapshot(): Point {
|
|
return latest
|
|
}
|
|
|
|
export function subscribeDragPointer(cb: () => void) {
|
|
listeners.add(cb)
|
|
return () => {
|
|
listeners.delete(cb)
|
|
}
|
|
}
|
|
|
|
export function resetDragPointer(x: number, y: number) {
|
|
if (rafId !== null) {
|
|
globalThis.cancelAnimationFrame(rafId)
|
|
rafId = null
|
|
}
|
|
latest = { x, y }
|
|
for (const cb of listeners) cb()
|
|
}
|