<commit_before>import { Observable } from 'rxjs';
import { changePeriod } from 'redux/modules/scheduler/period';
import { fetchEvents, insertEvent, updateEvent, deleteEvent } from 'services/api';
import * as EventsActions from 'redux/modules/scheduler/events';
import * as NotificationActions from 'redux/modules/scheduler/notification';
import * as PeriodActions from 'redux/modules/scheduler/period';
import 'utils/rxjs/add/takeWhileInclusive';
import actionPredicate from './utils/actionPredicate';
import allDayDnd from './allDayDnd';

// -------------
// Change Period
// -------------

const ROUTER_CHANGE = '@@router/LOCATION_CHANGE';

const changePeriodOnPathChange = (iterable) => iterable
  .filter(actionPredicate([ROUTER_CHANGE]))
  .map((value) => value.action)
  .map((routeChange) => routeChange.payload.pathname)
  .filter((path) => path.indexOf('/scheduler') >= 0)
  .map((path) => path.split('/')[2])
  .filter((period) => period)
  .map((period) => changePeriod(period));

// ---------------------------
// Fetch data on period change
// ---------------------------

const fetchData = (iterable) => {
  const dataChangedStream = iterable
    .filter(actionPredicate(Object.values(PeriodActions.actionTypes)))
    .map(({action, store}) => store.scheduler.period)
    .distinctUntilChanged()
    .debounceTime(250);

  const fetchStream = dataChangedStream.flatMap((period) => {
    const startDate = period.get('startDate');
    const endDate = period.get('endDate');
    const timezone = period.get('timezone');

    return Observable
      .fromPromise(fetchEvents({startDate, endDate, timezone}))
      .map((data) => {
        const { response, error } = data;
        if (response) {
          const events = response.entities.events;
          const result = response.result.events;

          return EventsActions.loadEventsSuccess(events, result);
        } else {
          return EventsActions.loadEventsFailure(error);
        }
      });
  });

  const isLoadingStream = dataChangedStream
    .map((x) => true)
    .merge(fetchStream.map((x) => false))
    .distinctUntilChanged()
    .filter((x) => x)
    .map((x) => EventsActions.pushFetching());

  return fetchStream.merge(isLoadingStream);
};

// ------------------------
// Modify Events In Backend
// ------------------------

const insertEventInBackend = (iterable) => iterable
  .filter(actionPredicate([EventsActions.actionTypes.INSERT_EVENT]))
  .flatMap(({action, store}) => {
    const timezone = store.scheduler.period.get('timezone');<commit_msg>Prevent double fire of fetch.<commit_after>import { Observable } from 'rxjs';
import { changePeriod } from 'redux/modules/scheduler/period';
import { fetchEvents, insertEvent, updateEvent, deleteEvent } from 'services/api';
import * as EventsActions from 'redux/modules/scheduler/events';
import * as NotificationActions from 'redux/modules/scheduler/notification';
import * as PeriodActions from 'redux/modules/scheduler/period';
import 'utils/rxjs/add/takeWhileInclusive';
import actionPredicate from './utils/actionPredicate';
import allDayDnd from './allDayDnd';

// -------------
// Change Period
// -------------

const ROUTER_CHANGE = '@@router/LOCATION_CHANGE';

const changePeriodOnPathChange = (iterable) => iterable
  .filter(actionPredicate([ROUTER_CHANGE]))
  .map((value) => value.action)
  .map((routeChange) => routeChange.payload.pathname)
  .filter((path) => path.indexOf('/scheduler') >= 0)
  .map((path) => path.split('/')[2])
  .filter((period) => period)
  .map((period) => changePeriod(period));

// ---------------------------
// Fetch data on period change
// ---------------------------

const fetchData = (iterable) => {
  const dataChangedStream = iterable
    .filter(actionPredicate(Object.values(PeriodActions.actionTypes)))
    .map(({action, store}) => store.scheduler.period)
    .distinctUntilChanged()
    .debounceTime(250)
    .share();

  const fetchStream = dataChangedStream.flatMap((period) => {
    const startDate = period.get('startDate');
    const endDate = period.get('endDate');
    const timezone = period.get('timezone');

    return Observable
      .fromPromise(fetchEvents({startDate, endDate, timezone}))
      .map((data) => {
        const { response, error } = data;
        if (response) {
          const events = response.entities.events;
          const result = response.result.events;

          return EventsActions.loadEventsSuccess(events, result);
        } else {
          return EventsActions.loadEventsFailure(error);
        }
      });
  }).share();

  const isLoadingStream = dataChangedStream
    .map(() => true)
    .merge(fetchStream.map(() => false))
    .distinctUntilChanged()
    .filter((x) => x)
    .map(() => EventsActions.pushFetching());

  return fetchStream.merge(isLoadingStream);
};

// ------------------------
// Modify Events In Backend
// ------------------------

const insertEventInBackend = (iterable) => iterable
  .filter(actionPredicate([EventsActions.actionTypes.INSERT_EVENT]))
  .flatMap(({action, store}) => {
    const timezone = store.scheduler.period.get('timezone');
{'fraction_non_alphanumeric': 0.1904296875, 'fraction_numerical': 0.001953125, 'mean_word_length': 3.1198712791633145, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6401067', 'n_tokens_mistral': 1459, 'n_tokens_neox': 1450, 'n_words': 293}