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

1930 local storage version control #1984

Conversation

Da-Colon
Copy link
Contributor

@Da-Colon Da-Colon commented Jun 8, 2024

Noticed that I missed the Average Block Time so added that here.

@Da-Colon Da-Colon self-assigned this Jun 8, 2024
Comment on lines 105 to 123
export const MASTER_CACHE_VERSION = 1;
export const FAVORITES_CACHE_VERSION = 1;
export const PROPOSAL_CACHE_VERSION = 1;
export const AVERAGE_BLOCK_TIME_CACHE_VERSION = 1;

export const getCacheVersion = (cacheName: CacheKeys): number => {
switch (cacheName) {
case CacheKeys.FAVORITES:
return FAVORITES_CACHE_VERSION;
case CacheKeys.MASTER_COPY:
return MASTER_CACHE_VERSION;
case CacheKeys.PROPOSAL_ARCHIVE:
return PROPOSAL_CACHE_VERSION;
case CacheKeys.AVERAGE_BLOCK_TIME:
return AVERAGE_BLOCK_TIME_CACHE_VERSION;
default:
throw new Error('Invalid cache name');
}
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this kinda what you are thinking? @adamgall

Copy link

netlify bot commented Jun 8, 2024

Deploy Preview for decent-interface-dev ready!

Name Link
🔨 Latest commit 3ae6699
🔍 Latest deploy log https://app.netlify.com/sites/decent-interface-dev/deploys/66699eac88271100081e0238
😎 Deploy Preview https://deploy-preview-1984.app.dev.decentdao.org
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

@mudrila mudrila left a comment

Choose a reason for hiding this comment

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

Have couple suggestions on renaming constants names, but other than that - looks good

src/hooks/utils/cache/cacheDefaults.ts Outdated Show resolved Hide resolved
src/hooks/utils/cache/cacheDefaults.ts Outdated Show resolved Hide resolved
src/hooks/utils/cache/cacheDefaults.ts Outdated Show resolved Hide resolved

/**
* Cache default values.
*
* Cache keys are not required to have a default value.
* @todo: The CACHE_DEFAULTs seem to be used for indexDB, But favorites is localstorage. We will need to revisit this.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd probably suggest migrating from indexedDB to localStorage - our cache is not that big and indexedDB was rather over-engineering. But yeah - not a topic for discussion in this PRs series

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah 100% Another series can take a look at cleaning up indexDB in the future

Copy link
Contributor

Choose a reason for hiding this comment

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

Seconded. Uh, thirded?

- MASTER_CACHE_VERSION -> MASTER_COPY_CACHE_VERSION
- PROPOSAL_ARCHIVE -> PROPOSAL_CACHE
@Da-Colon Da-Colon requested a review from mudrila June 10, 2024 13:28
default:
throw new Error('Invalid cache name');
}
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you combine above into sth like a constant mapCacheNameToVersion?

Copy link
Member

Choose a reason for hiding this comment

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

@DarksightKellar I don't follow what you're suggesting here...

Copy link
Contributor Author

@Da-Colon Da-Colon Jun 12, 2024

Choose a reason for hiding this comment

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

@DarksightKellar You mean like this? (Taking a guess)

const CACHE_VERSIONS: { [key: string]: number } = {
  [CacheKeys.FAVORITES]: 1,
  [CacheKeys.MASTER_COPY]: 1,
  [CacheKeys.PROPOSAL_CACHE]: 1,
  [CacheKeys.AVERAGE_BLOCK_TIME]: 1,
};

export const getCacheVersion = (cacheName: CacheKeys): number => {
  const version = CACHE_VERSIONS[cacheName];
  if (version === undefined) {
    throw new Error('Invalid cache name');
  }
  return version;
};

(In latest commit)

Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking more like:

export const CACHE_VERSIONS = {
  [CacheKeys.FAVORITES]: 1,
  [CacheKeys.MASTER_COPY]: 1,
  [CacheKeys.PROPOSAL_CACHE]: 1,
  [CacheKeys.AVERAGE_BLOCK_TIME]: 1,
};


// elsewhere:
const favsCacheVersion = CACHE_VERSIONS[CacheKeys.FAVORITES]

So we can get rid of getCacheVersion and not have to do dynamic error checking, as inferred typing will prevent CACHE_VERSIONS['invalidKey'] in the first place.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Da-Colon @adamgall Ouuu and, apparently Object.freeze:

export const CACHE_VERSIONS = Object.freeze({
  [CacheKeys.FAVORITES]: 1,
  [CacheKeys.MASTER_COPY]: 1,
  [CacheKeys.PROPOSAL_CACHE]: 1,
  [CacheKeys.AVERAGE_BLOCK_TIME]: 1,
})

makes CACHE_VERSIONS[CacheKeys. FAVORITES] = 0; impossible -- added bonus. Learnt that from chatGPT.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was sooo close ha. Coolio updated and pushed sir.


/**
* Cache default values.
*
* Cache keys are not required to have a default value.
* @todo: The CACHE_DEFAULTs seem to be used for indexDB, But favorites is localstorage. We will need to revisit this.
Copy link
Contributor

Choose a reason for hiding this comment

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

Seconded. Uh, thirded?

Copy link
Member

@adamgall adamgall left a comment

Choose a reason for hiding this comment

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

export enum CacheKeysV0 { ... looks unused, can remove?

@Da-Colon
Copy link
Contributor Author

export enum CacheKeysV0 { ... looks unused, can remove?

I was going keeping this around for the next PR's migration. But I don't think I really need it since I'm only going to migrate the Favorites. I'll remove this in the next PR.

Copy link
Contributor

@DarksightKellar DarksightKellar left a comment

Choose a reason for hiding this comment

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

Noice!

@adamgall adamgall dismissed mudrila’s stale review June 12, 2024 15:42

Requests have been addressed

@Da-Colon Da-Colon merged commit 00c5bd1 into issue/1931-local-storage-updates Jun 12, 2024
7 checks passed
@Da-Colon Da-Colon deleted the issue/1930-local-storage-version-control branch June 12, 2024 15:42
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.

4 participants