Allow events recycling and implement recyclable OnLayoutEvents.
Differential Revision: D2585530 fb-gh-sync-id: cb671e39fd64c27a9c11e3cd064bd19cabe7f3b6
This commit is contained in:
parent
20073fcab2
commit
b28ff0451d
|
@ -9,6 +9,9 @@
|
||||||
|
|
||||||
package com.facebook.react.uimanager;
|
package com.facebook.react.uimanager;
|
||||||
|
|
||||||
|
import android.os.SystemClock;
|
||||||
|
import android.support.v4.util.Pools;
|
||||||
|
|
||||||
import com.facebook.react.bridge.Arguments;
|
import com.facebook.react.bridge.Arguments;
|
||||||
import com.facebook.react.bridge.WritableMap;
|
import com.facebook.react.bridge.WritableMap;
|
||||||
import com.facebook.react.uimanager.events.Event;
|
import com.facebook.react.uimanager.events.Event;
|
||||||
|
@ -19,10 +22,30 @@ import com.facebook.react.uimanager.events.RCTEventEmitter;
|
||||||
*/
|
*/
|
||||||
/* package */ class OnLayoutEvent extends Event<OnLayoutEvent> {
|
/* package */ class OnLayoutEvent extends Event<OnLayoutEvent> {
|
||||||
|
|
||||||
private final int mX, mY, mWidth, mHeight;
|
private static final Pools.SynchronizedPool<OnLayoutEvent> EVENTS_POOL =
|
||||||
|
new Pools.SynchronizedPool<>(20);
|
||||||
|
|
||||||
protected OnLayoutEvent(int viewTag, int x, int y, int width, int height) {
|
private int mX, mY, mWidth, mHeight;
|
||||||
super(viewTag, 0);
|
|
||||||
|
public static OnLayoutEvent obtain(int viewTag, int x, int y, int width, int height) {
|
||||||
|
OnLayoutEvent event = EVENTS_POOL.acquire();
|
||||||
|
if (event == null) {
|
||||||
|
event = new OnLayoutEvent();
|
||||||
|
}
|
||||||
|
event.init(viewTag, x, y, width, height);
|
||||||
|
return event;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDispose() {
|
||||||
|
EVENTS_POOL.release(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
private OnLayoutEvent() {
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void init(int viewTag, int x, int y, int width, int height) {
|
||||||
|
super.init(viewTag, SystemClock.uptimeMillis());
|
||||||
mX = x;
|
mX = x;
|
||||||
mY = y;
|
mY = y;
|
||||||
mWidth = width;
|
mWidth = width;
|
||||||
|
|
|
@ -806,7 +806,7 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements
|
||||||
// notify JS about layout event if requested
|
// notify JS about layout event if requested
|
||||||
if (cssNode.shouldNotifyOnLayout()) {
|
if (cssNode.shouldNotifyOnLayout()) {
|
||||||
mEventDispatcher.dispatchEvent(
|
mEventDispatcher.dispatchEvent(
|
||||||
new OnLayoutEvent(
|
OnLayoutEvent.obtain(
|
||||||
tag,
|
tag,
|
||||||
cssNode.getScreenX(),
|
cssNode.getScreenX(),
|
||||||
cssNode.getScreenY(),
|
cssNode.getScreenY(),
|
||||||
|
|
|
@ -11,15 +11,31 @@ package com.facebook.react.uimanager.events;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A UI event that can be dispatched to JS.
|
* A UI event that can be dispatched to JS.
|
||||||
|
*
|
||||||
|
* For dispatching events {@link EventDispatcher#dispatchEvent} should be used. Once event object
|
||||||
|
* is passed to the EventDispatched it should no longer be used as EventDispatcher may decide
|
||||||
|
* to recycle that object (by calling {@link #dispose}).
|
||||||
*/
|
*/
|
||||||
public abstract class Event<T extends Event> {
|
public abstract class Event<T extends Event> {
|
||||||
|
|
||||||
private final int mViewTag;
|
private boolean mInitialized;
|
||||||
private final long mTimestampMs;
|
private int mViewTag;
|
||||||
|
private long mTimestampMs;
|
||||||
|
|
||||||
|
protected Event() {
|
||||||
|
}
|
||||||
|
|
||||||
protected Event(int viewTag, long timestampMs) {
|
protected Event(int viewTag, long timestampMs) {
|
||||||
|
init(viewTag, timestampMs);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method needs to be called before event is sent to event dispatcher.
|
||||||
|
*/
|
||||||
|
protected void init(int viewTag, long timestampMs) {
|
||||||
mViewTag = viewTag;
|
mViewTag = viewTag;
|
||||||
mTimestampMs = timestampMs;
|
mTimestampMs = timestampMs;
|
||||||
|
mInitialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -68,7 +84,16 @@ public abstract class Event<T extends Event> {
|
||||||
* Called when the EventDispatcher is done with an event, either because it was dispatched or
|
* Called when the EventDispatcher is done with an event, either because it was dispatched or
|
||||||
* because it was coalesced with another Event.
|
* because it was coalesced with another Event.
|
||||||
*/
|
*/
|
||||||
public void dispose() {
|
public void onDispose() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/*package*/ boolean isInitialized() {
|
||||||
|
return mInitialized;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*package*/ final void dispose() {
|
||||||
|
mInitialized = false;
|
||||||
|
onDispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -110,6 +110,7 @@ public class EventDispatcher implements LifecycleEventListener {
|
||||||
* Sends the given Event to JS, coalescing eligible events if JS is backed up.
|
* Sends the given Event to JS, coalescing eligible events if JS is backed up.
|
||||||
*/
|
*/
|
||||||
public void dispatchEvent(Event event) {
|
public void dispatchEvent(Event event) {
|
||||||
|
Assertions.assertCondition(event.isInitialized(), "Dispatched event hasn't been initialized");
|
||||||
synchronized (mEventsStagingLock) {
|
synchronized (mEventsStagingLock) {
|
||||||
mEventStaging.add(event);
|
mEventStaging.add(event);
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,7 +96,7 @@ public class TouchEvent extends Event<TouchEvent> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void dispose() {
|
public void onDispose() {
|
||||||
mMotionEvent.recycle();
|
mMotionEvent.recycle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue