Skip to content

Commit

Permalink
[DX-2954] refactor: game bridge status field (#1942)
Browse files Browse the repository at this point in the history
  • Loading branch information
nattb8 authored Jun 26, 2024
1 parent 1782970 commit b2df4eb
Showing 1 changed file with 38 additions and 41 deletions.
79 changes: 38 additions & 41 deletions packages/game-bridge/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const PASSPORT_FUNCTIONS = {
const initRequest = 'init';
const initRequestId = '1';

let passportClient: passport.Passport;
let passportClient: passport.Passport | null;
let providerInstance: provider.IMXProvider | null;
let zkEvmProviderInstance: passport.Provider | null;

Expand Down Expand Up @@ -102,7 +102,7 @@ const callbackToGame = (data: object) => {
};

const setProvider = (
passportProvider: provider.IMXProvider | null,
passportProvider: provider.IMXProvider | null | undefined,
): boolean => {
if (passportProvider !== null && passportProvider !== undefined) {
providerInstance = passportProvider;
Expand All @@ -120,7 +120,7 @@ const getProvider = (): provider.IMXProvider => {
return providerInstance;
};

const setZkEvmProvider = (zkEvmProvider: passport.Provider | null): boolean => {
const setZkEvmProvider = (zkEvmProvider: passport.Provider | null | undefined): boolean => {
if (zkEvmProvider !== null && zkEvmProvider !== undefined) {
zkEvmProviderInstance = zkEvmProvider;
console.log('zkEvm provider set');
Expand All @@ -141,6 +141,9 @@ track(moduleName, 'loadedGameBridge', {
sdkVersionTag,
});

// 'status' is set to true if:
// 1. the function executed successfully without throwing an error
// 2. the response object we expect to return to the game engine is there
window.callFunction = async (jsonData: string) => {
// eslint-disable-line no-unused-vars
console.log(`Call function ${jsonData}`);
Expand Down Expand Up @@ -213,11 +216,11 @@ window.callFunction = async (jsonData: string) => {
callbackToGame({
responseFor: fxName,
requestId,
success: true,
code: response.code,
deviceCode: response.deviceCode,
url: response.url,
interval: response.interval,
success: response !== undefined && response !== null,
code: response?.code,
deviceCode: response?.deviceCode,
url: response?.url,
interval: response?.interval,
});
break;
}
Expand All @@ -236,8 +239,7 @@ window.callFunction = async (jsonData: string) => {
callbackToGame({
responseFor: fxName,
requestId,
success: true,
result: userInfo !== null,
success: userInfo !== null,
});
break;
}
Expand All @@ -258,8 +260,7 @@ window.callFunction = async (jsonData: string) => {
callbackToGame({
responseFor: fxName,
requestId,
success: true,
result: providerSet,
success: providerSet,
});
break;
}
Expand All @@ -271,7 +272,7 @@ window.callFunction = async (jsonData: string) => {
callbackToGame({
responseFor: fxName,
requestId,
success: true,
success: response !== undefined && response !== null,
result: response,
});
break;
Expand All @@ -282,7 +283,7 @@ window.callFunction = async (jsonData: string) => {
request.authorizationCode,
request.state,
);
identify({ passportId: profile.sub });
identify({ passportId: profile?.sub });
track(moduleName, 'performedLoginPkce', {
timeMs: Date.now() - markStart,
});
Expand All @@ -302,7 +303,7 @@ window.callFunction = async (jsonData: string) => {
const passportProvider = await passportClient?.connectImx();
const providerSet = setProvider(passportProvider);
if (providerSet) {
identify({ passportId: profile.sub });
identify({ passportId: profile?.sub });
}
track(moduleName, 'performedConnectPkce', {
succeeded: providerSet,
Expand All @@ -311,8 +312,7 @@ window.callFunction = async (jsonData: string) => {
callbackToGame({
responseFor: fxName,
requestId,
success: true,
result: providerSet,
success: providerSet,
});
break;
}
Expand All @@ -324,7 +324,7 @@ window.callFunction = async (jsonData: string) => {
request.timeoutMs ?? null,
);

identify({ passportId: profile.sub });
identify({ passportId: profile?.sub });
track(moduleName, 'performedLoginConfirmCode', {
timeMs: Date.now() - markStart,
});
Expand All @@ -348,7 +348,7 @@ window.callFunction = async (jsonData: string) => {
const providerSet = setProvider(passportProvider);

if (providerSet) {
identify({ passportId: profile.sub });
identify({ passportId: profile?.sub });
}
track(moduleName, 'performedConnectConfirmCode', {
succeeded: providerSet,
Expand All @@ -358,8 +358,7 @@ window.callFunction = async (jsonData: string) => {
callbackToGame({
responseFor: fxName,
requestId,
success: true,
result: providerSet,
success: providerSet,
});
break;
}
Expand All @@ -373,7 +372,7 @@ window.callFunction = async (jsonData: string) => {
callbackToGame({
responseFor: fxName,
requestId,
success: true,
success: deviceFlowEndSessionEndpoint !== undefined && deviceFlowEndSessionEndpoint !== null,
result: deviceFlowEndSessionEndpoint,
});
break;
Expand All @@ -386,7 +385,7 @@ window.callFunction = async (jsonData: string) => {
callbackToGame({
responseFor: fxName,
requestId,
success: true,
success: accessToken !== undefined && accessToken !== null,
result: accessToken,
});
break;
Expand All @@ -399,7 +398,7 @@ window.callFunction = async (jsonData: string) => {
callbackToGame({
responseFor: fxName,
requestId,
success: true,
success: idToken !== undefined && idToken !== null,
result: idToken,
});
break;
Expand All @@ -412,7 +411,7 @@ window.callFunction = async (jsonData: string) => {
callbackToGame({
responseFor: fxName,
requestId,
success: true,
success: userProfile?.email !== undefined && userProfile?.email !== null,
result: userProfile?.email,
});
break;
Expand All @@ -425,7 +424,7 @@ window.callFunction = async (jsonData: string) => {
callbackToGame({
responseFor: fxName,
requestId,
success: true,
success: userProfile?.sub !== undefined && userProfile?.sub !== null,
result: userProfile?.sub,
});
break;
Expand All @@ -438,7 +437,7 @@ window.callFunction = async (jsonData: string) => {
callbackToGame({
responseFor: fxName,
requestId,
success: true,
success: linkedAddresses !== undefined && linkedAddresses !== null,
result: linkedAddresses,
});
break;
Expand All @@ -451,7 +450,7 @@ window.callFunction = async (jsonData: string) => {
callbackToGame({
responseFor: fxName,
requestId,
success: true,
success: address !== undefined && address !== null,
result: address,
});
break;
Expand All @@ -478,7 +477,7 @@ window.callFunction = async (jsonData: string) => {
...{
responseFor: fxName,
requestId,
success: true,
success: response !== null && response !== undefined,
},
...response,
});
Expand All @@ -494,7 +493,7 @@ window.callFunction = async (jsonData: string) => {
...{
responseFor: fxName,
requestId,
success: true,
success: response !== null && response !== undefined,
},
...response,
});
Expand All @@ -512,8 +511,7 @@ window.callFunction = async (jsonData: string) => {
...{
responseFor: fxName,
requestId,
success: true,
result: response,
success: response !== null && response !== undefined,
},
...response,
});
Expand All @@ -529,8 +527,7 @@ window.callFunction = async (jsonData: string) => {
callbackToGame({
responseFor: fxName,
requestId,
success: true,
result: providerSet,
success: providerSet,
});
break;
}
Expand All @@ -546,7 +543,7 @@ window.callFunction = async (jsonData: string) => {
callbackToGame({
responseFor: fxName,
requestId,
success: true,
success: transactionHash !== null && transactionHash !== undefined,
result: transactionHash,
});
break;
Expand All @@ -566,7 +563,7 @@ window.callFunction = async (jsonData: string) => {
...{
responseFor: fxName,
requestId,
success: true,
success: response !== null && response !== undefined,
},
...response,
});
Expand All @@ -582,7 +579,7 @@ window.callFunction = async (jsonData: string) => {
callbackToGame({
responseFor: fxName,
requestId,
success: true,
success: result !== null && result !== undefined,
accounts: result,
});
break;
Expand All @@ -599,7 +596,7 @@ window.callFunction = async (jsonData: string) => {
callbackToGame({
responseFor: fxName,
requestId,
success: true,
success: result !== null && result !== undefined,
result,
});
break;
Expand All @@ -617,7 +614,7 @@ window.callFunction = async (jsonData: string) => {
...{
responseFor: fxName,
requestId,
success: true,
success: response !== null && response !== undefined,
},
...response,
});
Expand Down Expand Up @@ -648,8 +645,8 @@ window.callFunction = async (jsonData: string) => {
responseFor: fxName,
requestId,
success: false,
error: error.message,
errorType: error instanceof passport.PassportError ? error.type : null,
error: error?.message !== null && error?.message !== undefined ? error.message : 'Error',
errorType: error instanceof passport.PassportError ? error?.type : null,
});
}
};
Expand Down

0 comments on commit b2df4eb

Please sign in to comment.