@litewash/redux-effect
v0.0.1
Published
外部调用示例: ========== xxxModel.ts: --------------- ```javascript export class Xxx { xxxId:string; yyy:string; zzz:string; } ``` xxxActions.ts: --------------- ```javascript import {ActionAsyncCreator, ActionCreator, createActionAsync, createActio
Downloads
2
Readme
外部调用示例:
xxxModel.ts:
export class Xxx {
xxxId:string;
yyy:string;
zzz:string;
}
xxxActions.ts:
import {ActionAsyncCreator, ActionCreator, createActionAsync, createAction} from '@litewash/redux-effect';
export const load= createActionAsync('xxx_load');
export const do= createActionAsync<{xxxId:string}>('xxx_do');
export const onDo= createAction<{xxxId:string}>('xxx_onDone');
export default {load, do, onDo};
xxxReducer.ts:
import update from 'immutability-helper';
import { Action, Reducer, createReducer } from 'redux-act';
import { Xxx } from './xxxModel';
import xxxActions from './xxxActions';
export const XxxsReducer={
[xxxActions.load.res.toString()]: (oldXxxs:Xxx, newXxxs:Xxx) => {
let result=update(oldXxxs, {$set:newXxxs});
return result;
},
[xxxActions.do.res.toString()]: (oldXxx:Xxx, newXxxs:Xxx) => {
let result=update(oldXxxs, {$set:newXxxs});
return result;
},
[xxxActions.onDone.toString()]: (oldXxxs:Report[], newXxx:Report) => {
let newXxxs = update(oldXxxs, {$push:[newXxx]});
return newXxxs;
}
};
export const xxxsReducer=createReducer<Xxx>(XxxsReducer, null);
xxxService.ts:
import gql from 'graphql-tag';
import {httpFetch, serviceManager, authActions, apolloService, storeService, ServiceType} from '@litewash/redux-effect';
import xxxActions from '../xxx/xxxActions';
import { Xxx } from './xxxModel';
serviceManager.reg(xxxActions.load, load);
function load(params, callback) {
httpFetch.get('/xxx/load', params, (err, xxxs) => {
if (err) return callback(err);
callback(null, xxxs);
});
}
serviceManager.reg(xxxActions.do, do, ServiceType.async);
async function do(order) {
let result=await apolloService.client.mutate({
variables:{ xxxId:xxxId },
mutation: gql`
mutation($xxxId:String!) {
do(xxxId:$xxxId) { xxxId, yyy, zzz }
}
`
});
let xxxs=result.data['xxxs'];
return xxxs;
}
export default {
load, do
}
xxxSubscription.ts:
import gql from 'graphql-tag';
import { Subscription } from 'apollo-client/util/Observable';
import { serviceManager, storeService, apolloService, apolloActions, ServiceType } from '@litewash/redux-effect';
import { Xxx } from '../xxx/xxxModel';
import xxxActions from './xxxActions';
let subscriptions:{[key:string]:Subscription}={};
function subscribe(actionType:string, roomId:string, subscription:Subscription) {
subscriptions[actionType+'_'+roomId]=subscription;
}
function unsubscribe(actionType:string, roomId:string) {
if (!subscriptions[actionType+'_'+roomId]) return;
try { subscriptions[actionType+'_'+roomId].unsubscribe(); }
catch(e) { }
}
export function unregDone() {
unsubscribe(xxxActions.onDone.toString(), '');
}
export function regDone() {
unregDone();
let subscription=apolloService.client.subscribe({
query: gql`
subscription {
onDone { xxxId, yyy, zzz }
}
`
}).subscribe({
next({data, err}) {
if (err) return;
let xxx:Xxx=data['onDone'];
storeService.dispatch(xxxActions.onDone(xxx));
},
error(err) { console.log(err); },
});
subscribe(xxxActions.onDone.toString(), '', subscription);
}
export default {
regDone
};