Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Providing process ID to all middleware callbacks #409

Merged
merged 2 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/stores/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export interface ProcessCallbackAfter<T = any> {
}

export interface ProcessCallbackBefore<T = any, P extends object = DefaultPayload> {
(payload: P, store: Store<T>): void | Promise<void>;
(payload: P, store: Store<T>, id: string): void | Promise<void>;
}

/**
Expand Down Expand Up @@ -202,7 +202,7 @@ export function processExecutor<T = any, P extends object = DefaultPayload>(
const payload = transformer ? transformer(executorPayload) : executorPayload;

if (before) {
let result = before(payload, store);
let result = before(payload, store, id);
if (result) {
await result;
}
Expand Down Expand Up @@ -374,7 +374,7 @@ export function createProcess<T = any, P extends object = DefaultPayload>(

const callback = callbacks.length
? callbacks.reduce((callback, nextCallback) => {
return combineCallbacks(nextCallback)(callback);
return combineCallbacks(nextCallback, id)(callback);
})
: undefined;

Expand Down Expand Up @@ -403,8 +403,9 @@ export function createProcessFactoryWith(callbacks: ProcessCallback[]) {
/**
* Creates a `ProcessCallbackDecorator` from a `ProcessCallback`.
* @param processCallback the process callback to convert to a decorator.
* @param id process id to be passed to the before callback
*/
function combineCallbacks(processCallback: ProcessCallback): ProcessCallbackDecorator {
function combineCallbacks(processCallback: ProcessCallback, id: string): ProcessCallbackDecorator {
const { before, after } = processCallback();
return (previousCallback?: ProcessCallback) => {
const { before: previousBefore = undefined, after: previousAfter = undefined } = previousCallback
Expand All @@ -422,11 +423,11 @@ function combineCallbacks(processCallback: ProcessCallback): ProcessCallbackDeco
},
before(payload: DefaultPayload, store: Store<any>) {
if (previousBefore) {
previousBefore(payload, store);
previousBefore(payload, store, id);
}

if (before) {
before(payload, store);
before(payload, store, id);
}
}
});
Expand Down
23 changes: 23 additions & 0 deletions tests/stores/unit/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,29 @@ const tests = (stateType: string, state?: () => MutableState<any>) => {
executor({});
assert.strictEqual(store.get(store.path('foo')), 'bar');
});

it('passes process id to middleware callbacks', () => {
let before: string[] = [];
let after: string[] = [];

function middleware(): ProcessCallback {
return () => ({
before(payload, store, id) {
before.push(id);
},
after(errorState, result) {
after.push(result.id);
}
});
}

const process = createProcess('test', [], [middleware(), middleware()]);
const executor = process(store);
executor({});

assert.deepEqual(before, ['test', 'test']);
assert.deepEqual(after, ['test', 'test']);
});
});
};

Expand Down