-
-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v3.4.0 #459
Conversation
* Fix statemapper eating up classes * Remove scratch file * Adds support for generic values * Adds support for generic values
* Adds useLocalStore hook * Adds typings for useLocalStore and it returns store now too * Adds docs for useLocalStore * Updates website
Codecov Report
@@ Coverage Diff @@
## master #459 +/- ##
==========================================
- Coverage 97.72% 97.11% -0.61%
==========================================
Files 20 21 +1
Lines 571 659 +88
Branches 104 125 +21
==========================================
+ Hits 558 640 +82
- Misses 11 15 +4
- Partials 2 4 +2
Continue to review full report at Codecov.
|
* fixes computed error when get() returns undefined * avoids implicit boolean conversion
…e config into a factory function
Ok, no worries. I am hoping that you will find it far more concise and useful. As it is described within your component you can hook into a context store or callback within your model now too. Far more powerful and composable. |
@ctrlplusb I used it and like it. One thing that would be amaze is the ability to add a i.e. const useMyComponentStore = () => {
const someThing = useMyOtherState();
return useLocalStore(() => {
someThing,
});
}
const MyComponent = () => {
const [store, actions] = useMyComponentStore();
<store.Provider>
//... child components can also call `useMyComponentStore` and get the same state
</store.Provider>
} |
Where does... await todosService.save(items); ... come from in the |
This will be replaced by v4. 😅 |
This PR is currently published as
easy-peasy@3.4.0-beta.2
We would appreciate early testing and feedback. 💜
The updated website for this PR can be found at https://easy-peasy-v3-4-0.now.sh/
Deprecations
Deprecated
createComponentStore
The API for
createComponentStore
was a bit verbose and limited. It will be removed in the next major release.I plan to release non breaking changes for a while still, so no stress.
We have introduced a new API,
useLocalStore
, which replaces this one.Features
Adds new
useLocalStore
hookThis API acts as a replacement of the deprecated
createComponentStore
API.Allows you to create a store to represent the state for an individual React component. This is essentially an alternative to the official
useState
anduseReducer
hooks provided by React for component-level state.Please see the new API documentation for more information on this API.
Closes #451
Closes #439
Adds new
unstable_effectOn
model APIAllows you to declare an effect within your model which will execute every time the targeted state changes.
Two arguments are provided to unstable_effectOn; namely the
stateResolvers
and thehandler
. ThestateResolvers
are an array of functions which should resolve the target state that should be tracked. When the tracked state changes thehandler
function is executed.The
handler
can be asynchronous or synchronous. It receives the store actions, achange
object containing theprev
values for the targeted state and thecurrent
values as well as theaction
that caused the change in state. It additionally receives ahelper
argument allowing you to access the store state etc.The
handler
additionally allows you to return a dispose function, which will be executed prior to the next execution of thehandler
. This can be useful in performing things like API call cancellation etc.Closes #419
TypeScript: Adds support for generics in models
Previously if you defined a model containing generic state, like below, TypeScript would break within your actions.
Unfortunately we were unable to directly resolve the case of generic properties due to current limitations with the TypeScript type system. We created a StackOverflow question which details the problem.
In a gist; the issue is that Easy Peasy's underlying
State
andAction
types map over the user provider model types in order to filter down to types that represent state and actions respectively. However, when defining a generic state, TypeScript assumes that the generic state intersects with types that are trying to be filtered out of each case. Therefore the filtering ends up always removing your generic state.To resolve the case of generic state we have introduce a new API helper. Any time you wish to have a generic state value within your model, simply wrap it with the
Generic
type, and then assigned the associated value within the model instance using thegeneric
helper.Note how you only need to use the helper at the point of defining the initial value of the generic model state. Within your actions and anywhere you consume the state you would treat the value as the underlying generic value (i.e. a
number
in the example).Closes #300
Closes #361
Improves the persist APIs on the store instances
We had undocumented APIs regarding persistence that were being exposed on the store instances. These are helpful in many cases, such as being able to flush persistence prior to navigating away from your application, or awaiting for persisted data to be rehydrated prior to rendering your application. We highly recommend you read the respective Store API docs.
Closes #454
Removes limit on TypeScript model mapping
Previously, if you had a model more than 6 levels deep, in terms of object structure, the TypeScript mapping wouldn't work as expected. This is no longer the case.
I still think having that deep of a model is a bit excessive though. 😅
Replaces
immer-peasy
with officialimmer
We have replaced our forked/patched version of immer with the official version. Thanks to their newly released support for computed properties. 🎉
Closes #462
Closes #446
Closes #440
Adds ability to await on rehydration of persisted data for dynamically added model
When utilising
persist
against a dynamically added model, i.e. viastore.addModel
, you may need to await on the rehydration due to utilising an asynchronous storage engine.You can now do so via the returned
resolveRehydration
helper.Closes #444
Moves internal
redux-thunk
binding to grant user defined middleware higher priorityThis will allow you to influence thunks prior to their execution. For advanced cases. 😊
Closes #390
Patches
TypeScript: Loads of fixes and improvements to the typings
The typings are being combed over multiple times and various fixes and improvements are being made. This is an ongoing task and you can expect many more improvements to be made still, including the addition of proper documentation on each type within the typings. This should improve the dev experience within your editor as you will get inline guidance on the APIs along with links to the official documentation for them.
Fixes
persist
data not rehydrated for dynamically added modelsData persisted within models added via the
store.addModel
API were not having their data rehydrated. This is now fixed.Closes #444
Fixes
merge
andmergeDeep
strategies forpersist
rehydrationA bug was identified where it was possible for persisted state to be misaligned with an evolving datamodel in terms of data types.
For example, you could have the following state persisted:
And since the state was persisted there occurred an update to the data model:
Note how the data structure for
todos
has evolved since the persisted state. It is now an object with nested properties.Previously, for
merge
andmergeDeep
it did not take into consideration the evolving store model and would rehydrate a persisted state despite it being misaligned in terms of tree data type structure. With the fix if a misaligned data type is found then the data specified on the store model is used instead of the persisted state.The logic for this ignores values where they are
null
orundefined
, but otherwise will compare data types of the model vs persisted state to ensure the types match. If the types do not match then the store model is used over the persisted value. So for our example above the rehydrated state would be the following:Note how
todos.items
matches the store model rather than the persisted state.Moving forward we strongly recommend that the
mergeDeep
strategy is used when rehydrating state. This will ensure that the types are aligned right down the entire tree of the persisted state vs your store model.Closes #355
TypeScript: Fixes the statemapper eating up "classes"
If you assigned a class instance to your state the typings from
getState
/useStoreState
etc would report your type as being something similar to:With this fix your state will correctly be reported back as the actual class type (
Person
in the examples case).Closes #402
TypeScript: Fixes action mapper so that state isn't display when viewing actions
The VSCode intellisense for actions was showing state. The types have been patched so that only actions will beb displayed.
TypeScript: Fixes state mapper where actions were still being represented on state
There were cases if you had a nested action and only primitive state next to it that you would see the nested action. This is no longer the case.
TypeScript: Fixes computed properties state resolvers not inferring resolved types correctly
If you used a state resolver array within your computed properties, whilst using TypeScript, the inferred types based on the resolved state was incorrect. This is now fixed.
Closes #427
Website: Adds a known issue in regards to computed properties + Typescript
Unfortunately, due to the way our typing system maps your model, you cannot declare a computed property as being optional via the
?
property postfix.For example:
Luckily there is a workaround; simply adjust the definition of your computed property to indicate that the result could be undefined.
Fixes computed properties error for dynamically added model
Thanks goes to @jchamb for this fix. 💜
Computed properties were throwing errors intermittently when the
store.addModel
API was being used. This fix puts a guard in place to protect against the error.Fixes an error on the website -> docs -> quick-start.md
Thanks goes to @hualu00 for this fix. 💜
Replaces
rollup
withmicrobundle
to bundle librarySaves some more valuable bytes in bundlesize.
Closes #452
Updates website to include known issue on computed property destructuring
Closes #386
Improves the
persist
API documentationCloses #454
Adds "Community Extensions" page to website
Closes #359