react-native-nitro-image-pipeline 1.5.0 → 1.7.0
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/README.md +142 -18
- package/android/src/main/java/com/margelo/nitro/nitroimagepipeline/HybridNitroImagePipeline.kt +72 -16
- package/android/src/main/java/com/margelo/nitro/nitroimagepipeline/PipelineImageLoader.kt +47 -8
- package/android/src/main/java/com/margelo/nitro/nitroimagepipeline/transform/FitGeometry.kt +104 -0
- package/android/src/main/java/com/margelo/nitro/nitroimagepipeline/transform/PipelineRoundedCornersTransformation.kt +103 -0
- package/android/src/main/java/com/margelo/nitro/nitroimagepipeline/transform/ResizeTransformation.kt +69 -16
- package/ios/FitResizeProcessor.swift +127 -0
- package/ios/HybridNitroImagePipeline.swift +88 -24
- package/ios/PipelineImageLoader.swift +108 -18
- package/lib/commonjs/NativePipelineImage.js +20 -4
- package/lib/commonjs/NativePipelineImage.js.map +1 -1
- package/lib/commonjs/PipelineImage.js +13 -1
- package/lib/commonjs/PipelineImage.js.map +1 -1
- package/lib/commonjs/index.js +13 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/resizeForStyle.js +3 -1
- package/lib/commonjs/resizeForStyle.js.map +1 -1
- package/lib/commonjs/resolveImageSource.js +89 -0
- package/lib/commonjs/resolveImageSource.js.map +1 -0
- package/lib/commonjs/useImage.js +14 -6
- package/lib/commonjs/useImage.js.map +1 -1
- package/lib/commonjs/usePipelineImageLoader.js +114 -11
- package/lib/commonjs/usePipelineImageLoader.js.map +1 -1
- package/lib/module/NativePipelineImage.js +20 -4
- package/lib/module/NativePipelineImage.js.map +1 -1
- package/lib/module/PipelineImage.js +13 -1
- package/lib/module/PipelineImage.js.map +1 -1
- package/lib/module/index.js +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/resizeForStyle.js +3 -1
- package/lib/module/resizeForStyle.js.map +1 -1
- package/lib/module/resolveImageSource.js +83 -0
- package/lib/module/resolveImageSource.js.map +1 -0
- package/lib/module/useImage.js +14 -6
- package/lib/module/useImage.js.map +1 -1
- package/lib/module/usePipelineImageLoader.js +115 -12
- package/lib/module/usePipelineImageLoader.js.map +1 -1
- package/lib/typescript/src/NativePipelineImage.d.ts +51 -5
- package/lib/typescript/src/NativePipelineImage.d.ts.map +1 -1
- package/lib/typescript/src/PipelineImage.d.ts +35 -3
- package/lib/typescript/src/PipelineImage.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +2 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/resizeForStyle.d.ts +3 -1
- package/lib/typescript/src/resizeForStyle.d.ts.map +1 -1
- package/lib/typescript/src/resolveImageSource.d.ts +56 -0
- package/lib/typescript/src/resolveImageSource.d.ts.map +1 -0
- package/lib/typescript/src/specs/nitro-image-toolkit.nitro.d.ts +95 -8
- package/lib/typescript/src/specs/nitro-image-toolkit.nitro.d.ts.map +1 -1
- package/lib/typescript/src/useImage.d.ts +13 -6
- package/lib/typescript/src/useImage.d.ts.map +1 -1
- package/lib/typescript/src/usePipelineImageLoader.d.ts +7 -3
- package/lib/typescript/src/usePipelineImageLoader.d.ts.map +1 -1
- package/nitrogen/generated/android/NitroImagePipelineOnLoad.cpp +4 -0
- package/nitrogen/generated/android/c++/JFunc_void_double_double.hpp +75 -0
- package/nitrogen/generated/android/c++/JFunc_void_std__string.hpp +76 -0
- package/nitrogen/generated/android/c++/JHybridNitroImagePipelineSpec.cpp +8 -0
- package/nitrogen/generated/android/c++/JOptions.hpp +2 -0
- package/nitrogen/generated/android/c++/JResizeFit.hpp +64 -0
- package/nitrogen/generated/android/c++/JResizeOptions.hpp +14 -4
- package/nitrogen/generated/android/c++/JViewOptions.hpp +42 -3
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroimagepipeline/Func_void_double_double.kt +78 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroimagepipeline/Func_void_std__string.kt +78 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroimagepipeline/ResizeFit.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroimagepipeline/ResizeOptions.kt +14 -4
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroimagepipeline/ViewOptions.kt +29 -5
- package/nitrogen/generated/ios/NitroImagePipeline-Swift-Cxx-Bridge.cpp +16 -0
- package/nitrogen/generated/ios/NitroImagePipeline-Swift-Cxx-Bridge.hpp +107 -0
- package/nitrogen/generated/ios/NitroImagePipeline-Swift-Cxx-Umbrella.hpp +4 -0
- package/nitrogen/generated/ios/c++/HybridNitroImagePipelineSpecSwift.hpp +4 -0
- package/nitrogen/generated/ios/swift/Func_void_double_double.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_std__string.swift +46 -0
- package/nitrogen/generated/ios/swift/ResizeFit.swift +48 -0
- package/nitrogen/generated/ios/swift/ResizeOptions.swift +31 -2
- package/nitrogen/generated/ios/swift/ViewOptions.swift +82 -1
- package/nitrogen/generated/shared/c++/ResizeFit.hpp +84 -0
- package/nitrogen/generated/shared/c++/ResizeOptions.hpp +14 -4
- package/nitrogen/generated/shared/c++/ViewOptions.hpp +24 -3
- package/package.json +1 -1
- package/src/NativePipelineImage.tsx +75 -6
- package/src/PipelineImage.tsx +46 -3
- package/src/index.ts +6 -0
- package/src/resizeForStyle.ts +3 -1
- package/src/resolveImageSource.ts +83 -0
- package/src/specs/nitro-image-toolkit.nitro.ts +96 -8
- package/src/useImage.ts +38 -17
- package/src/usePipelineImageLoader.ts +126 -11
|
@@ -15,13 +15,46 @@ export interface CornerRadii {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
18
|
+
* How the source is fitted into the {@linkcode ResizeOptions} box. Mirrors
|
|
19
|
+
* `NativeNitroImage`'s `resizeMode`, so a view's `resizeMode` and the bitmap
|
|
20
|
+
* it displays can be derived from one value. With a `w` × `h` source and a
|
|
21
|
+
* `W` × `H` box, the produced bitmap is:
|
|
22
|
+
*
|
|
23
|
+
* - `cover`: scale by `max(W/w, H/h)`, center-crop → exactly `W` × `H`
|
|
24
|
+
* (CSS `object-fit: cover`).
|
|
25
|
+
* - `contain`: scale by `min(W/w, H/h)` → `round(w·s)` × `round(h·s)`, the
|
|
26
|
+
* largest size with the source's aspect ratio that fits the box. The
|
|
27
|
+
* bitmap is *not* padded to the box.
|
|
28
|
+
* - `stretch`: scale each axis independently → exactly `W` × `H`, aspect
|
|
29
|
+
* ratio ignored.
|
|
30
|
+
* - `center`: no scaling; center-crop to `min(w, W)` × `min(h, H)`. A source
|
|
31
|
+
* smaller than the box is left untouched.
|
|
32
|
+
*/
|
|
33
|
+
export type ResizeFit = 'cover' | 'contain' | 'stretch' | 'center';
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Target bitmap size in pixels. By default the image is scaled to fill this
|
|
37
|
+
* size and center-cropped (like CSS `object-fit: cover`), upscaling if
|
|
38
|
+
* needed, so the result is exactly `width` × `height` pixels; {@linkcode fit}
|
|
39
|
+
* selects the other resize modes.
|
|
21
40
|
*/
|
|
22
41
|
export interface ResizeOptions {
|
|
23
42
|
width: number;
|
|
24
43
|
height: number;
|
|
44
|
+
/**
|
|
45
|
+
* How the source is fitted into `width` × `height` — see
|
|
46
|
+
* {@linkcode ResizeFit} for the size each mode produces.
|
|
47
|
+
* @default 'cover'
|
|
48
|
+
*/
|
|
49
|
+
fit?: ResizeFit;
|
|
50
|
+
/**
|
|
51
|
+
* `false` never enlarges the source: the scale is clamped to at most 1 for
|
|
52
|
+
* `cover`, `contain` and `stretch`, so a source smaller than the box keeps
|
|
53
|
+
* its own size (and, under `cover`, is only cropped to the box). No effect
|
|
54
|
+
* on `center`, which never scales.
|
|
55
|
+
* @default true
|
|
56
|
+
*/
|
|
57
|
+
allowUpscale?: boolean;
|
|
25
58
|
}
|
|
26
59
|
|
|
27
60
|
export type Options = {
|
|
@@ -55,10 +88,11 @@ export type Options = {
|
|
|
55
88
|
*/
|
|
56
89
|
cornerRadius?: number | CornerRadii;
|
|
57
90
|
/**
|
|
58
|
-
* Resize the image
|
|
59
|
-
*
|
|
60
|
-
* `cornerRadius`
|
|
61
|
-
* full-resolution bitmaps you only
|
|
91
|
+
* Resize the image into this box in pixels — aspect-fill + center-crop by
|
|
92
|
+
* default, or another {@linkcode ResizeFit} — before `blur` and
|
|
93
|
+
* `cornerRadius` are applied. Besides making `cornerRadius` predictable,
|
|
94
|
+
* this avoids decoding and processing full-resolution bitmaps you only
|
|
95
|
+
* display small.
|
|
62
96
|
*
|
|
63
97
|
* @default undefined (keep the source size)
|
|
64
98
|
*/
|
|
@@ -90,16 +124,65 @@ export type ViewOptions = {
|
|
|
90
124
|
/**
|
|
91
125
|
* Target bitmap size in **pixels**, overriding the size measured from the
|
|
92
126
|
* view. Rarely needed — without it the loader resizes to the view's
|
|
93
|
-
* laid-out size × screen scale, which is what you want in a UI.
|
|
127
|
+
* laid-out size × screen scale, which is what you want in a UI. Its own
|
|
128
|
+
* `fit`/`allowUpscale`, when set, win over the top-level ones below.
|
|
94
129
|
* @default undefined (measure the view)
|
|
95
130
|
*/
|
|
96
131
|
resize?: ResizeOptions;
|
|
132
|
+
/**
|
|
133
|
+
* How the source is fitted into the box the loader resizes to (the view's
|
|
134
|
+
* size, or {@linkcode resize}) — see {@linkcode ResizeFit}. Pass the view's
|
|
135
|
+
* `resizeMode` so the bitmap matches how the view displays it.
|
|
136
|
+
* @default 'cover'
|
|
137
|
+
*/
|
|
138
|
+
fit?: ResizeFit;
|
|
139
|
+
/**
|
|
140
|
+
* `false` never enlarges the source — see
|
|
141
|
+
* {@linkcode ResizeOptions.allowUpscale}.
|
|
142
|
+
* @default true
|
|
143
|
+
*/
|
|
144
|
+
allowUpscale?: boolean;
|
|
145
|
+
/**
|
|
146
|
+
* Called when a view finishes displaying the image, with the size of the
|
|
147
|
+
* bitmap it displays in **pixels**.
|
|
148
|
+
*
|
|
149
|
+
* The bitmap itself deliberately stays native — passing the `Image` into JS
|
|
150
|
+
* would keep it alive there and defeat the point of the native path. Use
|
|
151
|
+
* `useImage`/`loadImage` when you need the `Image` object.
|
|
152
|
+
*
|
|
153
|
+
* This reports "the view is now showing this image", not a one-shot event:
|
|
154
|
+
* a loader is per URL + options, not per view, so every view using it
|
|
155
|
+
* reports, a recycled cell reports again when it re-attaches, and a single
|
|
156
|
+
* view can report more than once (it requests the image both when its
|
|
157
|
+
* `image` prop is set and when it becomes visible). Memory-cache hits, which
|
|
158
|
+
* resolve synchronously, report too. It does not fire for imperative loads
|
|
159
|
+
* through `ImageLoader.loadImage()` — those resolve through their promise.
|
|
160
|
+
*
|
|
161
|
+
* Setting it means there *is* per-image JS work; leave it unset and the
|
|
162
|
+
* native path stays free of JS round trips entirely.
|
|
163
|
+
* @default undefined (nothing crosses into JS)
|
|
164
|
+
*/
|
|
165
|
+
onLoad?: (width: number, height: number) => void;
|
|
166
|
+
/**
|
|
167
|
+
* Called when a view fails to load the image, with the underlying error's
|
|
168
|
+
* message. Cancelling a load (the view detaching before it finishes) is not
|
|
169
|
+
* a failure and does not call this.
|
|
170
|
+
* @default undefined (failures are silent)
|
|
171
|
+
*/
|
|
172
|
+
onError?: (message: string) => void;
|
|
97
173
|
};
|
|
98
174
|
|
|
99
175
|
export interface NitroImagePipeline extends HybridObject<{
|
|
100
176
|
ios: 'swift';
|
|
101
177
|
android: 'kotlin';
|
|
102
178
|
}> {
|
|
179
|
+
/**
|
|
180
|
+
* Loads `url` through the pipeline. Besides `http(s)://`, `url` may be a
|
|
181
|
+
* `file://` URL or a plain absolute path (read from the file system, cached
|
|
182
|
+
* in memory only), a `data:` URL, or — on Android — a `content://` URI or
|
|
183
|
+
* the bare resource name a `require()` resolves to in a release build. Use
|
|
184
|
+
* `resolveImageUrl(require('./photo.png'))` to pass a bundled asset.
|
|
185
|
+
*/
|
|
103
186
|
loadImage(url: string, options?: Options): Promise<Image>;
|
|
104
187
|
/**
|
|
105
188
|
* Creates an {@linkcode ImageLoader} for `url` that a `<NativeNitroImage>`
|
|
@@ -114,6 +197,11 @@ export interface NitroImagePipeline extends HybridObject<{
|
|
|
114
197
|
* attaches, the load waits for its first layout.
|
|
115
198
|
*/
|
|
116
199
|
createImageLoader(url: string, options?: ViewOptions): ImageLoader;
|
|
200
|
+
/**
|
|
201
|
+
* Warms the disk cache with the download for `url`, without decoding it.
|
|
202
|
+
* Local sources (`file://`, paths, resources) have no download to warm it
|
|
203
|
+
* with and are a no-op.
|
|
204
|
+
*/
|
|
117
205
|
preLoadImage(url: string): Promise<void>;
|
|
118
206
|
preLoadImages(urls: string[]): Promise<void>;
|
|
119
207
|
/**
|
package/src/useImage.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { useEffect, useRef, useState } from 'react';
|
|
|
2
2
|
import type { Image } from 'react-native-nitro-image';
|
|
3
3
|
|
|
4
4
|
import { NitroImagePipeline } from './NitroImagePipeline';
|
|
5
|
+
import { type ImageSource, resolveImageUrl } from './resolveImageSource';
|
|
5
6
|
import type {
|
|
6
7
|
CacheOption,
|
|
7
8
|
CornerRadii,
|
|
@@ -26,11 +27,12 @@ type Result =
|
|
|
26
27
|
};
|
|
27
28
|
|
|
28
29
|
/**
|
|
29
|
-
* A hook to asynchronously load an image from the
|
|
30
|
-
*
|
|
30
|
+
* A hook to asynchronously load an image from `url` through the pipeline
|
|
31
|
+
* into memory.
|
|
31
32
|
* @example
|
|
32
33
|
* ```ts
|
|
33
|
-
* const { image, error } = useImage({
|
|
34
|
+
* const { image, error } = useImage({ url: 'https://example.com/photo.jpg' });
|
|
35
|
+
* const { image: logo } = useImage({ url: require('./logo.png') });
|
|
34
36
|
* ```
|
|
35
37
|
*/
|
|
36
38
|
export function useImage({
|
|
@@ -41,7 +43,11 @@ export function useImage({
|
|
|
41
43
|
cache,
|
|
42
44
|
enabled = true,
|
|
43
45
|
}: {
|
|
44
|
-
|
|
46
|
+
/**
|
|
47
|
+
* The image to load: a URL string (`https://`, `file://`, an absolute path)
|
|
48
|
+
* or a `require()`d asset — see {@linkcode ImageSource}.
|
|
49
|
+
*/
|
|
50
|
+
url: ImageSource;
|
|
45
51
|
/**
|
|
46
52
|
* Gaussian blur strength, as the standard deviation (sigma) of the blur in
|
|
47
53
|
* source-image pixels (of the resized bitmap when `resize` is set). Matches
|
|
@@ -56,8 +62,9 @@ export function useImage({
|
|
|
56
62
|
*/
|
|
57
63
|
cornerRadius?: number | CornerRadii;
|
|
58
64
|
/**
|
|
59
|
-
* Resize the bitmap
|
|
60
|
-
* center-crop
|
|
65
|
+
* Resize the bitmap into this box in pixels before blur/rounding —
|
|
66
|
+
* aspect-fill + center-crop by default, or another `fit` (see
|
|
67
|
+
* {@linkcode ResizeOptions}). Typically your display size in points
|
|
61
68
|
* multiplied by `PixelRatio.get()`. Inline object literals are fine.
|
|
62
69
|
*/
|
|
63
70
|
resize?: ResizeOptions;
|
|
@@ -89,6 +96,8 @@ export function useImage({
|
|
|
89
96
|
} = isUniformRadius ? {} : cornerRadius;
|
|
90
97
|
const resizeWidth = resize?.width ?? 0;
|
|
91
98
|
const resizeHeight = resize?.height ?? 0;
|
|
99
|
+
const resizeFit = resize?.fit;
|
|
100
|
+
const resizeAllowUpscale = resize?.allowUpscale;
|
|
92
101
|
|
|
93
102
|
useEffect(() => {
|
|
94
103
|
let cancelled = false;
|
|
@@ -103,17 +112,27 @@ export function useImage({
|
|
|
103
112
|
if (enabled) {
|
|
104
113
|
(async () => {
|
|
105
114
|
try {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
?
|
|
114
|
-
:
|
|
115
|
-
|
|
116
|
-
|
|
115
|
+
// Resolved inside the try so an unregistered `require()` id
|
|
116
|
+
// surfaces as the error state instead of throwing from the effect.
|
|
117
|
+
const result = await NitroImagePipeline.loadImage(
|
|
118
|
+
resolveImageUrl(url),
|
|
119
|
+
{
|
|
120
|
+
blur,
|
|
121
|
+
cornerRadius: isUniformRadius
|
|
122
|
+
? uniformRadius
|
|
123
|
+
: { topLeft, topRight, bottomLeft, bottomRight },
|
|
124
|
+
resize:
|
|
125
|
+
resizeWidth > 0 && resizeHeight > 0
|
|
126
|
+
? {
|
|
127
|
+
width: resizeWidth,
|
|
128
|
+
height: resizeHeight,
|
|
129
|
+
fit: resizeFit,
|
|
130
|
+
allowUpscale: resizeAllowUpscale,
|
|
131
|
+
}
|
|
132
|
+
: undefined,
|
|
133
|
+
cache,
|
|
134
|
+
},
|
|
135
|
+
);
|
|
117
136
|
|
|
118
137
|
if (!cancelled) {
|
|
119
138
|
setImage({ image: result, error: undefined });
|
|
@@ -141,6 +160,8 @@ export function useImage({
|
|
|
141
160
|
bottomRight,
|
|
142
161
|
resizeWidth,
|
|
143
162
|
resizeHeight,
|
|
163
|
+
resizeFit,
|
|
164
|
+
resizeAllowUpscale,
|
|
144
165
|
cache,
|
|
145
166
|
enabled,
|
|
146
167
|
]);
|
|
@@ -1,14 +1,51 @@
|
|
|
1
|
-
import { useMemo } from 'react';
|
|
1
|
+
import { type RefObject, useLayoutEffect, useMemo, useRef } from 'react';
|
|
2
2
|
import type { ImageLoader } from 'react-native-nitro-image';
|
|
3
3
|
|
|
4
4
|
import { NitroImagePipeline } from './NitroImagePipeline';
|
|
5
|
+
import {
|
|
6
|
+
type ImageSource,
|
|
7
|
+
resolveImageUrlOrFallback,
|
|
8
|
+
} from './resolveImageSource';
|
|
5
9
|
import type {
|
|
6
10
|
CornerRadii,
|
|
7
11
|
ViewOptions,
|
|
8
12
|
} from './specs/nitro-image-toolkit.nitro';
|
|
9
13
|
|
|
14
|
+
// Identifies a loader in the callbacks below without them capturing it. A
|
|
15
|
+
// callback the native loader owns that captured the loader back would be a
|
|
16
|
+
// cycle across the language boundary — neither side collectable, and the JS
|
|
17
|
+
// runtime left to take it apart at teardown.
|
|
18
|
+
let nextLoaderToken = 0;
|
|
19
|
+
|
|
20
|
+
type Callbacks = Pick<ViewOptions, 'onLoad' | 'onError'>;
|
|
21
|
+
|
|
10
22
|
/**
|
|
11
|
-
*
|
|
23
|
+
* The callbacks handed to a native loader. Built out here, where the scope
|
|
24
|
+
* holds nothing but the token and the two refs, so the loader can't end up
|
|
25
|
+
* captured by a callback it owns.
|
|
26
|
+
*/
|
|
27
|
+
function makeReporters(
|
|
28
|
+
token: number,
|
|
29
|
+
activeToken: RefObject<number>,
|
|
30
|
+
callbacks: RefObject<Callbacks>,
|
|
31
|
+
): Required<Callbacks> {
|
|
32
|
+
return {
|
|
33
|
+
onLoad: (width, height) => {
|
|
34
|
+
if (activeToken.current === token) {
|
|
35
|
+
callbacks.current.onLoad?.(width, height);
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
onError: (message) => {
|
|
39
|
+
if (activeToken.current === token) {
|
|
40
|
+
callbacks.current.onError?.(message);
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Creates (and memoizes) an {@linkcode ImageLoader} for `source` — a URL
|
|
48
|
+
* string or a `require()`d asset, see {@linkcode ImageSource} — to pass to
|
|
12
49
|
* `<NativeNitroImage image={...} />`. The view drives it entirely natively:
|
|
13
50
|
* the request starts when the view attaches — at the view's laid-out size,
|
|
14
51
|
* with no JS round trips — and is cancelled when it detaches. See
|
|
@@ -16,10 +53,12 @@ import type {
|
|
|
16
53
|
*
|
|
17
54
|
* `blur`/`cornerRadius` are in **points** (unlike `useImage`, where they are
|
|
18
55
|
* bitmap pixels); the screen scale is applied natively. Inline object
|
|
19
|
-
* literals are fine — options are compared by value, not identity
|
|
56
|
+
* literals are fine — options are compared by value, not identity, and the
|
|
57
|
+
* `onLoad`/`onError` callbacks are read through a ref, so inline arrows don't
|
|
58
|
+
* recreate the loader either.
|
|
20
59
|
*/
|
|
21
60
|
export function usePipelineImageLoader(
|
|
22
|
-
|
|
61
|
+
source: ImageSource,
|
|
23
62
|
options?: ViewOptions,
|
|
24
63
|
): ImageLoader {
|
|
25
64
|
// Split the options into primitives (like useImage does) so an inline
|
|
@@ -39,34 +78,93 @@ export function usePipelineImageLoader(
|
|
|
39
78
|
} = isUniformRadius || cornerRadius === undefined ? {} : cornerRadius;
|
|
40
79
|
const resizeWidth = options?.resize?.width;
|
|
41
80
|
const resizeHeight = options?.resize?.height;
|
|
81
|
+
const resizeFit = options?.resize?.fit;
|
|
82
|
+
const resizeAllowUpscale = options?.resize?.allowUpscale;
|
|
83
|
+
const fit = options?.fit;
|
|
84
|
+
const allowUpscale = options?.allowUpscale;
|
|
85
|
+
const onLoad = options?.onLoad;
|
|
86
|
+
const onError = options?.onError;
|
|
87
|
+
// Only *whether* a callback is set can change the loader: what the native
|
|
88
|
+
// side gets is a wrapper that reads the latest callbacks from a ref (like
|
|
89
|
+
// PipelineImage does). So an inline arrow — a new identity every render —
|
|
90
|
+
// doesn't recreate the loader and re-trigger the native load.
|
|
91
|
+
const hasOnLoad = onLoad !== undefined;
|
|
92
|
+
const hasOnError = onError !== undefined;
|
|
93
|
+
const callbacks = useRef({ onLoad, onError });
|
|
94
|
+
// A layout effect, not a passive one: passive effects are flushed in a later
|
|
95
|
+
// task, so a load reported between the commit and that flush would read the
|
|
96
|
+
// previous render's callbacks — a callback added alongside a new loader
|
|
97
|
+
// could miss its only event. This runs in the same task as the commit, and
|
|
98
|
+
// the view can't reach the loader before the commit installs it, so the ref
|
|
99
|
+
// is always current by the time native calls back. (Assigning it during
|
|
100
|
+
// render instead would mutate on renders React throws away.)
|
|
101
|
+
useLayoutEffect(() => {
|
|
102
|
+
callbacks.current = { onLoad, onError };
|
|
103
|
+
});
|
|
104
|
+
// The token of the loader the view is currently on. Swapping loaders (a
|
|
105
|
+
// changed url or options) never drops the old one — the view just stops
|
|
106
|
+
// referring to it — so a load it already had in flight can still report.
|
|
107
|
+
// Without this check the wrappers, which read the latest callbacks, would
|
|
108
|
+
// hand the previous image's size to the current `onLoad`.
|
|
109
|
+
const activeToken = useRef(-1);
|
|
42
110
|
|
|
43
|
-
|
|
111
|
+
const memoized = useMemo(() => {
|
|
112
|
+
// Runs during render, so an unregistered `require()` id must not throw:
|
|
113
|
+
// it becomes a URL the native loader fails on at load time instead.
|
|
114
|
+
const url = resolveImageUrlOrFallback(source);
|
|
44
115
|
const cornerRadiusOption: number | CornerRadii | undefined = isUniformRadius
|
|
45
116
|
? uniformRadius
|
|
46
117
|
: hasCornerObject
|
|
47
118
|
? { topLeft, topRight, bottomLeft, bottomRight }
|
|
48
119
|
: undefined;
|
|
120
|
+
// What is loaded — the callbacks are deliberately not part of it, so two
|
|
121
|
+
// loaders that differ only in them are the same image.
|
|
49
122
|
const stableOptions: ViewOptions = {
|
|
50
123
|
blur,
|
|
51
124
|
cache,
|
|
52
125
|
cornerRadius: cornerRadiusOption,
|
|
53
126
|
resize:
|
|
54
127
|
resizeWidth !== undefined && resizeHeight !== undefined
|
|
55
|
-
? {
|
|
128
|
+
? {
|
|
129
|
+
width: resizeWidth,
|
|
130
|
+
height: resizeHeight,
|
|
131
|
+
fit: resizeFit,
|
|
132
|
+
allowUpscale: resizeAllowUpscale,
|
|
133
|
+
}
|
|
56
134
|
: undefined,
|
|
135
|
+
fit,
|
|
136
|
+
allowUpscale,
|
|
57
137
|
};
|
|
58
|
-
const
|
|
138
|
+
const token = nextLoaderToken++;
|
|
139
|
+
// The reporters read the refs, but only when the native side calls them —
|
|
140
|
+
// the rule can't see that they aren't called here.
|
|
141
|
+
// oxlint-disable-next-line react/refs
|
|
142
|
+
const reporters = makeReporters(token, activeToken, callbacks);
|
|
143
|
+
const loader = NitroImagePipeline.createImageLoader(url, {
|
|
144
|
+
...stableOptions,
|
|
145
|
+
// Passed only when the caller wants them: without a callback nothing
|
|
146
|
+
// ever crosses into JS, which is the point of the native path.
|
|
147
|
+
onLoad: hasOnLoad ? reporters.onLoad : undefined,
|
|
148
|
+
onError: hasOnError ? reporters.onError : undefined,
|
|
149
|
+
});
|
|
59
150
|
// `NativeNitroImage` needs a way to tell two loader instances apart when
|
|
60
151
|
// diffing its `image` prop; tag the loader with what it will load (the
|
|
61
|
-
// same convention react-native-nitro-image's own loaders use).
|
|
152
|
+
// same convention react-native-nitro-image's own loaders use). Toggling a
|
|
153
|
+
// callback on or off makes a different loader, so the tag has to say so —
|
|
154
|
+
// otherwise the view keeps the old one installed. Their identities stay
|
|
155
|
+
// out of it: those don't change what is loaded, or which loader this is.
|
|
62
156
|
Object.defineProperty(loader, '__source', {
|
|
63
157
|
enumerable: true,
|
|
64
158
|
configurable: true,
|
|
65
|
-
value: {
|
|
159
|
+
value: {
|
|
160
|
+
url,
|
|
161
|
+
options: stableOptions,
|
|
162
|
+
callbacks: { onLoad: hasOnLoad, onError: hasOnError },
|
|
163
|
+
},
|
|
66
164
|
});
|
|
67
|
-
return loader;
|
|
165
|
+
return { loader, token };
|
|
68
166
|
}, [
|
|
69
|
-
|
|
167
|
+
source,
|
|
70
168
|
blur,
|
|
71
169
|
cache,
|
|
72
170
|
isUniformRadius,
|
|
@@ -78,5 +176,22 @@ export function usePipelineImageLoader(
|
|
|
78
176
|
bottomRight,
|
|
79
177
|
resizeWidth,
|
|
80
178
|
resizeHeight,
|
|
179
|
+
resizeFit,
|
|
180
|
+
resizeAllowUpscale,
|
|
181
|
+
fit,
|
|
182
|
+
allowUpscale,
|
|
183
|
+
hasOnLoad,
|
|
184
|
+
hasOnError,
|
|
185
|
+
// Stable ref objects; listed to satisfy the exhaustive-deps rules.
|
|
186
|
+
activeToken,
|
|
187
|
+
callbacks,
|
|
81
188
|
]);
|
|
189
|
+
|
|
190
|
+
// Same reasoning as the callbacks ref: a layout effect is current before the
|
|
191
|
+
// view — which the commit has only just handed this loader — can report.
|
|
192
|
+
useLayoutEffect(() => {
|
|
193
|
+
activeToken.current = memoized.token;
|
|
194
|
+
}, [memoized, activeToken]);
|
|
195
|
+
|
|
196
|
+
return memoized.loader;
|
|
82
197
|
}
|