Skip to content

Commit 52dde49

Browse files
docs: harmonize cleanup function naming
1 parent fe1662b commit 52dde49

13 files changed

Lines changed: 90 additions & 90 deletions

File tree

docs/api/auto-sizes.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ To lazy load images, refer to the [`lazyLoad`](/api/lazy-load) method.
1010

1111
| Option | Type | Default | Description |
1212
| --- | --- | --- | --- |
13-
| `updateOnResize` | `boolean` | `false` | Install a debounced `ResizeObserver` that re-resolves `data-sizes="auto"` on viewport changes. The returned disposer disconnects it. |
13+
| `updateOnResize` | `boolean` | `false` | Install a debounced `ResizeObserver` that re-resolves `data-sizes="auto"` on viewport changes. The returned cleanup function disconnects it. |
1414

1515
## Return Value
1616

17-
`autoSizes` returns a disposer. When called with `updateOnResize: false` (or no options), the disposer is a no-op. With `updateOnResize: true`, calling it disconnects every `ResizeObserver` created by that call:
17+
`autoSizes` returns a cleanup function. When called with `updateOnResize: false` (or no options), the cleanup is a no-op. With `updateOnResize: true`, calling it disconnects every `ResizeObserver` created by that call:
1818

1919
```ts
2020
import { autoSizes } from 'unlazy'
2121

22-
const dispose = autoSizes('img[data-sizes="auto"]', { updateOnResize: true })
22+
const cleanup = autoSizes('img[data-sizes="auto"]', { updateOnResize: true })
2323

2424
// Later, when cleaning up
25-
dispose()
25+
cleanup()
2626
```
2727

2828
## Type Declarations
@@ -47,9 +47,9 @@ import { autoSizes } from 'unlazy'
4747
autoSizes()
4848

4949
// Or: resolve and keep tracking on resize
50-
const dispose = autoSizes(undefined, { updateOnResize: true })
50+
const cleanup = autoSizes(undefined, { updateOnResize: true })
5151
```
5252

5353
::: tip
54-
For most users, calling [`lazyLoad`](/api/lazy-load) with `updateSizesOnResize: true` is the simpler path – it delegates to `autoSizes` internally and bundles the disposer into the same cleanup callback.
54+
For most users, calling [`lazyLoad`](/api/lazy-load) with `updateSizesOnResize: true` is the simpler path – it delegates to `autoSizes` internally and bundles the cleanup into the same callback.
5555
:::

docs/api/lazy-load.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Options can be passed to the function to customize its behavior:
3030
| `hash` | `boolean \| string` | `true` | Whether to use a hash for generating a blurry placeholder. Can be `true` (auto-detect from `data-blurhash`/`data-thumbhash`), `false` (disabled), or a hash string. |
3131
| `hashType` | `'blurhash' \| 'thumbhash'` | `'blurhash'` | The type of hash to use. Ignored when `hash` is boolean (auto-detected from data attributes). |
3232
| `placeholderSize` | `number` | `32` | The size of the longer edge for BlurHash decoding. Ignored for ThumbHash. |
33-
| `updateSizesOnResize` | `boolean` | `false` | Re-resolve `data-sizes="auto"` on viewport resize – applies to both `<img>` and `<source>` siblings inside a `<picture>`. Internally delegates to [`autoSizes`](/api/auto-sizes); the returned cleanup disconnects every observer. |
33+
| `updateSizesOnResize` | `boolean` | `false` | Re-resolve `data-sizes="auto"` on viewport resize – applies to both `<img>` and `<source>` siblings inside a `<picture>`. Internally delegates to [`autoSizes`](/api/auto-sizes); the returned cleanup function disconnects every observer. |
3434
| `onImageLoad` | `(image: HTMLImageElement) => void` | - | Callback invoked when an image loads successfully. |
3535
| `onImageError` | `(image: HTMLImageElement, error: Event) => void` | - | Callback invoked when an image fails to load. |
3636

docs/api/trigger-load.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ The function performs the following operations:
1010
4. Swaps `data-src` and `data-srcset` to their standard counterparts.
1111
5. Invokes the optional `onImageLoad` callback when loading completes, or `onImageError` if loading fails. On failure, a synthetic `error` event also fires on the visible `<img>`.
1212

13-
`triggerLoad` returns a disposer that detaches its listeners and, for standalone images, aborts the in-flight network fetch. Calling it after the load has already completed is a no-op.
13+
`triggerLoad` returns a cleanup function that detaches its listeners and, for standalone images, aborts the in-flight network fetch. Calling it after the load has already completed is a no-op.
1414

1515
::: tip
1616
`triggerLoad` is one-shot – it does not install any `ResizeObserver`. For ongoing source-size tracking on responsive `<picture>` layouts, pair it with [`autoSizes`](/api/auto-sizes) and `{ updateOnResize: true }`:
1717

1818
```ts
19-
const disposeSizes = autoSizes(image, { updateOnResize: true })
20-
const disposeLoad = triggerLoad(image)
19+
const cleanupSizes = autoSizes(image, { updateOnResize: true })
20+
const cleanupLoad = triggerLoad(image)
2121

