Example #1
0
function testContext() {
  interface Context {
    a: string;
    b: number;
  }

  // typings:expect-error
  createSagaMiddleware<Context>({context: {c: 42}});

  // typings:expect-error
  createSagaMiddleware({context: 42});

  const middleware = createSagaMiddleware<Context>({
    context: {a: '', b: 42},
  });

  // typings:expect-error
  middleware.setContext({c: 42});

  middleware.setContext({b: 42});

  const task = middleware.run(function* () {yield effect});
  task.setContext({b: 42});

  task.setContext<Context>({a: ''});
  // typings:expect-error
  task.setContext<Context>({c: ''});
}
Example #2
0
function testOptions() {
  const emptyOptions = createSagaMiddleware({});

  const withOptions = createSagaMiddleware({
    onError(error) {
      console.error(error);
    },

    logger(level, ...args) {
      console.log(level, ...args);
    },

    sagaMonitor: {
      effectTriggered() { },
    },

    emitter: emit => action => {
      if (Array.isArray(action)) {
        action.forEach(emit);
        return
      }
      emit(action);
    },
  });

  const withMonitor = createSagaMiddleware({
    sagaMonitor: {
      effectTriggered() {},
      effectResolved() {},
      effectRejected() {},
      effectCancelled() {},
      actionDispatched() {},
    }
  });
}
Example #3
0
export default (history: History, dsn: string) => {
  const sagaMiddleware: SagaMiddleware<{}> = createSagaMiddleware();
  let middlewares: Middleware[] = [];
  if (process.env.NODE_ENV !== 'production') {
    middlewares = [
      createLogger(),
    ];
  }
  Raven.config(dsn).install();
  const devtools: any = process.env.NODE_ENV !== 'production' && (window as any)._REDUX_DEVTOOLS_EXTENSION__ ?
    (window as any)._REDUX_DEVTOOLS_EXTENSION__() : (f: any) => f;
  const store = createStore(
    createRootReducer(history),
    compose(
      applyMiddleware(
        routerMiddleware(history),
        googleAnalytics,
        sagaMiddleware,
        createRavenMiddleware(Raven, {}),
        ...middlewares,
      ),
      devtools,
    ),
  );
  sagaMiddleware.run(rootSaga);
  return store;
};
Example #4
0
export function configureStore() {
    const sagaMiddleware = createSagaMiddleware({
        onError: () => null,
    });

    function runSaga(saga: Effect): Task {
        return sagaMiddleware.run(SagaRunner, saga);
    }

    function runSagaInitialized(saga: any): Task {
        return sagaMiddleware.run(SagaInitialized, saga);
    }

    const store = createStore(
        reducer,
        applyMiddleware(sagaMiddleware)
    );

    (store as any).runSaga = runSaga;
    (store as any).runSagaInitialized = runSagaInitialized;
    (store as any).sagaMiddleware = sagaMiddleware;

    return store as ExtendedStore<typeof store, {
        runSaga: RunSagaEffect;
        runSagaInitialized: RunSagaEffect;
        sagaMiddleware: SagaMiddleware,
    }>;
}
Example #5
0
const createStore = (initialState?: State): Store<State> => {
  const sagaMiddleware = createSagaMiddleware();
  const enhancer = applyMiddleware(sagaMiddleware);
  const store = create<State>(reducer, initialState, enhancer);
  sagaMiddleware.run(saga);
  return store;
};
Example #6
0
export const configureStore = () => {
  const sagaMiddleware = createSagaMiddleware()
  const logger = createLogger()
  const store = createStore(
    persistReducer({ key: 'reversi', storage, whitelist: ['history'] }, reducer),
    compose(
      applyMiddleware(sagaMiddleware, logger),
      getDevtools()
    )
  )
  const persistor = persistStore(store)

  if (module.hot) {
    module.hot.accept('./reducer', () => {
      store.replaceReducer(require('./reducer').default)
    })
  }

  return {
    store,
    persistor,
    runSaga: sagaMiddleware.run,
    close () {
      store.dispatch(END as any)
    }
  }
}
export function configureStore(preloadedState?: IApplicationState): SagaEnhancedStore<IApplicationState> {
  const sagaMiddleware = createSagaMiddleware()

  const middlewares = [
    routerMiddleware(browserHistory),
    sagaMiddleware,
  ]

  const store = createStore<IApplicationState | undefined>(
    rootReducer,
    preloadedState,
    compose<any, any>(
      applyMiddleware(...middlewares),
      devTools(),
    ),
  ) as SagaEnhancedStore<IApplicationState>

  sagaMiddleware.run(rootSaga)

  store.runSaga = sagaMiddleware.run
  store.close = () => store.dispatch(END)

  // Hot reload reducers:
  // https://github.com/reactjs/react-redux/releases/tag/v2.0.0
  /* tslint:disable:no-string-literal */
  if (process.env.NODE_ENV === "development" && module["hot"]) {
    module["hot"].accept("./reducers", () => {
      const nextRootReducer = require("./reducers").default
      store.replaceReducer(nextRootReducer)
    })
  }
  /* tslint:enable:no-string-literal */

  return store
}
Example #8
0
function testRun() {
  const middleware = createSagaMiddleware()

  middleware.run(function* saga(): SagaIterator {})

  // TODO: https://github.com/Microsoft/TypeScript/issues/28803
  {
    // typings:expect-error
    // middleware.run(function* saga(a: 'a'): SagaIterator {})
  }

  // typings:expect-error
  middleware.run(function* saga(a: 'a'): SagaIterator {}, 1)

  middleware.run(function* saga(a: 'a'): SagaIterator {}, 'a')

  // TODO: https://github.com/Microsoft/TypeScript/issues/28803
  {
    // typings:expect-error
    // middleware.run(function* saga(a: 'a', b: 'b'): SagaIterator {}, 'a')
  }

  // typings:expect-error
  middleware.run(function* saga(a: 'a', b: 'b'): SagaIterator {}, 'a', 1)

  // typings:expect-error
  middleware.run(function* saga(a: 'a', b: 'b'): SagaIterator {}, 1, 'b')

  middleware.run(function* saga(a: 'a', b: 'b'): SagaIterator {}, 'a', 'b')

  // test with any iterator i.e. when generator doesn't always yield Effects.
  middleware.run(function* saga() {
    yield promise
  })
}
Example #9
0
export default function configureStore(initialState: DeepPartial<RootState>) {
  const sagaMiddleware = createSagaMiddleware();
  
  const middleware = [
    sagaMiddleware
  ];

  const composeEnhancers = composeWithDevTools({});

  const composedEnhancers = composeEnhancers(
    applyMiddleware(...middleware)
  );

  const store = createStore(
    rootReducer,
    initialState,
    composedEnhancers
  );

  for (const sagaKey of Object.keys(sagas)) {
    sagaMiddleware.run(sagas[sagaKey]);
  }

  return store;
}
Example #10
0
export function createAdminUIStore() {
  const sagaMiddleware = createSagaMiddleware();

  const store = createStore(
    combineReducers<AdminUIState>({
      cachedData: apiReducersReducer,
      hover: hoverReducer,
      localSettings: localSettingsReducer,
      metrics: metricsReducer,
      queryManager: queryManagerReducer,
      routing: routerReducer,
      timewindow: timeWindowReducer,
      uiData: uiDataReducer,
    }),
    compose(
      applyMiddleware(thunk, sagaMiddleware),
      // Support for redux dev tools
      // https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd
      (window as any).devToolsExtension ? (window as any).devToolsExtension({
        // TODO(maxlang): implement {,de}serializeAction.
        // TODO(maxlang): implement deserializeState.
        serializeState: (_key: string, value: any): Object => {
          if (value && value.toRaw) {
            return value.toRaw();
          }
          return value;
        },
      }) : _.identity,
    ) as GenericStoreEnhancer,
  );

  sagaMiddleware.run(queryMetricsSaga);
  return store;
}