Skip to content

Commit

Permalink
Allow resetting nftCollection to shortcut polling (coral-xyz#1960)
Browse files Browse the repository at this point in the history
  • Loading branch information
Greenish authored Dec 25, 2022
1 parent 2b3b13d commit d31e6a1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
3 changes: 1 addition & 2 deletions packages/app-extension/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { lazy, Suspense } from "react";
import ReactDOM from "react-dom";
import { openPopupWindow } from "@coral-xyz/common/dist/esm/browser";
import { BACKPACK_FEATURE_POP_MODE } from "@coral-xyz/common/dist/esm/generated-config";
import { Loading } from "@coral-xyz/react-common/dist/esm/components/base/Loading";

import "./index.css";

Expand Down Expand Up @@ -35,7 +34,7 @@ document.addEventListener("keypress", async function onPress(event) {
//
ReactDOM.render(
<React.StrictMode>
<Suspense fallback={<Loading />}>
<Suspense fallback={null}>
<App />
</Suspense>
<Suspense fallback={null}>
Expand Down
17 changes: 14 additions & 3 deletions packages/recoil/src/atoms/nft.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,14 @@ const nftMetadata = selector<Map<string, Nft>>({
* Poll local NFT data and create list of all as source for nftById(s) atoms
* This atom breaks to global app rerender due to blockchain data polling.
*/
const nftCollections = atom<{
export const nftCollections = atom<{
[Blockchain.SOLANA]: NftCollection[] | null;
[Blockchain.ETHEREUM]: NftCollection[] | null;
}>({
key: "nftCollections",
effects: [
({ setSelf, getPromise }) => {
({ setSelf, onSet, getPromise }) => {
let timeout;
const pollLocalData = async (isInitial?: boolean) => {
try {
const [solana, ethereum, currentValue] = await Promise.all([
Expand All @@ -122,13 +123,23 @@ const nftCollections = atom<{
// ensure polling continues even on error.
console.error(e);
}
setTimeout(() => requestAnimationFrame(() => pollLocalData()), 1000);
timeout = setTimeout(
() => requestAnimationFrame(() => pollLocalData()),
1000
);
};
setSelf({
[Blockchain.SOLANA]: null,
[Blockchain.ETHEREUM]: null,
});
pollLocalData(true);
onSet(() => {
clearTimeout(timeout);
pollLocalData();
});
return () => {
clearTimeout(timeout);
};
},
],
});
15 changes: 10 additions & 5 deletions packages/recoil/src/context/Notifications.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import React, { useEffect } from "react";
import type {
Blockchain,
FEATURE_GATES_MAP,
Notification,
} from "@coral-xyz/common";
import type { FEATURE_GATES_MAP, Notification } from "@coral-xyz/common";
import {
BackgroundSolanaConnection,
Blockchain,
CHANNEL_POPUP_NOTIFICATIONS,
ChannelAppUi,
getLogger,
Expand Down Expand Up @@ -82,6 +79,13 @@ export function NotificationsProvider(props: any) {
const setKeyringStoreState = useSetRecoilState(atoms.keyringStoreState);
const setActiveUser = useSetRecoilState(atoms.user);
const resetAllUsers = useResetRecoilState(atoms.allUsers);
const _setNftCollections = useSetRecoilState(atoms.nftCollections);
const resetNftCollections = () => {
_setNftCollections({
[Blockchain.SOLANA]: null,
[Blockchain.ETHEREUM]: null,
});
};
// Preferences.
const setPreferences = useSetRecoilState(atoms.preferences);
const setFeatureGates = useSetRecoilState(atoms.featureGates);
Expand Down Expand Up @@ -570,6 +574,7 @@ export function NotificationsProvider(props: any) {
setWalletData(notif.data.walletData);
setActiveUser(notif.data.user);
resetAllUsers();
resetNftCollections();
};

const handleRemovedUser = (notif: Notification) => {
Expand Down

0 comments on commit d31e6a1

Please sign in to comment.