246 Commits

Author SHA1 Message Date
Daishi Kato
eb0a93b45d
chore: refactor zustand/context (#418) 2021-06-13 21:01:29 +09:00
daishi
ee77b33903 run prettier 2021-06-05 18:19:41 +09:00
Ehsan sarshar
f62d4487ba
Migrating Async/Await to Promise (#403)
* setItem migrated from asyn/await to promise

* setItem is more cleaner now

* rehydrate on initial state migrated to promise 🔥

* a tiny bug fixed in middleware

* .size-snapshot.json update

* don't invoke serialize when storage is undefined

Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>

* a makeThenable() used to mix sync/async thens

* toThenable() API finalized

* size snapshots updated

* Add tests for sync storage

* Fix loading the state from localStorage

* new snapshot

* types     added to `toThenable()` 🎉   🎉

* new size snapshot

* Update src/middleware.ts

Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>

* remaining parts also migrated to `toThenable()`

* a clear explanation of sync storage workaround

* fix some inconsistencies

* some inconsistency fixed

* useless IIFE removed

* some bugs and types fixed

* stateFromStorage renamed to stateFromStorageInSync

* Wrap tests into describe()

* Make sure that test for async persist() actually tests an async middleware

* size snap shot updated

* Correctly handle version discrepancies with missing migrate functions

* Update src/middleware.ts

Co-authored-by: Anatole Lucet <anatole@hey.com>

* Fix test

* chages addressed in reviews applied

* chore: refactor

* fix: setItem can throw error in sync

Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
Co-authored-by: Benjamin Arbogast <benjamin.arbogast@gmail.com>
Co-authored-by: Anatole Lucet <anatole@hey.com>
Co-authored-by: daishi <daishi@axlight.com>
2021-06-05 18:04:53 +09:00
Dominik Dorfmeister
9246dcec52
(types): improve types for setState (#397)
* (types): improve types for setState

allow for passing multiple key generics to PartialState; this will make sure that we can have paths in the PartialState setter function that return different keys

* (types): improve types for setState

replace `name` with `something`. for some reason, the property `name` does not yield errors :/

* (types): improve types for setState

streamline generic default values in middleware.ts with those in vanilla.ts

* (types): improve types for setState

use generic type S instead of State (S extends State)
2021-06-05 06:59:00 +09:00
Charles Kornoelje
eb27003cb4
Expose StateStorage type and update persist midlware documentation (#412)
* expose StateStorage type

* update documentation about persist StateStorage

* update readme.md
2021-06-05 06:55:45 +09:00
Jaga Apple
8aefb6b1c7
Fix a return value type of combine (#395)
Co-authored-by: Jaga Apple <jagaapple@uniboar.com>
2021-05-22 07:20:22 +09:00
Samuel Batista
f2a0efa2e2
Allow optional selector in zustand/vanilla (#384)
* Make selector optional in zustand/vanilla

This type better reflects the optional nature of the parameter.

* Remove unnecessary line

* Fix type inference, still allow for optional selector type

* Remove optional from state slice type

* Remove semis

* Update vanilla.ts

* Putting it back
2021-05-11 16:38:56 +09:00
Owen Masback
e93b8ec330
Add useStoreApi context hook (#380)
* Add useStoreApi context hook

* return only a plain object from useStoreApi context hook

* add test for useStoreApi context hook
2021-05-08 22:17:24 +09:00
Munawwar
a63bd91ae3
Removed "initialState" parameter from createContext() (#382)
* remove "initialState" parameter from createContext() to avoid confusion

* remove optional initialState comment for TS.
2021-05-08 22:08:07 +09:00
daishi
9d3a701e88 chore: rename to pmndrs from react-spring 2021-05-06 00:13:56 +09:00
Munawwar
611551c409
#182 SSR data is shared (#375)
* context utils for SSR

* move useIsomorphicLayoutEffect to new file

* added build commands

* 1. remove the useEffect we dont need anymore 2. wrap context and hook into createZustand() function, but keep defaults

* issue #182 - changed name to createContext and useStore + added tests

* remove default Provider

* use alias to index file

* change 'createState' prop to 'initialStore'

that accepts useStore func/object from create().

This is needed as store access is needed for merging/memoizing, in next.js integration

* updated tests

* code review changes

* snapshot update

* add a section in readme

* chore imports and so on

Co-authored-by: daishi <daishi@axlight.com>
2021-05-05 23:54:29 +09:00
Andrey Goncharov
f43d1127ee
fix: remove mandatory index signature (#376)
* Remove undesired index signature

* Use type assertion

* Update TypeScript readme

* Update TypeScript readme

* Replace Record with object

* Remove unnecessary keyof

* Minor fixes
2021-05-05 21:34:51 +09:00
José Vale
6bc11ed789
feat (devtools middlware): allow import of json to devtools (#373) 2021-04-27 09:02:43 +09:00
Anatole Lucet
5810e7c0e0
fix(persist middleware): incorrect deserialize type (#359) 2021-04-13 07:43:37 +09:00
Daishi Kato
17f89d88d5
fix: better ssr detection (#348)
* fix: better ssr detection

* fix: simpify condition

* chore: make custom hook name consistent with other pmndrs libs

* check user-agent for deno
2021-04-05 18:24:03 +09:00
Thomas Osmonson
77334ec851
build: use esbuild (#343) 2021-04-01 21:06:03 +09:00
Dominik Dorfmeister
52e6448602
(types): improve types for setState (#320)
* (types): improve types for setState

instead of using Partial, which would allow setting every field to undefined, we infer the types of the values depending on the keys passed into the set function

* (types): improve types for setState

add code comment to clarify why we are using Pick<T, K> | T

* (types): improve types for setState

apply the same changes we did for SetState also to namedSet for the devtools middleware

* (types): improve types for setState

tests for the partial logic

* empty commit

Co-authored-by: daishi <daishi@axlight.com>
2021-02-28 10:35:39 +09:00
Dimas
2ac38de048
Fix persist error forwarding (#292)
* feat(persist): add tests

* feat(persist): can throw persist setItem error

* feat(persist): can throw rehydrate error

* feat(persist): can migrate persisted state and throw error

* chore: push size-snapshot

* fix(vanilla): add Promise to return type of SetState

* fix(persist): merge onRehydrateError with onRehydrateStorage

* fix(persist): revert async setState back to sync

* fix(persist): change persistedState type to any

* fix(persist): refactor storage object in tests
2021-02-16 17:43:47 +09:00
Anatole Lucet
cf4d1365b6
doc(persist): fix miss-worded onRehydrateStorage jsdoc (#288) 2021-01-28 23:15:19 +09:00
Anatole Lucet
0ae00d7760
feat(persist): add warn if storage is unavailable (#284) 2021-01-20 23:02:39 +09:00
Leonardo Guarnieri de Bastiani
ba0396ab67
feat: works with useLayoutEffect (#280)
* feat: works with useLayoutEffect

* Feedback applied

* Added cleanup
2021-01-10 21:00:51 +09:00
Anatole Lucet
d5e85d247f
feat(persist): improve ssr & add versions & new callback (#275)
- add js doc for option object
- improve ssr support
- add a new version option #243
- refactor postRehydrationMiddleware to onRehydrateStorage
2021-01-08 19:07:09 +09:00
Rafał Pysz
58c286e6cb
fixed an issue in type infering while using eq function in hooks (#266)
* fixed an issue in type infering while using eq function in hooks

* moving back quotes removed by autoformatter

* tweaked test and run linter

* Update tests/basic.test.tsx

Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
2020-12-31 19:15:54 +09:00
Qcza
d4ba2b014b
feat: callback after persist (#258)
* feat: callback after persist

* chore: move callback to the options
2020-12-16 07:05:38 +09:00
Qcza
ccb700b0de
feat: blacklist and whitelist for the persist middleware (#254) 2020-12-08 20:47:38 +09:00
Qcza
b2bdd09db2
Persisting when using the api.setState() method (#251)
* Update middleware.ts

Allows for persisting the store when using the api.setState method.

* fix: linter issues

* Update src/middleware.ts

Co-authored-by: Anatole Lucet <anatole@hey.com>

* Update src/middleware.ts

Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>

* Update src/middleware.ts

Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>

Co-authored-by: Paweł Kuczyński <pawel.kuczynski@l4a-soft.com>
Co-authored-by: Anatole Lucet <anatole@hey.com>
Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
2020-12-04 00:14:54 +09:00
Daishi Kato
db017acf8b
persist middleware to check localStorage (#246) 2020-11-24 21:00:55 +09:00
Sebastiaan ten Pas
38ddba491a
Expose previous value in subscribe (#241) 2020-11-16 23:12:20 +09:00
daishi
458fcf6761 update size snapshot, no exporting types as they can extract 2020-11-08 14:41:59 +09:00
Anatole Lucet
055a3e9405
feat: add persist middleware (#229)
* feat: add persist middleware

* doc: add persit middleware to the readme

* Update src/middleware.ts

* Update readme.md

Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
2020-11-08 14:38:01 +09:00
daishi
9b4aadbde8 Revert "improve middleware/combine types (#222)"
This reverts commit 558845d99434c433faf6edc368bb262beea2456e.
2020-11-08 11:13:52 +09:00
Daishi Kato
558845d994
improve middleware/combine types (#222) 2020-11-05 16:45:33 +09:00
Lucas Westermann
3d9759468b
Hide warning when not run in browser (middleware) (#196)
* Hide warning when not run in browser (middleware)

I recently started using Zustand with next.js for my client-side state. In order to better debug/track the state, I've also started using the devtools middleware. This works fine on the client-side (in the browser), but results in lots of `console.warn` lines server-side (in the terminal when running the development server) stating "Please install/enable Redux devtools extension", which can obscure actual messages at times due to the number of times it is output. As the server side rendering happens outside of a browser, it seems a safe enough condition to add to this if statement.

The only other alternative I found to fix this locally is to assign the result of `create()` or `create(devtools())` conditionally, which results in a lot of duplicate code, as the devtools middleware requires extra parameters to the `set()` function (a name).

Please let me know if you're not happy with this solution (or if there are any drawbacks I haven't noticed).

I skipped opening an issue as well as the PR - if you prefer I open an issue as well, just drop me a comment.

* Fixed prettier complaint
2020-09-24 08:24:44 +09:00
Dominik Dorfmeister
880b3b28d2
make the commit button work in the devtools (#188)
by listening to the commit action and re-initializing the devtools with the current state, which will effectively clear the history
2020-09-12 19:26:23 +09:00
daishi
ec2283e78a follow up change 2020-09-12 09:20:12 +09:00
David Dios
97db4c5a15
Fix shallow types and add shallow test (#189)
* support object array comparison in shallow

* move shallow.test.ts to shallow.test.tsx

* revert shallow

Co-authored-by: daishi <daishi@axlight.com>
2020-09-12 09:16:03 +09:00
daishi
f32aa02d16 fix eslint warning 2020-09-10 14:09:17 +09:00
Daishi Kato
e81848fc0f
fix: evaluate selector if state is changed (#179) 2020-09-08 07:54:31 +09:00
Daishi Kato
a3857a9336
Stricter types (#174)
* no-any for exported types

* createPS middleware

* minor fix for import

* rename middleware

* update size snapshot

* rename to combine
2020-09-03 21:58:38 +09:00
Daishi Kato
925a90b0d1
fix: devtools to patch api.setState (#172)
* wip: devtools to patch api.setState

* fix: devtools to patch api.setState

* fix double logging with redux middleware

* fix redux middleware

* fix devtools middleware

* Revert "fix devtools middleware"

This reverts commit 611b696e9d46062582e81cb98e70d133c3aed0e6.

* Revert "fix redux middleware"

This reverts commit 99a9f64e398857ea4fd93def02f9147a1ae065ae.

* chore: code tweaks

* update size snapshot
2020-09-01 21:45:03 +09:00
Daishi Kato
0dc613240d
Merge pull request #169 from react-spring/fix-typing-for-immer
fix typing for immer
2020-08-26 22:59:43 +09:00
Daishi Kato
4675a5089a
Merge pull request #167 from react-spring/fix-devtools-and-type-middleware
fix devtools and type middleware properly
2020-08-26 22:59:29 +09:00
daishi
235e5cbbdc fix typing for immer 2020-08-26 18:06:35 +09:00
daishi
c035aaa335 fix devtools and type middleware properly 2020-08-25 20:08:26 +09:00
daishi
6418d36af6 fix: detect state change before subscription 2020-08-23 07:58:47 +09:00
Paul Henschel
e50f6080dc fix cjs build 2020-08-17 17:56:53 +02:00
Daishi Kato
bd1d775433
v3: unified api, vanilla store, and code clean up for v4 (#148)
* wip: no get subscriber

* wip: simplify subscribe

* feat: new unified api

* [v3] add slim subscribe and refactor useStore (#141)

* [v3] [breaking change] slim api.subscribe

* subscribe in useEffect instead of in render

* shave some bytes

* prefer-const

* add backward compatible subscribe

* add vanilla api

* chore: update dependencies and tsconfig

* chore: format

* [v3] create from store (#147)

* update readme

* update readme

* update readme

* update readme

* update readme

* update readme

* update readme

* update readme

* Update readme.md

* Update readme.md

Co-authored-by: Paul Henschel <drcmda@gmail.com>
2020-08-17 16:52:38 +02:00
James
2c17ac33b7
Uses prefix as devtools instance name
If a prefix is provided, it will be used as the instance name. If one is not provided, the default devtools behaviour will be used.
2020-08-17 09:36:25 +10:00
Daishi Kato
950b068f74
Add replace flag in set (#137)
* Added merge flag when using setState to override

* invert the merge flag to replace

Co-authored-by: Richard Gorman <richardgorman@outlook.com>
2020-08-13 10:45:38 +02:00
Bennett Piater
fa1a55ebf5
Fix typing of Partial<T> for immer usage (#99)
This fixes #96.
2020-08-09 08:23:03 +02:00