Skip to content

Commit

Permalink
- extends RunSagaOptions and add a dispatchSagaAction flag to enable/…
Browse files Browse the repository at this point in the history
…disable dispatchSagaAction
  • Loading branch information
albertleigh committed Nov 19, 2019
1 parent ca598a4 commit 2a446af
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
4 changes: 1 addition & 3 deletions sample/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"name": "sample-vue-ts-app",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
Expand All @@ -18,7 +16,7 @@
"vue-router": "^3.1.3",
"vuex": "^3.0.1",
"vuex-class": "^0.3.2",
"vuex-saga-ts": "^0.20.1"
"vuex-saga-ts": "^0.20.2"
},
"devDependencies": {
"@types/jest": "^24.0.11",
Expand Down
5 changes: 3 additions & 2 deletions sample/src/composables/compzApplicatoinCtx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { SagaActions } from '@/store/application'
import { mainStoreInjector } from './MainStore'
import {IApplicationState} from "@/store/application/types";

interface IApplicaitonCtx extends IApplicationState{
interface IApplicaitonCtx{
state:IApplicationState,
actions:{
start:()=>void;
}
Expand All @@ -18,7 +19,7 @@ export function compzApplicatoinCtx():IApplicaitonCtx{

return {
// @ts-ignore
...toRefs(applicationsState),
state:toRefs(applicationsState),
actions:{
start: ()=>{store.sagaDispatchAction(SagaActions.start())}
}
Expand Down
4 changes: 3 additions & 1 deletion sample/src/views/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export default createComponent({
},
setup(){
const {
loading,
state:{
loading
},
actions:{start}
} = compzApplicatoinCtx();
Expand Down
11 changes: 8 additions & 3 deletions src/VuexSaga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export function mapSagaActions(actions:SagaTree):MappedSagaMethodMap {
return res;
}

interface ExtSagaOption<Action,State> extends RunSagaOptions<Action,State> {
dispatchSagaAction:boolean;
}

/**
* Main plugin function
*
Expand All @@ -53,18 +57,19 @@ export function VuexSaga<
S extends Saga
>(
saga: S,
options?: RunSagaOptions<Action, State>,
options?: ExtSagaOption<Action, State>,
...args: Parameters<S>
) {
return (store:Store<State>) => {
const channel = stdChannel();
const dispatchSagaAction = options && options.dispatchSagaAction;
// eslint-disable-next-line no-param-reassign
(store as any ).sagaDispatch = (type, payload={}) => {
store.commit({ type, payload });
dispatchSagaAction && store.commit({ type, payload });
return channel.put({ type, payload })
};
(store as any ).sagaDispatchAction = (action:Action) =>{
store.commit(action);
dispatchSagaAction && store.commit(action);
return channel.put(action)
};
runSaga(
Expand Down

0 comments on commit 2a446af

Please sign in to comment.