npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dk-mobx-restore-state

v3.4.10

Published

Restores state from initial data object

Downloads

219

Readme

Library for safe merging of MobX observables

coverage npm license size

[!WARNING]
It's fine if you use this library from NPM package with a static versioning in case you want it for some pet-project or to test it's capabilities.

But for production use it's strongly recommended to create a fork, because I do not write Changelogs and may break / add some functionality without notice.

Purpose

The purpose of this library is to safely restore state during SSR.

If you want to restore your state and see a problem, it may be as follows:

  1. You use MobX 4. It has a bug where newly added objects are not observable, like
const result = Object.assign(observable({ str: '123' }), { str: '321', obj: {} });

expect(isObservable(result.obj)).to.eq(false); // BUG in MobX 4

Nowadays MobX 5 / 6 versions do not have this bug, we can use Object.assign. But when we speak about classes, the behavior remains inconsistent.

  1. You use a class mobx store without enumerated value
class Target {
  constructor() { makeAutoObservable(this); }
  str = '123';
}
    
const result = Object.assign(new Target(), { str: '321', obj: {} });

expect(isObservable(result.obj)).to.eq(false); // BUG in all MobX versions
  1. You use a class mobx store without initial value
class Target {
  constructor() { makeAutoObservable(this); }
  str = '123';
  obj?: SomeType;
}
    
const result = Object.assign(new Target(), { str: '321', obj: {} });

expect(isObservable(result.obj)).to.eq(false); // BUG in all MobX versions

This is very confusing and depends on transpilers (Babel, TSC, Esbuild, SWC) which all behave differently. So, this library makes everything consistent.

Now that you have fixed these errors and feel happy and still don't want dk-mobx-restore-state...

class Target {
  constructor() { makeAutoObservable(this); }
  str = '123';
  obj?: SomeType = undefined;
}
    
const result = Object.assign(new Target(), { str: '321', obj: {} });

expect(isObservable(result.obj)).to.eq(true); // No bug finally!
  1. You come across a problem that Object.assign is not a deep merge. So you can not restore a partial data like
class Target {
  constructor() { makeAutoObservable(this); }
  obj = { a: 1 };
}
    
const result = Object.assign(new Target(), { obj: { b: 2 } });

expect(result.obj.a).to.eq(undefined); // Lost some initial data!
  1. You try lodash.merge and see that it merges deeply, but with the same inconsistencies as Object.assign. You write a customizer and feel happy finally...
mergeWith(new Target(), source, (objValue, srcValue) => {
  if (objValue == null && Object.prototype.toString.call(srcValue) === '[object Object]') {
    return observable(srcValue);
  }
}

But... There are some edge-cases in the strategy of merging and you need to see the logs of the process... Actually, you may consider dk-mobx-restore-state just from the start. It is thoroughly tested, 1.8kb unminified, handles most of edge-cases and has no deps.

Usage

Install dk-mobx-restore-state and use it instead of Object.assign / mergeWith where needed. Everything will be observable (in MobX 4 or in class objects) in all the cases mentioned above.

Syntax

restoreState({ logs, target, source })

logs (boolean): logging of operations

target: object that needs to be filled with observable data

source: object with some data (may be observable or not)