Skip to content

V3#216

Merged
ctrlplusb merged 87 commits intomasterfrom
v3
Jul 23, 2019
Merged

V3#216
ctrlplusb merged 87 commits intomasterfrom
v3

Conversation

@ctrlplusb
Copy link
Owner

@ctrlplusb ctrlplusb commented Jun 24, 2019

Note

If you are upgrading from easy-peasy@3.0.0-rc.0 to easy-peasy@3.0.0-rc.6 please review the API and TypeScript docs relating to actionOn, thunkOn, and computed. In order to optimise the TypeScript DX I have made some breaking changes. Apologies for the break at RC phase, however, I hope you will agree it is worth it. As things stand I am very happy with where we are at. I will let easy-peasy@3.0.0-rc.6 sit for a short while and then will look to cut easy-peasy@3.0.0. Any early adopters and feedback will be super appreciated. 👍


Updated website

npm install easy-peasy@3.0.0-rc.6

v3 is considered the realisation of the "final" Easy Peasy API - taking all the evolution and learning from v2 to produce a long term stable API that we will commit to supporting and will do our best to avoid breaking changes moving forward.

Todo

  • Complete documentation on testing
  • Apply performance optimisations unlocked by removing deprecated APIs
  • Triple parse the website docs
  • Code cleanup triple parse
  • Ensure 100% code coverage

New Features

Hot reloading support

Hot reloading is supported via the store.reconfigure(model) API. See #168

New actionOn and thunkOn APIs

These are the new and only APIs by which to define action/thunk listeners with.

The v3 website has been updated with tutorials and API docs introducing these APIs.

We are really sorry about the churn around listener APIs! This API was driven by community feedback so feeling far better about it. 👍

Breaking Changes

Removed deprecated APIs

Thunks can be either asynchronous and synchronous

Using async/await or returning a Promise from a thunk will maintain its previous async behaviour.

However, if you do neither of the above your thunk will be executed synchronously. Therefore you would get eager updates to your state. This can be handy for encapsulating log based action dispatching within a think.

addProduct: thunk((actions, payload) => {
  switch (payload.type) {
    case 'SHOES': 
		actions.addShoe(payload);
        break;
    case 'VEGETABLE':
        // ...
  }
});

Returning immutable state from actions

If you prefer to return new immutable state from your actions, rather than mutating the state, you need to set the new disableImmer flag.

import { createStore, action } from 'easy-peasy';

const model = {
  todos: [],
  addTodo: action((state, payload) => {
    // 👇 new immutable state returned
    return [...state, payload];
  })
}

const store = createStore(model, {
  disableImmer: true // 👈 set the flag
})

Failing to disable immer may result in strange errors if you are using computed properties.

computed

In order to optimise the Typescript experience we have made a fairly small change to the computed API. If you wish to use state resolvers, these now need to be defined as the first argument.

Before

const basketModel = {
  productIds: [],
  products: computed(
    (productIds, products) => productIds.map(id => products[id]),
    [
      state => state.productIds,
      (state, storeState) => storeState.products.items
    ]
  )
};

After

const basketModel = {
  productIds: [],
  products: computed(
    [
      state => state.productIds,
      (state, storeState) => storeState.products.items
    ],
    (productIds, products) => productIds.map(id => products[id])
  )
};

Computed properties not using state resolvers remain unchanged.

useStoreState API update

