Giter Site home page Giter Site logo

johnsage25 / xreactive-react-native-storage Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bassihassan/xreactive-react-native-storage

0.0 0.0 0.0 3 KB

An AsyncStorage wrapper based on RxJs for React Native to simplify the code and take advantage of the power offered by Rx

License: MIT License

JavaScript 100.00%

xreactive-react-native-storage's Introduction

xreactive-react-native-storage

An AsyncStorage wrapper based on RxJs for React Native to simplify the code and take advantage of the power offered by Rx

How to install

npm i xreactive-react-native-storage --save

Why did I build this?

While working on a React Native project for the first time, I discovered that AsyncStorage and it's asynchronous nature was really a puzzle. It was also very difficult to memorize or copy the same piece of code everywhere to make a simple get or set so if we want to save several keys, values we must repeat operations to zip the two arrays (keys, values) It's very annoying . So, I decided to create this package based on rxjs to take advantage of it's power as well as it's simplicity to solve the problems I mentioned above and I hope someone else too!

Methods

All methods are based on observables.

  • save(key(s),value(s)) : Save value(s) to key(s).
    • key : may be string or string[] .
    • value may be any or any[] .
  • get(key(s)) : Get value(s) from local storage. * key may be a string or string[],
    • if the key is a string the function return an observable that hold the value of the key but if the key is string[] then the value is an object { key : value}
  • getArray(keys) : Get array of values .
    • keys must be an array of strings the function will return an observable that hold an array of values each value is in the same position of the key .
  • getAllKeys() : Get all keys from local storage.
  • update(key, value) : update value to existing key value.
    • key must be string
  • merge(key(s), value(s)) : merge value(s) to existing key(s) value. * key may be a string or string[] * value may be any or any[]
  • remove(key) : Remove key(s) from local storage.
    • key may be a string or string[] .
  • clear() : Remove all keys from local storage.

Examples

get and save :

import {Observable} from "rxjs"
import xstorage from "xreactive-react-native-storage"

var key  = 'key1'
var values = [1,3,4]

xstorage.save(key,values).subscribe((e)=>{
	 
})
var keys = ['key1','key2']
xstorage.get(keys).subscribe(({key1,key2})=>{
		console.log(`value of key 1 is ${key}`)
})

Real world Exemple :

import {Observable} from "rxjs"
import xstorage from "xreactive-react-native-storage"

function fetchTalkDetail(event,talksId,type) {
    let ids = talksId.map((id)=>`${event.code}-talk-${id}`);
    return xstorage.getArray(ids).switchMap((e)=>{
        return Observable.of({
            talks:e,
            event,
            type
        })
    }).toPromise()
   
}

Contributors

  • Bassi Hassan (Me)
  • You! Feel free to contribute in any way. :D

xreactive-react-native-storage's People

Contributors

bassihassan 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.