2222
// Later
23-
disposeSizes()
24-
disposeLoad()
23+
cleanupSizes()
24+
cleanupLoad()
2525
```
2626
:::
2727

@@ -37,11 +37,11 @@ import { triggerLoad } from 'unlazy'
3737
const image = document.querySelector<HTMLImageElement>('.priority-image')!
3838

3939
// Load immediately with callbacks
40-
const dispose = triggerLoad(image, {
40+
const cleanup = triggerLoad(image, {
4141
onImageLoad: img => console.log('Loaded:', img.src),
4242
onImageError: (img, error) => console.error('Failed to load:', img, error),
4343
})
4444

4545
// Later, cancel the load if it hasn't completed yet
46-
dispose()
46+
cleanup()
4747
```

docs/guide/migration.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,28 @@ The deprecated `loadImage` alias is gone. Replace every call site with `triggerL
1717

1818
### `triggerLoad` Signature
1919

20-
`triggerLoad` now takes an options object instead of positional callbacks, and returns a disposer that detaches listeners and (for standalone images) aborts the in-flight network fetch:
20+
`triggerLoad` now takes an options object instead of positional callbacks, and returns a cleanup function that detaches listeners and (for standalone images) aborts the in-flight network fetch:
2121

2222
```diff
2323
- triggerLoad(image, onLoad, onError)
24-
+ const dispose = triggerLoad(image, { onImageLoad: onLoad, onImageError: onError })
25-
+ // Optional: dispose() to cancel before the load completes
24+
+ const cleanup = triggerLoad(image, { onImageLoad: onLoad, onImageError: onError })
25+
+ // Optional: cleanup() to cancel before the load completes
2626
```
2727

2828
### `autoSizes` Owns Ongoing Size Tracking
2929

30-
`triggerLoad` is one-shot again – it no longer accepts `updateSizesOnResize`. Ongoing re-resolution of `data-sizes="auto"` lives on [`autoSizes`](/api/auto-sizes), which now accepts `{ updateOnResize: true }` and returns a disposer:
30+
`triggerLoad` is one-shot again – it no longer accepts `updateSizesOnResize`. Ongoing re-resolution of `data-sizes="auto"` lives on [`autoSizes`](/api/auto-sizes), which now accepts `{ updateOnResize: true }` and returns a cleanup function:
3131

3232
```diff
3333
- triggerLoad(image, { updateSizesOnResize: true })
34-
+ const disposeSizes = autoSizes(image, { updateOnResize: true })
35-
+ const disposeLoad = triggerLoad(image)
36-
+ // Later: disposeSizes(); disposeLoad()
34+
+ const cleanupSizes = autoSizes(image, { updateOnResize: true })
35+
+ const cleanupLoad = triggerLoad(image)
36+
+ // Later: cleanupSizes(); cleanupLoad()
3737
```
3838

3939
For the common case, [`lazyLoad`](/api/lazy-load) keeps `updateSizesOnResize` and delegates to `autoSizes` internally – no caller change needed.
4040

41-
`autoSizes` itself now always returns a function. With no options, the returned disposer is a no-op; with `updateOnResize: true`, it disconnects every `ResizeObserver` created by the call. Passing an `<img>` inside a `<picture>` walks to every `<source data-sizes="auto">` sibling in the same call, replacing the previous need to invoke `autoSizes` separately on each source.
41+
`autoSizes` itself now always returns a function. With no options, the returned cleanup is a no-op; with `updateOnResize: true`, it disconnects every `ResizeObserver` created by the call. Passing an `<img>` inside a `<picture>` walks to every `<source data-sizes="auto">` sibling in the same call, replacing the previous need to invoke `autoSizes` separately on each source.
4242

4343
### `isLazyLoadingSupported` Removal
4444

docs/guide/usage.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,16 @@ import { autoSizes } from 'unlazy'
104104
autoSizes()
105105
```
106106

107-
For responsive layouts where the display width changes – fluid containers, breakpoint switches, orientation changes – pass `{ updateOnResize: true }` to keep `sizes` synced. `autoSizes` returns a disposer – call it when you no longer need the observer:
107+
For responsive layouts where the display width changes – fluid containers, breakpoint switches, orientation changes – pass `{ updateOnResize: true }` to keep `sizes` synced. `autoSizes` returns a cleanup function – call it when you no longer need the observer:
108108

109109
```ts
110-
const dispose = autoSizes(undefined, { updateOnResize: true })
110+
const cleanup = autoSizes(undefined, { updateOnResize: true })
111111

112112
// Later, when unmounting
113-
dispose()
113+
cleanup()
114114
```
115115

116-
The same behavior is available on [`lazyLoad`](/api/lazy-load) via `updateSizesOnResize: true`, which delegates to `autoSizes` internally and bundles the disposer into the same cleanup callback.
116+
The same behavior is available on [`lazyLoad`](/api/lazy-load) via `updateSizesOnResize: true`, which delegates to `autoSizes` internally and bundles the cleanup into the same callback.
117117

118118
## Custom Selectors
119119

packages/core/src/lazyLoad.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function lazyLoad<T extends HTMLImageElement>(
2929
onImageError,
3030
}: UnLazyLoadOptions = {},
3131
): () => void {
32-
const cleanupHandlers = new Set<() => void>()
32+
const cleanups = new Set<() => void>()
3333

3434
// @ts-expect-error: Build-time variable
3535
if (typeof __UNLAZY_LOGGING__ === 'undefined' || __UNLAZY_LOGGING__)
@@ -50,7 +50,7 @@ export function lazyLoad<T extends HTMLImageElement>(
5050

5151
// Resolve `data-sizes="auto"` for the img and any picture-source siblings,
5252
// optionally installing a single `ResizeObserver` to retrack on resize
53-
cleanupHandlers.add(autoSizes(image, { updateOnResize: updateSizesOnResize }))
53+
cleanups.add(autoSizes(image, { updateOnResize: updateSizesOnResize }))
5454

5555
// Generate the blurry placeholder from a Blurhash or ThumbHash string if applicable
5656
if (
@@ -85,12 +85,12 @@ export function lazyLoad<T extends HTMLImageElement>(
8585
if (onImageLoad) {
8686
const loadHandler = () => onImageLoad(image)
8787
image.addEventListener('load', loadHandler, { once: true })
88-
cleanupHandlers.add(() => image.removeEventListener('load', loadHandler))
88+
cleanups.add(() => image.removeEventListener('load', loadHandler))
8989
}
9090
if (onImageError) {
9191
const errorHandler = (event: Event) => onImageError(image, event)
9292
image.addEventListener('error', errorHandler, { once: true })
93-
cleanupHandlers.add(() => image.removeEventListener('error', errorHandler))
93+
cleanups.add(() => image.removeEventListener('error', errorHandler))
9494
}
9595
swapPictureSources(image)
9696
swapDataAttribute(image, 'srcset')
@@ -107,24 +107,24 @@ export function lazyLoad<T extends HTMLImageElement>(
107107

108108
// Load the image immediately if is already in the viewport
109109
if (image.complete && image.naturalWidth > 0) {
110-
cleanupHandlers.add(triggerLoad(image, { onImageLoad, onImageError }))
110+
cleanups.add(triggerLoad(image, { onImageLoad, onImageError }))
111111
continue
112112
}
113113

114114
// Otherwise, load the image when it enters the viewport
115115
const loadHandler = () => {
116-
cleanupHandlers.add(triggerLoad(image, { onImageLoad, onImageError }))
116+
cleanups.add(triggerLoad(image, { onImageLoad, onImageError }))
117117
}
118118
image.addEventListener('load', loadHandler, { once: true })
119119

120-
cleanupHandlers.add(
120+
cleanups.add(
121121
() => image.removeEventListener('load', loadHandler),
122122
)
123123
}
124124

125125
return () => {
126-
for (const fn of cleanupHandlers) fn()
127-
cleanupHandlers.clear()
126+
for (const fn of cleanups) fn()
127+
cleanups.clear()
128128
}
129129
}
130130

@@ -135,7 +135,7 @@ export function lazyLoad<T extends HTMLImageElement>(
135135
* resolves them too. With `{ updateOnResize: true }`, a debounced
136136
* `ResizeObserver` retracks the rendered width on viewport changes.
137137
*
138-
* @returns A disposer that disconnects every observer created by this call.
138+
* @returns A cleanup function that disconnects every observer created by this call.
139139
* Calling it on a one-shot invocation is a no-op.
140140
*/
141141
export function autoSizes<T extends HTMLImageElement | HTMLSourceElement>(
@@ -152,22 +152,22 @@ export function autoSizes<T extends HTMLImageElement | HTMLSourceElement>(
152152
selectorsOrElements: string | T | NodeListOf<T> | T[] = 'img[data-sizes="auto"], source[data-sizes="auto"]',
153153
{ updateOnResize = false }: AutoSizesOptions = {},
154154
): () => void {
155-
const disposers: (() => void)[] = []
155+
const cleanups: (() => void)[] = []
156156

157157
for (const element of toElementArray<T>(selectorsOrElements))
158-
disposers.push(observeAutoSizes(element, updateOnResize))
158+
cleanups.push(observeAutoSizes(element, updateOnResize))
159159

160160
return () => {
161-
for (const fn of disposers) fn()
162-
disposers.length = 0
161+
for (const fn of cleanups) fn()
162+
cleanups.length = 0
163163
}
164164
}
165165

166166
// #region triggerLoad
167167
/**
168168
* Triggers the loading of a lazy image by swapping `data-src`/`data-srcset` to `src`/`srcset`.
169169
*
170-
* @returns A disposer that detaches listeners and, for standalone images, aborts any
170+
* @returns A cleanup function that detaches listeners and, for standalone images, aborts any
171171
* in-flight network fetch by clearing the temporary image's `src`. Calling it after the
172172
* load completes is a no-op.
173173
*
@@ -194,34 +194,34 @@ export function triggerLoad(
194194
image: HTMLImageElement,
195195
{ onImageLoad, onImageError }: TriggerLoadOptions = {},
196196
): () => void {
197-
const disposers: (() => void)[] = []
198-
const dispose = () => {
199-
for (const d of disposers) d()
200-
disposers.length = 0
197+
const cleanups: (() => void)[] = []
198+
const cleanup = () => {
199+
for (const fn of cleanups) fn()
200+
cleanups.length = 0
201201
}
202202

203203
if (isDescendantOfPicture(image)) {
204204
if (onImageLoad) {
205205
const handler = () => onImageLoad(image)
206206
image.addEventListener('load', handler, { once: true })
207-
disposers.push(() => image.removeEventListener('load', handler))
207+
cleanups.push(() => image.removeEventListener('load', handler))
208208
}
209209
if (onImageError) {
210210
const handler = (event: Event) => onImageError(image, event)
211211
image.addEventListener('error', handler, { once: true })
212-
disposers.push(() => image.removeEventListener('error', handler))
212+
cleanups.push(() => image.removeEventListener('error', handler))
213213
}
214214

215215
swapPictureSources(image)
216216
swapDataAttribute(image, 'srcset')
217217
swapDataAttribute(image, 'src')
218-
return dispose
218+
return cleanup
219219
}
220220

221221
const { srcset: dataSrcset, src: dataSrc, sizes: dataSizes } = image.dataset
222222

223223
if (!dataSrcset && !dataSrc)
224-
return dispose
224+
return cleanup
225225

226226
const temporaryImage = new Image()
227227

@@ -240,7 +240,7 @@ export function triggerLoad(
240240
temporaryImage.addEventListener('load', loadHandler, { once: true })
241241
temporaryImage.addEventListener('error', errorHandler, { once: true })
242242

243-
disposers.push(() => {
243+
cleanups.push(() => {
244244
temporaryImage.removeEventListener('load', loadHandler)
245245
temporaryImage.removeEventListener('error', errorHandler)
246246
// Empty `src` aborts the pending fetch per HTML "update the image data".
@@ -262,7 +262,7 @@ export function triggerLoad(
262262
if (dataSrc)
263263
temporaryImage.src = dataSrc
264264

265-
return dispose
265+
return cleanup
266266
}
267267

268268
// #region createPlaceholderFromHash

packages/core/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export interface AutoSizesOptions {
7777
/**
7878
* Whether `data-sizes="auto"` should retrack the rendered width on viewport
7979
* resize. Sets up a debounced `ResizeObserver` per call; the returned
80-
* disposer disconnects it.
80+
* cleanup function disconnects it.
8181
*
8282
* @default false
8383
*/

packages/core/test/triggerLoad.browser.test.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,57 +148,57 @@ describe('triggerLoad (browser)', () => {
148148
expect(nativeError).toHaveBeenCalled()
149149
})
150150

151-
it('disposer cancels an in-flight standalone preload', async () => {
151+
it('cleanup cancels an in-flight standalone preload', async () => {
152152
const img = document.createElement('img')
153153
img.dataset.src = TINY_PNG
154154
document.body.appendChild(img)
155155

156156
const onImageLoad = vi.fn()
157-
const dispose = triggerLoad(img, { onImageLoad })
158-
dispose()
157+
const cleanup = triggerLoad(img, { onImageLoad })
158+
cleanup()
159159

160160
await new Promise(resolve => setTimeout(resolve, 200))
161161

162162
expect(onImageLoad).not.toHaveBeenCalled()
163163
})
164164

165-
it('disposer cancels an in-flight picture-wrapped load', async () => {
165+
it('cleanup cancels an in-flight picture-wrapped load', async () => {
166166
const picture = document.createElement('picture')
167167
const img = document.createElement('img')
168168
img.dataset.src = TINY_PNG
169169
picture.appendChild(img)
170170
document.body.appendChild(picture)
171171

172172
const onImageLoad = vi.fn()
173-
const dispose = triggerLoad(img, { onImageLoad })
174-
dispose()
173+
const cleanup = triggerLoad(img, { onImageLoad })
174+
cleanup()
175175

176176
await new Promise(resolve => setTimeout(resolve, 200))
177177

178178
expect(onImageLoad).not.toHaveBeenCalled()
179179
})
180180

181-
it('tolerates calling the disposer twice', () => {
181+
it('tolerates calling cleanup twice', () => {
182182
const img = document.createElement('img')
183183
img.dataset.src = TINY_PNG
184184
document.body.appendChild(img)
185185

186-
const dispose = triggerLoad(img, { onImageLoad: vi.fn() })
187-
dispose()
188-
expect(() => dispose()).not.toThrow()
186+
const cleanup = triggerLoad(img, { onImageLoad: vi.fn() })
187+
cleanup()
188+
expect(() => cleanup()).not.toThrow()
189189
})
190190

191-
it('disposer is a no-op when called after the load completes', async () => {
191+
it('cleanup is a no-op when called after the load completes', async () => {
192192
const img = document.createElement('img')
193193
img.dataset.src = TINY_PNG
194194
document.body.appendChild(img)
195195

196196
const onImageLoad = vi.fn()
197-
let dispose!: () => void
197+
let cleanup!: () => void
198198

199199
await new Promise<void>((resolve, reject) => {
200200
const timer = setTimeout(() => reject(new Error('load did not complete')), 2000)
201-
dispose = triggerLoad(img, {
201+
cleanup = triggerLoad(img, {
202202
onImageLoad: (image) => {
203203
onImageLoad(image)
204204
clearTimeout(timer)
@@ -207,7 +207,7 @@ describe('triggerLoad (browser)', () => {
207207
})
208208
})
209209

210-
expect(() => dispose()).not.toThrow()
210+
expect(() => cleanup()).not.toThrow()
211211
expect(onImageLoad).toHaveBeenCalledTimes(1)
212212
})
213213

0 commit comments

Comments
 (0)