1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee

chore: some tweak

This commit is contained in:
sunnylqm 2023-02-20 13:03:12 +08:00
parent 55879198e8
commit d6c268f533
No known key found for this signature in database
7 changed files with 68 additions and 57 deletions

3
.gitignore vendored
View File

@ -42,5 +42,6 @@ node_modules/
npm-debug.log npm-debug.log
Example/**/update.json Example/**/update.json
Example/**/.update
Example/**/.pushy
yarn-error.log yarn-error.log
Example/testHotUpdate/.pushy

View File

@ -1,3 +0,0 @@
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0eXBlIjoidXNlciIsInVpZCI6OTQ3NywiZXhwIjoxNjc5MjczODYwOTgzLCJpYXQiOjE2NzY2ODE4NjB9.RtoiYnN_2hJ6-aRsx6JkRWil0-RxkdpL7yD7koAky8E"
}

View File

@ -1,21 +1,23 @@
import React from 'react'; /* eslint-disable react-native/no-inline-styles */
/* eslint-disable react/react-in-jsx-scope */
import {useState} from 'react';
import { import {
ActivityIndicator, ActivityIndicator,
Alert, Alert,
Modal, Modal,
TextInput, TextInput,
Button, Button,
NativeModules,
StyleSheet, StyleSheet,
SafeAreaView, SafeAreaView,
Text, Text,
View, View,
} from 'react-native'; } from 'react-native';
const Pushy = NativeModules.Pushy; import {PushyModule} from 'react-native-update';
export default function TestConsole({visible}) { export default function TestConsole({visible}) {
const [text, setText] = React.useState(''); const [text, setText] = useState('');
const [running, setRunning] = React.useState(false); const [running, setRunning] = useState(false);
return ( return (
<Modal visible={visible}> <Modal visible={visible}>
<SafeAreaView style={{flex: 1, padding: 10}}> <SafeAreaView style={{flex: 1, padding: 10}}>
@ -47,7 +49,7 @@ export default function TestConsole({visible}) {
const methodName = inputs[0]; const methodName = inputs[0];
let params; let params;
if (inputs.length === 1) { if (inputs.length === 1) {
await Pushy[methodName](); await PushyModule[methodName]();
} else { } else {
if (inputs.length === 2) { if (inputs.length === 2) {
params = inputs[1]; params = inputs[1];
@ -58,7 +60,7 @@ export default function TestConsole({visible}) {
} }
console.log({inputs, params}); console.log({inputs, params});
} }
await Pushy[methodName](params); await PushyModule[methodName](params);
} }
Alert.alert('done'); Alert.alert('done');
} catch (e) { } catch (e) {

View File

@ -3,7 +3,7 @@
#import "RCTPushyManager.h" #import "RCTPushyManager.h"
// Thanks to this guard, we won't import this header when we build for the old architecture. // Thanks to this guard, we won't import this header when we build for the old architecture.
#ifdef RCT_NEW_ARCH_ENABLED #ifdef RCT_NEW_ARCH_ENABLED
#import "RNUpdateSpec.h" #import "RCTPushySpec.h"
#endif #endif
#import <React/RCTConvert.h> #import <React/RCTConvert.h>

View File

@ -4,38 +4,46 @@
*/ */
'use strict'; 'use strict';
import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport'; import type { TurboModule } from 'react-native/Libraries/TurboModule/RCTExport';
import {TurboModuleRegistry} from 'react-native'; import { TurboModuleRegistry } from 'react-native';
export interface Spec extends TurboModule { export interface Spec extends TurboModule {
getConstants: () => { getConstants: () => {
downloadRootDir: string; downloadRootDir: string,
packageVersion: string; packageVersion: string,
currentVersion: string; currentVersion: string,
isFirstTime: boolean; isFirstTime: boolean,
rolledBackVersion: string; rolledBackVersion: string,
buildTime: string; buildTime: string,
blockUpdate: Object; blockUpdate: Object,
uuid: string; uuid: string,
isUsingBundleUrl: boolean; isUsingBundleUrl: boolean,
}; };
setLocalHashInfo(hash: string, info:string): void; setLocalHashInfo(hash: string, info: string): void;
getLocalHashInfo(hash: string):Promise<string>; getLocalHashInfo(hash: string): Promise<string>;
setUuid(uuid: string): void; setUuid(uuid: string): void;
setBlockUpdate(options: {reason: string,until: number}): void; setBlockUpdate(options: { reason: string, until: number }): void;
downloadPatchFromPpk(options: { updateUrl:string, downloadPatchFromPpk(options: {
updateUrl: string,
hash: string, hash: string,
originHash: string}):Promise<void>; originHash: string,
downloadPatchFromPackage(options: { updateUrl:string, }): Promise<void>;
hash: string}): Promise<void>; downloadPatchFromPackage(options: {
downloadFullUpdate(options: { updateUrl:string, updateUrl: string,
hash: string}): Promise<void>; hash: string,
reloadUpdate(options: { hash: string}): void; }): Promise<void>;
setNeedUpdate(options: { hash: string}): void; downloadFullUpdate(options: {
updateUrl: string,
hash: string,
}): Promise<void>;
reloadUpdate(options: { hash: string }): void;
setNeedUpdate(options: { hash: string }): void;
markSuccess(): void; markSuccess(): void;
downloadAndInstallApk(options: { url: string, downloadAndInstallApk(options: {
target : string, url: string,
hash: string}): Promise<void>; target: string,
hash: string,
}): Promise<void>;
} }
export default (TurboModuleRegistry.get<Spec>('Pushy'): ?Spec); export default (TurboModuleRegistry.get<Spec>('Pushy'): ?Spec);

View File

@ -16,14 +16,17 @@ const {
} = require('react-native/Libraries/Core/ReactNativeVersion'); } = require('react-native/Libraries/Core/ReactNativeVersion');
const RNVersion = `${v.major}.${v.minor}.${v.patch}`; const RNVersion = `${v.major}.${v.minor}.${v.patch}`;
const isTurboModuleEnabled = global.__turboModuleProxy != null; const isTurboModuleEnabled = global.__turboModuleProxy != null;
const Pushy = isTurboModuleEnabled ?
require("./NativeUpdate").default :
NativeModules.Pushy;
if (!Pushy) { export const PushyModule = isTurboModuleEnabled
? require('./NativeUpdate').default
: NativeModules.Pushy;
if (!PushyModule) {
throw new Error('react-native-update模块无法加载请对照安装文档检查配置。'); throw new Error('react-native-update模块无法加载请对照安装文档检查配置。');
} }
const PushyConstants = isTurboModuleEnabled ? Pushy.getConstants(): Pushy; const PushyConstants = isTurboModuleEnabled
? PushyModule.getConstants()
: PushyModule;
export const downloadRootDir = PushyConstants.downloadRootDir; export const downloadRootDir = PushyConstants.downloadRootDir;
export const packageVersion = PushyConstants.packageVersion; export const packageVersion = PushyConstants.packageVersion;
@ -43,22 +46,22 @@ if (Platform.OS === 'android' && !PushyConstants.isUsingBundleUrl) {
} }
function setLocalHashInfo(hash, info) { function setLocalHashInfo(hash, info) {
Pushy.setLocalHashInfo(hash, JSON.stringify(info)); PushyModule.setLocalHashInfo(hash, JSON.stringify(info));
} }
async function getLocalHashInfo(hash) { async function getLocalHashInfo(hash) {
return JSON.parse(await Pushy.getLocalHashInfo(hash)); return JSON.parse(await PushyModule.getLocalHashInfo(hash));
} }
export async function getCurrentVersionInfo() { export async function getCurrentVersionInfo() {
return currentVersion ? (await getLocalHashInfo(currentVersion)) || {} : {}; return currentVersion ? (await getLocalHashInfo(currentVersion)) || {} : {};
} }
const eventEmitter = new NativeEventEmitter(Pushy); const eventEmitter = new NativeEventEmitter(PushyModule);
if (!uuid) { if (!uuid) {
uuid = require('nanoid/non-secure').nanoid(); uuid = require('nanoid/non-secure').nanoid();
Pushy.setUuid(uuid); PushyModule.setUuid(uuid);
} }
function logger(text) { function logger(text) {
@ -166,7 +169,7 @@ function checkOperation(op) {
reason: action.reason, reason: action.reason,
until: Math.round((Date.now() + action.duration) / 1000), until: Math.round((Date.now() + action.duration) / 1000),
}; };
Pushy.setBlockUpdate(blockUpdate); PushyModule.setBlockUpdate(blockUpdate);
} }
}); });
} }
@ -212,7 +215,7 @@ export async function downloadUpdate(options, eventListeners) {
if (options.diffUrl) { if (options.diffUrl) {
logger('downloading diff'); logger('downloading diff');
try { try {
await Pushy.downloadPatchFromPpk({ await PushyModule.downloadPatchFromPpk({
updateUrl: options.diffUrl, updateUrl: options.diffUrl,
hash: options.hash, hash: options.hash,
originHash: currentVersion, originHash: currentVersion,
@ -225,7 +228,7 @@ export async function downloadUpdate(options, eventListeners) {
if (!succeeded && options.pdiffUrl) { if (!succeeded && options.pdiffUrl) {
logger('downloading pdiff'); logger('downloading pdiff');
try { try {
await Pushy.downloadPatchFromPackage({ await PushyModule.downloadPatchFromPackage({
updateUrl: options.pdiffUrl, updateUrl: options.pdiffUrl,
hash: options.hash, hash: options.hash,
}); });
@ -237,7 +240,7 @@ export async function downloadUpdate(options, eventListeners) {
if (!succeeded && options.updateUrl) { if (!succeeded && options.updateUrl) {
logger('downloading full patch'); logger('downloading full patch');
try { try {
await Pushy.downloadFullUpdate({ await PushyModule.downloadFullUpdate({
updateUrl: options.updateUrl, updateUrl: options.updateUrl,
hash: options.hash, hash: options.hash,
}); });
@ -276,7 +279,7 @@ export function switchVersion(hash) {
assertRelease(); assertRelease();
if (assertHash(hash)) { if (assertHash(hash)) {
logger('switchVersion: ' + hash); logger('switchVersion: ' + hash);
Pushy.reloadUpdate({ hash }); PushyModule.reloadUpdate({ hash });
} }
} }
@ -284,7 +287,7 @@ export function switchVersionLater(hash) {
assertRelease(); assertRelease();
if (assertHash(hash)) { if (assertHash(hash)) {
logger('switchVersionLater: ' + hash); logger('switchVersionLater: ' + hash);
Pushy.setNeedUpdate({ hash }); PushyModule.setNeedUpdate({ hash });
} }
} }
@ -296,7 +299,7 @@ export function markSuccess() {
return; return;
} }
marked = true; marked = true;
Pushy.markSuccess(); PushyModule.markSuccess();
report(currentVersion, 'success'); report(currentVersion, 'success');
} }
@ -326,7 +329,7 @@ export async function downloadAndInstallApk({ url, onDownloadProgress }) {
}, },
); );
} }
await Pushy.downloadAndInstallApk({ await PushyModule.downloadAndInstallApk({
url, url,
target: 'update.apk', target: 'update.apk',
hash, hash,

View File

@ -34,7 +34,7 @@
"codegenConfig": { "codegenConfig": {
"libraries": [ "libraries": [
{ {
"name": "RNUpdateSpec", "name": "RCTPushySpec",
"type": "modules", "type": "modules",
"jsSrcsDir": "lib" "jsSrcsDir": "lib"
} }