The library for web and native user interfaces.
Replace `persistentCloneOf` with `persist`
There are to reasons to prefer a `persist` method on the event rather than a static method: - In open source, people do not have access to `AbstractEvent`. - This will allow people to persist events without requiring another module. - This will make refactors easier and more flexible.
CommitSyncScript committed
Jun 7, 2013 at 22:04 UTC
0e9e64c550eb867d720c8a8ec05c5cecea913f93
2 files changed
+17
-34
src/event/AbstractEvent.js
+14
-33
@@ -22,16 +22,8 @@ var BrowserEnv = require('BrowserEnv');
22
var PooledClass = require('PooledClass');
23
var TouchEventUtils = require('TouchEventUtils');
24
25
-var throwIf = require('throwIf');
25
+var emptyFunction = require('emptyFunction');
26
27
-
28
-// Only accessed in __DEV__
29
-var CLONE_TYPE_ERR;
30
-if (__DEV__) {
31
- CLONE_TYPE_ERR =
32
- 'You may only clone instances of AbstractEvent for ' +
33
- 'persistent references. Check yourself.';
34
-}
27
var MAX_POOL_SIZE = 20;
28
29
/**
@@ -68,6 +60,7 @@ function AbstractEvent(
60
this._dispatchIDs = null;
61
62
this.isPropagationStopped = false;
63
+ this.isPersistent = emptyFunction.thatReturnsFalse;
64
}
65
66
/** `PooledClass` looks for this. */
@@ -104,6 +97,15 @@ AbstractEvent.prototype.preventDefault = function() {
97
AbstractEvent.preventDefaultOnNativeEvent(this.nativeEvent);
98
};
99
100
+/**
101
+ * We clear out all dispatched `AbstractEvent`s after each event loop, adding
102
+ * them back into the pool. This allows a way to hold onto a reference that
103
+ * won't be added back into the pool.
104
+ */
105
+AbstractEvent.prototype.persist = function() {
106
+ this.isPersistent = emptyFunction.thatReturnsTrue;
107
+};
108
+
109
/**
110
* Utility function for preventing default in cross browser manner.
111
*/
@@ -240,32 +242,11 @@ AbstractEvent.eventPageX = function(nativeEvent) {
242
};
243
244
/**
243
- * A semantic API around cloning an event for use in another event loop. We
244
- * clear out all dispatched `AbstractEvent`s after each event loop, adding them
245
- * back into the pool. This allows a way to hold onto a reference that won't be
246
- * added back into the pool. Please note that `AbstractEvent.nativeEvent` is
247
- * *not* cloned and you will run into problems in IE if you assume that it will
248
- * be! The moral of that story is to always normalize any data you need into the
249
- * `.data` field. The data field is not cloned either, but there won't be any
250
- * issues related to use of `.data` in a future event cycle so long as no part
251
- * of your application mutates it. We don't clone the private fields because
252
- * your application should never be accessing them.
253
- *
254
- * - TODO: In __DEV__ when "releasing" events, don't put them back into the
255
- * pool. Instead add ES5 getters on all their fields that throw errors so you
256
- * can detect any application that's hanging onto events and reusing them.
257
- * In prod - we can put them back into the pool for reuse.
245
+ * @deprecated
246
*/
247
AbstractEvent.persistentCloneOf = function(abstractEvent) {
260
- if (__DEV__) {
261
- throwIf(!(abstractEvent instanceof AbstractEvent), CLONE_TYPE_ERR);
262
- }
263
- return new AbstractEvent(
264
- abstractEvent.reactEventType,
265
- abstractEvent.reactTargetID,
266
- abstractEvent.nativeEvent,
267
- abstractEvent.data
268
- );
248
+ abstractEvent.persist();
249
+ return abstractEvent;
250
};
251
252
module.exports = AbstractEvent;
src/event/EventPluginHub.js
+3
-1
@@ -277,7 +277,9 @@ var executeDispatchesAndRelease = function(abstractEvent) {
277
abstractEvent,
278
pluginExecuteDispatch || EventPluginUtils.executeDispatch
279
);
280
- AbstractEvent.release(abstractEvent);
280
+ if (!abstractEvent.isPersistent()) {
281
+ AbstractEvent.release(abstractEvent);
282
+ }
283
}
284
};
285