Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Proxy adapter instead of having to manually create operations #177

Open
matt-gadd opened this issue Jun 26, 2018 · 0 comments
Open

Proxy adapter instead of having to manually create operations #177

matt-gadd opened this issue Jun 26, 2018 · 0 comments

Comments

@matt-gadd
Copy link
Contributor

matt-gadd commented Jun 26, 2018

Enhancement
If we leverage Proxies, I think we can just about track the appropriate paths and changes to allow the user to simply do object and array accessing and setting rather than having to use the get helper and returning the associated operations.

The following would go from:

function calculateCountsCommand({ get, path }: CommandRequest) {
	const todos = get(path('todos'));
	const completedTodos = todos.filter((todo: any) => todo.completed);
	const operations = [
		replace(path('activeCount'), todos.length - completedTodos.length),
		replace(path('completedCount'), completedTodos.length)
	];
	return operations;
}

to:

function calculateCountsCommand(state) {
	const completedTodos = state.todos.filter((todo: any) => todo.completed);
	state.activeCount = state.todos.length - completedTodos.length;
	state.completedCount = completedTodos.length;
}

This would work via the state object being passed in being Proxy wrapped and everything off it. Every time a set is called we'd create the appropriate operation.

A few caveats:

  • Won't work in IE11 (so we need to support the existing way)
  • The functions no longer take an input and return an output (would have to think about the testing implications)
  • Performance?
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant