Skip to content

Proxy adapter instead of having to manually create operations #40

@agubler

Description

@agubler

@matt-gadd commented on Tue 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?

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions