-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial support for xstate/store/solid (#5056)
* Add initial SolidJS support with `useSelector` function * Include changeset for xstate/store/solid * Remove test which doesn't test `useSelector` * Remove unnecessary types * Improve typing and remove unused import * Clean imports
- Loading branch information
1 parent
853f6da
commit 8c35da9
Showing
7 changed files
with
409 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
'@xstate/store': minor | ||
--- | ||
|
||
You can now use the xstate/store package with SolidJS. | ||
|
||
Import `useSelector` from `@xstate/store/solid`. Select the data you want via `useSelector(…)` and send events using `store.send(eventObject)`: | ||
|
||
```tsx | ||
import { donutStore } from './donutStore.ts'; | ||
import { useSelector } from '@xstate/store/solid'; | ||
|
||
function DonutCounter() { | ||
const donutCount = useSelector(donutStore, (state) => state.context.donuts); | ||
|
||
return ( | ||
<div> | ||
<button onClick={() => donutStore.send({ type: 'addDonut' })}> | ||
Add donut ({donutCount()}) | ||
</button> | ||
</div> | ||
); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"main": "dist/xstate-store-solid.cjs.js", | ||
"module": "dist/xstate-store-solid.esm.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* @jsxImportSource solid-js */ | ||
import { createEffect, createSignal, onCleanup } from 'solid-js'; | ||
import type { Store, SnapshotFromStore } from './types'; | ||
|
||
function defaultCompare<T>(a: T | undefined, b: T) { | ||
return a === b; | ||
} | ||
|
||
function useSelectorWithCompare<TStore extends Store<any, any>, T>( | ||
selector: (snapshot: SnapshotFromStore<TStore>) => T, | ||
compare: (a: T | undefined, b: T) => boolean | ||
): (snapshot: SnapshotFromStore<TStore>) => T { | ||
let previous: T | undefined; | ||
|
||
return (state): T => { | ||
const next = selector(state); | ||
|
||
if (previous === undefined || !compare(previous, next)) { | ||
previous = next; | ||
} | ||
|
||
return previous; | ||
}; | ||
} | ||
|
||
/** | ||
* Creates a selector which subscribes to the store and selects a value from the | ||
* store's snapshot, using an optional comparison function. | ||
* | ||
* @example | ||
* | ||
* ```tsx | ||
* import { donutStore } from './donutStore.ts'; | ||
* import { useSelector } from '@xstate/store/solid'; | ||
* | ||
* function DonutCounter() { | ||
* const donutCount = useSelector(donutStore, (state) => state.context.donuts); | ||
* | ||
* return ( | ||
* <div> | ||
* <button onClick={() => donutStore.send({ type: 'addDonut' })}> | ||
* Add donut ({donutCount()}) | ||
* </button> | ||
* </div> | ||
* ); | ||
* } | ||
* ``` | ||
* | ||
* @param store The store, created from `createStore(…)` | ||
* @param selector A function which takes in the snapshot and returns a selected | ||
* value from it | ||
* @param compare An optional function which compares the selected value to the | ||
* previously selected value | ||
* @returns A read-only signal of the selected value | ||
*/ | ||
export function useSelector<TStore extends Store<any, any>, T>( | ||
store: TStore, | ||
selector: (snapshot: SnapshotFromStore<TStore>) => T, | ||
compare: (a: T | undefined, b: T) => boolean = defaultCompare | ||
): () => T { | ||
const selectorWithCompare = useSelectorWithCompare(selector, compare); | ||
const [selectedValue, setSelectedValue] = createSignal( | ||
selectorWithCompare(store.getSnapshot() as SnapshotFromStore<TStore>) | ||
); | ||
|
||
createEffect(() => { | ||
const subscription = store.subscribe(() => { | ||
const newValue = selectorWithCompare( | ||
store.getSnapshot() as SnapshotFromStore<TStore> | ||
); | ||
setSelectedValue(() => newValue); | ||
}); | ||
|
||
onCleanup(() => { | ||
subscription.unsubscribe(); | ||
}); | ||
}); | ||
|
||
return selectedValue; | ||
} |
Oops, something went wrong.