react-native-update 10.41.0 → 10.42.1

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.41.0",
3
+ "version": "10.42.1",
4
4
  "description": "react-native hot update",
5
5
  "main": "src/index",
6
6
  "scripts": {
package/src/client.ts CHANGED
@@ -18,6 +18,7 @@ import {
18
18
  } from './core';
19
19
  import { PermissionsAndroid } from './permissions';
20
20
  import {
21
+ BeforeReloadContext,
21
22
  CheckResult,
22
23
  ClientOptions,
23
24
  EventType,
@@ -218,6 +219,18 @@ export class Pushy {
218
219
  log('afterCheckUpdate failed:', error?.message || error);
219
220
  });
220
221
  };
222
+ runBeforeReload = async (context: BeforeReloadContext) => {
223
+ const { beforeReload } = this.options;
224
+ if (!beforeReload) {
225
+ return true;
226
+ }
227
+ const shouldReload = await beforeReload(context);
228
+ if (shouldReload === false) {
229
+ log('beforeReload returned false, skipping reload');
230
+ return false;
231
+ }
232
+ return true;
233
+ };
221
234
  getCheckUrl = (endpoint: string) => {
222
235
  return `${endpoint}/checkUpdate/${this.options.appKey}`;
223
236
  };
@@ -325,6 +338,15 @@ export class Pushy {
325
338
  if (assertHash(hash) && !sharedState.applyingUpdate) {
326
339
  log(`switchVersion: ${hash}`);
327
340
  sharedState.applyingUpdate = true;
341
+ try {
342
+ if (!(await this.runBeforeReload({ type: 'switchVersion', hash }))) {
343
+ sharedState.applyingUpdate = false;
344
+ return;
345
+ }
346
+ } catch (e) {
347
+ sharedState.applyingUpdate = false;
348
+ throw e;
349
+ }
328
350
  return PushyModule.reloadUpdate({ hash });
329
351
  }
330
352
  };
@@ -452,6 +474,10 @@ export class Pushy {
452
474
  if (!updateInfo.update || !hash) {
453
475
  return;
454
476
  }
477
+ if (hash === currentVersion) {
478
+ log(`current hash ${currentVersion}, ignored`);
479
+ return;
480
+ }
455
481
  if (rolledBackVersion === hash) {
456
482
  log(`rolledback hash ${rolledBackVersion}, ignored`);
457
483
  return;
@@ -664,6 +690,9 @@ export class Pushy {
664
690
  }
665
691
  };
666
692
  restartApp = async () => {
693
+ if (!(await this.runBeforeReload({ type: 'restartApp' }))) {
694
+ return;
695
+ }
667
696
  return PushyModule.restartApp();
668
697
  };
669
698
  }
package/src/provider.tsx CHANGED
@@ -23,11 +23,10 @@ import {
23
23
  CheckResult,
24
24
  ProgressData,
25
25
  UpdateTestPayload,
26
- VersionInfo,
27
26
  } from './type';
28
27
  import { UpdateContext } from './context';
29
28
  import { URL } from 'react-native-url-polyfill';
30
- import { isInRollout } from './isInRollout';
29
+ import { resolveCheckResult } from './resolveCheckResult';
31
30
  import { assertWeb, log } from './utils';
32
31
 
33
32
  export const UpdateProvider = ({
@@ -181,94 +180,77 @@ export const UpdateProvider = ({
181
180
  if (!rootInfo) {
182
181
  return;
183
182
  }
184
- const versions = [rootInfo.expVersion, rootInfo].filter(
185
- Boolean,
186
- ) as VersionInfo[];
187
- delete rootInfo.expVersion;
188
- for (const versionInfo of versions) {
189
- const info: CheckResult = {
190
- ...rootInfo,
191
- ...versionInfo,
192
- };
193
- const rollout = info.config?.rollout?.[packageVersion];
194
- if (info.update && rollout) {
195
- if (!isInRollout(rollout)) {
196
- log(`${info.name} not in ${rollout}% rollout, ignored`);
197
- continue;
198
- }
199
- log(`${info.name} in ${rollout}% rollout, continue`);
200
- }
201
- if (info.update) {
202
- info.description = info.description ?? '';
183
+ const info = resolveCheckResult(rootInfo);
184
+ if (info.update) {
185
+ info.description = info.description ?? '';
186
+ }
187
+ updateInfoRef.current = info;
188
+ setUpdateInfo(info);
189
+ if (info.expired) {
190
+ if (
191
+ options.onPackageExpired &&
192
+ (await options.onPackageExpired(info)) === false
193
+ ) {
194
+ log('onPackageExpired returned false, skipping');
195
+ return;
203
196
  }
204
- updateInfoRef.current = info;
205
- setUpdateInfo(info);
206
- if (info.expired) {
207
- if (
208
- options.onPackageExpired &&
209
- (await options.onPackageExpired(info)) === false
210
- ) {
211
- log('onPackageExpired returned false, skipping');
212
- return;
213
- }
214
- const { downloadUrl } = info;
215
- if (downloadUrl && sharedState.apkStatus === null) {
216
- if (options.updateStrategy === 'silentAndNow') {
217
- if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) {
218
- downloadAndInstallApk(downloadUrl);
219
- } else {
220
- Linking.openURL(downloadUrl);
221
- }
222
- return info;
197
+ const { downloadUrl } = info;
198
+ if (downloadUrl && sharedState.apkStatus === null) {
199
+ if (options.updateStrategy === 'silentAndNow') {
200
+ if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) {
201
+ downloadAndInstallApk(downloadUrl);
202
+ } else {
203
+ Linking.openURL(downloadUrl);
223
204
  }
224
- alertUpdate(
225
- client.t('alert_title'),
226
- client.t('alert_app_updated'),
227
- [
228
- {
229
- text: client.t('alert_update_button'),
230
- onPress: () => {
231
- if (
232
- Platform.OS === 'android' &&
233
- downloadUrl.endsWith('.apk')
234
- ) {
235
- downloadAndInstallApk(downloadUrl);
236
- } else {
237
- Linking.openURL(downloadUrl);
238
- }
239
- },
240
- },
241
- ],
242
- );
243
- }
244
- } else if (info.update) {
245
- if (
246
- options.updateStrategy === 'silentAndNow' ||
247
- options.updateStrategy === 'silentAndLater'
248
- ) {
249
- downloadUpdate(info);
250
205
  return info;
251
206
  }
252
207
  alertUpdate(
253
208
  client.t('alert_title'),
254
- client.t('alert_new_version_found', {
255
- name: info.name!,
256
- description: info.description!,
257
- }),
209
+ client.t('alert_app_updated'),
258
210
  [
259
- { text: client.t('alert_cancel'), style: 'cancel' },
260
211
  {
261
- text: client.t('alert_confirm'),
262
- style: 'default',
212
+ text: client.t('alert_update_button'),
263
213
  onPress: () => {
264
- downloadUpdate();
214
+ if (
215
+ Platform.OS === 'android' &&
216
+ downloadUrl.endsWith('.apk')
217
+ ) {
218
+ downloadAndInstallApk(downloadUrl);
219
+ } else {
220
+ Linking.openURL(downloadUrl);
221
+ }
265
222
  },
266
223
  },
267
224
  ],
268
225
  );
269
226
  }
270
- return info;
227
+ } else if (info.update) {
228
+ if (
229
+ options.updateStrategy === 'silentAndNow' ||
230
+ options.updateStrategy === 'silentAndLater'
231
+ ) {
232
+ downloadUpdate(info);
233
+ return info;
234
+ }
235
+ alertUpdate(
236
+ client.t('alert_title'),
237
+ client.t('alert_new_version_found', {
238
+ name: info.name!,
239
+ description: info.description!,
240
+ }),
241
+ [
242
+ { text: client.t('alert_cancel'), style: 'cancel' },
243
+ {
244
+ text: client.t('alert_confirm'),
245
+ style: 'default',
246
+ onPress: () => {
247
+ downloadUpdate();
248
+ },
249
+ },
250
+ ],
251
+ );
271
252
  }
253
+ return info;
272
254
  },
273
255
  [
274
256
  client,
@@ -0,0 +1,30 @@
1
+ import { currentVersion, packageVersion } from './core';
2
+ import { isInRollout } from './isInRollout';
3
+ import { CheckResult } from './type';
4
+ import { log } from './utils';
5
+
6
+ export function resolveCheckResult(rootInfo: CheckResult): CheckResult {
7
+ const { expVersion, ...rootResult } = rootInfo;
8
+ const rollout = expVersion?.config?.rollout?.[packageVersion];
9
+ if (rootResult.update && expVersion && typeof rollout === 'number') {
10
+ if (isInRollout(rollout)) {
11
+ log(`${expVersion.name} in ${rollout}% rollout, continue`);
12
+ if (expVersion.hash === currentVersion) {
13
+ return { upToDate: true };
14
+ }
15
+ const info: CheckResult = {
16
+ update: true,
17
+ ...expVersion,
18
+ };
19
+ if (rootResult.paths) {
20
+ info.paths = rootResult.paths;
21
+ }
22
+ return info;
23
+ }
24
+ log(`${expVersion.name} not in ${rollout}% rollout, ignored`);
25
+ }
26
+ if (rootResult.update && rootResult.hash === currentVersion) {
27
+ return { upToDate: true };
28
+ }
29
+ return rootResult;
30
+ }
package/src/type.ts CHANGED
@@ -88,6 +88,11 @@ export interface UpdateServerConfig {
88
88
  queryUrls?: string[];
89
89
  }
90
90
 
91
+ export interface BeforeReloadContext {
92
+ type: 'switchVersion' | 'restartApp';
93
+ hash?: string;
94
+ }
95
+
91
96
  export interface ClientOptions {
92
97
  appKey: string;
93
98
  server?: UpdateServerConfig;
@@ -109,6 +114,9 @@ export interface ClientOptions {
109
114
  afterCheckUpdate?: (state: UpdateCheckState) => Promise<void> | void;
110
115
  beforeDownloadUpdate?: (info: CheckResult) => Promise<boolean> | boolean;
111
116
  afterDownloadUpdate?: (info: CheckResult) => Promise<boolean> | boolean;
117
+ beforeReload?: (
118
+ context: BeforeReloadContext,
119
+ ) => Promise<boolean | void> | boolean | void;
112
120
  onPackageExpired?: (info: CheckResult) => Promise<boolean> | boolean;
113
121
  overridePackageVersion?: string;
114
122
  }