Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -341,19 +341,28 @@ protected Void doInBackground(Void... params) {
try {
JSONObject mutableUpdatePackage = CodePushUtils.convertReadableToJsonObject(updatePackage);
mUpdateManager.downloadPackage(mutableUpdatePackage, mCodePush.getAssetsBundleFileName(), new DownloadProgressCallback() {
private boolean hasScheduledNextFrame = false;
private DownloadProgress latestDownloadProgress = null;
private volatile boolean hasScheduledNextFrame = false;
private volatile DownloadProgress latestDownloadProgress = null;
private final DownloadProgressEventDispatcher progressEventDispatcher = new DownloadProgressEventDispatcher(
new DownloadProgressEventDispatcher.EventEmitter() {
@Override
public void emit(DownloadProgress downloadProgress) {
getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(CodePushConstants.DOWNLOAD_PROGRESS_EVENT_NAME, downloadProgress.createWritableMap());
}
});

@Override
public void call(DownloadProgress downloadProgress) {
if (!notifyProgress) {
if (!notifyProgress || downloadProgress == null) {
return;
}

latestDownloadProgress = downloadProgress;
// If the download is completed, synchronously send the last event.
if (latestDownloadProgress.isCompleted()) {
dispatchDownloadProgressEvent();
if (downloadProgress.isCompleted()) {
progressEventDispatcher.dispatch(downloadProgress);
return;
}

Expand All @@ -368,22 +377,16 @@ public void run() {
ReactChoreographer.getInstance().postFrameCallback(ReactChoreographer.CallbackType.TIMERS_EVENTS, new Choreographer.FrameCallback() {
@Override
public void doFrame(long frameTimeNanos) {
if (!latestDownloadProgress.isCompleted()) {
dispatchDownloadProgressEvent();
try {
progressEventDispatcher.dispatch(latestDownloadProgress);
} finally {
hasScheduledNextFrame = false;
}

hasScheduledNextFrame = false;
}
});
}
});
}

public void dispatchDownloadProgressEvent() {
getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(CodePushConstants.DOWNLOAD_PROGRESS_EVENT_NAME, latestDownloadProgress.createWritableMap());
}
});

JSONObject newPackage = mUpdateManager.getPackage(CodePushUtils.tryGetString(updatePackage, CodePushConstants.PACKAGE_HASH_KEY));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ public WritableMap createWritableMap() {
public boolean isCompleted() {
return mTotalBytes == mReceivedBytes;
}

long getReceivedBytes() {
return mReceivedBytes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.microsoft.codepush.react;

class DownloadProgressEventDispatcher {
interface EventEmitter {
void emit(DownloadProgress downloadProgress);
}

private final EventEmitter mEventEmitter;
private long mLastDispatchedBytes = -1;

DownloadProgressEventDispatcher(EventEmitter eventEmitter) {
mEventEmitter = eventEmitter;
}

synchronized void dispatch(DownloadProgress downloadProgress) {
if (downloadProgress == null || downloadProgress.getReceivedBytes() <= mLastDispatchedBytes) {
return;
}

mEventEmitter.emit(downloadProgress);
mLastDispatchedBytes = downloadProgress.getReceivedBytes();
}
}
Loading