Skip to content

Commit b28ff04

Browse files
kmagierafacebook-github-bot-3
authored andcommitted
Allow events recycling and implement recyclable OnLayoutEvents.
Differential Revision: D2585530 fb-gh-sync-id: cb671e39fd64c27a9c11e3cd064bd19cabe7f3b6
1 parent 20073fc commit b28ff04

File tree

5 files changed

+57
-8
lines changed

5 files changed

+57
-8
lines changed

ReactAndroid/src/main/java/com/facebook/react/uimanager/OnLayoutEvent.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
package com.facebook.react.uimanager;
1111

12+
import android.os.SystemClock;
13+
import android.support.v4.util.Pools;
14+
1215
import com.facebook.react.bridge.Arguments;
1316
import com.facebook.react.bridge.WritableMap;
1417
import com.facebook.react.uimanager.events.Event;
@@ -19,10 +22,30 @@
1922
*/
2023
/* package */ class OnLayoutEvent extends Event<OnLayoutEvent> {
2124

22-
private final int mX, mY, mWidth, mHeight;
25+
private static final Pools.SynchronizedPool<OnLayoutEvent> EVENTS_POOL =
26+
new Pools.SynchronizedPool<>(20);
27+
28+
private int mX, mY, mWidth, mHeight;
29+
30+
public static OnLayoutEvent obtain(int viewTag, int x, int y, int width, int height) {
31+
OnLayoutEvent event = EVENTS_POOL.acquire();
32+
if (event == null) {
33+
event = new OnLayoutEvent();
34+
}
35+
event.init(viewTag, x, y, width, height);
36+
return event;
37+
}
38+
39+
@Override
40+
public void onDispose() {
41+
EVENTS_POOL.release(this);
42+
}
43+
44+
private OnLayoutEvent() {
45+
}
2346

24-
protected OnLayoutEvent(int viewTag, int x, int y, int width, int height) {
25-
super(viewTag, 0);
47+
protected void init(int viewTag, int x, int y, int width, int height) {
48+
super.init(viewTag, SystemClock.uptimeMillis());
2649
mX = x;
2750
mY = y;
2851
mWidth = width;

ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ private void applyUpdatesRecursive(ReactShadowNode cssNode, float absoluteX, flo
806806
// notify JS about layout event if requested
807807
if (cssNode.shouldNotifyOnLayout()) {
808808
mEventDispatcher.dispatchEvent(
809-
new OnLayoutEvent(
809+
OnLayoutEvent.obtain(
810810
tag,
811811
cssNode.getScreenX(),
812812
cssNode.getScreenY(),

ReactAndroid/src/main/java/com/facebook/react/uimanager/events/Event.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,31 @@
1111

1212
/**
1313
* A UI event that can be dispatched to JS.
14+
*
15+
* For dispatching events {@link EventDispatcher#dispatchEvent} should be used. Once event object
16+
* is passed to the EventDispatched it should no longer be used as EventDispatcher may decide
17+
* to recycle that object (by calling {@link #dispose}).
1418
*/
1519
public abstract class Event<T extends Event> {
1620

17-
private final int mViewTag;
18-
private final long mTimestampMs;
21+
private boolean mInitialized;
22+
private int mViewTag;
23+
private long mTimestampMs;
24+
25+
protected Event() {
26+
}
1927

2028
protected Event(int viewTag, long timestampMs) {
29+
init(viewTag, timestampMs);
30+
}
31+
32+
/**
33+
* This method needs to be called before event is sent to event dispatcher.
34+
*/
35+
protected void init(int viewTag, long timestampMs) {
2136
mViewTag = viewTag;
2237
mTimestampMs = timestampMs;
38+
mInitialized = true;
2339
}
2440

2541
/**
@@ -68,7 +84,16 @@ public short getCoalescingKey() {
6884
* Called when the EventDispatcher is done with an event, either because it was dispatched or
6985
* because it was coalesced with another Event.
7086
*/
71-
public void dispose() {
87+
public void onDispose() {
88+
}
89+
90+
/*package*/ boolean isInitialized() {
91+
return mInitialized;
92+
}
93+
94+
/*package*/ final void dispose() {
95+
mInitialized = false;
96+
onDispose();
7297
}
7398

7499
/**

ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcher.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public EventDispatcher(ReactApplicationContext reactContext) {
110110
* Sends the given Event to JS, coalescing eligible events if JS is backed up.
111111
*/
112112
public void dispatchEvent(Event event) {
113+
Assertions.assertCondition(event.isInitialized(), "Dispatched event hasn't been initialized");
113114
synchronized (mEventsStagingLock) {
114115
mEventStaging.add(event);
115116
}

ReactAndroid/src/main/java/com/facebook/react/uimanager/events/TouchEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void dispatch(RCTEventEmitter rctEventEmitter) {
9696
}
9797

9898
@Override
99-
public void dispose() {
99+
public void onDispose() {
100100
mMotionEvent.recycle();
101101
}
102102
}

0 commit comments

Comments
 (0)