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

feat: andrel - 185 general refactors #187

Merged
merged 14 commits into from
Mar 17, 2023

Conversation

andrehadianto
Copy link
Contributor

@andrehadianto andrehadianto commented Mar 15, 2023

Description

This PR handles:

  • add cache control to getDropInformation with 5 seconds validity, store drop info and timestamp in local storage, compare timestamp and revalidate
  • Add comments to Buttons component to improve readability

Fixes #185

How to test

Buttons storybook

  1. yarn run storybook
  2. Go to localhost:3000

Add cache control:

  1. Go to AllDrops.
  2. Select any key in page 1, and go back to My Drops using the navigation button (to prevent reloading)

Screenshots/Screen Recording of Implementation

See comments

@andrehadianto andrehadianto self-assigned this Mar 15, 2023
@andrehadianto
Copy link
Contributor Author

Screen.Recording.2023-03-15.at.1.58.34.PM.mov
re-fetching getDrops

is only called when we run await getDrops(...)

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Mar 15, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 58e68c8
Status: ✅  Deploy successful!
Preview URL: https://7b51cb07.keypom-airfoil.pages.dev
Branch Preview URL: https://andrel-185-general-refactors.keypom-airfoil.pages.dev

View logs

@andrehadianto andrehadianto changed the title Andrel/185 general refactors feat: andrel - 185 general refactors Mar 15, 2023
@andrehadianto
Copy link
Contributor Author

image

storybook for buttons

@andrehadianto andrehadianto requested a review from tzeweiwee March 15, 2023 07:28
@andrehadianto andrehadianto marked this pull request as ready for review March 15, 2023 07:29
Copy link
Contributor

@tzeweiwee tzeweiwee left a comment

Choose a reason for hiding this comment

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

Looks good overall but I'd like to suggest the drop store for the caching mechanism. Let me know your thoughts!

src/lib/keypom.ts Outdated Show resolved Hide resolved
@@ -66,6 +66,16 @@ class KeypomJS {
this.nearConnection = await connect(connectionConfig);
};

dropInformationMeta: {
Copy link
Contributor

Choose a reason for hiding this comment

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

this object should be placed above constructor for consistency

Copy link
Contributor

Choose a reason for hiding this comment

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

Currently, we are only storing all drops. I think we should also store drop information (getDropInformation).

Copy link
Contributor

@tzeweiwee tzeweiwee Mar 15, 2023

Choose a reason for hiding this comment

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

The current logic works, but I wanna extend the caching mechanism to Drop Manager as well. In addition, if we're caching the data here, I suggest we create something like a Redux store here to handle 2 scenarios.

  1. get all drops, after rpc calling getDrops, we populate the ids OR only store properties required by AllDrops table (drop_id, metadata, next_key_id) in dropStore.paginatedDrops. Set expiry time for getDrops.

  2. user clicks on a drop, in Drop Manager, page component will get the drop from the store

Suggestions:

// drop store structure
const dropStore = {
  paginatedDrops: {
    // pageIndex => array of dropIds or partial drop object
    1: ['dropid-1', 'dropid-2', ...],
    2: ['dropid-11', 'dropid-12', ...],
  },
  drops: {
    // dropId => dropData
    'dropid-1': {
      rop_id: 'dropid-1',
      config: {},
      ...
    },
    'dropid-2': {
      drop_id: 'dropid-2',
      config: {},
      ...
    },
  },
  getDropsExpiryTime: 16334382,
};

// in getDrops()
// setting cache expiry time for getDrops
const CACHE_MAX_AGE = 5000; // ms
const newGetDropsExpiryTime = new Date(new Date().getTime() + CACHE_MAX_AGE).getTime();

// comparing current time and expiry time
if (Date.now() > getDropsExpiryTime) {
  // getDropsExpired
  // reset store
  // fetch the drops again
}

what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also i prefer the expiry/max-age approach/terminologies, just referencing Cache control mechanism in http headers

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agree with the terminologies. will make some changes to this. However, i'd stick with Date.now() compared to new Date().getTime() for performance reason 👍

src/lib/keypom.ts Outdated Show resolved Hide resolved
src/App.tsx Show resolved Hide resolved
this.dropStore.paginatedDrops[dropId] = {
...(await this.getDropInfo({ dropId })),
getPageIndex: pageIndex,
getExpiryTime: newGetPaginatedDropsExpiryTime,
Copy link
Contributor

Choose a reason for hiding this comment

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

sorry my bad, can we remove get from the variable names. getXX sounds like a function name

const newGetDropsExpiryTime = new Date(Date.now() + CACHE_MAX_AGE).getTime();
this.dropStore.getDropsExpiryTime = newGetDropsExpiryTime;

this.dropStore.getDropsLastPage = start;
Copy link
Contributor

Choose a reason for hiding this comment

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

getDropsLastPage a bit confused by this, is it referencing the current last item index?

@@ -190,7 +207,21 @@ class KeypomJS {
return null;
};

getDrops = async ({ accountId, start, limit }) => await getDrops({ accountId, start, limit });
getDrops = async ({ accountId, start, limit }) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
getDrops = async ({ accountId, start, limit }) => {
getDrops = async ({ accountId, start, limit }: { account: string; start: string | number; limit: number; }) => {

/** Get Drops caching logic */
if (
Date.now() > this.dropStore.getDropsExpiryTime ||
start !== this.dropStore.getDropsLastPage
Copy link
Contributor

Choose a reason for hiding this comment

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

i tested locally, if I paginate to next page (page 1 -> 2), then back to previous (2 -> 1), it's not showing the previous drops from store (1)

@tzeweiwee
Copy link
Contributor

tzeweiwee commented Mar 16, 2023

@andrehadianto works very well when i go to a drop manager from all drops page and back to all drops on page 1. But i think we should cache all paginated drops.

All scenarios before expiry time.
Scenario 1:

  1. Go to all drops (page 1)
  2. paginate to page 2
  3. click previous button (page 1) - this is still calling getDrops

Scenario 2:

  1. Go to all drops
  2. paginate to page 2
  3. click a drop, enter drop manager
  4. user click back or all drops (breadcrumb)
  5. user lands on all drops page 1 - this still calls getDrops

My suggestion would be to concatenate dropStore.drops or to restructure dropStore such that if user navigate to any page index, it will fetch the drops of that page index in store which is how the following structure came to mind at first

paginatedDrops: { 1: [drop1, drop2], 2: [drop11, drop12], ... }

@andrehadianto andrehadianto requested a review from tzeweiwee March 17, 2023 08:08
Copy link
Contributor

@tzeweiwee tzeweiwee left a comment

Choose a reason for hiding this comment

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

Looks good! thanks for working on this

@andrehadianto andrehadianto merged commit 7cd9184 into testnet Mar 17, 2023
@tzeweiwee tzeweiwee deleted the andrel/185-general-refactors branch March 17, 2023 08:48
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.

2 participants