1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee

parseTestPayload

This commit is contained in:
sunnylqm 2024-07-28 21:26:21 +08:00
parent a77c3c85f3
commit 4f80f96a8d
No known key found for this signature in database
3 changed files with 40 additions and 11 deletions

View File

@ -14,7 +14,7 @@ export const defaultContext = {
downloadUpdate: asyncNoop, downloadUpdate: asyncNoop,
downloadAndInstallApk: asyncNoop, downloadAndInstallApk: asyncNoop,
getCurrentVersionInfo: () => Promise.resolve({}), getCurrentVersionInfo: () => Promise.resolve({}),
parseTestPayload: () => Promise.resolve(false), parseTestQrCode: () => false,
currentHash: '', currentHash: '',
packageVersion: '', packageVersion: '',
}; };
@ -32,7 +32,7 @@ export const PushyContext = createContext<{
description?: string; description?: string;
metaInfo?: string; metaInfo?: string;
}>; }>;
parseTestPayload: (code: string) => Promise<boolean>; parseTestQrCode: (code: string) => boolean;
currentHash: string; currentHash: string;
packageVersion: string; packageVersion: string;
client?: Pushy; client?: Pushy;

View File

@ -263,13 +263,7 @@ export const PushyProvider = ({
}, [checkUpdate, options, dismissError, markSuccess]); }, [checkUpdate, options, dismissError, markSuccess]);
const parseTestPayload = useCallback( const parseTestPayload = useCallback(
async (code: string) => { (payload: PushyTestPayload) => {
let payload: PushyTestPayload;
try {
payload = JSON.parse(code);
} catch {
return false;
}
if (payload && payload.type && payload.type.startsWith('__rnPushy')) { if (payload && payload.type && payload.type.startsWith('__rnPushy')) {
const logger = options.logger || (() => {}); const logger = options.logger || (() => {});
options.logger = ({ type, data }) => { options.logger = ({ type, data }) => {
@ -294,6 +288,41 @@ export const PushyProvider = ({
[checkUpdate, options], [checkUpdate, options],
); );
const parseTestQrCode = useCallback(
(code: string) => {
let payload: PushyTestPayload;
try {
payload = JSON.parse(code);
return parseTestPayload(payload);
} catch {
return false;
}
},
[parseTestPayload],
);
useEffect(() => {
const parseLinking = (url: string | null) => {
if (!url) {
return;
}
const params = new URLSearchParams(url);
const payload = {
type: params.get('type'),
data: params.get('data'),
};
parseTestPayload(payload);
};
Linking.getInitialURL().then(parseLinking);
const linkingListener = Linking.addEventListener('url', ({ url }) =>
parseLinking(url),
);
return () => {
linkingListener.remove();
};
}, [parseTestPayload]);
return ( return (
<PushyContext.Provider <PushyContext.Provider
value={{ value={{
@ -311,7 +340,7 @@ export const PushyProvider = ({
progress, progress,
downloadAndInstallApk, downloadAndInstallApk,
getCurrentVersionInfo, getCurrentVersionInfo,
parseTestPayload, parseTestQrCode,
}}> }}>
{children} {children}
</PushyContext.Provider> </PushyContext.Provider>

View File

@ -83,6 +83,6 @@ export interface PushyOptions {
} }
export interface PushyTestPayload { export interface PushyTestPayload {
type: '__rnPushyVersionHash'; type: '__rnPushyVersionHash' | string | null;
data: any; data: any;
} }