Skip to content

Commit

Permalink
Merge pull request itinance#91 from grabbou/feat/optimise-subs
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneslumpe committed May 19, 2016
2 parents 9c08351 + f90ec05 commit c432a6b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
22 changes: 6 additions & 16 deletions FS.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ if (typeof self === 'undefined') {
}

var RNFSManager = require('react-native').NativeModules.RNFSManager;
var NativeAppEventEmitter = require('react-native').NativeAppEventEmitter; // iOS
var DeviceEventEmitter = require('react-native').DeviceEventEmitter; // Android
var NativeAppEventEmitter = require('react-native').NativeAppEventEmitter;
var Promise = require('bluebird');
var base64 = require('base-64');
var utf8 = require('utf8');
Expand Down Expand Up @@ -158,31 +157,22 @@ var RNFS = {

downloadFile(fromUrl, toFile, begin, progress) {
var jobId = getJobId();
var subscriptionIos, subscriptionAndroid;

if (!begin) begin = (info) => {
var subscriptions = [];
var beginListener = begin || (info) => {
console.log('Download begun:', info);
};

if (begin) {
// Two different styles of subscribing to events for different platforms, hmmm....
if (NativeAppEventEmitter.addListener)
subscriptionIos = NativeAppEventEmitter.addListener('DownloadBegin-' + jobId, begin);
if (DeviceEventEmitter.addListener)
subscriptionAndroid = DeviceEventEmitter.addListener('DownloadBegin-' + jobId, begin);
subscriptions.push(NativeAppEventEmitter.addListener('DownloadBegin-' + jobId, begin));
}

if (progress) {
if (NativeAppEventEmitter.addListener)
subscriptionIos = NativeAppEventEmitter.addListener('DownloadProgress-' + jobId, progress);
if (DeviceEventEmitter.addListener)
subscriptionAndroid = DeviceEventEmitter.addListener('DownloadProgress-' + jobId, progress);
subscriptions.push(NativeAppEventEmitter.addListener('DownloadProgress-' + jobId, progress));
}

return _downloadFile(fromUrl, toFile, jobId)
.then(res => {
if (subscriptionIos) subscriptionIos.remove();
if (subscriptionAndroid) subscriptionAndroid.remove();
subscriptions.forEach(sub => sub.remove());
return res;
})
.catch(convertError);
Expand Down
4 changes: 2 additions & 2 deletions android/src/main/java/com/rnfs/RNFSManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.modules.core.RCTNativeAppEventEmitter;

public class RNFSManager extends ReactContextBaseJavaModule {

Expand Down Expand Up @@ -215,7 +215,7 @@ public void mkdir(String filepath, Boolean excludeFromBackup, Callback callback)

private void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.getJSModule(RCTNativeAppEventEmitter.class)
.emit(eventName, params);
}

Expand Down

0 comments on commit c432a6b

Please sign in to comment.