Skip to content

Commit

Permalink
parseTestPayload
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnylqm committed Jul 28, 2024
1 parent a77c3c8 commit 4f80f96
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const defaultContext = {
downloadUpdate: asyncNoop,
downloadAndInstallApk: asyncNoop,
getCurrentVersionInfo: () => Promise.resolve({}),
parseTestPayload: () => Promise.resolve(false),
parseTestQrCode: () => false,
currentHash: '',
packageVersion: '',
};
Expand All @@ -32,7 +32,7 @@ export const PushyContext = createContext<{
description?: string;
metaInfo?: string;
}>;
parseTestPayload: (code: string) => Promise<boolean>;
parseTestQrCode: (code: string) => boolean;
currentHash: string;
packageVersion: string;
client?: Pushy;
Expand Down
45 changes: 37 additions & 8 deletions src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,7 @@ export const PushyProvider = ({
}, [checkUpdate, options, dismissError, markSuccess]);

const parseTestPayload = useCallback(
async (code: string) => {
let payload: PushyTestPayload;
try {
payload = JSON.parse(code);
} catch {
return false;
}
(payload: PushyTestPayload) => {
if (payload && payload.type && payload.type.startsWith('__rnPushy')) {
const logger = options.logger || (() => {});
options.logger = ({ type, data }) => {
Expand All @@ -294,6 +288,41 @@ export const PushyProvider = ({
[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 (
<PushyContext.Provider
value={{
Expand All @@ -311,7 +340,7 @@ export const PushyProvider = ({
progress,
downloadAndInstallApk,
getCurrentVersionInfo,
parseTestPayload,
parseTestQrCode,
}}>
{children}
</PushyContext.Provider>
Expand Down
2 changes: 1 addition & 1 deletion src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ export interface PushyOptions {
}

export interface PushyTestPayload {
type: '__rnPushyVersionHash';
type: '__rnPushyVersionHash' | string | null;
data: any;
}

0 comments on commit 4f80f96

Please sign in to comment.