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

Provide process id to before middleware #308

Closed
devpaul opened this issue Mar 25, 2019 · 0 comments · Fixed by #409
Closed

Provide process id to before middleware #308

devpaul opened this issue Mar 25, 2019 · 0 comments · Fixed by #409
Labels
area: core Core enhancement New feature or request next Issue/Pull Request for the next major version

Comments

@devpaul
Copy link
Member

devpaul commented Mar 25, 2019

Enhancement

I created a middleware that automatically updates the store to indicate that the process is in progress using an object literal mapping in the store. Its easy to update the related state in the after middleware because it gets the process id. Unfortunately the before middleware does not. It'd be great if it did! :)

Package Version: Dojo 5

Code

Here's the example middleware. It needs to be created via a function to provide the id to before.

const setComplete = createProcess('request-complete', [
	commandFactory(({ path, payload: { id } }) => {
		return [
			replace(path('request', id), {
				isLoading: false
			})
		];
	})
]);

const setStarting = createProcess('request-sent', [
	commandFactory(({ path, payload: { id } }) => {
		return [
			replace(path('request', id), {
				isLoading: true
			})
		];
	})
]);

const setError = createProcess('set-error', [
	({ path, payload: { id, error } }) => {
		return [replace(path('request', id), { isLoading: false, message: error.message, error })];
	}
]);

export function createRequestMiddleware(id: string): ProcessCallback<State> {
	return () => ({
		before(payload, store) {
			setStarting(store)({ id });
		},
		after(errorState, result) {
			if (errorState) {
				const { error } = errorState;
				if (error) {
					result.executor(setError, { id, error });
				}
			} else {
				result.executor(setComplete, { id });
			}
		}
	});
}

It's currently used like this

export const login = appProcess('login', [loginCommand, getSelfCommand], createRequestMiddleware('login'));

The Login container uses state created by this middleware to get error messages automatically and know when the process is loading and disable the login button.

Expected behavior:

If the process id was provided to before I could create a generic requestMiddleware and use it as such:

export const login = appProcess('login', [loginCommand, getSelfCommand], [requestMiddleware]);

this would eliminate accidentally passing the wrong id when creating the middleware and obviate the need for the middleware factory, not to mention it should be an easy PR to add id here https://github.com/dojo/framework/blob/master/src/stores/process.ts#L178

@agubler agubler added enhancement New feature or request next Issue/Pull Request for the next major version area: core Core labels Mar 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: core Core enhancement New feature or request next Issue/Pull Request for the next major version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants