Skip to content

Commit

Permalink
Fix issue vuejs#1115
Browse files Browse the repository at this point in the history
  • Loading branch information
Podgorniy Mikhail committed Oct 29, 2018
1 parent bd23b9a commit abeb847
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ explorations
*.log
test/e2e/reports
test/e2e/screenshots
*.history
12 changes: 10 additions & 2 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,19 @@ export class Store {
return
}

this._actionSubscribers.forEach(sub => sub(action, this.state))
this._actionSubscribers
.filter(sub => sub.before)
.forEach(sub => sub.before(action, this.state))

return entry.length > 1
const result = entry.length > 1
? Promise.all(entry.map(handler => handler(payload)))
: entry[0](payload)

result.then(() => this._actionSubscribers
.filter(sub => sub.after)
.forEach(sub => sub.after(action, this.state)))

return result
}

subscribe (fn) {
Expand Down
11 changes: 10 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export declare class Store<S> {
commit: Commit;

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

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

export type ActionSubscriber<P, S> = (action: P, state: S) => any;

export interface ActionSubscribersObject<P, S> {
before?: ActionSubscriber<P, S>;
after?: ActionSubscriber<P, S>;
}

export type SubscribeActionOptions<P, S> = ActionSubscriber<P, S> | ActionSubscribersObject<P, S>;

export interface DispatchOptions {
root?: boolean;
}
Expand Down

0 comments on commit abeb847

Please sign in to comment.