mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-09-16 12:51:44 +08:00
Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
94d2e18900 | ||
![]() |
d000c40e0f | ||
![]() |
5659c79726 | ||
![]() |
b20d987473 | ||
![]() |
14c9c0b1f5 | ||
![]() |
10178e1e64 |
@@ -75,7 +75,7 @@ RCT_EXPORT_MODULE(RCTPushy);
|
||||
if (needClearPushyInfo) {
|
||||
[defaults setObject:nil forKey:keyPushyInfo];
|
||||
[defaults setObject:@(YES) forKey:KeyPackageUpdatedMarked];
|
||||
[defaults synchronize];
|
||||
|
||||
// ...need clear files later
|
||||
}
|
||||
else {
|
||||
@@ -97,7 +97,7 @@ RCT_EXPORT_MODULE(RCTPushy);
|
||||
newInfo[paramIsFirstTime] = @(NO);
|
||||
[defaults setObject:newInfo forKey:keyPushyInfo];
|
||||
[defaults setObject:@(YES) forKey:keyFirstLoadMarked];
|
||||
[defaults synchronize];
|
||||
|
||||
}
|
||||
|
||||
NSString *downloadDir = [RCTPushy downloadDir];
|
||||
@@ -137,7 +137,7 @@ RCT_EXPORT_MODULE(RCTPushy);
|
||||
[defaults setObject:nil forKey:keyPushyInfo];
|
||||
}
|
||||
[defaults setObject:curVersion forKey:keyRolledBackMarked];
|
||||
[defaults synchronize];
|
||||
|
||||
return lastVersion;
|
||||
}
|
||||
|
||||
@@ -176,7 +176,7 @@ RCT_EXPORT_MODULE(RCTPushy);
|
||||
[defaults setObject:nil forKey:KeyPackageUpdatedMarked];
|
||||
[self clearInvalidFiles];
|
||||
}
|
||||
[defaults synchronize];
|
||||
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -196,7 +196,7 @@ RCT_EXPORT_METHOD(setUuid:(NSString *)uuid resolver:(RCTPromiseResolveBlock)res
|
||||
@try {
|
||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||
[defaults setObject:uuid forKey:keyUuid];
|
||||
[defaults synchronize];
|
||||
|
||||
resolve(@true);
|
||||
}
|
||||
@catch (NSException *exception) {
|
||||
@@ -214,7 +214,7 @@ RCT_EXPORT_METHOD(setLocalHashInfo:(NSString *)hash
|
||||
if (object && [object isKindOfClass:[NSDictionary class]]) {
|
||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||
[defaults setObject:value forKey:[keyHashInfo stringByAppendingString:hash]];
|
||||
[defaults synchronize];
|
||||
|
||||
resolve(@true);
|
||||
} else {
|
||||
reject(@"json格式校验报错", nil, nil);
|
||||
@@ -295,7 +295,7 @@ RCT_EXPORT_METHOD(setNeedUpdate:(NSDictionary *)options
|
||||
newInfo[paramPackageVersion] = [RCTPushy packageVersion];
|
||||
[defaults setObject:newInfo forKey:keyPushyInfo];
|
||||
|
||||
[defaults synchronize];
|
||||
|
||||
resolve(@true);
|
||||
}else{
|
||||
reject(@"执行报错", nil, nil);
|
||||
@@ -329,8 +329,7 @@ RCT_EXPORT_METHOD(reloadUpdate:(NSDictionary *)options
|
||||
}
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(markSuccess:
|
||||
resolver:(RCTPromiseResolveBlock)resolve
|
||||
RCT_EXPORT_METHOD(markSuccess:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(RCTPromiseRejectBlock)reject)
|
||||
{
|
||||
|
||||
@@ -347,7 +346,7 @@ RCT_EXPORT_METHOD(markSuccess:
|
||||
[pushyInfo removeObjectForKey:[keyHashInfo stringByAppendingString:lastVersion]];
|
||||
}
|
||||
[defaults setObject:pushyInfo forKey:keyPushyInfo];
|
||||
[defaults synchronize];
|
||||
|
||||
|
||||
// clear other package dir
|
||||
[self clearInvalidFiles];
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "react-native-update",
|
||||
"version": "10.11.6",
|
||||
"version": "10.13.0",
|
||||
"description": "react-native hot update",
|
||||
"main": "src/index",
|
||||
"scripts": {
|
||||
|
@@ -1,5 +1,5 @@
|
||||
import { CheckResult, PushyOptions, ProgressData, EventType } from './type';
|
||||
import { log, testUrls } from './utils';
|
||||
import { joinUrls, log, testUrls } from './utils';
|
||||
import { EmitterSubscription, Platform } from 'react-native';
|
||||
import { PermissionsAndroid } from './permissions';
|
||||
import {
|
||||
@@ -64,8 +64,7 @@ export class Pushy {
|
||||
setOptions = (options: Partial<PushyOptions>) => {
|
||||
for (const [key, value] of Object.entries(options)) {
|
||||
if (value !== undefined) {
|
||||
// @ts-expect-error
|
||||
this.options[key] = value;
|
||||
(this.options as any)[key] = value;
|
||||
if (key === 'logger') {
|
||||
if (isRolledBack) {
|
||||
this.report({
|
||||
@@ -126,7 +125,7 @@ export class Pushy {
|
||||
PushyModule.markSuccess();
|
||||
this.report({ type: 'markSuccess' });
|
||||
};
|
||||
switchVersion = (hash: string) => {
|
||||
switchVersion = async (hash: string) => {
|
||||
if (__DEV__) {
|
||||
console.warn(
|
||||
'您调用了switchVersion方法,但是当前是开发环境,不会进行任何操作。',
|
||||
@@ -136,11 +135,11 @@ export class Pushy {
|
||||
if (this.assertHash(hash) && !this.applyingUpdate) {
|
||||
log('switchVersion: ' + hash);
|
||||
this.applyingUpdate = true;
|
||||
PushyModule.reloadUpdate({ hash });
|
||||
return PushyModule.reloadUpdate({ hash });
|
||||
}
|
||||
};
|
||||
|
||||
switchVersionLater = (hash: string) => {
|
||||
switchVersionLater = async (hash: string) => {
|
||||
if (__DEV__) {
|
||||
console.warn(
|
||||
'您调用了switchVersionLater方法,但是当前是开发环境,不会进行任何操作。',
|
||||
@@ -149,7 +148,7 @@ export class Pushy {
|
||||
}
|
||||
if (this.assertHash(hash)) {
|
||||
log('switchVersionLater: ' + hash);
|
||||
PushyModule.setNeedUpdate({ hash });
|
||||
return PushyModule.setNeedUpdate({ hash });
|
||||
}
|
||||
};
|
||||
checkUpdate = async (extra?: Record<string, any>) => {
|
||||
@@ -163,6 +162,13 @@ export class Pushy {
|
||||
console.warn('web 端不支持热更新检查');
|
||||
return;
|
||||
}
|
||||
if (
|
||||
this.options.beforeCheckUpdate &&
|
||||
(await this.options.beforeCheckUpdate()) === false
|
||||
) {
|
||||
log('beforeCheckUpdate 返回 false, 忽略检查');
|
||||
return;
|
||||
}
|
||||
const now = Date.now();
|
||||
if (
|
||||
this.lastRespJson &&
|
||||
@@ -265,16 +271,21 @@ export class Pushy {
|
||||
) => {
|
||||
const {
|
||||
hash,
|
||||
diffUrl: _diffUrl,
|
||||
diffUrls,
|
||||
pdiffUrl: _pdiffUrl,
|
||||
pdiffUrls,
|
||||
updateUrl: _updateUrl,
|
||||
updateUrls,
|
||||
diff,
|
||||
pdiff,
|
||||
full,
|
||||
paths = [],
|
||||
name,
|
||||
description,
|
||||
metaInfo,
|
||||
} = info;
|
||||
if (
|
||||
this.options.beforeDownloadUpdate &&
|
||||
(await this.options.beforeDownloadUpdate(info)) === false
|
||||
) {
|
||||
log('beforeDownloadUpdate 返回 false, 忽略下载');
|
||||
return;
|
||||
}
|
||||
if (!info.update || !hash) {
|
||||
return;
|
||||
}
|
||||
@@ -302,7 +313,7 @@ export class Pushy {
|
||||
let succeeded = '';
|
||||
this.report({ type: 'downloading' });
|
||||
let lastError: any;
|
||||
const diffUrl = (await testUrls(diffUrls)) || _diffUrl;
|
||||
const diffUrl = await testUrls(joinUrls(paths, diff));
|
||||
if (diffUrl) {
|
||||
log('downloading diff');
|
||||
try {
|
||||
@@ -321,7 +332,7 @@ export class Pushy {
|
||||
}
|
||||
}
|
||||
}
|
||||
const pdiffUrl = (await testUrls(pdiffUrls)) || _pdiffUrl;
|
||||
const pdiffUrl = await testUrls(joinUrls(paths, pdiff));
|
||||
if (!succeeded && pdiffUrl) {
|
||||
log('downloading pdiff');
|
||||
try {
|
||||
@@ -339,12 +350,12 @@ export class Pushy {
|
||||
}
|
||||
}
|
||||
}
|
||||
const updateUrl = (await testUrls(updateUrls)) || _updateUrl;
|
||||
if (!succeeded && updateUrl) {
|
||||
const fullUrl = await testUrls(joinUrls(paths, full));
|
||||
if (!succeeded && fullUrl) {
|
||||
log('downloading full patch');
|
||||
try {
|
||||
await PushyModule.downloadFullUpdate({
|
||||
updateUrl: updateUrl,
|
||||
updateUrl: fullUrl,
|
||||
hash,
|
||||
});
|
||||
succeeded = 'full';
|
||||
|
@@ -7,8 +7,8 @@ const asyncNoop = () => Promise.resolve();
|
||||
|
||||
export const defaultContext = {
|
||||
checkUpdate: asyncNoop,
|
||||
switchVersion: noop,
|
||||
switchVersionLater: noop,
|
||||
switchVersion: asyncNoop,
|
||||
switchVersionLater: asyncNoop,
|
||||
markSuccess: noop,
|
||||
dismissError: noop,
|
||||
downloadUpdate: asyncNoop,
|
||||
@@ -21,8 +21,8 @@ export const defaultContext = {
|
||||
|
||||
export const PushyContext = createContext<{
|
||||
checkUpdate: () => Promise<void>;
|
||||
switchVersion: () => void;
|
||||
switchVersionLater: () => void;
|
||||
switchVersion: () => Promise<void>;
|
||||
switchVersionLater: () => Promise<void>;
|
||||
markSuccess: () => void;
|
||||
dismissError: () => void;
|
||||
downloadUpdate: () => Promise<void>;
|
||||
|
@@ -4,9 +4,7 @@ const {
|
||||
version: v,
|
||||
} = require('react-native/Libraries/Core/ReactNativeVersion');
|
||||
const RNVersion = `${v.major}.${v.minor}.${v.patch}`;
|
||||
const isTurboModuleEnabled =
|
||||
// @ts-expect-error
|
||||
global.__turboModuleProxy != null;
|
||||
const isTurboModuleEnabled = (global as any).__turboModuleProxy != null;
|
||||
|
||||
export const PushyModule =
|
||||
Platform.OS === 'web'
|
||||
|
@@ -73,18 +73,18 @@ export const PushyProvider = ({
|
||||
);
|
||||
|
||||
const switchVersion = useCallback(
|
||||
(info: CheckResult | undefined = updateInfoRef.current) => {
|
||||
async (info: CheckResult | undefined = updateInfoRef.current) => {
|
||||
if (info && info.hash) {
|
||||
client.switchVersion(info.hash);
|
||||
return client.switchVersion(info.hash);
|
||||
}
|
||||
},
|
||||
[client],
|
||||
);
|
||||
|
||||
const switchVersionLater = useCallback(
|
||||
(info: CheckResult | undefined = updateInfoRef.current) => {
|
||||
async (info: CheckResult | undefined = updateInfoRef.current) => {
|
||||
if (info && info.hash) {
|
||||
client.switchVersionLater(info.hash);
|
||||
return client.switchVersionLater(info.hash);
|
||||
}
|
||||
},
|
||||
[client],
|
||||
|
12
src/type.ts
12
src/type.ts
@@ -7,12 +7,10 @@ export interface CheckResult {
|
||||
hash?: string;
|
||||
description?: string;
|
||||
metaInfo?: string;
|
||||
pdiffUrl?: string;
|
||||
pdiffUrls?: string[];
|
||||
diffUrl?: string;
|
||||
diffUrls?: string[];
|
||||
updateUrl?: string;
|
||||
updateUrls?: string[];
|
||||
pdiff?: string;
|
||||
diff?: string;
|
||||
full?: string;
|
||||
paths?: string[];
|
||||
paused?: 'app' | 'package';
|
||||
message?: string;
|
||||
}
|
||||
@@ -81,6 +79,8 @@ export interface PushyOptions {
|
||||
dismissErrorAfter?: number;
|
||||
debug?: boolean;
|
||||
throwError?: boolean;
|
||||
beforeCheckUpdate?: () => Promise<boolean>;
|
||||
beforeDownloadUpdate?: (info: CheckResult) => Promise<boolean>;
|
||||
}
|
||||
|
||||
export interface PushyTestPayload {
|
||||
|
11
src/utils.ts
11
src/utils.ts
@@ -31,9 +31,18 @@ const ping =
|
||||
|
||||
const canUseGoogle = ping('https://www.google.com');
|
||||
|
||||
export function joinUrls(paths: string[], fileName?: string) {
|
||||
if (fileName) {
|
||||
return paths.map(path => 'https://' + path + '/' + fileName);
|
||||
}
|
||||
}
|
||||
|
||||
export const testUrls = async (urls?: string[]) => {
|
||||
if (!urls?.length || (await canUseGoogle)) {
|
||||
if (!urls?.length) {
|
||||
return null;
|
||||
}
|
||||
if (await canUseGoogle) {
|
||||
return urls[0];
|
||||
}
|
||||
return Promise.race(urls.map(ping)).catch(() => null);
|
||||
};
|
||||
|
Reference in New Issue
Block a user