There seems to be a problem with getRefsResolvingTo after loading the state from a snapshot. Specifically, I'm retrieving backreferences in a computed property which uses getRefsResolvingTo and all works fine with the "original" state, but when I load the state from a snapshot, the array of backreferences returned by the computed property is empty.
I've created a test case that demonstrates the problem, but I haven't been able to find its cause.
import { computed } from 'mobx';
import {
fromSnapshot,
getParent,
getRefsResolvingTo,
getSnapshot,
model,
Model,
prop,
Ref,
registerRootStore,
rootRef,
} from 'mobx-keystone';
test('getRefsResolvingTo after loading from snapshot', () => {
@model('#56/Root')
class Root extends Model({
a: prop<A>(),
b: prop<B>(),
}) {}
@model('#56/A')
class A extends Model({}) {
@computed
public get bs(): B[] {
return Array.from(getRefsResolvingTo(this), ref => getParent<B>(ref)!);
}
}
@model('#56/B')
class B extends Model({
a: prop<Ref<A>>(),
}) {}
const aRef = rootRef<A>('aRef');
const a = new A({});
const b = new B({ a: aRef(a) });
const root = registerRootStore(new Root({ a, b }));
expect(root.a.bs).toHaveLength(1);
expect(root.a.bs[0]).toBe(b);
const newRoot = registerRootStore(fromSnapshot<Root>(getSnapshot(root)));
expect(newRoot.a.bs).toHaveLength(1); // ERROR: length is 0
});
There seems to be a problem with
getRefsResolvingTo
after loading the state from a snapshot. Specifically, I'm retrieving backreferences in a computed property which usesgetRefsResolvingTo
and all works fine with the "original" state, but when I load the state from a snapshot, the array of backreferences returned by the computed property is empty.I've created a test case that demonstrates the problem, but I haven't been able to find its cause.