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: replaces noop store with memory store #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

gabteles
Copy link

Table of Contents

Description

This implements a basic memory store instead of noop.

Motivation and Context

When server-side rendering, having a noop store causes useExperiment calls to fail even if they were made after a emitter.defineVariants call.

How Has This Been Tested?

I've added tests to store.jsx to check if it could set and get items from it. The most important part is to delete window.localStorage and keep it working properly.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@moretti
Copy link
Member

moretti commented Sep 23, 2021

Hey Gabriel, thank you for this!

There's another PR that it's adding a cookie storage for browsers that do not support window.localstorage: #28
Just thinking about what could be the best way to support every scenario...this could a third fallback option?

Basically when localStorage and and cookieStorage are not available, which would be the case of node.js running without JSDOM, then we could fallback to memoryStorage...what do you think?

@gabteles
Copy link
Author

gabteles commented Sep 24, 2021

I think memoryStorage should be the last fallback option, indeed, since it's not persistent across navigation through pages.
In order to support multiple stores, we could introduce an interface like this (using typescript just to illustrate):

interface IStorage {
  isAvailable(): boolean;
  getItem(key: string): string | null;
  setItem(key: string, value: string): void;
}

So we could select storage like this:

const localStorageWrapper = {
  isAvailable() {
    if (typeof window === 'undefined' || !'localStorage' in window) {
      return false
    }

    try {
      let key = '__pushtell_react__';
      window.localStorage.setItem(key, key);
      if (window.localStorage.getItem(key) === key) {
        window.localStorage.removeItem(key);
        return true;
      }
    } catch (e) {
      // DO NOTHING 
    }

    return false
  },
  getItem(key) { return localStorage.getItem(key) },
  setItem(key, value) { localStorage.setItem(key, value) }
}

const cookieStorageWrapper = {
  // ...
}

const memoryStorageWrapper = {
  values: {},
  isAvailable() { return true },
  getItem(key) { return this.values[key] },
  setItem(key, value) { this.values[key] = value }
}

// Ordered by preference 
const storages = [
  localStorageWrapper,
  cookieStorageWrapper,
  memoryStorageWrapper
]

const availableStorage = storages.find((storage) => storage.isAvailable())
export default availableStorage;

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