1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee
react-native-pushy/lib/index.js

109 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-10-04 22:27:33 +08:00
import { NativeAppEventEmitter, NativeModules } from 'react-native';
2019-11-16 00:31:30 +08:00
const Pushy = NativeModules.Pushy || {};
2016-04-04 23:02:28 +08:00
2019-10-04 22:27:33 +08:00
const host = 'https://update.reactnative.cn/api';
2016-04-04 23:02:28 +08:00
2019-11-16 00:31:30 +08:00
export const downloadRootDir = Pushy.downloadRootDir;
export const packageVersion = Pushy.packageVersion;
export const currentVersion = Pushy.currentVersion;
export const isFirstTime = Pushy.isFirstTime;
export const isRolledBack = Pushy.isRolledBack;
export const buildTime = Pushy.buildTime;
2016-04-04 23:02:28 +08:00
/*
Return json:
Package was expired:
{
expired: true,
downloadUrl: 'http://appstore/downloadUrl',
}
Package is up to date:
{
upToDate: true,
}
There is available update:
{
update: true,
name: '1.0.3-rc',
hash: 'hash',
description: '添加聊天功能\n修复商城页面BUG',
metaInfo: '{"silent":true}',
updateUrl: 'http://update-packages.reactnative.cn/hash',
pdiffUrl: 'http://update-packages.reactnative.cn/hash',
diffUrl: 'http://update-packages.reactnative.cn/hash',
}
*/
2019-10-04 22:27:33 +08:00
function assertRelease() {
if (__DEV__) {
throw new Error('react-native-update can only run on RELEASE version.');
}
}
2016-04-04 23:02:28 +08:00
export async function checkUpdate(APPKEY) {
2019-10-04 22:27:33 +08:00
assertRelease();
2016-04-04 23:02:28 +08:00
const resp = await fetch(`${host}/checkUpdate/${APPKEY}`, {
method: 'POST',
2016-04-05 00:48:22 +08:00
headers: {
2019-10-04 22:27:33 +08:00
Accept: 'application/json',
2016-04-05 00:48:22 +08:00
'Content-Type': 'application/json',
},
2016-04-04 23:02:28 +08:00
body: JSON.stringify({
2019-10-06 21:56:58 +08:00
packageVersion,
2016-04-04 23:02:28 +08:00
hash: currentVersion,
2019-10-06 21:56:58 +08:00
buildTime,
2016-04-04 23:02:28 +08:00
}),
});
if (resp.status !== 200) {
throw new Error((await resp.json()).message);
2016-04-04 23:02:28 +08:00
}
2019-10-04 22:27:33 +08:00
return resp.json();
2016-04-04 23:02:28 +08:00
}
export async function downloadUpdate(options) {
2019-10-04 22:27:33 +08:00
assertRelease();
2016-04-04 23:02:28 +08:00
if (!options.update) {
return;
}
2016-04-05 15:36:05 +08:00
if (options.diffUrl) {
2019-11-16 00:31:30 +08:00
await Pushy.downloadPatchFromPpk({
2016-04-04 23:02:28 +08:00
updateUrl: options.diffUrl,
hashName: options.hash,
2016-04-06 00:14:36 +08:00
originHashName: currentVersion,
2016-04-04 23:02:28 +08:00
});
2016-04-05 15:36:05 +08:00
} else if (options.pdiffUrl) {
2019-11-16 00:31:30 +08:00
await Pushy.downloadPatchFromPackage({
2016-04-04 23:02:28 +08:00
updateUrl: options.pdiffUrl,
hashName: options.hash,
});
} else {
2019-11-16 00:31:30 +08:00
await Pushy.downloadUpdate({
2016-04-04 23:02:28 +08:00
updateUrl: options.updateUrl,
hashName: options.hash,
});
}
2016-04-05 17:55:04 +08:00
return options.hash;
2016-04-04 23:02:28 +08:00
}
2019-10-04 22:27:33 +08:00
export function switchVersion(hash) {
assertRelease();
2019-11-16 00:31:30 +08:00
Pushy.reloadUpdate({ hashName: hash });
2016-04-04 23:02:28 +08:00
}
2019-10-04 22:27:33 +08:00
export function switchVersionLater(hash) {
assertRelease();
2019-11-16 00:31:30 +08:00
Pushy.setNeedUpdate({ hashName: hash });
2016-04-04 23:02:28 +08:00
}
2016-04-04 23:45:29 +08:00
2016-04-05 17:00:03 +08:00
export function markSuccess() {
2019-10-04 22:27:33 +08:00
assertRelease();
2019-11-16 00:31:30 +08:00
Pushy.markSuccess();
2016-04-04 23:45:29 +08:00
}
2016-04-06 09:47:26 +08:00
2019-11-16 00:31:30 +08:00
NativeAppEventEmitter.addListener('RCTPushyDownloadProgress', params => {});
2016-04-06 09:47:26 +08:00
2019-11-16 00:31:30 +08:00
NativeAppEventEmitter.addListener('RCTPushyUnzipProgress', params => {});