Skip to content

Commit abeb847

Browse files
author
Podgorniy Mikhail
committed
Fix issue vuejs#1115
1 parent bd23b9a commit abeb847

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ explorations
1010
*.log
1111
test/e2e/reports
1212
test/e2e/screenshots
13+
*.history

src/store.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,19 @@ export class Store {
129129
return
130130
}
131131

132-
this._actionSubscribers.forEach(sub => sub(action, this.state))
132+
this._actionSubscribers
133+
.filter(sub => sub.before)
134+
.forEach(sub => sub.before(action, this.state))
133135

134-
return entry.length > 1
136+
const result = entry.length > 1
135137
? Promise.all(entry.map(handler => handler(payload)))
136138
: entry[0](payload)
139+
140+
result.then(() => this._actionSubscribers
141+
.filter(sub => sub.after)
142+
.forEach(sub => sub.after(action, this.state)))
143+
144+
return result
137145
}
138146

139147
subscribe (fn) {

types/index.d.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export declare class Store<S> {
1919
commit: Commit;
2020

2121
subscribe<P extends MutationPayload>(fn: (mutation: P, state: S) => any): () => void;
22-
subscribeAction<P extends ActionPayload>(fn: (action: P, state: S) => any): () => void;
22+
subscribeAction<P extends ActionPayload>(fn: SubscribeActionOptions<P, S>): () => void;
2323
watch<T>(getter: (state: S, getters: any) => T, cb: (value: T, oldValue: T) => void, options?: WatchOptions): () => void;
2424

2525
registerModule<T>(path: string, module: Module<T, S>, options?: ModuleOptions): void;
@@ -69,6 +69,15 @@ export interface ActionPayload extends Payload {
6969
payload: any;
7070
}
7171

72+
export type ActionSubscriber<P, S> = (action: P, state: S) => any;
73+
74+
export interface ActionSubscribersObject<P, S> {
75+
before?: ActionSubscriber<P, S>;
76+
after?: ActionSubscriber<P, S>;
77+
}
78+
79+
export type SubscribeActionOptions<P, S> = ActionSubscriber<P, S> | ActionSubscribersObject<P, S>;
80+
7281
export interface DispatchOptions {
7382
root?: boolean;
7483
}

0 commit comments

Comments
 (0)