Skip to content
Source
Sign inSign up
TanStack/store

Solid Adapter #5

Closedcrutchcorn(GitHub profile, opens in a new tab) opened this issue 8 comments

It would be really cool to see a Solid adapter for this project. I have a few questions I'm unclear about that would need to be solved:

Ideally the API should look nearly identical to the React API

  1. crutchcorn(GitHub profile, opens in a new tab) added labelsenhancement

  2. crutchcorn(GitHub profile, opens in a new tab) added labelsgood first issue

  3. crutchcorn(GitHub profile, opens in a new tab) added labelshelp wanted

  4. wobsoriano(GitHub profile, opens in a new tab)commented

    I can think of 2 ways to implement this:

    1. Use the shallow function we have in both React and Vue adapters, and pass it to createSignal as diff function:
    import { createSignal, onCleanup } from 'solid-js'
    
    export function useStore(store, selector:) {
      const [slice, setSlice] = createSignal(selector(store.state), {
        equals: shallow, // Pass the shallow function here for testing equality.
      })
    
      const unsub = store.subscribe(() => {
        const newValue = selector(store.state)
        setSlice(newValue)
      })
    
      onCleanup(() => {
        unsub()
      })
    
      return slice
    }
    1. Take advantage of Solid's built-in stores
    import { onCleanup } from 'solid-js'
    import { createStore, reconcile } from 'solid-js/store'
    
    export function useStore(store, selector) {
      const [slice, setSlice] = createStore({
        value: selector(store.state)
      })
    
      const unsub = store.subscribe(() => {
        const newValue = selector(store.state)
        setSlice('value', reconcile(newValue)) // recursive diff on the new value
      })
    
      onCleanup(() => {
        unsub()
      })
    
      return () => slice.value
    }

    Then it should now have the same API as React, only that it returns a signal.

    const store = new Store({
      likes: 0
    })
    
    function Likes() {
      const likes = useStore(store, (state) => state.likes)
      return <div>Likes: {likes()}</div>
    }
  5. I thought about this a lot and even gone down very dangerous paths with Ryan Carniato talking about how we can bridge the gap between signals and immutable values. The only secure place that I’ve landed is that reconcile is the answer.

  6. If ever number 2 from my comment would work, I can open a PR with some tests 👍🏼

  7. The selector could be optional at that point since you wouldn’t need it for fine grained subscriptions, its only purpose would be to maybe cut down on the amount of work that reconcile has to do. Thoughts?

  8. https://github.com/TanStack/router/pull/671/files#diff-9be81768de8c03b212d1afde126315756a7ad4dccc75840d1925a58973238923

    Here is a PR I've been working on slowly where I have a working solid version. Would love any feedback on my choice. It's assigned to routerStore because the selector value could be a string/number/etc.

    The shallowFindMissingKeys function was to help remove values that were previously there in the case of url search params being removed.

  9. The selector could be optional at that point

    Can you elaborate on this @tannerlinsley? If I understand correctly, you meant removing the selector? So if you want to do data transform, you do it outside the primitive?

  10. Something like this:

    import { onCleanup } from 'solid-js'
    import { createStore, reconcile } from 'solid-js/store'
    
    const defaultSelector = <T>(state: T): T => state
    
    export function useStore<TState, TSelected = TState>(store: Store<TState>, selector?: (state: TState) => TSelected): TSelected {
      selector = selector || (defaultSelector as any)
    
      const [slice, setSlice] = createStore({
        value: selector(store.state)
      })
    
      const unsub = store.subscribe(() => {
        const newValue = selector(store.state)
        setSlice('value', reconcile(newValue)) // recursive diff on the new value
      })
    
      onCleanup(() => {
        unsub()
      })
    
      return () => slice.value
    }
  11. Aight, that makes sense @tannerlinsley! Thanks.

Sign in to join this conversation.