<commit_before>// @flow
import R from 'ramda';
import {
  createStore,
  applyMiddleware,
  combineReducers
} from 'redux';
import ReduxThunkMiddleware from 'redux-thunk';
import { composeWithDevTools } from 'redux-devtools-extension';
import PouchDB from 'pouchdb';


import reducers from './reducers';
import middlewares from './middlewares';
import selectors from './selectors';
import actionCreators from './actionCreators';

import defaultConfig from './defaultConfig';

import type { AppConfig, StrictAppConfig, SyncPath, ActionCreator } from './types';

export default (appConfig: AppConfig, preloadedState) => {

  const syncPaths: Array<SyncPath> = R.append({
    name: "events",
    filter: (doc) => {
      return doc.type == "EVENT"; // & !doc.appliedOnClient;
    },
    actions: {
      remove: 'REMOVE_EVENT',
      update: 'UPDATE_EVENT',
      insert: 'ADD_EVENT',
      load: 'LOAD_EVENTS'
    }
  }, appConfig.syncPaths || []);

  const config = {
    ...defaultConfig,
    ...appConfig,
    syncPaths
  }

  const _allActionCreators: { [string]: ActionCreator } = {
    ...actionCreators,
    ...config.actionCreators
  }
  const allActionCreators = R.map(actionCreator => (...args) => actionCreator(...args, config), _allActionCreators);

  config.actionCreators = allActionCreators;

  const _allReducers = {
    ...reducers,
    ...config.reducers
  }
  const allReducers = R.map(reducer => (...args) => reducer(...args, config), _allReducers);

  const _allMiddlewares = config.ssr ? [] : [
    ...config.middlewares,
    ...middlewares
  ];
  const allMiddlewares = R.map(middleware => store => middleware(store, config), _allMiddlewares);

  const allSelectors = {
    ...selectors,
    ...config.selectors
  }

  const AppReducer = combineReducers(allReducers);
  const AppMiddleware = applyMiddleware(ReduxThunkMiddleware, ...allMiddlewares);

  let store = {};
  if (config.useReduxDevTools) {
    store = createStore(AppReducer, preloadedState, composeWithDevTools(AppMiddleware));
  } else {
    store = createStore(AppReducer, preloadedState, AppMiddleware);
  }

  const AppStore = { ...store, actions: allActionCreators, selectors: allSelectors };

  if (config.ssr) {
    const initialActions = config.getInitialActions(AppStore.getState, allActionCreators);
    initialActions.forEach(AppStore.dispatch);
    return AppStore;
  }

  setTimeout(() => {
    const initialActions = config.getInitialActions(AppStore.getState, allActionCreators);
    initialActions.forEach(AppStore.dispatch);
  }, 500);

  setInterval(async () => {
    const hasLocalEvents = R.values(AppStore.getState().events).some(R.prop('localOnly'));
    const isProcessing = AppStore.getState().processing_local.isProcessing;
    const isConnected = await config.isConnected();
    if (hasLocalEvents && !isProcessing && isConnected) {
      AppStore.dispatch({
        type: 'PROCESS_LOCAL_ONLY'
      });
    }
  }, 1000);

  const localSharedDB = new PouchDB("shared", { auto_compaction: true });
  setTimeout(async () => {
    const sharedDocs = await localSharedDB.allDocs({ include_docs: true });
    store.dispatch({
      type: 'LOAD_SHARED_DOCS',
      docs: sharedDocs.rows.filter(r => !r.id.startsWith('_design')).reduce((obj, row) => { return { ...obj, [row.id]: row.doc } }, {})
    })

    const changes = localSharedDB.changes({
      since: 'now',
      live: true,
      include_docs: true
    });<commit_msg>load shared docs<commit_after>// @flow
import R from 'ramda';
import {
  createStore,
  applyMiddleware,
  combineReducers
} from 'redux';
import ReduxThunkMiddleware from 'redux-thunk';
import { composeWithDevTools } from 'redux-devtools-extension';
import PouchDB from 'pouchdb';


import reducers from './reducers';
import middlewares from './middlewares';
import selectors from './selectors';
import actionCreators from './actionCreators';

import defaultConfig from './defaultConfig';

import type { AppConfig, StrictAppConfig, SyncPath, ActionCreator } from './types';

export default (appConfig: AppConfig, preloadedState) => {

  const syncPaths: Array<SyncPath> = R.append({
    name: "events",
    filter: (doc) => {
      return doc.type == "EVENT"; // & !doc.appliedOnClient;
    },
    actions: {
      remove: 'REMOVE_EVENT',
      update: 'UPDATE_EVENT',
      insert: 'ADD_EVENT',
      load: 'LOAD_EVENTS'
    }
  }, appConfig.syncPaths || []);

  const config = {
    ...defaultConfig,
    ...appConfig,
    syncPaths
  }

  const _allActionCreators: { [string]: ActionCreator } = {
    ...actionCreators,
    ...config.actionCreators
  }
  const allActionCreators = R.map(actionCreator => (...args) => actionCreator(...args, config), _allActionCreators);

  config.actionCreators = allActionCreators;

  const _allReducers = {
    ...reducers,
    ...config.reducers
  }
  const allReducers = R.map(reducer => (...args) => reducer(...args, config), _allReducers);

  const _allMiddlewares = config.ssr ? [] : [
    ...config.middlewares,
    ...middlewares
  ];
  const allMiddlewares = R.map(middleware => store => middleware(store, config), _allMiddlewares);

  const allSelectors = {
    ...selectors,
    ...config.selectors
  }

  const AppReducer = combineReducers(allReducers);
  const AppMiddleware = applyMiddleware(ReduxThunkMiddleware, ...allMiddlewares);

  let store = {};
  if (config.useReduxDevTools) {
    store = createStore(AppReducer, preloadedState, composeWithDevTools(AppMiddleware));
  } else {
    store = createStore(AppReducer, preloadedState, AppMiddleware);
  }

  const AppStore = { ...store, actions: allActionCreators, selectors: allSelectors };

  if (config.ssr) {
    const initialActions = config.getInitialActions(AppStore.getState, allActionCreators);
    initialActions.forEach(AppStore.dispatch);
    return AppStore;
  }

  setTimeout(() => {
    const initialActions = config.getInitialActions(AppStore.getState, allActionCreators);
    initialActions.forEach(AppStore.dispatch);
  }, 500);

  setInterval(async () => {
    const hasLocalEvents = R.values(AppStore.getState().events).some(R.prop('localOnly'));
    const isProcessing = AppStore.getState().processing_local.isProcessing;
    const isConnected = await config.isConnected();
    if (hasLocalEvents && !isProcessing && isConnected) {
      AppStore.dispatch({
        type: 'PROCESS_LOCAL_ONLY'
      });
    }
  }, 1000);

  const localSharedDB = new PouchDB("shared", { auto_compaction: true });
  setTimeout(async () => {
    const sharedDocs = await localSharedDB.allDocs({ include_docs: true });
    store.dispatch({
      type: 'LOAD_SHARED_DOCS',
      docs: sharedDocs.rows.filter(r => !r.id.startsWith('_design')).map(r => r.doc)
    })

    const changes = localSharedDB.changes({
      since: 'now',
      live: true,
      include_docs: true
    });
{'fraction_non_alphanumeric': 0.13963039014373715, 'fraction_numerical': 0.002053388090349076, 'mean_word_length': 3.683379120879121, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '2596946', 'n_tokens_mistral': 2178, 'n_tokens_neox': 2127, 'n_words': 440}