Rxjs local store

by amindunited

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.4.3/Rx.min.js"></script>

JavaScript

/**
	Developer Globals
*/
const initLocal = [{id: 'local_1'}, {id: 'local_2'}, {id: 'both_1'}, {id: 'both_2'}];
const initSession = [{id: 'session_1'}, {id: 1}, {id: 2}];
const initServer = [{id: 'server_1'}, {id: 'server_2'}, {id: 'both_1'}, {id: 'both_2'}];


/**
	STORE
	Stores all Observable Objects
*/
class Store {
	constructor () {
  	this.user = new Rx.BehaviorSubject();
  	this.listArray = []
		this.listOfDetails = new Rx.BehaviorSubject();
  }
}


/**
	LIST SERVICE
	* Get Local Data *
	Query data from the local storage (if annonymous user)
  * or *
  Query data from the session storage (if authenticated user)
  
  * Get Remote Data *
  * Then *
   Request update from server
   * Then *
   	merge the two data sources
    
   * Finally *
   * Store Local Data *
   Save data to the local storage (if annonymous user)
   * or *
   Save data to session storage (if authenticated user)
*/
class ListService {
	constructor(store) {
  	this.store = store;
  }
  
  mergeArrays(targetArray, sourceArray) {
  	let finalArray = targetArray; 
    sourceArray.forEach((sourceVal) => {
      let hasMatch = false;
      targetArray.forEach((targetVal) => {
        if (targetVal.id === sourceVal.id) {
          hasMatch = true;
        }
      });
      if (!hasMatch) {
        finalArray.push(sourceVal);
      }
    });
    return finalArray;
  }
  
	getList () {
  	console.log('in get list ', this.store.user.value);
    let authLevel = this.store.user.value;
    let localData;
    
    if (authLevel > 0) {
      if (window.sessionStorage) {
        let values = JSON.parse(session.localStorage.getItem('searchResults'));
        localData = values;
        this.store.listOfDetails.next(values);
      }
    } else {
      if (window.localStorage) {
        let values = JSON.parse(window.localStorage.getItem('searchResults'));
        localData = values;
        this.store.listOfDetails.next(values);
      }
    }
    
		setTimeout(()=>{
    	let response = initServer;//[{id:...