react-native-update 10.5.4 → 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.4",
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
  };
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(
@@ -128,6 +133,14 @@ export const PushyProvider = ({
128
133
  if (info.expired) {
129
134
  const { downloadUrl } = info;
130
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
+ }
131
144
  showAlert('提示', '您的应用版本已更新,点击更新下载安装新版本', [
132
145
  {
133
146
  text: '更新',
@@ -142,6 +155,12 @@ export const PushyProvider = ({
142
155
  ]);
143
156
  }
144
157
  } else if (info.update) {
158
+ if (
159
+ options.updateStrategy === 'silentAndNow' ||
160
+ options.updateStrategy === 'silentAndLater'
161
+ ) {
162
+ return downloadUpdate(info);
163
+ }
145
164
  showAlert(
146
165
  '提示',
147
166
  '检查到新的版本' + info.name + ',是否下载?\n' + info.description,
@@ -157,7 +176,13 @@ export const PushyProvider = ({
157
176
  ],
158
177
  );
159
178
  }
160
- }, [client, downloadAndInstallApk, downloadUpdate, showAlert]);
179
+ }, [
180
+ client,
181
+ downloadAndInstallApk,
182
+ downloadUpdate,
183
+ options.updateStrategy,
184
+ showAlert,
185
+ ]);
161
186
 
162
187
  const markSuccess = client.markSuccess;
163
188
 
@@ -168,11 +193,11 @@ export const PushyProvider = ({
168
193
  );
169
194
  return;
170
195
  }
171
- const { strategy, dismissErrorAfter, autoMarkSuccess } = options;
196
+ const { checkStrategy, dismissErrorAfter, autoMarkSuccess } = options;
172
197
  if (isFirstTime && autoMarkSuccess) {
173
198
  markSuccess();
174
199
  }
175
- if (strategy === 'both' || strategy === 'onAppResume') {
200
+ if (checkStrategy === 'both' || checkStrategy === 'onAppResume') {
176
201
  stateListener.current = AppState.addEventListener(
177
202
  'change',
178
203
  nextAppState => {
@@ -182,7 +207,7 @@ export const PushyProvider = ({
182
207
  },
183
208
  );
184
209
  }
185
- if (strategy === 'both' || strategy === 'onAppStart') {
210
+ if (checkStrategy === 'both' || checkStrategy === 'onAppStart') {
186
211
  checkUpdate();
187
212
  }
188
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
@@ -18,13 +18,15 @@ export const emptyModule = new EmptyModule();
18
18
 
19
19
  const ping =
20
20
  Platform.OS === 'web'
21
- ? () => Promise.resolve(true)
21
+ ? Promise.resolve
22
22
  : async (url: string) =>
23
23
  Promise.race([
24
24
  fetch(url, {
25
25
  method: 'HEAD',
26
- }).then(({ status }) => status === 200),
27
- 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)),
28
30
  ]);
29
31
 
30
32
  const canUseGoogle = ping('https://www.google.com');
@@ -33,7 +35,5 @@ export const testUrls = async (urls?: string[]) => {
33
35
  if (!urls?.length || (await canUseGoogle)) {
34
36
  return null;
35
37
  }
36
- return Promise.race(urls.map(url => ping(url).then(() => url))).catch(
37
- () => null,
38
- );
38
+ return Promise.race(urls.map(ping)).catch(() => null);
39
39
  };