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 +1 -1
- package/src/client.ts +12 -2
- package/src/provider.tsx +91 -70
- package/src/type.ts +1 -0
package/package.json
CHANGED
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
|
-
|
|
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
|
-
[
|
|
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(
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
if (
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
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
|
-
|
|
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
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
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
|
|