Observing "MemoryWarningNotification" and proxying it up to the DeviceEventEmitter
This commit is contained in:
parent
56d6ee3f0f
commit
aad54006e3
|
@ -28,13 +28,19 @@ var AppStateSubscription = React.createClass({
|
||||||
return {
|
return {
|
||||||
appState: AppStateIOS.currentState,
|
appState: AppStateIOS.currentState,
|
||||||
previousAppStates: [],
|
previousAppStates: [],
|
||||||
|
memoryWarnings: 0,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
componentDidMount: function() {
|
componentDidMount: function() {
|
||||||
AppStateIOS.addEventListener('change', this._handleAppStateChange);
|
AppStateIOS.addEventListener('change', this._handleAppStateChange);
|
||||||
|
AppStateIOS.addEventListener('memoryWarning', this._handleMemoryWarning);
|
||||||
},
|
},
|
||||||
componentWillUnmount: function() {
|
componentWillUnmount: function() {
|
||||||
AppStateIOS.removeEventListener('change', this._handleAppStateChange);
|
AppStateIOS.removeEventListener('change', this._handleAppStateChange);
|
||||||
|
AppStateIOS.removeEventListener('memoryWarning', this._handleMemoryWarning);
|
||||||
|
},
|
||||||
|
_handleMemoryWarning: function() {
|
||||||
|
this.setState({memoryWarnings: this.state.memoryWarnings + 1})
|
||||||
},
|
},
|
||||||
_handleAppStateChange: function(appState) {
|
_handleAppStateChange: function(appState) {
|
||||||
var previousAppStates = this.state.previousAppStates.slice();
|
var previousAppStates = this.state.previousAppStates.slice();
|
||||||
|
@ -45,6 +51,13 @@ var AppStateSubscription = React.createClass({
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
render() {
|
render() {
|
||||||
|
if (this.props.showMemoryWarnings) {
|
||||||
|
return (
|
||||||
|
<View>
|
||||||
|
<Text>{this.state.memoryWarnings}</Text>
|
||||||
|
</View>
|
||||||
|
);
|
||||||
|
}
|
||||||
if (this.props.showCurrentOnly) {
|
if (this.props.showCurrentOnly) {
|
||||||
return (
|
return (
|
||||||
<View>
|
<View>
|
||||||
|
@ -77,4 +90,9 @@ exports.examples = [
|
||||||
title: 'Previous states:',
|
title: 'Previous states:',
|
||||||
render(): ReactElement { return <AppStateSubscription showCurrentOnly={false} />; }
|
render(): ReactElement { return <AppStateSubscription showCurrentOnly={false} />; }
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
title: 'Memory Warnings',
|
||||||
|
description: "In the simulator, hit Shift+Command+M to simulate a memory warning.",
|
||||||
|
render(): ReactElement { return <AppStateSubscription showMemoryWarnings={true} />; }
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
|
@ -16,10 +16,12 @@ var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
|
||||||
var RCTAppState = NativeModules.AppState;
|
var RCTAppState = NativeModules.AppState;
|
||||||
|
|
||||||
var logError = require('logError');
|
var logError = require('logError');
|
||||||
|
var invariant = require('invariant');
|
||||||
|
|
||||||
var DEVICE_APPSTATE_EVENT = 'appStateDidChange';
|
var _eventHandlers = {
|
||||||
|
change: new Map(),
|
||||||
var _appStateHandlers = {};
|
memoryWarning: new Map(),
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* `AppStateIOS` can tell you if the app is in the foreground or background,
|
* `AppStateIOS` can tell you if the app is in the foreground or background,
|
||||||
|
@ -82,12 +84,23 @@ var AppStateIOS = {
|
||||||
type: string,
|
type: string,
|
||||||
handler: Function
|
handler: Function
|
||||||
) {
|
) {
|
||||||
_appStateHandlers[handler] = RCTDeviceEventEmitter.addListener(
|
invariant(
|
||||||
DEVICE_APPSTATE_EVENT,
|
['change', 'memoryWarning'].indexOf(type) !== -1,
|
||||||
(appStateData) => {
|
'Trying to subscribe to unknown event: "%s"', type
|
||||||
handler(appStateData.app_state);
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
if (type === 'change') {
|
||||||
|
_eventHandlers[type].set(handler, RCTDeviceEventEmitter.addListener(
|
||||||
|
'appStateDidChange',
|
||||||
|
(appStateData) => {
|
||||||
|
handler(appStateData.app_state);
|
||||||
|
}
|
||||||
|
));
|
||||||
|
} else if (type === 'memoryWarning') {
|
||||||
|
_eventHandlers[type].set(handler, RCTDeviceEventEmitter.addListener(
|
||||||
|
'memoryWarning',
|
||||||
|
handler
|
||||||
|
));
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -97,11 +110,15 @@ var AppStateIOS = {
|
||||||
type: string,
|
type: string,
|
||||||
handler: Function
|
handler: Function
|
||||||
) {
|
) {
|
||||||
if (!_appStateHandlers[handler]) {
|
invariant(
|
||||||
|
['change', 'memoryWarning'].indexOf(type) !== -1,
|
||||||
|
'Trying to remove listener for unknown event: "%s"', type
|
||||||
|
);
|
||||||
|
if (!_eventHandlers[type].has(handler)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_appStateHandlers[handler].remove();
|
_eventHandlers[type].get(handler).remove();
|
||||||
_appStateHandlers[handler] = null;
|
_eventHandlers[type].delete(handler);
|
||||||
},
|
},
|
||||||
|
|
||||||
currentState: (null : ?String),
|
currentState: (null : ?String),
|
||||||
|
@ -109,7 +126,7 @@ var AppStateIOS = {
|
||||||
};
|
};
|
||||||
|
|
||||||
RCTDeviceEventEmitter.addListener(
|
RCTDeviceEventEmitter.addListener(
|
||||||
DEVICE_APPSTATE_EVENT,
|
'appStateDidChange',
|
||||||
(appStateData) => {
|
(appStateData) => {
|
||||||
AppStateIOS.currentState = appStateData.app_state;
|
AppStateIOS.currentState = appStateData.app_state;
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,11 +53,21 @@ RCT_EXPORT_MODULE()
|
||||||
selector:@selector(handleAppStateDidChange)
|
selector:@selector(handleAppStateDidChange)
|
||||||
name:name
|
name:name
|
||||||
object:nil];
|
object:nil];
|
||||||
|
|
||||||
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||||
|
selector:@selector(handleMemoryWarning)
|
||||||
|
name:UIApplicationDidReceiveMemoryWarningNotification
|
||||||
|
object:nil];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)handleMemoryWarning
|
||||||
|
{
|
||||||
|
[_bridge.eventDispatcher sendDeviceEventWithName:@"memoryWarning"
|
||||||
|
body:nil];
|
||||||
|
}
|
||||||
|
|
||||||
- (void)dealloc
|
- (void)dealloc
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue