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

Session storage support #8

Open
opiation opened this issue Dec 9, 2020 · 0 comments
Open

Session storage support #8

opiation opened this issue Dec 9, 2020 · 0 comments

Comments

@opiation
Copy link

opiation commented Dec 9, 2020

While this package is called fp-ts-local-storage, it seems perhaps low-hanging fruit to support sessionStorage as well since it shares the same API. Should you decide to support sessionStorage, an adaption of something similar I've done in my own work might look like...

function setup(storage: Storage) {
  return {
    clear(): void {
      return storage.clear()
    },

    getItem(key: string): IO<Option<string>> {
      return () => fromNullable(storage.getItem(key))
    },

    key(index: number): IO<Option<string>> {
      return () => fromNullable(storage.key(index))
    },

    length(): number {
      return storage.length
    },

    removeItem(key: string): IO<void> {
      return () => storage.removeItem(key)
    },

    setItem(key: string, value: string): IO<void> {
      return () => storage.setItem(key, value)
   }
}

and used as...

// storage.ts
import { setup } from 'insert-lib-name-here'

export const LocalStorage = setup(localStorage)
export const SessionStorage = setup(sessionStorage)

SessionStorage.getItem // ...

Again, I don't know whether sessionStorage is in the roadmap for this lib or if it was intentionally omitted, nor is the above a PR. Just sharing an idea in the event it hasn't been considered yet. Happy to submit a PR if this is deemed worthwhile.

Cheers!

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

No branches or pull requests

1 participant