@tldraw/utils 5.1.1 → 5.2.0-canary.019da1aa690a
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.
- package/DOCS.md +930 -0
- package/README.md +9 -1
- package/dist-cjs/index.d.ts +14 -4
- package/dist-cjs/index.js +1 -1
- package/dist-cjs/lib/debounce.js +4 -0
- package/dist-cjs/lib/debounce.js.map +2 -2
- package/dist-cjs/lib/function.js +2 -2
- package/dist-cjs/lib/function.js.map +2 -2
- package/dist-cjs/lib/media/avif.js +2 -2
- package/dist-cjs/lib/media/avif.js.map +2 -2
- package/dist-cjs/lib/network.js +2 -2
- package/dist-cjs/lib/network.js.map +2 -2
- package/dist-cjs/lib/url.js +2 -2
- package/dist-cjs/lib/url.js.map +2 -2
- package/dist-esm/index.d.mts +14 -4
- package/dist-esm/index.mjs +1 -1
- package/dist-esm/lib/debounce.mjs +4 -0
- package/dist-esm/lib/debounce.mjs.map +2 -2
- package/dist-esm/lib/function.mjs +2 -2
- package/dist-esm/lib/function.mjs.map +2 -2
- package/dist-esm/lib/media/avif.mjs +2 -2
- package/dist-esm/lib/media/avif.mjs.map +2 -2
- package/dist-esm/lib/network.mjs +2 -2
- package/dist-esm/lib/network.mjs.map +2 -2
- package/dist-esm/lib/url.mjs +2 -2
- package/dist-esm/lib/url.mjs.map +2 -2
- package/package.json +7 -3
- package/src/lib/PerformanceTracker.test.ts +3 -4
- package/src/lib/debounce.test.ts +60 -0
- package/src/lib/debounce.ts +21 -3
- package/src/lib/function.ts +1 -1
- package/src/lib/media/avif.ts +1 -1
- package/src/lib/network.ts +1 -1
- package/src/lib/url.ts +1 -1
- package/src/lib/version.test.ts +1 -1
package/src/lib/debounce.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { noop } from './function'
|
|
1
2
|
import type { Awaitable } from './types'
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -8,6 +9,12 @@ import type { Awaitable } from './types'
|
|
|
8
9
|
* function returns a Promise that resolves with the result of the original function. Includes a
|
|
9
10
|
* cancel method to prevent execution if needed.
|
|
10
11
|
*
|
|
12
|
+
* Calling `cancel()` while a call is pending rejects the returned promise with an `Error`
|
|
13
|
+
* whose message is `'Debounced function was cancelled'`. Callers that discard the promise
|
|
14
|
+
* are not affected — internal handling suppresses the unhandled-rejection warning — but
|
|
15
|
+
* any code that `await`s or chains `.then()` on the promise must be prepared to handle the
|
|
16
|
+
* rejection.
|
|
17
|
+
*
|
|
11
18
|
* @param callback - The function to debounce (can be sync or async)
|
|
12
19
|
* @param wait - The delay in milliseconds before executing the function
|
|
13
20
|
* @returns A debounced function that returns a Promise and includes a cancel method
|
|
@@ -23,15 +30,19 @@ import type { Awaitable } from './types'
|
|
|
23
30
|
* debouncedSearch('react hooks') // This cancels the previous call
|
|
24
31
|
* debouncedSearch('react typescript') // Only this will execute
|
|
25
32
|
*
|
|
26
|
-
* // Cancel pending execution
|
|
33
|
+
* // Cancel pending execution — any in-flight promise rejects
|
|
27
34
|
* debouncedSearch.cancel()
|
|
28
35
|
*
|
|
29
|
-
* // With async/await
|
|
36
|
+
* // With async/await — wrap in try/catch if you might cancel
|
|
30
37
|
* const saveData = debounce(async (data: any) => {
|
|
31
38
|
* return await api.save(data)
|
|
32
39
|
* }, 1000)
|
|
33
40
|
*
|
|
34
|
-
*
|
|
41
|
+
* try {
|
|
42
|
+
* const result = await saveData({name: 'John'})
|
|
43
|
+
* } catch (err) {
|
|
44
|
+
* // handle cancellation or callback error
|
|
45
|
+
* }
|
|
35
46
|
* ```
|
|
36
47
|
*
|
|
37
48
|
* @public
|
|
@@ -79,7 +90,14 @@ export function debounce<T extends unknown[], U>(
|
|
|
79
90
|
fn.cancel = () => {
|
|
80
91
|
if (!state) return
|
|
81
92
|
clearTimeout(state.timeout)
|
|
93
|
+
const s = state
|
|
82
94
|
state = undefined
|
|
95
|
+
// Attach a no-op handler so callers that discard the promise don't
|
|
96
|
+
// trigger an unhandled-rejection warning. Consumers that did `.then`,
|
|
97
|
+
// `.catch`, or `await` on the returned promise still observe the
|
|
98
|
+
// rejection through their own chain.
|
|
99
|
+
s.promise.catch(noop)
|
|
100
|
+
s.reject(new Error('Debounced function was cancelled'))
|
|
83
101
|
}
|
|
84
102
|
return fn
|
|
85
103
|
}
|
package/src/lib/function.ts
CHANGED
package/src/lib/media/avif.ts
CHANGED
package/src/lib/network.ts
CHANGED
|
@@ -24,7 +24,7 @@ export async function fetch(input: RequestInfo | URL, init?: RequestInit): Promi
|
|
|
24
24
|
* @returns HTMLImageElement with referrerPolicy set to 'strict-origin-when-cross-origin'
|
|
25
25
|
* @internal
|
|
26
26
|
*/
|
|
27
|
-
export
|
|
27
|
+
export function Image(width?: number, height?: number) {
|
|
28
28
|
// eslint-disable-next-line tldraw/no-restricted-properties
|
|
29
29
|
const img = new window.Image(width, height)
|
|
30
30
|
img.referrerPolicy = 'strict-origin-when-cross-origin'
|
package/src/lib/url.ts
CHANGED
package/src/lib/version.test.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
2
2
|
import { clearRegisteredVersionsForTests, registerTldrawLibraryVersion } from './version'
|
|
3
3
|
|
|
4
4
|
describe('version utilities', () => {
|
|
5
|
-
let mockConsoleLog: ReturnType<typeof vi.fn
|
|
5
|
+
let mockConsoleLog: ReturnType<typeof vi.fn<(...args: any[]) => any>>
|
|
6
6
|
|
|
7
7
|
beforeEach(() => {
|
|
8
8
|
mockConsoleLog = vi.fn()
|