Mobx-state-tree: applyPatch will not replace root of store

Created on 28 Oct 2019  Â·  6Comments  Â·  Source: mobxjs/mobx-state-tree

  • [x] I've checked documentation and searched for existing issues
  • [x] I tried the spectrum channel

Is there anyway to use applyPatch to update a snapshot in the same way you might do an applySnapshot? With JSON patching, I'd expect to be able to replace a store using a patch like:
{
op: "replace",
path: "",
value: {} // new store snapshot
}
However, patches like this never work for me in MST. See this example code showing it in action with a simple model:
https://codesandbox.io/embed/lively-shape-lmjgo

bug

Most helpful comment

Yeah, thinking longer about, I think it is a bug and it should behave is
one would expect :)

On Mon, Oct 28, 2019 at 6:58 PM Speros Kokenes notifications@github.com
wrote:

I see. How does that differ from applying a snapshot? Don't they
conceptually do the same thing? Ie, replace the entire structure

On Mon, Oct 28, 2019, 7:53 PM Michel Weststrate notifications@github.com
wrote:

A Test + PR that will make MST error on such cases is welcome (condition:
node is root, path is empty)

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<
https://github.com/mobxjs/mobx-state-tree/issues/1411?email_source=notifications&email_token=ABKMBROWMU2OZI2RGWSOX6TQQ4YJDA5CNFSM4JF3LTAKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECN777Y#issuecomment-547094527
,
or unsubscribe
<
https://github.com/notifications/unsubscribe-auth/ABKMBRP3B7JW7BX2JGJ3B3LQQ4YJDANCNFSM4JF3LTAA

.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/mobxjs/mobx-state-tree/issues/1411?email_source=notifications&email_token=AAN4NBCQHRQ6DESG2U2FRJDQQ4Y5TA5CNFSM4JF3LTAKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECOARUI#issuecomment-547096785,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAN4NBHAHCLZTFZPBYVQYITQQ4Y5TANCNFSM4JF3LTAA
.

All 6 comments

It is not possible to replace a thing itself, as that would require all
javascript pointer that point to that object to be updated, which is
impossible. Either create patches that replace all fields of the root
object, or wrap another object around the root so that there is an entry
point.

On Mon, Oct 28, 2019 at 2:57 PM Speros Kokenes notifications@github.com
wrote:

>

Is there anyway to use applyPatch to update a snapshot in the same way you
might do an applySnapshot? With JSON patching, I'd expect to be able to
replace a store using a patch like:
{
op: "replace",
path: "",
value: {} // new store snapshot
}
However, patches like this never work for me in MST. See this example code
showing it in action with a simple model:
https://codesandbox.io/embed/lively-shape-lmjgo

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/mobxjs/mobx-state-tree/issues/1411?email_source=notifications&email_token=AAN4NBHQQVHPPPAKVZDINZTQQ34VFA5CNFSM4JF3LTAKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HUZHERA,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAN4NBBROB6JMRS7JPJBOH3QQ34VFANCNFSM4JF3LTAA
.

A Test + PR that will make MST error on such cases is welcome (condition: node is root, path is empty), or alternative, a fix that automatically "unpacks" the value to apply it to the individual fields

I see. How does that differ from applying a snapshot? Don't they
conceptually do the same thing? Ie, replace the entire structure

On Mon, Oct 28, 2019, 7:53 PM Michel Weststrate notifications@github.com
wrote:

A Test + PR that will make MST error on such cases is welcome (condition:
node is root, path is empty)

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/mobxjs/mobx-state-tree/issues/1411?email_source=notifications&email_token=ABKMBROWMU2OZI2RGWSOX6TQQ4YJDA5CNFSM4JF3LTAKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECN777Y#issuecomment-547094527,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ABKMBRP3B7JW7BX2JGJ3B3LQQ4YJDANCNFSM4JF3LTAA
.

Yeah, thinking longer about, I think it is a bug and it should behave is
one would expect :)

On Mon, Oct 28, 2019 at 6:58 PM Speros Kokenes notifications@github.com
wrote:

I see. How does that differ from applying a snapshot? Don't they
conceptually do the same thing? Ie, replace the entire structure

On Mon, Oct 28, 2019, 7:53 PM Michel Weststrate notifications@github.com
wrote:

A Test + PR that will make MST error on such cases is welcome (condition:
node is root, path is empty)

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<
https://github.com/mobxjs/mobx-state-tree/issues/1411?email_source=notifications&email_token=ABKMBROWMU2OZI2RGWSOX6TQQ4YJDA5CNFSM4JF3LTAKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECN777Y#issuecomment-547094527
,
or unsubscribe
<
https://github.com/notifications/unsubscribe-auth/ABKMBRP3B7JW7BX2JGJ3B3LQQ4YJDANCNFSM4JF3LTAA

.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/mobxjs/mobx-state-tree/issues/1411?email_source=notifications&email_token=AAN4NBCQHRQ6DESG2U2FRJDQQ4Y5TA5CNFSM4JF3LTAKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECOARUI#issuecomment-547096785,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAN4NBHAHCLZTFZPBYVQYITQQ4Y5TANCNFSM4JF3LTAA
.

I don't know if its directly related but,
I have a WTF feeling earlier this year when after using MST for a while,
this code didn't work on sub property and not the root

   const MyModel = types.model("MyModel", {
      arr1: types.array(types.string)
    });

    const model = MyModel.create({
      arr1: ["c"]
    });

    unprotect(model);

    const fn = jest.fn();
    reaction(() => model.arr1, fn);

    // not passing also with that
    // applySnapshot(model.arr1, ["a", "b"]);
    // @ts-ignore
    model.arr1 = ["a", "b"];

Full example:
https://codesandbox.io/s/mobx-state-tree-arrays-wtf-zonww?fontsize=14&previewwindow=tests

Was this page helpful?
0 / 5 - 0 ratings