Skip to content

Commit

Permalink
chore(llc): observable variables names
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandremgo committed Jun 20, 2022
1 parent 9fbd74c commit f20fa5f
Showing 1 changed file with 41 additions and 39 deletions.
80 changes: 41 additions & 39 deletions libs/ledger-live-common/src/hw/getOnboardingStatePolling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ export const getOnboardingStatePolling = ({
}): Observable<OnboardingStatePollingResult> => {
let firstRun = true;

const delayedOnboardingStateOnce$: Observable<OnboardingStatePollingResult> =
const delayedOnceOnboardingStateObservable: Observable<OnboardingStatePollingResult> =
new Observable((subscriber) => {
const delayMs = firstRun ? 0 : pollingPeriodMs;
firstRun = false;

const getOnboardingStateOnce = () => {
const firmwareInfoOrAllowedError$ = withDevice(deviceId)((t) =>
const firmwareInfoOrAllowedErrorObservable = withDevice(deviceId)((t) =>
from(getVersion(t))
).pipe(
timeout(fetchingTimeoutMs), // Throws a TimeoutError
Expand All @@ -69,52 +69,54 @@ export const getOnboardingStatePolling = ({

// If an error is catched previously, and this error is "allowed",
// the value from the observable is not a FirmwareInfo but an Error
const [firmwareInfo$, allowedError$] = partition(
firmwareInfoOrAllowedError$,
const [firmwareInfoObservable, allowedErrorObservable] = partition(
firmwareInfoOrAllowedErrorObservable,
// TS cannot infer correctly the value given to RxJS partition
(value: any) => Boolean(value?.flags)
);

const onboardingStateFromFirmwareInfo$ = firmwareInfo$.pipe(
map((firmwareInfo: FirmwareInfo) => {
let onboardingState: OnboardingState | null = null;
const onboardingStateFromFirmwareInfoObservable =
firmwareInfoObservable.pipe(
map((firmwareInfo: FirmwareInfo) => {
let onboardingState: OnboardingState | null = null;

try {
onboardingState = extractOnboardingState(firmwareInfo.flags);
} catch (error: any) {
if (error instanceof DeviceExtractOnboardingStateError) {
return {
onboardingState: null,
allowedError: error,
};
} else {
return {
onboardingState: null,
allowedError: new DeviceOnboardingStatePollingError(
`SyncOnboarding: Unknown error while extracting the onboarding state ${
error?.name ?? error
} ${error?.message}`
),
};
try {
onboardingState = extractOnboardingState(firmwareInfo.flags);
} catch (error: any) {
if (error instanceof DeviceExtractOnboardingStateError) {
return {
onboardingState: null,
allowedError: error,
};
} else {
return {
onboardingState: null,
allowedError: new DeviceOnboardingStatePollingError(
`SyncOnboarding: Unknown error while extracting the onboarding state ${
error?.name ?? error
} ${error?.message}`
),
};
}
}
}
return { onboardingState, allowedError: null };
})
);
return { onboardingState, allowedError: null };
})
);

// Handles the case of an (allowed) Error value
const onboardingStateFromAllowedError$ = allowedError$.pipe(
map((allowedError: Error) => {
return {
onboardingState: null,
allowedError: allowedError,
};
})
);
const onboardingStateFromAllowedErrorObservable =
allowedErrorObservable.pipe(
map((allowedError: Error) => {
return {
onboardingState: null,
allowedError: allowedError,
};
})
);

return merge(
onboardingStateFromFirmwareInfo$,
onboardingStateFromAllowedError$
onboardingStateFromFirmwareInfoObservable,
onboardingStateFromAllowedErrorObservable
);
};

Expand All @@ -132,7 +134,7 @@ export const getOnboardingStatePolling = ({
}, delayMs);
});

return delayedOnboardingStateOnce$.pipe(repeat());
return delayedOnceOnboardingStateObservable.pipe(repeat());
};

export const isAllowedOnboardingStatePollingError = (error: Error): boolean => {
Expand Down

0 comments on commit f20fa5f

Please sign in to comment.