Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(deps): Use typed-redux-saga #3961

Merged
merged 19 commits into from
Jul 26, 2023
Merged

chore(deps): Use typed-redux-saga #3961

merged 19 commits into from
Jul 26, 2023

Conversation

jh2oman
Copy link
Contributor

@jh2oman jh2oman commented Jul 18, 2023

Description

Using the typed-redux-saga package to bring type checking to our sagas. This is in preparation for bumping our typescript version.

  • First I used this eslint rule which adds the * to the yields and fixes all of the imports
  • Then I went through and fixed all of the broken types we had (there were many 💀 )
  • Most of it is very small nits, but i tried to add comments to highlight the more interesting parts.

Test plan

Updated unit tests

Related issues

Preparing for https://linear.app/valora/issue/ACT-784/typed-contracts-with-typechain

@codecov
Copy link

codecov bot commented Jul 18, 2023

Codecov Report

Merging #3961 (2faea19) into main (4bf5dfd) will decrease coverage by 0.04%.
The diff coverage is 60.76%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3961      +/-   ##
==========================================
- Coverage   83.21%   83.18%   -0.04%     
==========================================
  Files         711      711              
  Lines       26232    26248      +16     
  Branches     3580     3584       +4     
==========================================
+ Hits        21830    21835       +5     
- Misses       4340     4352      +12     
+ Partials       62       61       -1     
Files Changed Coverage Δ
src/identity/selectors.ts 87.50% <0.00%> (ø)
src/dappkit/dappkit.ts 28.15% <5.26%> (-0.85%) ⬇️
src/identity/revoke.ts 29.62% <5.55%> (ø)
src/recipients/saga.ts 15.15% <7.69%> (ø)
src/redux/sagas.ts 49.50% <9.30%> (+1.98%) ⬆️
src/paymentRequest/saga.ts 26.96% <14.28%> (+0.52%) ⬆️
src/web3/contracts.ts 20.21% <26.66%> (ø)
src/exchange/saga.ts 51.85% <27.77%> (-0.98%) ⬇️
src/escrow/saga.ts 30.61% <28.00%> (+0.40%) ⬆️
src/firebase/saga.ts 37.64% <31.81%> (ø)
... and 42 more

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4bf5dfd...2faea19. Read the comment docs.

@jh2oman
Copy link
Contributor Author

jh2oman commented Jul 18, 2023

I think codecov is having trouble understanding the yield* generator format

