solidjs-community / solid-primitives

A library of high-quality primitives that extend SolidJS reactivity.
https://primitives.solidjs.community
MIT License
1.28k stars 124 forks source link

Add `createLens` primitive #453

Open nathanbabcock opened 1 year ago

nathanbabcock commented 1 year ago

Describe The Problem To Be Solved

Utilities for working with nested reactivity in a modular way.

See previous discussion:

Originally described by @clinuxrulz:

Say our application state holds a collection of states of things in the tabs of the application. It can be handy to have a Lens to focus on a subset of the state in order to provide a store for use by the UI in the tabs state. So that rhe end-user does not need to alway push state changes to top level (under the hood the state still reaches the top, it is just hidden boiler plate).

Suggest A Solution

Proposed primitives:

I've started a PR to explore this concept (WIP): #452

It's essentially just syntactic sugar on top of regular Stores, with some tricks to maintain Typescript support.

In previous discussion people theorized about a performance benefit from this approach as well, by caching a partially applied path within the Store object -- for example: https://github.com/solidjs/solid/issues/506#issuecomment-869300170.

I'm not as familiar under the hood of how the Store works, so my goal will be the replicate the intended DX and then unit test thoroughly enough that someone else could come in and work some performance magic. For now it is Lens-like DX with the same behavior/performance as a regular Store.

I'm also curious to get people's thoughts on:

Cheers 🍺

nathanbabcock commented 1 year ago

Source code: https://github.com/nathanbabcock/solid-primitives/tree/lens/packages/lenses PR: https://github.com/solidjs-community/solid-primitives/pull/452