react-native-update 10.7.4 → 10.9.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.7.4",
3
+ "version": "10.9.0",
4
4
  "description": "react-native hot update",
5
5
  "main": "src/index",
6
6
  "scripts": {
package/src/client.ts CHANGED
@@ -37,6 +37,7 @@ export class Pushy {
37
37
  checkStrategy: 'both',
38
38
  logger: noop,
39
39
  debug: false,
40
+ throwError: false,
40
41
  };
41
42
 
42
43
  lastChecking?: number;
@@ -149,7 +150,7 @@ export class Pushy {
149
150
  PushyModule.setNeedUpdate({ hash });
150
151
  }
151
152
  };
152
- checkUpdate = async () => {
153
+ checkUpdate = async (extra?: Record<string, any>) => {
153
154
  if (__DEV__ && !this.options.debug) {
154
155
  console.info(
155
156
  '您当前处于开发环境且未启用 debug,不会进行热更检查。如需在开发环境中调试热更,请在 client 中设置 debug 为 true',
@@ -175,6 +176,7 @@ export class Pushy {
175
176
  hash: currentVersion,
176
177
  buildTime,
177
178
  cInfo,
179
+ ...extra,
178
180
  };
179
181
  if (__DEV__) {
180
182
  delete fetchBody.buildTime;
@@ -289,6 +291,7 @@ export class Pushy {
289
291
  }
290
292
  let succeeded = false;
291
293
  this.report({ type: 'downloading' });
294
+ let lastError: any;
292
295
  const diffUrl = (await testUrls(diffUrls)) || _diffUrl;
293
296
  if (diffUrl) {
294
297
  log('downloading diff');
@@ -300,6 +303,7 @@ export class Pushy {
300
303
  });
301
304
  succeeded = true;
302
305
  } catch (e: any) {
306
+ lastError = e;
303
307
  if (__DEV__) {
304
308
  succeeded = true;
305
309
  } else {
@@ -317,6 +321,7 @@ export class Pushy {
317
321
  });
318
322
  succeeded = true;
319
323
  } catch (e: any) {
324
+ lastError = e;
320
325
  if (__DEV__) {
321
326
  succeeded = true;
322
327
  } else {
@@ -334,6 +339,7 @@ export class Pushy {
334
339
  });
335
340
  succeeded = true;
336
341
  } catch (e: any) {
342
+ lastError = e;
337
343
  if (__DEV__) {
338
344
  succeeded = true;
339
345
  } else {
@@ -349,10 +355,14 @@ export class Pushy {
349
355
  return hash;
350
356
  }
351
357
  if (!succeeded) {
352
- return this.report({
358
+ this.report({
353
359
  type: 'errorUpdate',
354
360
  data: { newVersion: hash },
355
361
  });
362
+ if (lastError) {
363
+ throw lastError;
364
+ }
365
+ return;
356
366
  }
357
367
  log('downloaded hash:', hash);
358
368
  setLocalHashInfo(hash, {
package/src/provider.tsx CHANGED
@@ -37,6 +37,15 @@ export const PushyProvider = ({
37
37
  const [lastError, setLastError] = useState<Error>();
38
38
  const lastChecking = useRef(0);
39
39
 
40
+ const throwErrorIfEnabled = useCallback(
41
+ (e: Error) => {
42
+ if (options.throwError) {
43
+ throw e;
44
+ }
45
+ },
46
+ [options.throwError],
47
+ );
48
+
40
49
  const dismissError = useCallback(() => {
41
50
  setLastError(undefined);
42
51
  }, []);
@@ -115,9 +124,16 @@ export const PushyProvider = ({
115
124
  } catch (e: any) {
116
125
  setLastError(e);
117
126
  alertError('更新失败', e.message);
127
+ throwErrorIfEnabled(e);
118
128
  }
119
129
  },
120
- [alertError, client, options.updateStrategy, alertUpdate],
130
+ [
131
+ client,
132
+ options.updateStrategy,
133
+ alertUpdate,
134
+ alertError,
135
+ throwErrorIfEnabled,
136
+ ],
121
137
  );
122
138
 
123
139
  const downloadAndInstallApk = useCallback(
@@ -129,79 +145,84 @@ export const PushyProvider = ({
129
145
  [client],
130
146
  );
131
147
 
132
- const checkUpdate = useCallback(async () => {
133
- const now = Date.now();
134
- if (lastChecking.current && now - lastChecking.current < 1000) {
135
- return;
136
- }
137
- lastChecking.current = now;
138
- let info: CheckResult;
139
- try {
140
- info = await client.checkUpdate();
141
- } catch (e: any) {
142
- setLastError(e);
143
- alertError('更新检查失败', e.message);
144
- return;
145
- }
146
- if (!info) {
147
- return;
148
- }
149
- updateInfoRef.current = info;
150
- setUpdateInfo(info);
151
- if (info.expired) {
152
- const { downloadUrl } = info;
153
- if (downloadUrl) {
154
- if (options.updateStrategy === 'silentAndNow') {
155
- if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) {
156
- downloadAndInstallApk(downloadUrl);
157
- } else {
158
- Linking.openURL(downloadUrl);
148
+ const checkUpdate = useCallback(
149
+ async (extra?: Record<string, any>) => {
150
+ const now = Date.now();
151
+ if (lastChecking.current && now - lastChecking.current < 1000) {
152
+ return;
153
+ }
154
+ lastChecking.current = now;
155
+ let info: CheckResult;
156
+ try {
157
+ info = await client.checkUpdate(extra);
158
+ } catch (e: any) {
159
+ setLastError(e);
160
+ alertError('更新检查失败', e.message);
161
+ throwErrorIfEnabled(e);
162
+ return;
163
+ }
164
+ if (!info) {
165
+ return;
166
+ }
167
+ updateInfoRef.current = info;
168
+ setUpdateInfo(info);
169
+ if (info.expired) {
170
+ const { downloadUrl } = info;
171
+ if (downloadUrl) {
172
+ if (options.updateStrategy === 'silentAndNow') {
173
+ if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) {
174
+ downloadAndInstallApk(downloadUrl);
175
+ } else {
176
+ Linking.openURL(downloadUrl);
177
+ }
178
+ return;
159
179
  }
160
- return;
180
+ alertUpdate('提示', '您的应用版本已更新,点击更新下载安装新版本', [
181
+ {
182
+ text: '更新',
183
+ onPress: () => {
184
+ if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) {
185
+ downloadAndInstallApk(downloadUrl);
186
+ } else {
187
+ Linking.openURL(downloadUrl);
188
+ }
189
+ },
190
+ },
191
+ ]);
161
192
  }
162
- alertUpdate('提示', '您的应用版本已更新,点击更新下载安装新版本', [
163
- {
164
- text: '更新',
165
- onPress: () => {
166
- if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) {
167
- downloadAndInstallApk(downloadUrl);
168
- } else {
169
- Linking.openURL(downloadUrl);
170
- }
193
+ } else if (info.update) {
194
+ if (
195
+ options.updateStrategy === 'silentAndNow' ||
196
+ options.updateStrategy === 'silentAndLater'
197
+ ) {
198
+ return downloadUpdate(info);
199
+ }
200
+ alertUpdate(
201
+ '提示',
202
+ '检查到新的版本' + info.name + ',是否下载?\n' + info.description,
203
+ [
204
+ { text: '取消', style: 'cancel' },
205
+ {
206
+ text: '确定',
207
+ style: 'default',
208
+ onPress: () => {
209
+ downloadUpdate();
210
+ },
171
211
  },
172
- },
173
- ]);
174
- }
175
- } else if (info.update) {
176
- if (
177
- options.updateStrategy === 'silentAndNow' ||
178
- options.updateStrategy === 'silentAndLater'
179
- ) {
180
- return downloadUpdate(info);
212
+ ],
213
+ );
181
214
  }
182
- alertUpdate(
183
- '提示',
184
- '检查到新的版本' + info.name + ',是否下载?\n' + info.description,
185
- [
186
- { text: '取消', style: 'cancel' },
187
- {
188
- text: '确定',
189
- style: 'default',
190
- onPress: () => {
191
- downloadUpdate();
192
- },
193
- },
194
- ],
195
- );
196
- }
197
- }, [
198
- alertError,
199
- client,
200
- downloadAndInstallApk,
201
- downloadUpdate,
202
- options.updateStrategy,
203
- alertUpdate,
204
- ]);
215
+ },
216
+ [
217
+ client,
218
+ alertError,
219
+ throwErrorIfEnabled,
220
+ options.updateStrategy,
221
+ alertUpdate,
222
+ downloadAndInstallApk,
223
+ downloadUpdate,
224
+ ],
225
+ );
205
226
 
206
227
  const markSuccess = client.markSuccess;
207
228
 
package/src/type.ts CHANGED
@@ -79,4 +79,5 @@ export interface PushyOptions {
79
79
  autoMarkSuccess?: boolean;
80
80
  dismissErrorAfter?: number;
81
81
  debug?: boolean;
82
+ throwError?: boolean;
82
83
  }