Skip to content

Commit b29b0fe

Browse files
committed
RNWebGLTexture: use a AtomicBoolean for listenAttached
1 parent ba7ebdf commit b29b0fe

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

android/src/main/java/fr/greweb/rnwebgl/RNWebGLTexture.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.facebook.react.bridge.ReadableMap;
44

55
import java.util.ArrayList;
6+
import java.util.concurrent.atomic.AtomicBoolean;
67

78
import static fr.greweb.rnwebgl.RNWebGL.*;
89

@@ -11,7 +12,7 @@ public class RNWebGLTexture {
1112
public final int objId;
1213
public final int width;
1314
public final int height;
14-
private boolean attached = false;
15+
private AtomicBoolean attached = new AtomicBoolean(false);
1516
private ArrayList<Runnable> mAttachEventQueue = new ArrayList<>();
1617

1718

@@ -22,9 +23,9 @@ public RNWebGLTexture(ReadableMap config, int width, int height) {
2223
this.height = height;
2324
}
2425

25-
public void attachTexture (int texture) {
26+
public synchronized void attachTexture (int texture) {
2627
RNWebGLContextMapObject(ctxId, objId, texture);
27-
attached = true;
28+
attached.set(true);
2829
if (!mAttachEventQueue.isEmpty()) {
2930
for (Runnable r : new ArrayList<>(mAttachEventQueue)) {
3031
r.run();
@@ -33,12 +34,13 @@ public void attachTexture (int texture) {
3334
}
3435
}
3536

36-
public boolean isAttached () {
37-
return attached;
38-
}
39-
40-
public boolean listenAttached (Runnable r) {
41-
return mAttachEventQueue.add(r);
37+
public synchronized boolean listenAttached (Runnable r) {
38+
if(attached.get()){
39+
r.run();
40+
return true;
41+
} else {
42+
return mAttachEventQueue.add(r);
43+
}
4244
}
4345

4446
public void destroy() {

android/src/main/java/fr/greweb/rnwebgl/RNWebGLTextureLoader.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,11 @@ public void loadWithConfigAndWaitAttached (final ReadableMap config, final RNWeb
6464
loadWithConfig(config, new RNWebGLTextureCompletionBlock() {
6565
@Override
6666
public void call(final Exception e, final RNWebGLTexture obj) {
67-
if (obj.isAttached()) {
68-
callback.call(e, obj);
69-
}
70-
else {
71-
obj.listenAttached(new Runnable() {
72-
public void run() {
73-
callback.call(e, obj);
74-
}
75-
});
76-
}
67+
obj.listenAttached(new Runnable() {
68+
public void run() {
69+
callback.call(e, obj);
70+
}
71+
});
7772
}
7873
});
7974
}

0 commit comments

Comments
 (0)