useStoreState(previouslyuseStore) no longer needs/accepts the dependencies 2nd argument. Your state will get mapped correctly if they use external values, like props, within the mapState` function.

Typescript

We now requires >= typescript@3.5.1 if you use the Typescript definitions. We recommend using the latest version, typescript@3.5.3 at the time of writing, in order to ensure you are up to date with the latest bug fixes.

Create React App users can just install typescript@3.5.3 as a dev dependency and the CRA build system will use that version. 👍

Note: using a lower version of TypeScript may still work, however, we will not be officially supporting them. Moving forward we will provide support for >= typescript@3.5.x

Computed

The Computed type no longer requires you to define the types for state resolvers. These will automatically be inferred.

Before

interface BasketModel {
  productIds: string[];
  products: Computed<
    BasketModel, 
    Product[], 
    ResolvedState2<string[], Product[]>, 
    StoreModel
   >
}

After

interface BasketModel {
  productIds: string[];
  products: Computed<
    BasketModel, 
    Product[],
    StoreModel
   >
}

@codecov
Copy link

codecov bot commented Jun 24, 2019

Codecov Report

Merging #216 into master will decrease coverage by 1.42%.
The diff coverage is 98.61%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #216      +/-   ##
=========================================
- Coverage   98.13%   96.7%   -1.43%     
=========================================
  Files          12      12              
  Lines         535     364     -171     
  Branches       87      52      -35     
=========================================
- Hits          525     352     -173     
- Misses          8      10       +2     
  Partials        2       2
Impacted Files Coverage Δ
src/index.js 100% <ø> (ø) ⬆️
src/helpers.js 100% <100%> (ø) ⬆️
src/create-store.js 100% <100%> (ø) ⬆️
src/lib.js 100% <100%> (ø) ⬆️
src/hooks.js 100% <100%> (ø) ⬆️
src/create-component-store.js 100% <100%> (ø) ⬆️
src/constants.js 100% <100%> (ø) ⬆️
src/create-store-internals.js 97.04% <97.61%> (-1.86%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1dca1f3...871016f. Read the comment docs.

@BjoernRave
Copy link

wow, that diff, so sexy

@ctrlplusb
Copy link
Owner Author

Soooooo satisfying.

@waynevanson
Copy link

  • createTypedHooks (use the typed hooks bound to store instead)
  • useStore (renamed to useStoreState)
  • useActions (renamed to useStoreActions)
  • useDispatch (renamed to useStoreDispatch)

Loving the convention and consistency!

@codecov
Copy link

codecov bot commented Jul 11, 2019

Codecov Report

Merging #216 into master will increase coverage by 1.86%.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff            @@
##           master   #216      +/-   ##
========================================
+ Coverage   98.13%   100%   +1.86%     
========================================
  Files          12     12              
  Lines         535    353     -182     
  Branches       87     54      -33     
========================================
- Hits          525    353     -172     
+ Misses          8      0       -8     
+ Partials        2      0       -2
Impacted Files Coverage Δ
src/index.js 100% <ø> (ø) ⬆️
src/helpers.js 100% <100%> (ø) ⬆️
src/create-store.js 100% <100%> (ø) ⬆️
src/__tests__/utils.js 100% <100%> (+43.75%) ⬆️
src/lib.js 100% <100%> (ø) ⬆️
src/hooks.js 100% <100%> (ø) ⬆️
src/create-store-internals.js 100% <100%> (+1.09%) ⬆️
src/create-component-store.js 100% <100%> (ø) ⬆️
src/constants.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f3edf25...036f4d2. Read the comment docs.

@jmyrland
Copy link
Collaborator

jmyrland commented Jul 13, 2019

Hi @ctrlplusb ! This is looking real good 👏

Because we have a pretty well testet system with easy-peasy integrated, I figured I would install the new version to check it out!

Quick feedback: I would recommend moving the section about createTypedHooks on the typescript docs to the top. Got stuck on exporting useStoreState<StoreModel> until I scrolled down on the docs page. Note to self; read the full page before getting into the code 🤦‍♂

Loving the new listener API! For listeners against multiple actions - I assume returning an array from the "target resolver" is the way to go. If that is the case - a quick note in the docs would be great:

The target resolver might return an array of actions - if you want the listener to fire for multiple actions/thunks 👍

I'll continue my journey of upgrading & testing the new version. If I stumble upon something strange - I'll be sure to let you know!

High-fives & well done to all the involved! 👏 🎉 🎊

Update: Upgrading to 3.0 from 2.6.3 was a breeze!

@codecov
Copy link

codecov bot commented Jul 22, 2019

Codecov Report

Merging #216 into master will increase coverage by 1.58%.
The diff coverage is 99.56%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #216      +/-   ##
==========================================
+ Coverage   98.13%   99.72%   +1.58%     
==========================================
  Files          12       12              
  Lines         535      358     -177     
  Branches       87       55      -32     
==========================================
- Hits          525      357     -168     
+ Misses          8        1       -7     
+ Partials        2        0       -2
Impacted Files Coverage Δ
src/index.js 100% <ø> (ø) ⬆️
src/helpers.js 100% <100%> (ø) ⬆️
src/create-store.js 100% <100%> (ø) ⬆️
src/__tests__/utils.js 100% <100%> (+43.75%) ⬆️
src/lib.js 100% <100%> (ø) ⬆️
src/hooks.js 100% <100%> (ø) ⬆️
src/create-component-store.js 100% <100%> (ø) ⬆️
src/constants.js 100% <100%> (ø) ⬆️
src/create-store-internals.js 99.43% <99.24%> (+0.53%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f5fbe87...39ec13b. Read the comment docs.

@ctrlplusb ctrlplusb merged commit a297302 into master Jul 23, 2019
@ctrlplusb ctrlplusb deleted the v3 branch October 30, 2019 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants