{"size": 1205, "ext": "js", "max_stars_repo_path": "src/modules/settings/settings.epics.js", "max_stars_repo_name": "just4fun/uestc-bbs-react-native", "max_stars_repo_stars_event_min_datetime": "2015-10-17T12:15:57.000Z", "max_stars_repo_stars_event_max_datetime": "2017-08-29T01:53:02.000Z", "max_issues_repo_path": "src/modules/settings/settings.epics.js", "max_issues_repo_name": "just4fun/stuhome", "max_issues_repo_issues_event_min_datetime": "2018-03-05T15:13:34.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-25T09:58:14.000Z", "max_forks_repo_path": "src/modules/settings/settings.epics.js", "max_forks_repo_name": "just4fun/uestc-bbs-react-native", "max_forks_repo_forks_event_min_datetime": "2015-11-25T15:08:50.000Z", "max_forks_repo_forks_event_max_datetime": "2017-08-23T10:39:43.000Z"}
{"max_stars_count": 186, "max_issues_count": 29, "max_forks_count": 61, "avg_line_length": 29.3902439024, "max_line_length": 80, "alphanum_fraction": 0.6929460581}
import { combineEpics, ofType } from 'redux-observable';
import { from as fromPromise, of, concat } from 'rxjs';
import { mergeMap, map, filter } from 'rxjs/operators';
import { AsyncStorage } from 'react-native';
import {
  SETTINGS_RETRIEVE,
  SETTINGS_STORE,
  storeSettingsToRedux
} from './settings.ducks';
import api from '~/services/api';

const retrieveSettings = (action$) => action$.pipe(
  ofType(SETTINGS_RETRIEVE),
  mergeMap(action => fromPromise(AsyncStorage.getItem('settings')).pipe(
    filter(settings => settings),
    map(settings => {
      settings = JSON.parse(settings);
      return storeSettingsToRedux(settings);
    })
  ))
);

const storeSettings = (action$) => action$.pipe(
  ofType(SETTINGS_STORE),
  mergeMap(action => fromPromise(AsyncStorage.getItem('settings')).pipe(
    map(settings => {
      // Get new settings.
      let newSettings = Object.assign({}, JSON.parse(settings), action.payload);
      // Store new settings in storage.
      AsyncStorage.setItem('settings', JSON.stringify(newSettings));
      // Update redux store.
      return storeSettingsToRedux(newSettings);
    })
  ))
);

export default combineEpics(
  retrieveSettings,
  storeSettings
);
