-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add solid-js adapter * build: move solid-js dependency to root * update eslint and tsconfig opts * test(solid): add basic test * chore: remove extra log * test(solid): add setter test * test(solid): update setter test * test(solid): add not working test * test(solid): remove unused async keywords
- Loading branch information
1 parent
5d93d53
commit cfda5c5
Showing
9 changed files
with
638 additions
and
507 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
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,14 @@ | ||
// @ts-check | ||
|
||
/** @type {import('eslint').Linter.Config} */ | ||
const config = { | ||
parserOptions: { | ||
tsconfigRootDir: __dirname, | ||
project: './tsconfig.json', | ||
}, | ||
rules: { | ||
'no-unused-vars': 'off', | ||
}, | ||
} | ||
|
||
module.exports = config |
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,58 @@ | ||
{ | ||
"name": "@tanstack/solid-store", | ||
"author": "Tanner Linsley", | ||
"version": "0.0.0", | ||
"license": "MIT", | ||
"repository": "tanstack/solid-store", | ||
"homepage": "https://tanstack.com/", | ||
"description": "", | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org/" | ||
}, | ||
"keywords": [ | ||
"store", | ||
"solid", | ||
"typescript" | ||
], | ||
"funding": { | ||
"type": "github", | ||
"url": "https://github.com/sponsors/tannerlinsley" | ||
}, | ||
"scripts": { | ||
"clean": "rimraf ./build && rimraf ./coverage", | ||
"test:eslint": "eslint --ext .ts,.tsx ./src", | ||
"test:types": "tsc", | ||
"test:lib": "vitest run --coverage", | ||
"test:lib:dev": "pnpm run test:lib --watch", | ||
"test:build": "publint --strict", | ||
"build": "tsup" | ||
}, | ||
"files": [ | ||
"build", | ||
"src" | ||
], | ||
"type": "module", | ||
"types": "build/legacy/index.d.ts", | ||
"main": "build/legacy/index.cjs", | ||
"module": "build/legacy/index.js", | ||
"exports": { | ||
".": { | ||
"import": { | ||
"types": "./build/modern/index.d.ts", | ||
"default": "./build/modern/index.js" | ||
}, | ||
"require": { | ||
"types": "./build/modern/index.d.cts", | ||
"default": "./build/modern/index.cjs" | ||
} | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"sideEffects": false, | ||
"peerDependencies": { | ||
"solid-js": "^1.6.0" | ||
}, | ||
"dependencies": { | ||
"@tanstack/store": "workspace:*" | ||
} | ||
} |
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,43 @@ | ||
import { render, renderHook } from '@solidjs/testing-library' | ||
import '@testing-library/jest-dom' | ||
import { Store } from '@tanstack/store' | ||
import { useStore } from '../index' | ||
|
||
describe('useStore', () => { | ||
it.todo('allows us to select state using a selector', async () => { | ||
const store = new Store({ | ||
select: 0, | ||
ignored: 1, | ||
}) | ||
|
||
function Comp() { | ||
const storeVal = useStore(store, (state) => state.select) | ||
|
||
return <p>Store: {storeVal()}</p> | ||
} | ||
|
||
const { getByText } = render(() => <Comp />) | ||
expect(getByText('Store: 0')).toBeInTheDocument() | ||
}) | ||
|
||
it('allows us to select state using a selector', () => { | ||
const store = new Store({ | ||
select: 0, | ||
ignored: 1, | ||
}) | ||
|
||
const { result } = renderHook(() => useStore(store, (state) => state.select)) | ||
|
||
expect(result()).toBe(0) | ||
}) | ||
|
||
it('updates accessor value when state is updated', () => { | ||
const store = new Store(0) | ||
|
||
const { result } = renderHook(() => useStore(store)) | ||
|
||
store.setState((prev) => prev + 1) | ||
|
||
expect(result()).toBe(1) | ||
}) | ||
}) |
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,32 @@ | ||
import type { AnyUpdater, Store } from '@tanstack/store' | ||
import type { Accessor} from 'solid-js'; | ||
import { onCleanup } from 'solid-js' | ||
import { createStore, reconcile } from 'solid-js/store' | ||
|
||
export * from '@tanstack/store' | ||
|
||
export type NoInfer<T> = [T][T extends any ? 0 : never] | ||
|
||
export function useStore< | ||
TState, | ||
TSelected = NoInfer<TState>, | ||
TUpdater extends AnyUpdater = AnyUpdater, | ||
>( | ||
store: Store<TState, TUpdater>, | ||
selector: (state: NoInfer<TState>) => TSelected = (d) => d as any, | ||
): Accessor<TSelected> { | ||
const [slice, setSlice] = createStore({ | ||
value: selector(store.state) | ||
}) | ||
|
||
const unsub = store.subscribe(() => { | ||
const newValue = selector(store.state) | ||
setSlice('value', reconcile(newValue)) | ||
}) | ||
|
||
onCleanup(() => { | ||
unsub() | ||
}) | ||
|
||
return () => slice.value | ||
} |
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,9 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"jsx": "preserve", | ||
"jsxImportSource": "solid-js", | ||
"types": ["vitest/globals"] | ||
}, | ||
"include": ["src/**/*.ts", "src/**/*.tsx", ".eslintrc.cjs", "tsup.config.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,9 @@ | ||
// @ts-check | ||
|
||
import { defineConfig } from 'tsup' | ||
import { legacyConfig, modernConfig } from '../../scripts/getTsupConfig.js' | ||
|
||
export default defineConfig([ | ||
modernConfig({ entry: ['src/*.ts', 'src/*.tsx'] }), | ||
legacyConfig({ entry: ['src/*.ts', 'src/*.tsx'] }), | ||
]) |
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,12 @@ | ||
import { defineConfig } from 'vitest/config' | ||
|
||
export default defineConfig({ | ||
test: { | ||
name: 'solid-store', | ||
dir: './src', | ||
watch: false, | ||
environment: 'jsdom', | ||
globals: true, | ||
coverage: { provider: 'istanbul' }, | ||
} | ||
}) |
Oops, something went wrong.