Skip to content

add a localStorage abstraction to tidy up our parsing work - #5979

Merged
iAmWillShepherd merged 20 commits into
masterfrom
local-storage-abstraction
Oct 26, 2018
Merged

iAmWillShepherd merged 20 commits into
masterfrom
local-storage-abstraction

Conversation

@shiftkey

@shiftkey shiftkey commented Oct 23, 2018 •

Copy link
Copy Markdown
Member

I've got a PR on the way which interacts with local storage, but I'm opening this PR in advance because I found myself writing this code again:

  • reading and writing a boolean to localStorage
  • reading and writing a number to localStorage

localStorage only deals with strings, so we have various parsing code sprinkled around to convert between strings and boolean/number values.

This centralizes those rules and makes our current usage a bit easier to follow.

  • add unit tests for new APIs

@shiftkey shiftkey added ready-for-review Pull Requests that are ready to be reviewed by the maintainers infrastructure Issues and pull requests related to scripts and tooling for GitHub Desktop labels Oct 23, 2018
@shiftkey
shiftkey force-pushed the local-storage-abstraction branch from 0cd09cc to 88a2903 Compare October 23, 2018 18:41
@shiftkey
shiftkey requested review from a team October 23, 2018 18:44
@j-f1

j-f1 commented Oct 23, 2018 •

Copy link
Copy Markdown
Contributor

Why not create just one API that gets/sets JSON? This allows the API to be used for arrays and objects too.

@shiftkey

Copy link
Copy Markdown
Member Author

@j-f1

Why not create just one API that gets/sets JSON? This allows the API to be used for arrays and objects too.

Why didn't I go further than this?

  • this was inspired by existing code that wants to manage primitives but needs to do additional validation
  • this is a chance to enforce some consistency with how we get and set these values

I did start with writing an API for string but it felt too trivial, and our current code does handle situations fine where the key may not be found (yay types). For more complex things like arrays and objects I basically didn't want to tackle a generic solution without thinking more about problems like deserializing arbitrary JSON and handling different versions of shapes being returned.

@shiftkey
shiftkey force-pushed the local-storage-abstraction branch from e214dc7 to 47901e2 Compare October 24, 2018 12:42

@niik niik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Other than the one comment I made about wanting callers to be explicit about whether the care about non-existence or not I think this is a nice cleanup.

Comment thread app/src/lib/local-storage.ts Outdated
* @param key local storage entry to read
* @param defaultValue fallback value if unable to find key or valid value
*/
export function getNumber(key: string, defaultValue = 0): number {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

niik
niik previously requested changes Oct 24, 2018

@niik niik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like it @shiftkey. One small nitpick but feel free to dismiss my review after and get one from someone else seeing as I likely won't be able to review again until Monday.

Comment thread app/src/lib/stats/stats-store.ts Outdated
function getLocalStorageTimestamp(key: string): number | null {
const value = parseInt(localStorage.getItem(key) || '', 10)
return isNaN(value) ? null : value
return getNumber(key) || null

This comment was marked as spam.

This comment was marked as spam.

@shiftkey

Copy link
Copy Markdown
Member Author

One small nitpick but feel free to dismiss my review after and get one from someone else seeing as I likely won't be able to review again until Monday.

Can someone from @desktop/engineering give this another 👀 and a 👍?

@shiftkey
shiftkey dismissed niik’s stale review October 24, 2018 18:42

Markus isn't back until Monday and I'd like to get this into the beta

@shiftkey
shiftkey requested a review from a team October 25, 2018 11:11
@shiftkey shiftkey added tech-debt Issues and pull requests related to addressing technical debt or improving the codebase and removed infrastructure Issues and pull requests related to scripts and tooling for GitHub Desktop labels Oct 25, 2018
@iAmWillShepherd iAmWillShepherd self-assigned this Oct 25, 2018

@outofambit outofambit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💯

@shiftkey shiftkey added this to the 1.5.0 milestone Oct 25, 2018
@shiftkey

Copy link
Copy Markdown
Member Author

FYI: moving this into 1.5.0 but this doesn't have to go out with the beta tomorrow

@iAmWillShepherd

Copy link
Copy Markdown
Contributor

yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-review Pull Requests that are ready to be reviewed by the maintainers tech-debt Issues and pull requests related to addressing technical debt or improving the codebase

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants