Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

mnemosyne-synced-storage

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mnemosyne-synced-storage

save and get data from multiple storage types and keep them in sync

latest
Source
npmnpm
Version
1.0.4
Version published
Weekly downloads
12
100%
Maintainers
1
Weekly downloads
 
Created
Source

mnemosyne-synced-storage

This library helps storing data in between different providers and keeping them in sync. For example: you can keep data in sync between the url parameters, localStorage, and browser cache.

Instalation

npm i --save mnemosyne-synced-storage
yarn add mnemosyne-synced-storage

Getting Started

React

// App.tsx
import {MultiStorageAdapter} from 'mnemosyne-synced-storage'
import {urlParamsAdapter, chromeStorageAdapter, localStorageAdapter} from 'mnemosyne-synced-storage/adapters'
import {MnemosyneProvider} from 'mnemosyne-synced-storage/react'

const App = () => {
  const storageAdapter = new MultiStorageAdapter([
    urlParamsAdapter(),
    chromeStorageAdapter(),
    localStorageAdapter(),
  ])
  
  return (
   <MnemosyneProvider adapter={storageAdapter}>
    <MyComponent />
   </MnemosyneProvider> 
  )
}
// MyComponent.tsx
import {useSyncedStorage} from 'mnemosyne-synced-storage/react'

const MyComponent = () => {
    // use it just like "useState"
    const [userId, setUserId] = useSyncedStorage<string>({key: 'user-id', defaultValue: ''})
    const handleUserIdChange = (e) => setUserId(e.target.value)
    return (
        <input onChange={handleUserIdChange} name="user-id" />
    )
}

Vanilla

import {MultiStorageAdapter} from 'mnemosyne-synced-storage'
import {urlParamsAdapter, chromeStorageAdapter, localStorageAdapter} from 'mnemosyne-synced-storage/adapters'

const storageAdapter = new MultiStorageAdapter([
    urlParamsAdapter(),
    chromeStorageAdapter(),
    localStorageAdapter(),
  ])
  
storageAdapter.set('user-id', '123')

const userId = storageAdapter.get('user-id')

Order of storage

For the MultistorageAdapter:

  • it will first try to get data from the first adapter and then the next one, if none of it has data then it will return null.
  • If one of the storage types has data then it will return the first one it encounters, in the example above it will try to get first data from the url and then from chrome cache and then from localStorage.

FAQs

Package last updated on 24 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts