4.2.2 • Published 6 years ago

@cerebral/storage v4.2.2

Weekly downloads
46
License
MIT
Repository
github
Last release
6 years ago

@cerebral/storage

Install

NPM

npm install @cerebral/storage

Description

This module exposes local storage or session storage as a provider, where it by default parses and serializes to JSON.

Instantiate

import { Module, Controller } from 'cerebral'
import StorageModule from '@cerebral/storage'

const storage = StorageModule({
  // instance of storage, can be window.localStorage / localStorage,
  // window.sessionStorage / sessionStorage, or asyncStorage on
  // react native. Async storage API is automatically managed
  target: localStorage,
  // Serializes and parses to JSON by default
  json: true,
  // Synchronize state when it changes
  sync: {
    'someStorageKey': 'some.state.path'
  },
  // Set prefix for storagekey "somePrefix.someStorageKey"
  prefix: 'somePrefix'
})

const app = Module({
  modules: { storage }
})

const controller = Controller(app)

error

StorageProviderError

import {StorageProviderError} from '@cerebral/storage'

// Error structure
{
  name: 'StorageProviderError',
  message: 'Some storage error'
  stack: '...'  
}

get

Get data from storage.

action

function someAction({storage}) {
  // sync
  const value = storage.get('someKey')
  // async
  return storage.get('someKey')
    .then(value => ({value}))
}

operator

import {state, props} from 'cerebral/tags'
import {getStorage} from '@cerebral/storage/operators'

export default [
  // sync and async
  getStorage('someKey'),
  function someAction ({props}) {
    props.value // Whatever was on "someKey"
  }
]

remove

Remove data from storage.

action

function someAction({storage}) {
  // sync
  storage.remove('someKey')
  // async
  return storage.remove('someKey')
}

operator

import {state} from 'cerebral/tags'
import {removeStorage} from '@cerebral/storage/operators'

export default [
  // sync and async
  removeStorage(state`currentStorageKey`)
]

set

Write data to storage.

action

function someAction({storage}) {
  // sync
  storage.set('someKey', {foo: 'bar'})
  // async
  return storage.set('someKey', {foo: 'bar'})
}

operator

import {state, props} from 'cerebral/tags'
import {setStorage} from '@cerebral/storage/operators'

export default [
  // sync and async
  setStorage(state`currentStorageKey`, props`someData`)
]
4.2.2

6 years ago

4.2.1

6 years ago

4.2.0

6 years ago

4.1.1

6 years ago

4.1.0

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.4.0

6 years ago

3.3.0

7 years ago

3.2.0

7 years ago

3.1.0

7 years ago

3.0.0

7 years ago

2.1.0

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago

0.0.0-beta.3

7 years ago

0.0.0-beta.2

7 years ago