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(store-sdk): Add Session to store-sdk #896

Merged
merged 14 commits into from
Aug 18, 2021
Merged

Conversation

tlgimenes
Copy link
Contributor

@tlgimenes tlgimenes commented Aug 16, 2021

What's the purpose of this pull request?

This PR adds the session context to store-sdk.
Session context is a minimalistic React context that can be used by store implementors to store session value on it. Also, note that the storage is scoped, so different bindings can actually have different session values stored on the browser without overriding one anotther.

How it works?

The ideia behind this context is to be very simple and leave to the store implementors chose how they are going to change these values. The standard way will be implemented in our graphql infrastructure via a query similar to:

type Mutation {
 session {
   update (key: string, value: string, session: Session): Session
 }
}

this way devs can cause sideEffects on the backend, like setting the postalCode and changing the region/priceTable for instance.

How to test it?

There is currently nobody using this session.

@netlify
Copy link

netlify bot commented Aug 16, 2021

✔️ Deploy Preview for storeui ready!

🔨 Explore the source changes: 79bf0b0

🔍 Inspect the deploy log: https://app.netlify.com/sites/storeui/deploys/611d25663874980007535d48

😎 Browse the preview: https://deploy-preview-896--storeui.netlify.app

@tlgimenes tlgimenes added this to the Store SDK milestone Aug 16, 2021
@tlgimenes tlgimenes linked an issue Aug 16, 2021 that may be closed by this pull request
@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 16, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 79bf0b0:

Sandbox Source
Store UI Typescript Configuration

@tlgimenes tlgimenes marked this pull request as ready for review August 16, 2021 16:01
@tlgimenes tlgimenes requested a review from a team as a code owner August 16, 2021 16:01
packages/store-sdk/src/storage/useLocalStorage.ts Outdated Show resolved Hide resolved
packages/store-sdk/src/storage/useLocalStorage.ts Outdated Show resolved Hide resolved
packages/store-sdk/src/storage/useLocalStorage.ts Outdated Show resolved Hide resolved
*/
import { useState, useEffect, useCallback } from 'react'

const getItem = <T>(key: string) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should consider using localForage for this. It's not a heavy library (but certainly heavier than this ofc) and it brings numerous advantages over using only localStorage directly. I think it's way more scalable to go with localForage.

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'm not sure. According to bundlephobia this lib has 8.5 min+gzip, which is huge, specially comparing to other deps, like swr that has 5 min+gzip and handles the whole IO or react-use-cart that is 1.5 min+gzip

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you think on leaving this localStorage iimplementation and if we see this doesn't scale well we use a different approach

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense. Then I propose for us to migrate to IndexedDB, since it doesn't block the main thread as LocalStorage does. We also gain more freedom regarding storage quotas.

I have found this good read.

LocalStorage should be avoided because it is synchronous and will block the main thread. It is limited to about 5MB and can contain only strings. LocalStorage is not accessible from web workers or service workers.

Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think on leaving this localStorage iimplementation and if we see this doesn't scale well we use a different approach

I only worry about having to release a major for store-sdk only to upgrade it to indexeddb. I think that for 99% of use-cases, localStorage will be enough, but we would have to break this 99% because of the 1% that isn't contemplated. I don't think transferring data between systems would be a trivial task.

indexeddb is by far more complicated to use, but I think we can make an opportunity out of this. Maybe create a super simple to manage wrapper around indexeddb that's also super lightweight.

I'm okay with using localstorage as long we don't go stable without changing it to indexeddb. For me, that's a must.

packages/store-sdk/src/session/Provider.tsx Outdated Show resolved Hide resolved
@tlgimenes tlgimenes requested a review from icazevedo August 17, 2021 17:58
@tlgimenes tlgimenes requested a review from icazevedo August 18, 2021 12:25
tlgimenes and others added 2 commits August 18, 2021 12:17
Co-authored-by: Ícaro Azevedo <icaro.azevedo@vtex.com.br>
@tlgimenes tlgimenes merged commit 6a7f1b5 into master Aug 18, 2021
@tlgimenes tlgimenes deleted the feat/session-sdk branch August 18, 2021 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.

Session
3 participants