本文整理汇总了TypeScript中@ngrx/store.createMiddleware函数的典型用法代码示例。如果您正苦于以下问题:TypeScript createMiddleware函数的具体用法?TypeScript createMiddleware怎么用?TypeScript createMiddleware使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了createMiddleware函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: main
export function main() {
return bootstrap(AsyncApp, [
ELEMENT_PROBE_PROVIDERS,
HTTP_PROVIDERS,
provideStore({selectedReddit, postsByReddit}),
usePreMiddleware(createMiddleware(redditPreMiddleware, [Reddit])),
Reddit
])
.catch(err => console.error(err));
}
示例2: createMiddleware
import 'rxjs/add/operator/withLatestFrom';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/do';
import { Provider } from '@angular/core';
import { createMiddleware, Dispatcher } from '@ngrx/store';
import { SagaRunner } from './runner';
export const sagaMiddleware = createMiddleware(function middlewareFactory(
dispatcher$: Dispatcher<any>, runner: SagaRunner) {
return state$ => state$
.withLatestFrom(dispatcher$, (state, action) => ({ state, action }))
.do(runner)
.map(({ state }) => state);
}, [ Dispatcher, SagaRunner ]);
示例3: createMiddleware
import 'rxjs/add/operator/do';
import {provide, Provider, Injector} from 'angular2/core';
import {createMiddleware} from '@ngrx/store';
import {RouterSubscriber} from './subscriber';
import {Router, Location} from 'angular2/router';
export default createMiddleware((router: Router, location: Location, rs: any) => {
return state$ => state$.do(s => {
if (s.router.url !== location.path()) {
router.navigateByUrl(s.router.url);
}
})
}, [Router, Location, RouterSubscriber]);
示例4: middlewareFactory
import 'rxjs/add/operator/withLatestFrom';
import 'rxjs/add/operator/map';
import { Injector, Provider } from 'angular2/core';
import { createMiddleware, Dispatcher } from '@ngrx/store';
import { SagaRunner } from './runner';
export function middlewareFactory(dispatcher$: Dispatcher<any>, injector: Injector) {
let runner: SagaRunner;
return state$ => state$
.withLatestFrom(dispatcher$)
.map(([ state, action ]) => {
if( !runner ) {
runner = injector.get(SagaRunner);
}
runner.next({ state, action });
return state;
});
}
export const sagaMiddleware = createMiddleware(middlewareFactory, [
Dispatcher,
Injector
]);
示例5: catch
try {
logger.groupEnd();
} catch (e) {
logger.log(`—— log end ——`);
}
});
logBuffer.length = 0;
};
const preLogger = createMiddleware((log, options) => {
return action$ => action$
.do(action => {
const {stateTransformer} = options;
let logEntry = {
started: timer.now(),
startedTime: new Date(),
prevState: stateTransformer(log.getValue()),
action
};
log.next(logEntry);
});
}, [ LOGGER, LOGGER_OPTIONS ]);
const postLogger = createMiddleware((log, loggerBuffer, options) => {
const {stateTransformer} = options;
return state$ => state$
.do(state => {
if(state.type !== INIT_ACTION) {
let logInfo = log.getValue();
//fixes issue caused by using with dev tools
if(logInfo) {
示例6: createMiddleware
export const localStorageMiddleware = key => createMiddleware(localStorageService => {
return state => {
//sync specified state slice with local storage
return state.do(state => localStorageService.setItem(key, state[key]));
}
}, [LocalStorageService]);