Skip to content
This repository was archived by the owner on Apr 15, 2023. It is now read-only.

Commit b086136

Browse files
committed
Add close method
1 parent 76d3863 commit b086136

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ var {
7070
DeviceEventEmitter,
7171
} = React;
7272

73-
var EventSource = require('NativeModules').RNEventSource;
73+
var EventSource = require('NativeModules').RNEventSource,
74+
subscription;
7475

7576
var MyFancyApp = React.createClass({
7677
getDefaultProps: function() {
@@ -79,14 +80,15 @@ var MyFancyApp = React.createClass({
7980
};
8081
},
8182
componentDidMount: function() {
82-
var subscription = DeviceEventEmitter.addListener(
83+
subscription = DeviceEventEmitter.addListener(
8384
'EventSourceMessage', function(message) {
8485
console.log(message.event);
8586
});
8687

8788
EventSource.connectWithURL(this.props.url);
8889
},
8990
componentDidUmnount: function() {
91+
EventSource.close();
9092
subscription.remove();
9193
},
9294
render: function() {

RNEventSource/RNEventSource.m

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,15 @@ - (void)dealloc{
5353
RCTLogInfo(@"%@: %@", e.event, e.data);
5454

5555
[self.bridge.eventDispatcher sendDeviceEventWithName:@"EventSourceMessage"
56-
body:@{@"event": e.event,
57-
// Guard against null values as NSMutableDictionary
58-
// expects objects
56+
body:@{@"event": e.event ? e.event : [NSNull null],
5957
@"data": e.data ? e.data : [NSNull null]}];
6058
}];
6159

6260
}
6361

62+
RCT_EXPORT_METHOD(close){
63+
[self.eventSource close];
64+
RCTLogInfo(@"EventSource: Closed");
65+
}
66+
6467
@end

0 commit comments

Comments
 (0)