react-native-update 10.5.3 → 10.6.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-update",
3
- "version": "10.5.3",
3
+ "version": "10.6.0-beta.0",
4
4
  "description": "react-native hot update",
5
5
  "main": "src/index",
6
6
  "scripts": {
package/src/client.ts CHANGED
@@ -18,7 +18,7 @@ const defaultServer = {
18
18
  main: 'https://update.react-native.cn/api',
19
19
  backups: ['https://update.reactnative.cn/api'],
20
20
  queryUrl:
21
- 'https://raw.githubusercontent.com/reactnativecn/react-native-pushy/master/endpoints.json',
21
+ 'https://cdn.jsdelivr.net/gh/reactnativecn/react-native-pushy@master/endpoints.json',
22
22
  };
23
23
 
24
24
  const empty = {};
@@ -33,8 +33,8 @@ export class Pushy {
33
33
  appKey: '',
34
34
  server: defaultServer,
35
35
  autoMarkSuccess: true,
36
- useAlert: true,
37
- strategy: 'both',
36
+ updateStrategy: 'alwaysAlert',
37
+ checkStrategy: 'both',
38
38
  logger: noop,
39
39
  debug: false,
40
40
  };
@@ -50,8 +50,10 @@ export class Pushy {
50
50
  version = cInfo.pushy;
51
51
 
52
52
  constructor(options: PushyOptions) {
53
- if (!options.appKey) {
54
- throw new Error('appKey is required');
53
+ if (Platform.OS === 'ios' || Platform.OS === 'android') {
54
+ if (!options.appKey) {
55
+ throw new Error('appKey is required');
56
+ }
55
57
  }
56
58
  this.setOptions(options);
57
59
  }
package/src/core.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
2
- import { EmptyModule, log } from './utils';
2
+ import { emptyModule, log } from './utils';
3
3
  const {
4
4
  version: v,
5
5
  } = require('react-native/Libraries/Core/ReactNativeVersion');
@@ -10,7 +10,7 @@ const isTurboModuleEnabled =
10
10
 
11
11
  export const PushyModule =
12
12
  Platform.OS === 'web'
13
- ? new EmptyModule()
13
+ ? emptyModule
14
14
  : isTurboModuleEnabled
15
15
  ? require('./NativePushy').default
16
16
  : NativeModules.Pushy;
@@ -1,4 +1,4 @@
1
1
  import type { PermissionsAndroidStatic } from 'react-native';
2
- import { EmptyModule } from './utils';
2
+ import { emptyModule } from './utils';
3
3
 
4
- export const PermissionsAndroid = new EmptyModule() as PermissionsAndroidStatic;
4
+ export const PermissionsAndroid = emptyModule as PermissionsAndroidStatic;
package/src/provider.tsx CHANGED
@@ -43,7 +43,7 @@ export const PushyProvider = ({
43
43
 
44
44
  const showAlert = useCallback(
45
45
  (...args: Parameters<typeof Alert.alert>) => {
46
- if (options.useAlert) {
46
+ if (options.updateStrategy === 'alwaysAlert') {
47
47
  Alert.alert(...args);
48
48
  }
49
49
  },
@@ -73,6 +73,11 @@ export const PushyProvider = ({
73
73
  return;
74
74
  }
75
75
  stateListener.current && stateListener.current.remove();
76
+ if (options.updateStrategy === 'silentAndNow') {
77
+ return client.switchVersion(hash);
78
+ } else if (options.updateStrategy === 'silentAndLater') {
79
+ return client.switchVersionLater(hash);
80
+ }
76
81
  showAlert('提示', '下载完毕,是否立即更新?', [
77
82
  {
78
83
  text: '下次再说',
@@ -94,7 +99,7 @@ export const PushyProvider = ({
94
99
  showAlert('更新失败', e.message);
95
100
  }
96
101
  },
97
- [client, showAlert],
102
+ [client, options.updateStrategy, showAlert],
98
103
  );
99
104
 
100
105
  const downloadAndInstallApk = useCallback(
@@ -120,11 +125,22 @@ export const PushyProvider = ({
120
125
  showAlert('更新检查失败', e.message);
121
126
  return;
122
127
  }
128
+ if (!info) {
129
+ return;
130
+ }
123
131
  updateInfoRef.current = info;
124
132
  setUpdateInfo(info);
125
133
  if (info.expired) {
126
134
  const { downloadUrl } = info;
127
135
  if (downloadUrl) {
136
+ if (options.updateStrategy === 'silentAndNow') {
137
+ if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) {
138
+ downloadAndInstallApk(downloadUrl);
139
+ } else {
140
+ Linking.openURL(downloadUrl);
141
+ }
142
+ return;
143
+ }
128
144
  showAlert('提示', '您的应用版本已更新,点击更新下载安装新版本', [
129
145
  {
130
146
  text: '更新',
@@ -139,6 +155,12 @@ export const PushyProvider = ({
139
155
  ]);
140
156
  }
141
157
  } else if (info.update) {
158
+ if (
159
+ options.updateStrategy === 'silentAndNow' ||
160
+ options.updateStrategy === 'silentAndLater'
161
+ ) {
162
+ return downloadUpdate(info);
163
+ }
142
164
  showAlert(
143
165
  '提示',
144
166
  '检查到新的版本' + info.name + ',是否下载?\n' + info.description,
@@ -154,7 +176,13 @@ export const PushyProvider = ({
154
176
  ],
155
177
  );
156
178
  }
157
- }, [client, downloadAndInstallApk, downloadUpdate, showAlert]);
179
+ }, [
180
+ client,
181
+ downloadAndInstallApk,
182
+ downloadUpdate,
183
+ options.updateStrategy,
184
+ showAlert,
185
+ ]);
158
186
 
159
187
  const markSuccess = client.markSuccess;
160
188
 
@@ -165,11 +193,11 @@ export const PushyProvider = ({
165
193
  );
166
194
  return;
167
195
  }
168
- const { strategy, dismissErrorAfter, autoMarkSuccess } = options;
196
+ const { checkStrategy, dismissErrorAfter, autoMarkSuccess } = options;
169
197
  if (isFirstTime && autoMarkSuccess) {
170
198
  markSuccess();
171
199
  }
172
- if (strategy === 'both' || strategy === 'onAppResume') {
200
+ if (checkStrategy === 'both' || checkStrategy === 'onAppResume') {
173
201
  stateListener.current = AppState.addEventListener(
174
202
  'change',
175
203
  nextAppState => {
@@ -179,7 +207,7 @@ export const PushyProvider = ({
179
207
  },
180
208
  );
181
209
  }
182
- if (strategy === 'both' || strategy === 'onAppStart') {
210
+ if (checkStrategy === 'both' || checkStrategy === 'onAppStart') {
183
211
  checkUpdate();
184
212
  }
185
213
  let dismissErrorTimer: ReturnType<typeof setTimeout>;
package/src/type.ts CHANGED
@@ -69,8 +69,8 @@ export interface PushyOptions {
69
69
  appKey: string;
70
70
  server?: PushyServerConfig;
71
71
  logger?: UpdateEventsLogger;
72
- useAlert?: boolean;
73
- strategy?: 'onAppStart' | 'onAppResume' | 'both' | null;
72
+ updateStrategy?: 'alwaysAlert' | 'silentAndNow' | 'silentAndLater' | null;
73
+ checkStrategy?: 'onAppStart' | 'onAppResume' | 'both' | null;
74
74
  autoMarkSuccess?: boolean;
75
75
  dismissErrorAfter?: number;
76
76
  debug?: boolean;
package/src/utils.ts CHANGED
@@ -5,7 +5,7 @@ export function log(...args: any[]) {
5
5
  }
6
6
 
7
7
  const noop = () => {};
8
- export class EmptyModule {
8
+ class EmptyModule {
9
9
  constructor() {
10
10
  return new Proxy(this, {
11
11
  get() {
@@ -14,16 +14,19 @@ export class EmptyModule {
14
14
  });
15
15
  }
16
16
  }
17
+ export const emptyModule = new EmptyModule();
17
18
 
18
19
  const ping =
19
20
  Platform.OS === 'web'
20
- ? () => Promise.resolve(true)
21
+ ? Promise.resolve
21
22
  : async (url: string) =>
22
23
  Promise.race([
23
24
  fetch(url, {
24
25
  method: 'HEAD',
25
- }).then(({ status }) => status === 200),
26
- new Promise<false>(r => setTimeout(() => r(false), 2000)),
26
+ })
27
+ .then(({ status }) => (status === 200 ? url : null))
28
+ .catch(() => null),
29
+ new Promise(r => setTimeout(() => r(null), 2000)),
27
30
  ]);
28
31
 
29
32
  const canUseGoogle = ping('https://www.google.com');
@@ -32,7 +35,5 @@ export const testUrls = async (urls?: string[]) => {
32
35
  if (!urls?.length || (await canUseGoogle)) {
33
36
  return null;
34
37
  }
35
- return Promise.race(urls.map(url => ping(url).then(() => url))).catch(
36
- () => null,
37
- );
38
+ return Promise.race(urls.map(ping)).catch(() => null);
38
39
  };