react-native-fast-image 10.8.1 → 10.8.3

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.
@@ -1,5 +1,8 @@
1
1
  package com.fastimage;
2
2
 
3
+ import android.os.Handler;
4
+ import android.os.Looper;
5
+
3
6
  import com.facebook.react.bridge.Arguments;
4
7
  import com.facebook.react.bridge.ReactContext;
5
8
  import com.facebook.react.bridge.WritableMap;
@@ -15,6 +18,8 @@ final class FastImageEvents {
15
18
  static final int ERROR = 1 << 3;
16
19
  static final int LOAD_END = 1 << 4;
17
20
 
21
+ private static final Handler MAIN = new Handler(Looper.getMainLooper());
22
+
18
23
  private FastImageEvents() {
19
24
  }
20
25
 
@@ -57,8 +62,21 @@ final class FastImageEvents {
57
62
  send(view, FastImageRequestListener.REACT_ON_LOAD_END_EVENT, event);
58
63
  }
59
64
 
60
- // Only the events JS has a handler for: most images have none.
65
+ // Only the events JS has a handler for: most images have none. Posted to
66
+ // the main thread rather than dispatched here: this is called from Glide's
67
+ // onResourceReady and onLoadFailed, and EventDispatcher calls its listeners
68
+ // inline. Reanimated's listener flushes pending mount items, which can
69
+ // drop this view, and the drop clears the request inside its own
70
+ // callback, which Glide doesn't allow (it throws). Every event goes through
71
+ // here, so they keep their order: e.g. a view's onProgress comes after its
72
+ // onLoadStart, even from progress the UI thread had waiting before the
73
+ // load started.
61
74
  static void send(FastImageViewWithUrl view, String name, WritableMap data) {
75
+ if (!view.handles(bit(name))) return;
76
+ MAIN.post(() -> dispatch(view, name, data));
77
+ }
78
+
79
+ private static void dispatch(FastImageViewWithUrl view, String name, WritableMap data) {
62
80
  if (!view.handles(bit(name))) return;
63
81
  ReactContext context = FastImageViewManager.getReactContext(view.getContext());
64
82
  if (context == null) return;
@@ -9,7 +9,6 @@ import android.content.Context;
9
9
  import android.content.ContextWrapper;
10
10
  import android.graphics.Bitmap;
11
11
  import android.graphics.PorterDuff;
12
- import android.util.Log;
13
12
  import android.graphics.Rect;
14
13
  import android.graphics.drawable.BitmapDrawable;
15
14
  import android.util.DisplayMetrics;
@@ -64,7 +63,6 @@ class FastImageViewManager extends SimpleViewManager<FastImageViewWithUrl>
64
63
  static final String REACT_CLASS = "FastImageView";
65
64
  static final String REACT_ON_LOAD_START_EVENT = "onFastImageLoadStart";
66
65
  static final String REACT_ON_PROGRESS_EVENT = "onFastImageProgress";
67
- static final String LOG_TAG = "FastImage";
68
66
  // Download progress, by the key downloads are shared by
69
67
  // (FastImageSharedDownloads.key): the views that get it, those with an onProgress from
70
68
  // their load's onLoadStart to its onLoad or onError, and the latest step a
@@ -82,7 +80,7 @@ class FastImageViewManager extends SimpleViewManager<FastImageViewWithUrl>
82
80
  private static final class Progress {
83
81
  final Set<FastImageViewWithUrl> views = new LinkedHashSet<>();
84
82
  // A step waits to be sent: later ones replace it meanwhile, so a url
85
- // has at most one post to the UI thread at a time.
83
+ // has at most one step waiting at a time.
86
84
  boolean posted;
87
85
  long loaded;
88
86
  long total;
@@ -504,10 +502,12 @@ class FastImageViewManager extends SimpleViewManager<FastImageViewWithUrl>
504
502
 
505
503
  // A download's progress (FastImageSharedDownloads), on its thread, which
506
504
  // only notes it if a view wants it (so downloads of images without an
507
- // onProgress post nothing). It's sent from the UI thread, where
508
- // views start and end their loads: a view only gets it during its own load,
509
- // not after its onLoad or once it loads again, and progress posted during
510
- // a download comes before the onLoad Glide posts after it.
505
+ // onProgress post nothing). The UI thread, where views start and end their
506
+ // loads, picks the views that take it, and posts it behind their other
507
+ // events (FastImageEvents.send): a view only gets it during its own load,
508
+ // after its onLoadStart, not after its onLoad or once it loads again, and
509
+ // progress posted during a download comes before the onLoad Glide posts
510
+ // after it.
511
511
  static void onDownloadProgress(final String key, long loaded, long total) {
512
512
  synchronized (PROGRESS) {
513
513
  Progress progress = PROGRESS.get(key);
@@ -554,14 +554,8 @@ class FastImageViewManager extends SimpleViewManager<FastImageViewWithUrl>
554
554
  event.putInt("total", (int) total);
555
555
  // Downloads only report a known length (see FastImageSharedDownloads).
556
556
  event.putDouble("progress", Math.min(1.0, Math.max(0.0, (double) loaded / total)));
557
- try {
558
- FastImageEvents.send(view, REACT_ON_PROGRESS_EVENT, event);
559
- } catch (RuntimeException e) {
560
- // E.g. the view's React instance is gone: progress isn't worth
561
- // crashing the app for.
562
- // (Not the key: it has the request's headers.)
563
- Log.w(LOG_TAG, "Couldn't send onProgress", e);
564
- }
557
+ // Posted after the view's onLoadStart, which may still be waiting.
558
+ FastImageEvents.send(view, REACT_ON_PROGRESS_EVENT, event);
565
559
  }
566
560
  }
567
561
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-fast-image",
3
- "version": "10.8.1",
3
+ "version": "10.8.3",
4
4
  "description": "FastImage, performant React Native image component.",
5
5
  "keywords": [
6
6
  "cache",