JSPM

  • Created
  • Published
  • Downloads 299812
  • Score
    100M100P100Q166658F
  • License MIT

Efficient creation of immutable state

Package Exports

  • mutative
  • mutative/dist/index.js
  • mutative/dist/mutative.esm.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mutative) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

mutative

Node CI npm

Efficient creation of immutable state

10x faster than immer

Motivation

TBD

Performance

TBD

Features

  • apply patches
  • Supports optional freezing
  • Custom shallow copy
  • immutable and mutable markable
  • Strict mode for safer mutable data access

Difference between Immer and Mutative

- Mutative Immer
Best performance
Default common data structure ❌(auto freeze)
Non-invasive marking
Automatic type inference
Complete freeze data

Installation

yarn install mutative # npm install mutative

Examples

const baseState = {
  foo: 'bar',
  list: [{ text: 'coding' }],
};

const state = create(baseState, (draft) => {
  draft.foo = 'foobar';
  draft.list.push({ text: 'learning' });
});

Migration from Immer to Mutative

TBD

APIs

  • create()
const baseState = {
  foo: 'bar',
  list: [{ text: 'todo' }],
};

const state = create(baseState, (draft) => {
  draft.foo = 'foobar';
  draft.list.push({ text: 'learning' });
});
  • apply()
const baseState = {
  foo: 'bar',
  list: [{ text: 'todo' }],
};

const [state, patches, inversePatches] = create(
  baseState,
  (draft) => {
    draft.foo = 'foobar';
    draft.list.push({ text: 'learning' });
  },
  {
    enablePatches: true,
  }
);

const nextState = apply(baseState, patches);
expect(nextState).toEqual(state);
const prevState = apply(state, inversePatches);
expect(prevState).toEqual(baseState);
  • current()
const baseState = {
  foo: 'bar',
  list: [{ text: 'todo' }],
};

const state = create(baseState, (draft) => {
  draft.foo = 'foobar';
  draft.list.push({ text: 'learning' });
  expect(current(draft.list)).toEqual([{ text: 'todo' }, { text: 'learning' }]);
});
  • original()
const baseState = {
  foo: 'bar',
  list: [{ text: 'todo' }],
};

const state = create(baseState, (draft) => {
  draft.foo = 'foobar';
  draft.list.push({ text: 'learning' });
  expect(original(draft.list)).toEqual([{ text: 'todo' }]);
});
  • isDraft()
  • unsafe()
  • castDraft()
  • castImmutable()
  • Mutable<T>
  • Immutable<T>
  • Patches
  • Patch

FAQs

TBD