bassihassan / 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
MIT License
8 stars 3 forks source link

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.

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