Skip to content

Commit 4436654

Browse files
committed
fix class properties
1 parent 3ccc365 commit 4436654

File tree

4 files changed

+57
-40
lines changed

4 files changed

+57
-40
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-update",
3-
"version": "10.24.1",
3+
"version": "10.24.2",
44
"description": "react-native hot update",
55
"main": "src/index",
66
"scripts": {

src/client.ts

+32-34
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
import { CheckResult, ClientOptions, ProgressData, EventType } from './type';
2-
import { emptyObj, joinUrls, log, noop, promiseAny, testUrls } from './utils';
2+
import {
3+
assertDev,
4+
assertWeb,
5+
emptyObj,
6+
joinUrls,
7+
log,
8+
noop,
9+
promiseAny,
10+
testUrls,
11+
} from './utils';
312
import { EmitterSubscription, Platform } from 'react-native';
413
import { PermissionsAndroid } from './permissions';
514
import {
@@ -17,7 +26,7 @@ import {
1726

1827
const SERVER_PRESETS = {
1928
// cn
20-
pushy: {
29+
Pushy: {
2130
main: 'https://update.react-native.cn/api',
2231
backups: ['https://update.reactnative.cn/api'],
2332
queryUrls: [
@@ -26,19 +35,16 @@ const SERVER_PRESETS = {
2635
],
2736
},
2837
// i18n
29-
cresc: {
38+
Cresc: {
3039
main: 'https://api.cresc.dev',
3140
backups: ['https://api.cresc.app'],
3241
queryUrls: [
3342
'https://cdn.jsdelivr.net/gh/reactnativecn/react-native-update@master/endpoints_cresc.json',
3443
],
3544
},
3645
};
37-
if (Platform.OS === 'web') {
38-
console.warn(
39-
'react-native-update does not support hot updates on the web platform and will not perform any operations',
40-
);
41-
}
46+
47+
assertWeb();
4248

4349
const defaultClientOptions: ClientOptions = {
4450
appKey: '',
@@ -52,11 +58,8 @@ const defaultClientOptions: ClientOptions = {
5258

5359
// for China users
5460
export class Pushy {
55-
options: ClientOptions = {
56-
...defaultClientOptions,
57-
server: SERVER_PRESETS.pushy,
58-
};
59-
clientType: 'pushy' | 'cresc' = 'pushy';
61+
options = defaultClientOptions;
62+
clientType: 'Pushy' | 'Cresc' = 'Pushy';
6063
lastChecking?: number;
6164
lastRespJson?: Promise<any>;
6265

@@ -85,6 +88,8 @@ export class Pushy {
8588
throw new Error('appKey is required');
8689
}
8790
}
91+
this.clientType = new.target.name as 'Pushy' | 'Cresc';
92+
this.options.server = SERVER_PRESETS[this.clientType];
8893
this.setOptions(options);
8994
if (isRolledBack) {
9095
this.report({
@@ -150,6 +155,15 @@ export class Pushy {
150155
}
151156
return true;
152157
};
158+
assertDebug = () => {
159+
if (__DEV__ && !this.options.debug) {
160+
console.info(
161+
'You are currently in the development environment and have not enabled debug mode. The hot update check will not be performed. If you need to debug hot updates in the development environment, please set debug to true in the client.',
162+
);
163+
return false;
164+
}
165+
return true;
166+
};
153167
markSuccess = () => {
154168
if (Pushy.marked || __DEV__ || !isFirstTime) {
155169
return;
@@ -159,10 +173,7 @@ export class Pushy {
159173
this.report({ type: 'markSuccess' });
160174
};
161175
switchVersion = async (hash: string) => {
162-
if (__DEV__) {
163-
console.warn(
164-
'switchVersion() is not supported in development environment; no action taken.',
165-
);
176+
if (!assertDev('switchVersion()')) {
166177
return;
167178
}
168179
if (Pushy.assertHash(hash) && !Pushy.applyingUpdate) {
@@ -173,10 +184,7 @@ export class Pushy {
173184
};
174185

175186
switchVersionLater = async (hash: string) => {
176-
if (__DEV__) {
177-
console.warn(
178-
'switchVersionLater() is not supported in development environment; no action taken.',
179-
);
187+
if (!assertDev('switchVersionLater()')) {
180188
return;
181189
}
182190
if (Pushy.assertHash(hash)) {
@@ -185,14 +193,10 @@ export class Pushy {
185193
}
186194
};
187195
checkUpdate = async (extra?: Record<string, any>) => {
188-
if (__DEV__ && !this.options.debug) {
189-
console.info(
190-
'You are currently in the development environment and have not enabled debug mode. The hot update check will not be performed. If you need to debug hot updates in the development environment, please set debug to true in the client.',
191-
);
196+
if (!this.assertDebug()) {
192197
return;
193198
}
194-
if (Platform.OS === 'web') {
195-
console.warn('web platform does not support hot update check');
199+
if (!assertWeb()) {
196200
return;
197201
}
198202
if (
@@ -508,10 +512,4 @@ export class Pushy {
508512
}
509513

510514
// for international users
511-
export class Cresc extends Pushy {
512-
clientType: 'cresc' | 'pushy' = 'cresc';
513-
options: ClientOptions = {
514-
...defaultClientOptions,
515-
server: SERVER_PRESETS.cresc,
516-
};
517-
}
515+
export class Cresc extends Pushy {}

src/provider.tsx

+4-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ export const UpdateProvider = ({
2727
client: Pushy | Cresc;
2828
children: ReactNode;
2929
}) => {
30+
client = useRef(client).current;
3031
const { options } = client;
32+
3133
const stateListener = useRef<NativeEventSubscription>();
3234
const [updateInfo, setUpdateInfo] = useState<CheckResult>();
3335
const updateInfoRef = useRef(updateInfo);
@@ -239,10 +241,7 @@ export const UpdateProvider = ({
239241
const markSuccess = client.markSuccess;
240242

241243
useEffect(() => {
242-
if (__DEV__ && !options.debug) {
243-
console.info(
244-
'您当前处于开发环境且未启用debug,不会进行热更检查。如需在开发环境中调试热更,请在client中设置debug为true',
245-
);
244+
if (!client.assertDebug()) {
246245
return;
247246
}
248247
const { checkStrategy, dismissErrorAfter, autoMarkSuccess } = options;
@@ -272,7 +271,7 @@ export const UpdateProvider = ({
272271
stateListener.current && stateListener.current.remove();
273272
clearTimeout(dismissErrorTimer);
274273
};
275-
}, [checkUpdate, options, dismissError, markSuccess]);
274+
}, [checkUpdate, options, dismissError, markSuccess, client]);
276275

277276
const parseTestPayload = useCallback(
278277
(payload: UpdateTestPayload) => {

src/utils.ts

+20
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,23 @@ export const testUrls = async (urls?: string[]) => {
8484
log('all ping failed, use first url:', urls[0]);
8585
return urls[0];
8686
};
87+
88+
export const assertWeb = () => {
89+
if (Platform.OS === 'web') {
90+
console.warn(
91+
'react-native-update does not support the Web platform and will not perform any operations',
92+
);
93+
return false;
94+
}
95+
return true;
96+
};
97+
98+
export const assertDev = (matter: string) => {
99+
if (__DEV__) {
100+
console.warn(
101+
`${matter} is not supported in development environment; no action taken.`,
102+
);
103+
return false;
104+
}
105+
return true;
106+
};

0 commit comments

Comments
 (0)