Skip to content

Commit aad5400

Browse files
author
Jared Forsyth
committed
Observing "MemoryWarningNotification" and proxying it up to the DeviceEventEmitter
1 parent 56d6ee3 commit aad5400

3 files changed

Lines changed: 57 additions & 12 deletions

File tree

Examples/UIExplorer/AppStateIOSExample.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,19 @@ var AppStateSubscription = React.createClass({
2828
return {
2929
appState: AppStateIOS.currentState,
3030
previousAppStates: [],
31+
memoryWarnings: 0,
3132
};
3233
},
3334
componentDidMount: function() {
3435
AppStateIOS.addEventListener('change', this._handleAppStateChange);
36+
AppStateIOS.addEventListener('memoryWarning', this._handleMemoryWarning);
3537
},
3638
componentWillUnmount: function() {
3739
AppStateIOS.removeEventListener('change', this._handleAppStateChange);
40+
AppStateIOS.removeEventListener('memoryWarning', this._handleMemoryWarning);
41+
},
42+
_handleMemoryWarning: function() {
43+
this.setState({memoryWarnings: this.state.memoryWarnings + 1})
3844
},
3945
_handleAppStateChange: function(appState) {
4046
var previousAppStates = this.state.previousAppStates.slice();
@@ -45,6 +51,13 @@ var AppStateSubscription = React.createClass({
4551
});
4652
},
4753
render() {
54+
if (this.props.showMemoryWarnings) {
55+
return (
56+
<View>
57+
<Text>{this.state.memoryWarnings}</Text>
58+
</View>
59+
);
60+
}
4861
if (this.props.showCurrentOnly) {
4962
return (
5063
<View>
@@ -77,4 +90,9 @@ exports.examples = [
7790
title: 'Previous states:',
7891
render(): ReactElement { return <AppStateSubscription showCurrentOnly={false} />; }
7992
},
93+
{
94+
title: 'Memory Warnings',
95+
description: "In the simulator, hit Shift+Command+M to simulate a memory warning.",
96+
render(): ReactElement { return <AppStateSubscription showMemoryWarnings={true} />; }
97+
},
8098
];

Libraries/AppStateIOS/AppStateIOS.ios.js

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
1616
var RCTAppState = NativeModules.AppState;
1717

1818
var logError = require('logError');
19+
var invariant = require('invariant');
1920

20-
var DEVICE_APPSTATE_EVENT = 'appStateDidChange';
21-
22-
var _appStateHandlers = {};
21+
var _eventHandlers = {
22+
change: new Map(),
23+
memoryWarning: new Map(),
24+
};
2325

2426
/**
2527
* `AppStateIOS` can tell you if the app is in the foreground or background,
@@ -82,12 +84,23 @@ var AppStateIOS = {
8284
type: string,
8385
handler: Function
8486
) {
85-
_appStateHandlers[handler] = RCTDeviceEventEmitter.addListener(
86-
DEVICE_APPSTATE_EVENT,
87-
(appStateData) => {
88-
handler(appStateData.app_state);
89-
}
87+
invariant(
88+
['change', 'memoryWarning'].indexOf(type) !== -1,
89+
'Trying to subscribe to unknown event: "%s"', type
9090
);
91+
if (type === 'change') {
92+
_eventHandlers[type].set(handler, RCTDeviceEventEmitter.addListener(
93+
'appStateDidChange',
94+
(appStateData) => {
95+
handler(appStateData.app_state);
96+
}
97+
));
98+
} else if (type === 'memoryWarning') {
99+
_eventHandlers[type].set(handler, RCTDeviceEventEmitter.addListener(
100+
'memoryWarning',
101+
handler
102+
));
103+
}
91104
},
92105

93106
/**
@@ -97,19 +110,23 @@ var AppStateIOS = {
97110
type: string,
98111
handler: Function
99112
) {
100-
if (!_appStateHandlers[handler]) {
113+
invariant(
114+
['change', 'memoryWarning'].indexOf(type) !== -1,
115+
'Trying to remove listener for unknown event: "%s"', type
116+
);
117+
if (!_eventHandlers[type].has(handler)) {
101118
return;
102119
}
103-
_appStateHandlers[handler].remove();
104-
_appStateHandlers[handler] = null;
120+
_eventHandlers[type].get(handler).remove();
121+
_eventHandlers[type].delete(handler);
105122
},
106123

107124
currentState: (null : ?String),
108125

109126
};
110127

111128
RCTDeviceEventEmitter.addListener(
112-
DEVICE_APPSTATE_EVENT,
129+
'appStateDidChange',
113130
(appStateData) => {
114131
AppStateIOS.currentState = appStateData.app_state;
115132
}

React/Modules/RCTAppState.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,21 @@ - (instancetype)init
5353
selector:@selector(handleAppStateDidChange)
5454
name:name
5555
object:nil];
56+
57+
[[NSNotificationCenter defaultCenter] addObserver:self
58+
selector:@selector(handleMemoryWarning)
59+
name:UIApplicationDidReceiveMemoryWarningNotification
60+
object:nil];
5661
}
5762
}
5863
return self;
5964
}
6065

66+
- (void)handleMemoryWarning
67+
{
68+
[_bridge.eventDispatcher sendDeviceEventWithName:@"memoryWarning"
69+
body:nil];
70+
}
6171

6272
- (void)dealloc
6373
{

0 commit comments

Comments
 (0)