本文整理汇总了TypeScript中store-saga.createSaga函数的典型用法代码示例。如果您正苦于以下问题:TypeScript createSaga函数的具体用法?TypeScript createSaga怎么用?TypeScript createSaga使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了createSaga函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: createSaga
import {Http} from '@angular/http';
import {Router} from '@angular/router-deprecated';
import {Observable} from 'rxjs';
import {createSaga} from 'store-saga';
import {INCREMENT, DECREMENT, GOTO_ABOUT} from './../actions';
const BASE_WEB_URL = 'http://jsonplaceholder.typicode.com/';
const increment = createSaga(function() {
return iteration$ => iteration$
.filter(iter => iter.action.type === DECREMENT)
.map(() => {
return { type: INCREMENT };
});
});
const remoteMethod = () => new Promise((res, rej) => {
setTimeout(() => res(20), 1000);
});
const asyncEffect = createSaga(function sagaFactory(http: Http) {
return function loginSaga(iteration$: Observable<any>) {
return iteration$
.filter(iteration => iteration.action.type === DECREMENT)
.map(iteration => iteration.action.payload)
.mergeMap(payload => {
return Observable.fromPromise(remoteMethod())
.map(res => {
return {
type: INCREMENT
示例2: Schema
PATCH_PERSON_SUCCESS,
PATCH_PERSON_FAIL,
ADD_PERSON_REQUEST,
ADD_PERSON_SUCCESS,
ADD_PERSON_FAIL
} from '../reducers/people.reducer';
export const peopleSchema = new Schema('people');
const list = createSaga((_appHttp: AppHttp): Saga<any> => {
return saga$ => saga$
.filter(whenAction(LOAD_PEOPLE_REQUEST))
.mergeMap(
saga$ => _appHttp
.get('http://localhost:3100'+ getUrlWithIds('people', saga$.action.payload)),
(saga$, payload) => generateResponse(
payload,
LOAD_PEOPLE_SUCCESS,
LOAD_PEOPLE_FAIL,
(res) => normalize(res, arrayOf(peopleSchema))
)
);
}, [ AppHttp ]);
const one = createSaga((_appHttp: AppHttp): Saga<any> => {
return saga$ => saga$
.filter(whenAction(LOAD_PERSON_REQUEST))
.let(shouldLoad('people', 'payload'))
.mergeMap(
saga$ => _appHttp
.get(`http://localhost:3100/people/${saga$.action.payload}`),
(saga$, payload) => generateResponse(
示例3: createSaga
LOGIN_FAIL,
LOG_OUT_REQUEST,
LOG_OUT_SUCCESS
} from '../reducers/auth.reducer';
const login = createSaga((_http: AppHttp): Saga<any> => {
return saga$ => saga$
.filter(whenAction(LOGIN_REQUEST))
.delay(1)
.mergeMap(
saga$ => _http.post(
'http://localhost:3100/login',
JSON.stringify(saga$.action.payload)
),
(saga$, payload) => generateResponse(
payload,
LOGIN_SUCCESS,
LOGIN_FAIL,
(res) => {
localStorage.setItem('auth', JSON.stringify(res));
return res;
}
)
);
}, [ AppHttp ]);
const logout = createSaga((): Saga<any> => {
return saga$ => saga$
.filter(whenAction(LOG_OUT_REQUEST))
.delay(1)
示例4: createSaga
].map(effect => createSaga(effect));
示例5: createSaga
ADD_NOTIFICATION,
START_NOTIFICATIONS,
STOP_NOTIFICATIONS
} from '../reducers/notifications.reducer';
const add = createSaga((_store: Store<any>) => {
return saga$ => saga$
.filter(whenAction(START_NOTIFICATIONS))
.delay(1)
.mergeMap(authData => Observable
.webSocket(`ws://localhost:3200`)
.retryWhen((res) => {
return res.delay(5000);
})
.takeUntil(saga$.filter(whenAction(STOP_NOTIFICATIONS)))
)
.catch(res => {
return Observable.of(false);
})
.filter((res) => {
return !!res
})
.map(res => ({
type: ADD_NOTIFICATION,
payload: res
}));
}, [ Store ]);
export const notificationsSagas = [
add
];
示例6: createSaga
import {Observable} from 'rxjs/Observable';
export const loginRequest = createSaga((http:Http)=>{
return saga$=>saga$.filter(whenAction('LOGIN_REQUEST'))
.do(e=>{console.log('login request ',e);
e.state.loading=true;
})
//.delay(1000)
.flatMap((saga)=>{
console.log(saga);
var data={username:saga.action.payload.username,password:saga.action.payload.password};
console.log(saga.state);
const query=http.post('http://virtual2.ballistix.co.uk:8000/api/token',JSON.stringify(data));
return query.map(e=>e.json());
})
.map( res => {
console.log(res);
if (res.status==200){
localStorage.setItem('token',res.token);
return {type:'LOGIN_COMPLETE',payload:res.token};
}
})
.catch(error => {
console.log('error');
return Observable.of(false);
}
)
},[Http]);
export const increment = createSaga((http:Http)=>{
示例7: createSaga
import {FETCH, UPDATE, DETAIL, UPDATE_SELECTED} from './../reducers/repos';
const BASE_WEB_URL = 'https://api.github.com/';
const fetchEffect = createSaga(function sagaFactory(http: Http) {
return function fetchSaga(iteration$: Observable<any>) {
return iteration$
.filter(iteration => iteration.action.type === FETCH)
.map(iteration => iteration.action.payload)
.mergeMap(payload => {
return http.get(BASE_WEB_URL + 'orgs/' + payload + '/repos')
.map(res => {
return {
type: UPDATE,
payload: res.json()
}
})
.catch(err => {
return Observable.of({
type: UPDATE,
payload: []
});
});
});
};
}, [Http]);
const fetchSingleEffect = createSaga(function sagaFactory(http: Http) {
return function fetchSingleSaga(iteration$: Observable<any>) {
return iteration$
示例8: createSaga
import { Provider } from '@angular/core';
import { createSaga, toPayload, whenAction } from 'store-saga';
import { ApiService } from 'src/core/api';
import * as actions from './action-creators';
import * as types from './action-types';
//=====================================
// SAGAS
//-------------------------------------
export const createTask = createSaga((api: ApiService) => {
return saga$ => saga$
.filter(whenAction(types.CREATE_TASK))
.map(toPayload)
.switchMap(payload => api
.createTask(payload.task)
.map(task => actions.createTaskSuccess(task)));
}, [ApiService]);
export const deleteTask = createSaga((api: ApiService) => {
return saga$ => saga$
.filter(whenAction(types.DELETE_TASK))
.map(toPayload)
.switchMap(payload => api
.deleteTask(payload.taskId)
.map(task => actions.deleteTaskSuccess(task)));
}, [ApiService]);
示例9: createSaga
import {ADD_AUDIOITEM_TO_PLAYLIST, UPDATE_AUDIOITEM_IN_PLAYLIST} from '../reducers/playlistReducer';
import {IAudiodata} from '../reducers/audioReducer';
import {PLAY_INITIATED, PLAY_START, PLAY_STOP, TOGGLE_PLAY_PAUSE} from '../reducers/audioplayerReducer';
import * as ArtistAPI from '../../api/artistAPI';
import * as DataLoadAPI from '../../api/dataloadAPI';
import { WebAudioPlayerAPI } from '../../api/webaudioAPI';
import { Observable } from 'rxjs/Observable';
const artistFetch = createSaga( function(){
return iteration$ => iteration$
.filter(whenAction(REQUEST_ARTISTS))
.mergeMap(() => ArtistAPI.default.getArtists(300))
.map(res => {
return {
type: RECEIVED_ARTISTS,
payload: res
};
});
});
const createPlayListItem = createSaga( function sagaFactory() {
return function someService(iteration$: Observable<any>) {
return iteration$ .filter(whenAction(CREATE_AUDIODATA))
.mergeMap((iteration) => {
if(iteration.state.audioItem){
var trackURL = Object.keys(iteration.state.artists)
.filter( x => {
return iteration.state.artists[x].id === iteration.state.audioItem.artistId})