GithubHelp home page GithubHelp logo

msluther / storage-engine Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 3rd-eden/storage-engine

0.0 1.0 0.0 11 KB

EventEmitter abstraction on top the React-Native AsyncStorage API

License: MIT License

JavaScript 100.00%

storage-engine's Introduction

storage-engine

Storage Engine is an abstraction on the AsyncStorage API of React-Native, as stated in their documentation:

It is recommended that you use an abstraction on top of AsyncStorage instead of AsyncStorage directly for anything more than light usage since it operates globally.

So there you go, an abstraction that aims to make working with the AsyncStorage API a bit more developer friendly. The major selling point of this module is the fact that it infuses the EventEmitter pattern with the AsyncStorage API which will allow you to listen to any modification that you make to the storage (while using this module).

Installation

This project is released in the public npm registry and can be installed using:

npm install --save storage-engine

Please note that this module is designed for react-native and therefor has a peerDependency upon it.

Usage

The StorageEngine will emit an event with the name of the key as event name. This allows you to subscribe to any operation that might affect the key.

import StorageEngine from 'storage-engine';

const storage = new StorageEmitter();

//
// Subscribe to changes to the `foobar` key.
//
storage.on('foobar', (action, ...args) => {
  if (action === 'setItem') {
    console.log('foobar has been updated to', ...args);
  }
});

await storage.setItem('foobar', 'what is going on');

The following API methods are available:

getItem

Retrieve the item from async Storage. It accepts the following arguments:

  • name (string), Name of the key that needs to be retrieved.

If no value is found, null will be returned instead.

const name = await storage.getItem('name');
console.log(name); // what ever value was stored.

setItem

Store the item from async Storage. It accepts the following arguments:

  • name (string), Name of the key that needs to be retrieved.
  • value (Object), Value that needs to be stored.
await storage.setItem('name', 'value');

const name = await storage.getItem('name');
console.log(name); // value
await storage.setItem('name', { object: 'value' });

const name = await storage.getItem('name');
console.log(name); // { object: 'value' }

removeItem

Removes the item from async Storage. It accepts the following arguments:

  • name (string), Name of the key that needs to be removed.
await storage.removeItem('name');

const name = await storage.getItem('name');
console.log(name); // null

mergeItem

It merges the given value with the previous stored value. It accepts the following arguments:

  • name (string), Name of the key that needs to be removed.
  • value, (string), Value that needs to be merged.
await storage.setItem('name', '{"bar":"baz"}');
await storage.mergeItem('name', '{"foo":"bar"}');

const name = await storage.getItem('name'); // { bar: baz, foo: bar }

clear

Removes all items from async Storage.

await storage.clear();

flushGetRequests

Flushes any pending requests using a single batch call to get the data.

await storage.flushGetRequests();

multiGet

license

MIT

storage-engine's People

Contributors

3rd-eden avatar dsanudovacas avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.