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

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

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