1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-09-16 12:51:44 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee

Compare commits

...

6 Commits

Author SHA1 Message Date
sunnylqm
94d2e18900 v10.13.0 2024-08-26 18:52:44 +08:00
sunnylqm
d000c40e0f v10.12.0 2024-08-24 15:28:27 +08:00
sunnylqm
5659c79726 v10.11.8 2024-08-21 20:02:47 +08:00
sunnylqm
b20d987473 fix marksuccess 2024-08-21 19:58:28 +08:00
sunnylqm
14c9c0b1f5 v10.11.7 2024-08-16 11:03:25 +08:00
sunnylqm
10178e1e64 chore: Update switchVersion and switchVersionLater to be async functions 2024-08-16 10:49:35 +08:00
8 changed files with 64 additions and 47 deletions

View File

@@ -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];

View File

@@ -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": {

View File

@@ -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';

View File

@@ -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>;

View File

@@ -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'

View File

@@ -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],

View File

@@ -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 {

View File

@@ -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);
};