add client lib @kittycad/lib (#214)

* add client lib

* tsc after build

* update after spec update

* remove uneeded check

* updates

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* fixes

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* fix camera drag

* fix throttle typing

* comment with link to issue

---------

Signed-off-by: Jess Frazelle <github@jessfraz.com>
Co-authored-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
Kurt Hutten
2023-08-02 15:41:59 +10:00
committed by GitHub
parent aabb88ee45
commit 5419039fae
12 changed files with 140 additions and 126 deletions

View File

@ -28,26 +28,26 @@ export function normaliseAngle(angle: number): number {
return result > 180 ? result - 360 : result
}
export function throttle(
func: (...args: any[]) => any,
export function throttle<T>(
func: (args: T) => any,
wait: number
): (...args: any[]) => any {
): (args: T) => any {
let timeout: ReturnType<typeof setTimeout> | null
let latestArgs: any[]
let latestArgs: T
let latestTimestamp: number
function later() {
timeout = null
func(...latestArgs)
func(latestArgs)
}
function throttled(...args: any[]) {
function throttled(args: T) {
const currentTimestamp = Date.now()
latestArgs = args
if (!latestTimestamp || currentTimestamp - latestTimestamp >= wait) {
latestTimestamp = currentTimestamp
func(...latestArgs)
func(latestArgs)
} else if (!timeout) {
timeout = setTimeout(later, wait - (currentTimestamp - latestTimestamp))
}