Skip to content

Commit

Permalink
fix: fixed trigger order
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcourtice committed Sep 8, 2021
1 parent c886f3f commit 4d4d26e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
9 changes: 5 additions & 4 deletions core/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,22 @@ export default class Store<TState extends BaseState = any> implements InternalSt
throw new Error('Circular mutation reference detected. Avoid calling mutations inside other mutations to prevent circular references.');
}

const providedState = this.providers.write(this.writeState) ?? this.writeState;
const providedPayload = this.providers.payload(payload) ?? payload;
this.stack.add(name);

let result: TResult;

const emit = (event: string) => this.emit(event, sender, {
mutation: name,
payload: providedPayload,
payload,
result,
} as MutationEventData<TPayload, TResult>);

this.stack.add(name);
emit(EVENTS.mutation.before);

try {
const providedState = this.providers.write(this.writeState) ?? this.writeState;
const providedPayload = this.providers.payload(payload) ?? payload;

result = mutator(providedState, providedPayload);

emit(EVENTS.mutation.success);
Expand Down
5 changes: 3 additions & 2 deletions extensions/action/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ export default function actionsExtension<TState extends BaseState>() {

const task = new Task<TResult>(async (resolve, reject, controller, onAbort) => {
const id = Symbol(name);
const providedPayload = _store.providers.payload(payload) ?? payload;

const complete = () => (tasks.delete(task), removeInstance(name, id));
const fail = () => reject(new ActionAbortError(name, id));
Expand All @@ -114,7 +113,7 @@ export default function actionsExtension<TState extends BaseState>() {

const emit = (event: string) => _store.emit(event, SENDER, {
action: name,
payload: providedPayload,
payload,
result,
} as ActionEventData);

Expand All @@ -124,6 +123,8 @@ export default function actionsExtension<TState extends BaseState>() {
emit(EVENTS.action.before);

try {
const providedPayload = _store.providers.payload(payload) ?? payload;

result = await body(providedPayload, mutate, controller, onAbort);

emit(EVENTS.action.success);
Expand Down
4 changes: 1 addition & 3 deletions extensions/transaction/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,15 @@ export default function transactionExtension<TState extends BaseState>() {
mutationName: '$transaction-rollback',
})(store);


function transaction<TPayload>(name: string, transactor: Transactor<TState, TPayload>): Transaction<TPayload> {
const mutate = (mutator: Mutator<TState, undefined, void>) => store.write(name, SENDER, mutator);

return ((payload: TPayload) => {
const snap = snapshot();
const providedPayload = store.providers.payload(payload) ?? payload;

const emit = (event: string) => store.emit(event, SENDER, {
transaction: name,
payload: providedPayload,
payload,
} as TransactionEventData);

emit(EVENTS.transaction.before);
Expand Down

0 comments on commit 4d4d26e

Please sign in to comment.