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

Enforce initial state when creating a store #744

Open
devpaul opened this issue Apr 21, 2020 · 0 comments
Open

Enforce initial state when creating a store #744

devpaul opened this issue Apr 21, 2020 · 0 comments

Comments

@devpaul
Copy link
Member

devpaul commented Apr 21, 2020

Enhancement

I often run into discrepancies between the declared state of a store and the actual values held by the store. Much of this stems from improper or incomplete initialization of the store. This was ok when using the JSON patch syntax in commands because that would often create missing intermediary objects. However, since I've been using state it is much more difficult. and has made the problem of incorrect store state more obvious.

The problem with mismatched state is compounded by the difficulty in actually testing if your application correctly uses state. For instance if you have two commands, one that creates an object and another that updates that object. These commands can be used one after another without issue, but if the update command is used before the create command then often a TypeError: undefined will be thrown. This interaction cannot be caught with unit tests because the store is usually mocked to adhere to the declared state (and the declared state practically never includes undefined everywhere for uninitialized stores). The only way to catch these class of errors is to e2e test all combinations of command paths allowed by the application -- a very daunting task.

Instead, if Dojo had an initialization step that enforced the initial state of the application it would eliminate these type of difficult to catch errors that are likely to slip into production. Ideally when the store is initialized it would return a Promise so a user could initialize asynchronously and while an initial state is provided a loading screen could be displayed or a value could be set in the store to indicate the application hasn't been initialized.

Having this or any form of required initialization for the store would greatly help to close the loop between the actual value and declared values of stores.

Package Version: 7.alpha.16

Code

Ideally the defined state should always match the declared state. This is the best way I've been able to make that happen.

const commandFactory = createCommandFactory<State>();
const initializeCommand = commandFactory<State>(({ payload, path }) => {
	return Object.entries(payload).map(([key, value]) => replace(path(key as keyof State), value));
});
const initialize = createProcess('initialize', [initializeCommand]);

export const store = createStoreMiddleware<State>(async (store) => {
	const matrixVersion = await getMatrixVersion(matrix);
	const initialState: State = {
		matrix,
		matrixVersion,
		skills: {},
		compare: {
			assessments: []
		}
	};

	initialize(store)(initialState);
});

Expected behavior:

I would like Dojo to go through an initialization step that ensures an initial state matching the defined state. I would like this initializer to return a promise so I can act on its completion.

Actual behavior:

Dojo doesn't require any initialization of the store. This is often a source of error that is extremely difficult to test.

@maier49 maier49 self-assigned this May 22, 2020
@maier49 maier49 removed their assignment May 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants