本文整理匯總了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]);