提交 f2c67d2d authored 作者: vipcxj's avatar vipcxj

原生接口不支持回调和promise混用,只留下promise,emitEvent代替回调

上级 c8707552
...@@ -6,11 +6,13 @@ import android.net.Uri; ...@@ -6,11 +6,13 @@ import android.net.Uri;
import android.util.Log; import android.util.Log;
import com.bolan.android.modules.Utils; import com.bolan.android.modules.Utils;
import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
...@@ -35,17 +37,18 @@ public class Updater extends ReactContextBaseJavaModule { ...@@ -35,17 +37,18 @@ public class Updater extends ReactContextBaseJavaModule {
return "Updater"; return "Updater";
} }
private void emit(String eventName, Object params) {
getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
}
@ReactMethod @ReactMethod
public synchronized void update(final String sUrl, final Callback cb, final Promise promise) { public synchronized void update(final String sUrl, final Promise promise) {
Log.i("Updater/url", sUrl);
Log.i("Updater/cb", cb != null ? cb.toString() : "null");
if (!updating) { if (!updating) {
updating = true; updating = true;
cancel = false; cancel = false;
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
Log.i("Updater", cb != null ? cb.toString() : "null");
InputStream is = null; InputStream is = null;
FileOutputStream fos = null; FileOutputStream fos = null;
File cacheFile = Utils.getDiskCacheDir(getCurrentActivity()); File cacheFile = Utils.getDiskCacheDir(getCurrentActivity());
...@@ -72,17 +75,22 @@ public class Updater extends ReactContextBaseJavaModule { ...@@ -72,17 +75,22 @@ public class Updater extends ReactContextBaseJavaModule {
fos = new FileOutputStream(apkFile); fos = new FileOutputStream(apkFile);
int count = 0; int count = 0;
byte buf[] = new byte[1024 * 64]; byte buf[] = new byte[1024 * 64];
WritableMap event;
do { do {
int numRead = is.read(buf); int numRead = is.read(buf);
count += numRead; count += numRead;
int progress = (int) (((float) count / length) * 100); int progress = (int) (((float) count / length) * 100);
if (cb != null) { event = Arguments.createMap();
cb.invoke("downloading", progress, count, length); event.putInt("progress", progress);
} event.putInt("current", count);
event.putInt("all", length);
emit("Updater/downloading", event);
if (numRead <= 0) { if (numRead <= 0) {
if (cb != null) { event = Arguments.createMap();
cb.invoke("downloaded", progress, count, length); event.putInt("progress", progress);
} event.putInt("current", count);
event.putInt("all", length);
emit("Updater/downloaded", event);
break; break;
} }
fos.write(buf, 0, numRead); fos.write(buf, 0, numRead);
......
/** @module native/Updater */ /** @module native/Updater */
import { NativeModules } from 'react-native'; import { NativeModules, DeviceEventEmitter } from 'react-native';
const { Updater } = NativeModules; const { Updater } = NativeModules;
/** /**
* @callback UpdateCallback 更新回调 * @typedef {Object} UpdateEvent 下载事件
* @param {string} status 可能值为downloading(下载中)和downloaded(下载完成) * @property {number} progress 下载进度,0到100的整数
* @param {number} progress 下载进度,0到100的整数 * @property {number} current 已下载字节数
* @param {number} count 已下载字节数 * @property {number} all 总字节数
* @param {number} length 总字节数
*/ */
/** /**
* 下载更新包并自动安装 * @callback UpdateCallback 下载回调
* @param {UpdateEvent} event 下载事件
*/
/**
* @typedef {Object} CallbackHandle
*/
/**
* @member {Function} CallbackHandle~remove
*/
/**
*
* @param {UpdateCallback} cb 下载回调
* @return {CallbackHandle} 回调具柄,用以删除回调
*/
export const addUpdaterDownloadingCallback = (cb) => {
return DeviceEventEmitter.addListener('Updater/downloading', cb);
};
/**
*
* @param {UpdateCallback} cb 下载回调
* @return {CallbackHandle} 回调具柄,用以删除回调
*/
export const addUpdaterDownloadedCallback = (cb) => {
return DeviceEventEmitter.addListener('Updater/downloaded', cb);
};
/**
* 下载更新包并自动安装, 下载过程中会发送Updater/downloading事件,下载完成时会发送Updater/downloaded事件
* @param {string} url 更新地址 * @param {string} url 更新地址
* @param {UpdateCallback} cb 更新回调 * @returns {Promise.<void>}
* @returns {Promise.<null>}
*/ */
export const update = async (url, cb) => { export const update = async (url) => {
console.log(Updater.update); return Updater.update(url);
return Updater.update(url, cb);
}; };
/** /**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论