Skip to content

Commit

Permalink
Upgrade SWR (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpierre authored Aug 24, 2021
1 parent e136d48 commit 8ea3d94
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 40 deletions.
13 changes: 8 additions & 5 deletions examples/ethereum/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7062,14 +7062,14 @@ fsevents@~2.3.2:
languageName: node
linkType: hard

"swr@npm:^1.0.0-beta.10":
version: 1.0.0-beta.10
resolution: "swr@npm:1.0.0-beta.10"
"swr@npm:1.0.0-beta.13":
version: 1.0.0-beta.13
resolution: "swr@npm:1.0.0-beta.13"
dependencies:
dequal: 2.0.2
peerDependencies:
react: ^16.11.0 || ^17.0.0
checksum: 6895e0bc2d849cfb805857fa1f8dbb792136b9bb5177e401f21b0e96e41b2e427de289ee820d4d75850b500e50d8695d89985cf8ea164a5db0e38f45e57bffb8
checksum: fb552f2d4f4d0e15ee981ffab081ac8cc04a3684c5f60e90ec95949cd92b7006fc0ab8cfd36e2c1d1cf2892623d86753d7c2107d817148b71f044b370e718d0c
languageName: node
linkType: hard

Expand Down Expand Up @@ -7352,11 +7352,14 @@ typescript@^4.3.2:
version: 0.0.0-use.local
resolution: "use-nft@portal:../..::locator=example-ethereum%40workspace%3A."
dependencies:
swr: ^1.0.0-beta.10
swr: 1.0.0-beta.13
peerDependencies:
"@ethersproject/contracts": ">=5.2.0 <6"
ethers: ">=5.2.0 <6"
react: ">=17"
peerDependenciesMeta:
"@ethersproject/contracts":
optional: true
ethers:
optional: true
languageName: node
Expand Down
10 changes: 5 additions & 5 deletions examples/ethers/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6795,14 +6795,14 @@ resolve@~1.17.0:
languageName: node
linkType: hard

"swr@npm:^1.0.0-beta.10":
version: 1.0.0-beta.10
resolution: "swr@npm:1.0.0-beta.10"
"swr@npm:1.0.0-beta.13":
version: 1.0.0-beta.13
resolution: "swr@npm:1.0.0-beta.13"
dependencies:
dequal: 2.0.2
peerDependencies:
react: ^16.11.0 || ^17.0.0
checksum: 6895e0bc2d849cfb805857fa1f8dbb792136b9bb5177e401f21b0e96e41b2e427de289ee820d4d75850b500e50d8695d89985cf8ea164a5db0e38f45e57bffb8
checksum: fb552f2d4f4d0e15ee981ffab081ac8cc04a3684c5f60e90ec95949cd92b7006fc0ab8cfd36e2c1d1cf2892623d86753d7c2107d817148b71f044b370e718d0c
languageName: node
linkType: hard

Expand Down Expand Up @@ -7067,7 +7067,7 @@ typescript@^4.3.2:
version: 0.0.0-use.local
resolution: "use-nft@portal:../..::locator=example-ethers%40workspace%3A."
dependencies:
swr: ^1.0.0-beta.10
swr: 1.0.0-beta.13
peerDependencies:
"@ethersproject/contracts": ">=5.2.0 <6"
ethers: ">=5.2.0 <6"
Expand Down
17 changes: 12 additions & 5 deletions examples/fetchWrapper/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9287,14 +9287,14 @@ resolve@^2.0.0-next.3:
languageName: node
linkType: hard

"swr@npm:^1.0.0-beta.10":
version: 1.0.0-beta.10
resolution: "swr@npm:1.0.0-beta.10"
"swr@npm:1.0.0-beta.13":
version: 1.0.0-beta.13
resolution: "swr@npm:1.0.0-beta.13"
dependencies:
dequal: 2.0.2
peerDependencies:
react: ^16.11.0 || ^17.0.0
checksum: 6895e0bc2d849cfb805857fa1f8dbb792136b9bb5177e401f21b0e96e41b2e427de289ee820d4d75850b500e50d8695d89985cf8ea164a5db0e38f45e57bffb8
checksum: fb552f2d4f4d0e15ee981ffab081ac8cc04a3684c5f60e90ec95949cd92b7006fc0ab8cfd36e2c1d1cf2892623d86753d7c2107d817148b71f044b370e718d0c
languageName: node
linkType: hard

Expand Down Expand Up @@ -9740,9 +9740,16 @@ resolve@^2.0.0-next.3:
version: 0.0.0-use.local
resolution: "use-nft@portal:../..::locator=fetchwrapper%40workspace%3A."
dependencies:
swr: ^1.0.0-beta.10
swr: 1.0.0-beta.13
peerDependencies:
"@ethersproject/contracts": ">=5.2.0 <6"
ethers: ">=5.2.0 <6"
react: ">=17"
peerDependenciesMeta:
"@ethersproject/contracts":
optional: true
ethers:
optional: true
languageName: node
linkType: soft

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"coverage": "npx live-server coverage/lcov-report"
},
"dependencies": {
"swr": "^1.0.0-beta.10"
"swr": "1.0.0-beta.13"
},
"devDependencies": {
"@ethersproject/contracts": "^5.4.1",
Expand Down
22 changes: 8 additions & 14 deletions src/core.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type { EthersFetcherConfig } from "./fetchers/ethers/types"
import type { EthereumFetcherConfigDeclaration } from "./fetchers/ethereum/types"

import React, { createContext, useCallback, useContext, useMemo } from "react"
import useSWR, { SWRConfig, createCache } from "swr"
import useSWR, { useSWRConfig, SWRConfig } from "swr"
import ethersFetcher from "./fetchers/ethers"
import ethereumFetcher from "./fetchers/ethereum"
import { identity, ipfsUrlDefault } from "./utils"
Expand Down Expand Up @@ -55,17 +55,16 @@ function normalizeFetcher(fetcher: FetcherProp): Fetcher<unknown> {

// ethereum
if (isFetcherDeclarationEthereum(fetcher)) {
return ethereumFetcher(fetcher[1]) as Fetcher<
EthereumFetcherConfigDeclaration
>
return ethereumFetcher(
fetcher[1]
) as Fetcher<EthereumFetcherConfigDeclaration>
}

// custom fetcher (or wrong value)
return fetcher
}

const NftContext = createContext<{
cacheStorage: Map<string, unknown>
fetcher: Fetcher<unknown> | null
imageProxy: ImageProxyFn
ipfsUrl: IpfsUrlFn
Expand All @@ -89,21 +88,15 @@ const NftProvider: FC<{
throw new Error("Please set the fetcher prop on <NftProvider />")
}

const [cacheStorage, { cache: swrCache }] = useMemo(() => {
const cache = new Map()
return [cache, createCache(cache)]
}, [])

const context = {
cacheStorage,
fetcher: normalizeFetcher(fetcher),
imageProxy,
ipfsUrl,
jsonProxy,
}

return (
<SWRConfig value={{ cache: swrCache }}>
<SWRConfig value={{ provider: () => new Map() }}>
<NftContext.Provider value={context}>{children}</NftContext.Provider>
</SWRConfig>
)
Expand All @@ -115,7 +108,7 @@ function useNft(contractAddress: Address, tokenId: string): NftResult {
throw new Error("Please wrap your app with <NftProvider />")
}

const { cacheStorage, fetcher, imageProxy, ipfsUrl, jsonProxy } = context
const { fetcher, imageProxy, ipfsUrl, jsonProxy } = context
const fetchContext = useMemo<FetchContext>(
() => ({ imageProxy, ipfsUrl, jsonProxy }),
[imageProxy, ipfsUrl, jsonProxy]
Expand All @@ -127,7 +120,8 @@ function useNft(contractAddress: Address, tokenId: string): NftResult {
: { ...NFT_METADATA_DEFAULT }
}, [contractAddress, fetcher, fetchContext, tokenId])

const cached = cacheStorage.has(contractAddress + tokenId)
const { cache } = useSWRConfig()
const cached = cache.has(contractAddress + tokenId)

const result = useSWR<NftMetadata, Error>(
contractAddress + tokenId,
Expand Down
10 changes: 5 additions & 5 deletions website/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2387,14 +2387,14 @@ resolve@^1.20.0:
languageName: node
linkType: hard

"swr@npm:^1.0.0-beta.10":
version: 1.0.0-beta.10
resolution: "swr@npm:1.0.0-beta.10"
"swr@npm:1.0.0-beta.13":
version: 1.0.0-beta.13
resolution: "swr@npm:1.0.0-beta.13"
dependencies:
dequal: 2.0.2
peerDependencies:
react: ^16.11.0 || ^17.0.0
checksum: 6895e0bc2d849cfb805857fa1f8dbb792136b9bb5177e401f21b0e96e41b2e427de289ee820d4d75850b500e50d8695d89985cf8ea164a5db0e38f45e57bffb8
checksum: fb552f2d4f4d0e15ee981ffab081ac8cc04a3684c5f60e90ec95949cd92b7006fc0ab8cfd36e2c1d1cf2892623d86753d7c2107d817148b71f044b370e718d0c
languageName: node
linkType: hard

Expand Down Expand Up @@ -2497,7 +2497,7 @@ typescript@^4.3.5:
version: 0.0.0-use.local
resolution: "use-nft@portal:..::locator=use-nft-website%40workspace%3A."
dependencies:
swr: ^1.0.0-beta.10
swr: 1.0.0-beta.13
peerDependencies:
"@ethersproject/contracts": ">=5.2.0 <6"
ethers: ">=5.2.0 <6"
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15331,14 +15331,14 @@ resolve@^2.0.0-next.3:
languageName: node
linkType: hard

"swr@npm:^1.0.0-beta.10":
version: 1.0.0-beta.10
resolution: "swr@npm:1.0.0-beta.10"
"swr@npm:1.0.0-beta.13":
version: 1.0.0-beta.13
resolution: "swr@npm:1.0.0-beta.13"
dependencies:
dequal: 2.0.2
peerDependencies:
react: ^16.11.0 || ^17.0.0
checksum: 6895e0bc2d849cfb805857fa1f8dbb792136b9bb5177e401f21b0e96e41b2e427de289ee820d4d75850b500e50d8695d89985cf8ea164a5db0e38f45e57bffb8
checksum: fb552f2d4f4d0e15ee981ffab081ac8cc04a3684c5f60e90ec95949cd92b7006fc0ab8cfd36e2c1d1cf2892623d86753d7c2107d817148b71f044b370e718d0c
languageName: node
linkType: hard

Expand Down Expand Up @@ -16215,7 +16215,7 @@ typescript@^4.3.5:
react: ^17.0.2
react-dom: ^17.0.2
rollpkg: ^0.5.7
swr: ^1.0.0-beta.10
swr: 1.0.0-beta.13
ts-jest: ^27.0.4
typescript: ^4.3.5
peerDependencies:
Expand Down

0 comments on commit 8ea3d94

Please sign in to comment.