src/app/saga.ts Outdated Show resolved Hide resolved
@@ -66,11 +66,7 @@ exports[`CircleButton renders correctly with minimum props 1`] = `
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M2.141,89.13c1.425,1.429,3.299,2.142,5.167,2.142c1.869,0,3.742-0.713,5.167-2.142l33.591-33.592L79.657,89.13
c1.426,1.429,3.299,2.142,5.167,2.142c1.867,0,3.74-0.713,5.167-2.142c2.854-2.854,2.854-7.48,0-10.334L56.398,45.205
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these three snaps are the same, just different indentations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious why these changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was this which isn't needed. They'll go back to the way they were :) https://github.com/valora-inc/wallet/pull/3961/files#r1270625689

return !dek ? null : hexToBuffer(dek)
const dek: string = yield* call(accountsWrapper.getDataEncryptionKey, accountAddress)
yield* put(updateAddressDekMap(accountAddress, dek || null))
return dek
}

export function* doFetchDataEncryptionKey(walletAddress: string) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function was previously returning null | string | Buffer. Various places that used it usually assumed it was either string or buffer. I updated it to always return null | string

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the dependencies of this assume non null? If so, do they need to be updated to handle nulls?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, the dependencies assumed it could be nulll

@silasbw
Copy link
Collaborator

silasbw commented Jul 19, 2023

Nice 🙌 !

Copy link
Contributor

@satish-ravi satish-ravi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks mostly good. Nice job!

Few things:

  • So with this PR we get typings for sagas, but lose code coverage because jest doesn't work with yield* right?
  • I started leaving comments on removing explicit types for yield* call, but seems like its removed only in a few places, so feel free to ignore them. But eventually I guess we can remove the explicit types since they all should be inferred now
  • Do we need to specify the type for yield* take always?
  • There are a few places where we do as unknown as type without any comments, are these the same as the bug you mentioned in some places?

babel.config.js Outdated
@@ -34,6 +34,7 @@ module.exports = {
'@babel/plugin-transform-flow-strip-types', // for a bug in FlatList caused by adding @babel/plugin-proposal-class-properties
'@babel/plugin-proposal-class-properties', // for ethers
'@babel/plugin-transform-private-methods', // for ethers
'macros',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a comment similar to other entries here on why this is required?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it needed?
I looked at https://github.com/celo-tools/celo-web-wallet/blob/master/.babelrc and https://github.com/Uniswap/wallet/blob/main/apps/mobile/babel.config.js they both use typed-redux-saga but I can't yet find this there as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems it may be unnecessary.
Not that both the projects listed above import like this:

import { call, put } from 'typed-redux-saga'

instead of

import { call, put } from 'typed-redux-saga/macro'

like we did in this PR.

Could we do the same?
Maybe this will help with the code coverage issue too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answering myself, after re-reading the README for https://github.com/agiledigital/typed-redux-saga 😄

This gives you all the benefits of strong types during development without the overhead induced by all the calls to typed-redux-saga's proxies.

Not entirely sure about the overhead of not using macros but it seems it would be quite lite.
If this helps with code coverage, I think we should probably favor not using macros.
What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without macros all of our redux-saga-test-plan tests were failing with various issues. I couldn't get it to work. See branch here: #3991

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh thanks, I found a way to make it work: #4002

Shown on a single test, but I think all tests should be able to work similarly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Thanks @jeanregisser , I'll try to get it working on this PR.

src/analytics/saga.ts Outdated Show resolved Hide resolved
src/app/saga.ts Outdated Show resolved Hide resolved
src/app/saga.ts Outdated Show resolved Hide resolved
src/app/saga.ts Show resolved Hide resolved
src/identity/privateHashing.ts Outdated Show resolved Hide resolved
src/networkInfo/saga.ts Outdated Show resolved Hide resolved
src/paymentRequest/saga.ts Outdated Show resolved Hide resolved
src/web3/contracts.ts Outdated Show resolved Hide resolved
return !dek ? null : hexToBuffer(dek)
const dek: string = yield* call(accountsWrapper.getDataEncryptionKey, accountAddress)
yield* put(updateAddressDekMap(accountAddress, dek || null))
return dek
}

export function* doFetchDataEncryptionKey(walletAddress: string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the dependencies of this assume non null? If so, do they need to be updated to handle nulls?

Copy link
Member

@jeanregisser jeanregisser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Epic work!!
Can't express my gratitude enough ❤️

Left a couple of small comments.
Looks like we can drop a few more types, but no big deal.

Too bad about the coverage changing. It seems it's caused by the automatic replacement of yield* via the macros.

Maybe this will get fixed with an upgrade to jest? I'm not too hopeful though.
Update: see #3961 (comment)

jest.config.js Outdated Show resolved Hide resolved
src/app/saga.ts Outdated Show resolved Hide resolved
src/app/saga.ts Outdated Show resolved Hide resolved
src/consumerIncentives/saga.ts Outdated Show resolved Hide resolved
src/consumerIncentives/saga.ts Outdated Show resolved Hide resolved
Comment on lines 121 to 122
case NotificationTypes.PAYMENT_RECEIVED:
yield call(handlePaymentReceived, message.data as unknown as TransferNotificationData)
handlePaymentReceived(message.data as unknown as TransferNotificationData)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why aren't we turning it to yield* call?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handlePaymentReceived is not an async function

src/identity/saga.ts Outdated Show resolved Hide resolved
src/identity/selectors.ts Show resolved Hide resolved
src/paymentRequest/saga.ts Outdated Show resolved Hide resolved
src/web3/saga.ts Outdated Show resolved Hide resolved
@jh2oman
Copy link
Contributor Author

jh2oman commented Jul 25, 2023

@satish-ravi

So with this PR we get typings for sagas, but lose code coverage because jest doesn't work with yield* right?

correct

Do we need to specify the type for yield* take always?

No, only for channels that don't specify a type. Some, like the WalletConnectActions channel do that.

There are a few places where we do as unknown as type without any comments, are these the same as the bug you mentioned in some places?

most of them are. I've updated with more comments

Copy link
Contributor

@satish-ravi satish-ravi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@jh2oman jh2oman merged commit e1a43de into main Jul 26, 2023
14 of 15 checks passed
@jh2oman jh2oman deleted the typed-redux-saga branch July 26, 2023 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants