hydux-mutator
A statically-typed immutable update helper library.
For Hydux.
Features
- Statically-typed
- Support class state
- Using constructor to create new instance
- Class can define a
shallowClone
method to customize shallow clone.
install
yarn add hydux-mutator # or npm i hydux-mutator
Usage
name = 'aa' age = 0 let user = user = user =
Note: The accessor lambda function only support plain object/array. e.g.
Which not support:
- Map/Set/Third-party collection library
- Function calls
But how can I use it in these scenarios? The answer is nesting:
let state = userMap: <string User> userObjMap: {} as key: string: Userlet key = 'a'state =
Dynamic keys
Because hydux-mutator get the deep key path by parsing the lambda string, we cannot get the dynamic path in the accessor scope, so we have to pass it to the function as ctx, see:
let state = userObjMap: {} as key: string: Userlet key = 'some key'state =
NOTE: The order of ctx should be the same as the occurrence order of dynamic keys.
Collections
We also provide some immutable collections like immutable-js, which has O(1) - O(logN) performance for update operations.
- ImmuList: Under the hood is @funkia/list, a modified implemenation of RRB tree with pretty good performance.
- ImmuMap: Based on an OCaml's immutable balanced tree implementation.
- ImmuSet: Based on ImmuMap.
Note: Polyfill for Symbol.iterator
is required!
All these collections can work seamlessly with setIn/updateIn/getIn/unsetIn
functions!
setInstate, _.list.get0.title, 'new title'
We also support fb's immutable-js or others contains .get(key: string | number)
and .set(key: string | number, value: any)
methods.
immer
Immer like api.
Note: This is based on es6 Proxy, you might need a proxy polyfill if you want to use in es5 environment.
// nextState => {// subState: {// nestedSubState: 2// }// }
monolite
What's the difference with The main difference is monolite is using es6's Proxy<T>
under the hood, which might not support well in many browsers.
hydux-mutator
is implement by parsing lambda function's source string(fn.toString()
), this have better browser support. And the parsing part can easily be cached, which means it can have better performance.
What's more, hydux-mutator
support class state, which I rarely see in other immuatble update helpers.
{ // constructor should have an overload of zero parameters. thisname = name thisage = age }let state = user: state = mutatorstateuser instanceof User // true!
Known Issues
- flow has bug in checking
setIn
, see: #5569
Benchmark
Start Suit: Set 1 keyimmutable x 932,328 ops/sec ±14.93% seamless-immutable x 67,021 ops/sec ±16.99% timm x 881,627 ops/sec ±17.53% monolite x 151,347 ops/sec ±13.51% mutator x 219,049 ops/sec ±5.03% Fastest is immutable,timm Start Suit: Set 2 keyimmutable x 732,474 ops/sec ±9.49% seamless-immutable x 28,802 ops/sec ±12.65% timm x 715,487 ops/sec ±9.31% monolite x 97,454 ops/sec ±12.41% mutator x 147,161 ops/sec ±16.37% Fastest is immutable,timm Start Suit: Set 5 keyimmutable x 374,647 ops/sec ±13.05% seamless-immutable x 19,725 ops/sec ±8.40% timm x 217,508 ops/sec ±9.00% monolite x 80,403 ops/sec ±6.32% mutator x 111,625 ops/sec ±4.92% Fastest is immutable Start Suit: Set 10 keyimmutable x 257,998 ops/sec ±4.59% seamless-immutable x 11,238 ops/sec ±10.45% timm x 219,370 ops/sec ±8.29% monolite x 32,778 ops/sec ±7.16% mutator x 54,496 ops/sec ±8.72% Fastest is immutable
License
MIT