[ReactNative] introduce mountSafeCallback

Summary:
`mountSafeCallback` simply wraps a callback in an `isMounted()` check to prevent crashes when old callbacks are called on unmounted components.

@public

Test Plan:
Added logging and made sure callbacks were getting called through
`mountSafeCallback` and that things worked (e.g. photo viewer rotation etc).
This commit is contained in:
Spencer Ahrens
2015-05-13 18:33:43 -07:00
parent a6b29a0b1a
commit 6e179fb7cd
2 changed files with 35 additions and 4 deletions

View File

@@ -20,6 +20,7 @@ var findNodeHandle = require('findNodeHandle');
var flattenStyle = require('flattenStyle');
var invariant = require('invariant');
var mergeFast = require('mergeFast');
var mountSafeCallback = require('mountSafeCallback');
var precomputeStyle = require('precomputeStyle');
type MeasureOnSuccessCallback = (
@@ -52,7 +53,11 @@ var animationIDInvariant = function(
var NativeMethodsMixin = {
addAnimation: function(anim: number, callback?: (finished: bool) => void) {
animationIDInvariant('addAnimation', anim);
RCTPOPAnimationManager.addAnimation(findNodeHandle(this), anim, callback);
RCTPOPAnimationManager.addAnimation(
findNodeHandle(this),
anim,
mountSafeCallback(this, callback)
);
},
removeAnimation: function(anim: number) {
@@ -61,7 +66,10 @@ var NativeMethodsMixin = {
},
measure: function(callback: MeasureOnSuccessCallback) {
RCTUIManager.measure(findNodeHandle(this), callback);
RCTUIManager.measure(
findNodeHandle(this),
mountSafeCallback(this, callback)
);
},
measureLayout: function(
@@ -72,8 +80,8 @@ var NativeMethodsMixin = {
RCTUIManager.measureLayout(
findNodeHandle(this),
relativeToNativeNode,
onFail,
onSuccess
mountSafeCallback(this, onFail),
mountSafeCallback(this, onSuccess)
);
},