react-native-fast-image 9.0.3 → 9.1.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 CHANGED
@@ -539,15 +539,15 @@ await FastImage.preload(photos.map((uri) => ({ uri, memoryCache: false })))
539
539
 
540
540
  ### `clearMemoryCache()`
541
541
 
542
- **Returns:** `Promise<void>`
542
+ **Returns:** `Promise<ClearCacheResult>`
543
543
 
544
- Removes every image from the memory cache, e.g. to free memory. They're decoded from the disk cache again when they're next shown.
544
+ Removes every image from the memory cache, e.g. to free memory. They're decoded from the disk cache again when they're next shown. Resolves with `{ ok: true }` once it's done, or `{ ok: false, error }` if it couldn't (Android: Glide failed to start). Never rejects. On the web, it does nothing (the browser manages its cache) and resolves with an error.
545
545
 
546
546
  ### `clearDiskCache()`
547
547
 
548
- **Returns:** `Promise<void>`
548
+ **Returns:** `Promise<ClearCacheResult>`
549
549
 
550
- Removes every image from the disk cache, including the HTTP cache of `cache: 'web'` images, e.g. when a user logs out. They're downloaded again when they're next shown. There's no way to remove a single image: to load one again after it changed, change its [`cacheKey`](#sourcecachekey).
550
+ Removes every image from the disk cache, including the HTTP cache of `cache: 'web'` images, e.g. when a user logs out. They're downloaded again when they're next shown. There's no way to remove a single image: to load one again after it changed, change its [`cacheKey`](#sourcecachekey). Resolves like `clearMemoryCache`.
551
551
 
552
552
  ### `getCachePath(source)`
553
553
 
@@ -668,6 +668,14 @@ How fresh an image must be: see [`source.cache`](#sourcecache).
668
668
 
669
669
  ---
670
670
 
671
+ ### `ClearCacheResult`
672
+
673
+ **Type:** `{ ok: true } | { ok: false; error: string }`
674
+
675
+ `clearMemoryCache`'s and `clearDiskCache`'s result: `ok` once the cache is cleared, or not `ok` with the error.
676
+
677
+ ---
678
+
671
679
  ### `LoadResult`
672
680
 
673
681
  **Type:** `{ ok: true; width: number; height: number } | { ok: false; error: string }`
@@ -37,6 +37,13 @@ final class FastImageGlide {
37
37
  registered = glide;
38
38
  }
39
39
 
40
+ // Whether Glide has started with FastImage's AppGlideModule or has
41
+ // FastImage's components: until then, none of FastImage's images are in
42
+ // its memory cache.
43
+ static boolean used() {
44
+ return appModule || registered != null;
45
+ }
46
+
40
47
  // Glide, with FastImage's components. Glide registers them if the app's
41
48
  // generated Glide module lists FastImage's module, as FastImage's
42
49
  // AppGlideModule's does. Another library's (e.g. expo-image's, which ships
@@ -1,6 +1,5 @@
1
1
  package com.dylanvann.fastimage;
2
2
 
3
- import android.app.Activity;
4
3
  import android.content.Context;
5
4
  import android.graphics.BitmapFactory;
6
5
  import android.graphics.drawable.Drawable;
@@ -566,32 +565,38 @@ class FastImageViewModule extends ReactContextBaseJavaModule {
566
565
  });
567
566
  }
568
567
 
568
+ // The cache functions use the app's context, not the current Activity's:
569
+ // there may be no Activity while JS runs (e.g. after Android destroyed it
570
+ // in the background). Glide clears its memory cache on the main thread.
569
571
  @ReactMethod
570
572
  public void clearMemoryCache(final Promise promise) {
571
- final Activity activity = getCurrentActivity();
572
- if (activity == null) {
573
- promise.resolve(null);
574
- return;
575
- }
576
-
577
- activity.runOnUiThread(new Runnable() {
573
+ final ReactApplicationContext context = getReactApplicationContext();
574
+ UiThreadUtil.runOnUiThread(new Runnable() {
578
575
  @Override
579
576
  public void run() {
580
- FastImageGlide.get(activity.getApplicationContext()).clearMemory();
581
- promise.resolve(null);
577
+ try {
578
+ // Not before FastImage has used Glide: there's nothing of
579
+ // its in memory, and starting Glide here would set its
580
+ // disk cache size before a configureCache could.
581
+ if (FastImageGlide.used()) Glide.get(context).clearMemory();
582
+ promise.resolve(cleared());
583
+ } catch (RuntimeException e) {
584
+ promise.resolve(failure(e));
585
+ }
582
586
  }
583
587
  });
584
588
  }
585
589
 
590
+ // On the native modules' thread: Glide clears its disk cache off the main
591
+ // thread.
586
592
  @ReactMethod
587
593
  public void clearDiskCache(Promise promise) {
588
- final Activity activity = getCurrentActivity();
589
- if (activity == null) {
590
- promise.resolve(null);
594
+ try {
595
+ FastImageGlide.getRegistered(getReactApplicationContext()).clearDiskCache();
596
+ } catch (RuntimeException e) {
597
+ promise.resolve(failure(e));
591
598
  return;
592
599
  }
593
-
594
- FastImageGlide.getRegistered(activity.getApplicationContext()).clearDiskCache();
595
600
  // And the HTTP cache of `cache: 'web'` images, which Glide doesn't
596
601
  // cache (set up when FastImage's components are registered).
597
602
  try {
@@ -599,6 +604,20 @@ class FastImageViewModule extends ReactContextBaseJavaModule {
599
604
  } catch (IOException e) {
600
605
  // Cleared as far as it could.
601
606
  }
602
- promise.resolve(null);
607
+ promise.resolve(cleared());
608
+ }
609
+
610
+ private static WritableMap cleared() {
611
+ WritableMap result = Arguments.createMap();
612
+ result.putBoolean("ok", true);
613
+ return result;
614
+ }
615
+
616
+ // Glide failing to start, e.g. when an app's Glide module calls
617
+ // Glide.get() from registerComponents (#962): resolved as an error, not
618
+ // thrown, which would crash the app (on the main thread) or leave the
619
+ // promise unsettled.
620
+ private static WritableMap failure(RuntimeException e) {
621
+ return failure(e.getMessage() != null ? e.getMessage() : e.toString());
603
622
  }
604
623
  }
package/dist/index.cjs.js CHANGED
@@ -245,8 +245,8 @@ FastImage.preload = (sources) => Promise.resolve(react_native.NativeModules.Fast
245
245
  uri
246
246
  };
247
247
  }));
248
- FastImage.clearMemoryCache = () => react_native.NativeModules.FastImageView.clearMemoryCache();
249
- FastImage.clearDiskCache = () => react_native.NativeModules.FastImageView.clearDiskCache();
248
+ FastImage.clearMemoryCache = () => Promise.resolve(react_native.NativeModules.FastImageView.clearMemoryCache());
249
+ FastImage.clearDiskCache = () => Promise.resolve(react_native.NativeModules.FastImageView.clearDiskCache());
250
250
  FastImage.configureCache = (limits = {}) => Promise.resolve(react_native.NativeModules.FastImageView.configureCache(limits));
251
251
  FastImage.writeToCache = (source, file) => Array.isArray(source) ? Promise.resolve({
252
252
  ok: false,
@@ -73,6 +73,8 @@ export type CachePathResult =
73
73
  | {| ok: true, path: string |}
74
74
  | {| ok: false, error: string |}
75
75
 
76
+ export type ClearCacheResult = {| ok: true |} | {| ok: false, error: string |}
77
+
76
78
  export type Transition = $ReadOnly<{|
77
79
  duration?: ?number,
78
80
  betweenImages?: ?boolean,
@@ -147,8 +149,8 @@ declare export default class FastImage extends React$Component<FastImageProps> {
147
149
  static priority: Priority;
148
150
  static cacheControl: CacheControl;
149
151
  static preload: PreloadFn;
150
- static clearMemoryCache: () => Promise<void>;
151
- static clearDiskCache: () => Promise<void>;
152
+ static clearMemoryCache: () => Promise<ClearCacheResult>;
153
+ static clearDiskCache: () => Promise<ClearCacheResult>;
152
154
  static getCachePath: (source: FastImageSource) => Promise<CachePathResult>;
153
155
  static configureCache: (limits?: CacheLimits) => Promise<CacheState>;
154
156
  static writeToCache: (
@@ -239,8 +239,8 @@ FastImage.preload = (sources) => Promise.all(sources.map((source) => {
239
239
  uri
240
240
  }));
241
241
  }));
242
- FastImage.clearMemoryCache = () => Promise.resolve();
243
- FastImage.clearDiskCache = () => Promise.resolve();
242
+ FastImage.clearMemoryCache = () => Promise.resolve(notSupported);
243
+ FastImage.clearDiskCache = () => Promise.resolve(notSupported);
244
244
  FastImage.configureCache = () => Promise.resolve({});
245
245
  FastImage.writeToCache = () => Promise.resolve(notSupported);
246
246
  FastImage.getCachePath = () => Promise.resolve(notSupported);
@@ -73,6 +73,8 @@ export type CachePathResult =
73
73
  | {| ok: true, path: string |}
74
74
  | {| ok: false, error: string |}
75
75
 
76
+ export type ClearCacheResult = {| ok: true |} | {| ok: false, error: string |}
77
+
76
78
  export type Transition = $ReadOnly<{|
77
79
  duration?: ?number,
78
80
  betweenImages?: ?boolean,
@@ -147,8 +149,8 @@ declare export default class FastImage extends React$Component<FastImageProps> {
147
149
  static priority: Priority;
148
150
  static cacheControl: CacheControl;
149
151
  static preload: PreloadFn;
150
- static clearMemoryCache: () => Promise<void>;
151
- static clearDiskCache: () => Promise<void>;
152
+ static clearMemoryCache: () => Promise<ClearCacheResult>;
153
+ static clearDiskCache: () => Promise<ClearCacheResult>;
152
154
  static getCachePath: (source: FastImageSource) => Promise<CachePathResult>;
153
155
  static configureCache: (limits?: CacheLimits) => Promise<CacheState>;
154
156
  static writeToCache: (
package/dist/index.d.ts CHANGED
@@ -208,6 +208,16 @@ export type CachePathResult = {
208
208
  ok: false;
209
209
  error: string;
210
210
  };
211
+ /**
212
+ * `clearMemoryCache`'s and `clearDiskCache`'s result: `ok` once the cache is
213
+ * cleared, or not `ok` with the error.
214
+ */
215
+ export type ClearCacheResult = {
216
+ ok: true;
217
+ } | {
218
+ ok: false;
219
+ error: string;
220
+ };
211
221
  /**
212
222
  * `onProgress`'s event: `nativeEvent` has the bytes `loaded` and the `total`,
213
223
  * and `progress`, `loaded / total` from 0 to 1.
@@ -529,8 +539,18 @@ export interface FastImageStaticProperties {
529
539
  priority: typeof priority;
530
540
  cacheControl: typeof cacheControl;
531
541
  preload: (sources: Source[]) => Promise<PreloadResult[]>;
532
- clearMemoryCache: () => Promise<void>;
533
- clearDiskCache: () => Promise<void>;
542
+ /**
543
+ * Removes every image from the memory cache. Resolves once it's done, or
544
+ * not `ok` with the error if it couldn't (Android: Glide failed to
545
+ * start). Never rejects.
546
+ */
547
+ clearMemoryCache: () => Promise<ClearCacheResult>;
548
+ /**
549
+ * Removes every image from the disk cache, and the HTTP cache of
550
+ * `cache: 'web'` images. Resolves once it's done, or not `ok` with the
551
+ * error if it couldn't (Android: Glide failed to start). Never rejects.
552
+ */
553
+ clearDiskCache: () => Promise<ClearCacheResult>;
534
554
  /**
535
555
  * The path of the source's downloaded file in the disk cache, downloading
536
556
  * it first if it isn't there (without decoding it). With
@@ -550,10 +570,14 @@ export interface FastImageStaticProperties {
550
570
  /**
551
571
  * Changes the cache's limits at runtime (only those given; 0 for no
552
572
  * limit, null to go back to the app's native config) and saves them, then
553
- * resolves with the limits in effect and the disk cache's size. Without
554
- * limits, only resolves. iOS applies changes at once. Android only has
555
- * `maxDiskSize`, applied when Glide starts: at once if no image has
556
- * loaded yet, otherwise from the next launch.
573
+ * resolves with the limits in effect and the disk cache's size; without
574
+ * limits, it only resolves with them. iOS applies changes at once.
575
+ * Android only has `maxDiskSize`, applied when Glide starts: at once if
576
+ * it hasn't started yet in this launch, otherwise from the next launch.
577
+ * FastImage starts it when its first view is created (before the view has
578
+ * a source), and for `preload`, `getCachePath`, `writeToCache`,
579
+ * `clearDiskCache` and `configureCache` itself, even without limits (e.g.
580
+ * to read the disk cache's size).
557
581
  */
558
582
  configureCache: (limits?: CacheLimits) => Promise<CacheState>;
559
583
  }
package/dist/index.js CHANGED
@@ -218,8 +218,8 @@ FastImage.preload = (sources) => Promise.resolve(NativeModules.FastImageView.pre
218
218
  uri
219
219
  };
220
220
  }));
221
- FastImage.clearMemoryCache = () => NativeModules.FastImageView.clearMemoryCache();
222
- FastImage.clearDiskCache = () => NativeModules.FastImageView.clearDiskCache();
221
+ FastImage.clearMemoryCache = () => Promise.resolve(NativeModules.FastImageView.clearMemoryCache());
222
+ FastImage.clearDiskCache = () => Promise.resolve(NativeModules.FastImageView.clearDiskCache());
223
223
  FastImage.configureCache = (limits = {}) => Promise.resolve(NativeModules.FastImageView.configureCache(limits));
224
224
  FastImage.writeToCache = (source, file) => Array.isArray(source) ? Promise.resolve({
225
225
  ok: false,
@@ -73,6 +73,8 @@ export type CachePathResult =
73
73
  | {| ok: true, path: string |}
74
74
  | {| ok: false, error: string |}
75
75
 
76
+ export type ClearCacheResult = {| ok: true |} | {| ok: false, error: string |}
77
+
76
78
  export type Transition = $ReadOnly<{|
77
79
  duration?: ?number,
78
80
  betweenImages?: ?boolean,
@@ -147,8 +149,8 @@ declare export default class FastImage extends React$Component<FastImageProps> {
147
149
  static priority: Priority;
148
150
  static cacheControl: CacheControl;
149
151
  static preload: PreloadFn;
150
- static clearMemoryCache: () => Promise<void>;
151
- static clearDiskCache: () => Promise<void>;
152
+ static clearMemoryCache: () => Promise<ClearCacheResult>;
153
+ static clearDiskCache: () => Promise<ClearCacheResult>;
152
154
  static getCachePath: (source: FastImageSource) => Promise<CachePathResult>;
153
155
  static configureCache: (limits?: CacheLimits) => Promise<CacheState>;
154
156
  static writeToCache: (
package/dist/index.web.js CHANGED
@@ -212,8 +212,8 @@ FastImage.preload = (sources) => Promise.all(sources.map((source) => {
212
212
  uri
213
213
  }));
214
214
  }));
215
- FastImage.clearMemoryCache = () => Promise.resolve();
216
- FastImage.clearDiskCache = () => Promise.resolve();
215
+ FastImage.clearMemoryCache = () => Promise.resolve(notSupported);
216
+ FastImage.clearDiskCache = () => Promise.resolve(notSupported);
217
217
  FastImage.configureCache = () => Promise.resolve({});
218
218
  FastImage.writeToCache = () => Promise.resolve(notSupported);
219
219
  FastImage.getCachePath = () => Promise.resolve(notSupported);
@@ -73,6 +73,8 @@ export type CachePathResult =
73
73
  | {| ok: true, path: string |}
74
74
  | {| ok: false, error: string |}
75
75
 
76
+ export type ClearCacheResult = {| ok: true |} | {| ok: false, error: string |}
77
+
76
78
  export type Transition = $ReadOnly<{|
77
79
  duration?: ?number,
78
80
  betweenImages?: ?boolean,
@@ -147,8 +149,8 @@ declare export default class FastImage extends React$Component<FastImageProps> {
147
149
  static priority: Priority;
148
150
  static cacheControl: CacheControl;
149
151
  static preload: PreloadFn;
150
- static clearMemoryCache: () => Promise<void>;
151
- static clearDiskCache: () => Promise<void>;
152
+ static clearMemoryCache: () => Promise<ClearCacheResult>;
153
+ static clearDiskCache: () => Promise<ClearCacheResult>;
152
154
  static getCachePath: (source: FastImageSource) => Promise<CachePathResult>;
153
155
  static configureCache: (limits?: CacheLimits) => Promise<CacheState>;
154
156
  static writeToCache: (
@@ -467,7 +467,7 @@ RCT_EXPORT_METHOD(configureCache:(NSDictionary *)limits
467
467
  RCT_EXPORT_METHOD(clearMemoryCache:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
468
468
  {
469
469
  [SDImageCache.sharedImageCache clearMemory];
470
- resolve(NULL);
470
+ resolve(@{@"ok": @YES});
471
471
  }
472
472
 
473
473
  RCT_EXPORT_METHOD(clearDiskCache:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
@@ -475,7 +475,7 @@ RCT_EXPORT_METHOD(clearDiskCache:(RCTPromiseResolveBlock)resolve reject:(RCTProm
475
475
  // And the HTTP cache of `cache: 'web'` images.
476
476
  [FFFastImageSource.webURLCache removeAllCachedResponses];
477
477
  [SDImageCache.sharedImageCache clearDiskOnCompletion:^(){
478
- resolve(NULL);
478
+ resolve(@{@"ok": @YES});
479
479
  }];
480
480
  }
481
481
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-fast-image",
3
- "version": "9.0.3",
3
+ "version": "9.1.0",
4
4
  "description": "FastImage, performant React Native image component.",
5
5
  "keywords": [
6
6
  "cache",