react-native-update 10.8.0 → 10.10.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.8.0",
3
+ "version": "10.10.0",
4
4
  "description": "react-native hot update",
5
5
  "main": "src/index",
6
6
  "scripts": {
package/src/client.ts CHANGED
@@ -150,7 +150,7 @@ export class Pushy {
150
150
  PushyModule.setNeedUpdate({ hash });
151
151
  }
152
152
  };
153
- checkUpdate = async () => {
153
+ checkUpdate = async (extra?: Record<string, any>) => {
154
154
  if (__DEV__ && !this.options.debug) {
155
155
  console.info(
156
156
  '您当前处于开发环境且未启用 debug,不会进行热更检查。如需在开发环境中调试热更,请在 client 中设置 debug 为 true',
@@ -176,6 +176,7 @@ export class Pushy {
176
176
  hash: currentVersion,
177
177
  buildTime,
178
178
  cInfo,
179
+ ...extra,
179
180
  };
180
181
  if (__DEV__) {
181
182
  delete fetchBody.buildTime;
package/src/context.ts CHANGED
@@ -14,6 +14,7 @@ export const defaultContext = {
14
14
  downloadUpdate: asyncNoop,
15
15
  downloadAndInstallApk: asyncNoop,
16
16
  getCurrentVersionInfo: () => Promise.resolve({}),
17
+ parseTestPayload: () => Promise.resolve(false),
17
18
  currentHash: '',
18
19
  packageVersion: '',
19
20
  };
@@ -31,6 +32,7 @@ export const PushyContext = createContext<{
31
32
  description?: string;
32
33
  metaInfo?: string;
33
34
  }>;
35
+ parseTestPayload: (code: string) => Promise<boolean>;
34
36
  currentHash: string;
35
37
  packageVersion: string;
36
38
  client?: Pushy;
package/src/provider.tsx CHANGED
@@ -19,7 +19,7 @@ import {
19
19
  packageVersion,
20
20
  getCurrentVersionInfo,
21
21
  } from './core';
22
- import { CheckResult, ProgressData } from './type';
22
+ import { CheckResult, ProgressData, PushyTestPayload } from './type';
23
23
  import { PushyContext } from './context';
24
24
 
25
25
  export const PushyProvider = ({
@@ -145,81 +145,84 @@ export const PushyProvider = ({
145
145
  [client],
146
146
  );
147
147
 
148
- const checkUpdate = useCallback(async () => {
149
- const now = Date.now();
150
- if (lastChecking.current && now - lastChecking.current < 1000) {
151
- return;
152
- }
153
- lastChecking.current = now;
154
- let info: CheckResult;
155
- try {
156
- info = await client.checkUpdate();
157
- } catch (e: any) {
158
- setLastError(e);
159
- alertError('更新检查失败', e.message);
160
- throwErrorIfEnabled(e);
161
- return;
162
- }
163
- if (!info) {
164
- return;
165
- }
166
- updateInfoRef.current = info;
167
- setUpdateInfo(info);
168
- if (info.expired) {
169
- const { downloadUrl } = info;
170
- if (downloadUrl) {
171
- if (options.updateStrategy === 'silentAndNow') {
172
- if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) {
173
- downloadAndInstallApk(downloadUrl);
174
- } else {
175
- 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;
176
179
  }
177
- 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
+ ]);
178
192
  }
179
- alertUpdate('提示', '您的应用版本已更新,点击更新下载安装新版本', [
180
- {
181
- text: '更新',
182
- onPress: () => {
183
- if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) {
184
- downloadAndInstallApk(downloadUrl);
185
- } else {
186
- Linking.openURL(downloadUrl);
187
- }
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
+ },
188
211
  },
189
- },
190
- ]);
191
- }
192
- } else if (info.update) {
193
- if (
194
- options.updateStrategy === 'silentAndNow' ||
195
- options.updateStrategy === 'silentAndLater'
196
- ) {
197
- return downloadUpdate(info);
212
+ ],
213
+ );
198
214
  }
199
- alertUpdate(
200
- '提示',
201
- '检查到新的版本' + info.name + ',是否下载?\n' + info.description,
202
- [
203
- { text: '取消', style: 'cancel' },
204
- {
205
- text: '确定',
206
- style: 'default',
207
- onPress: () => {
208
- downloadUpdate();
209
- },
210
- },
211
- ],
212
- );
213
- }
214
- }, [
215
- client,
216
- alertError,
217
- throwErrorIfEnabled,
218
- options.updateStrategy,
219
- alertUpdate,
220
- downloadAndInstallApk,
221
- downloadUpdate,
222
- ]);
215
+ },
216
+ [
217
+ client,
218
+ alertError,
219
+ throwErrorIfEnabled,
220
+ options.updateStrategy,
221
+ alertUpdate,
222
+ downloadAndInstallApk,
223
+ downloadUpdate,
224
+ ],
225
+ );
223
226
 
224
227
  const markSuccess = client.markSuccess;
225
228
 
@@ -259,6 +262,25 @@ export const PushyProvider = ({
259
262
  };
260
263
  }, [checkUpdate, options, dismissError, markSuccess]);
261
264
 
265
+ const parseTestPayload = useCallback(
266
+ async (code: string) => {
267
+ let payload: PushyTestPayload;
268
+ try {
269
+ payload = JSON.parse(code);
270
+ } catch {
271
+ return false;
272
+ }
273
+ if (payload && payload.type) {
274
+ if (payload.type === '__rnPushyVersionHash') {
275
+ await checkUpdate({ toHash: payload.data });
276
+ return true;
277
+ }
278
+ }
279
+ return false;
280
+ },
281
+ [checkUpdate],
282
+ );
283
+
262
284
  return (
263
285
  <PushyContext.Provider
264
286
  value={{
@@ -276,6 +298,7 @@ export const PushyProvider = ({
276
298
  progress,
277
299
  downloadAndInstallApk,
278
300
  getCurrentVersionInfo,
301
+ parseTestPayload,
279
302
  }}>
280
303
  {children}
281
304
  </PushyContext.Provider>
package/src/type.ts CHANGED
@@ -81,3 +81,8 @@ export interface PushyOptions {
81
81
  debug?: boolean;
82
82
  throwError?: boolean;
83
83
  }
84
+
85
+ export interface PushyTestPayload {
86
+ type: '__rnPushyVersionHash';
87
+ data: any;
88
+ }