You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, thanks for the great mobx-keystone library! I've run into what I believe might be an edge use case that might not be supported by mobx-keystone and wanted some feedback on the issue.
I have an application that implements a basic file system using mobx-keystone that gets persisted in local storage as a json string. This file system is a recursive tree-like structure of folders and files. While it would be natural to store this data in the mobx-keystone root tree in its nested form, I'd really like the store to be normalized backed by an ObjectMap and use Refs to resolve the parent / child relationships of the folders and files. However when I try to deserialize the normalized store of folders I run into an error when trying to resolve the refs.
Calling fromSnapshot<FileStore>(JSON.parse(storeJsonString)) results in error:
Uncaught Error: a reference of type '...' could not resolve an object with id ...
I assume this is because fromSnapshot is trying to deserialize the normalized collection, but the items in the collection contain references to other items in the same collection currently being deserialized.
Here's what my models look like at a high level (sorry if all the abstractions make it difficult to follow):
/**
* Represents a normalized store of file system nodes
*/
@model('file-store')
export class FileStore extends ExtendedModel(
modelClass<DataStore<IFilesystemNode>>(DataStore), {}
) {
onAttachedToRootStore() {
// On snapshot, persist the contents of the file store to local storage.
const dispose = onSnapshot<FileStore>(this, (snapshot) => {
localStorage.setItem(FILE_STORE_KEY, JSON.stringify(snapshot));
});
return () => dispose();
}
}
/**
* Represents an abstract base node in a filesystem
*/
@model('filesystem-node')
export class FilesystemNode extends Model({
id: idProp,
filename: prop<string>('').withSetter(),
}) {
@observable
nodeType = FilesystemNodeType.None;
}
My question: Is there a way to deserialize a model's snapshot JSON when it contains Refs to other objects in the same collection? Please let me know if my question is unclear. Thanks again for the great work!
The text was updated successfully, but these errors were encountered:
Hi, thanks for the great mobx-keystone library! I've run into what I believe might be an edge use case that might not be supported by mobx-keystone and wanted some feedback on the issue.
I have an application that implements a basic file system using mobx-keystone that gets persisted in local storage as a json string. This file system is a recursive tree-like structure of folders and files. While it would be natural to store this data in the mobx-keystone root tree in its nested form, I'd really like the store to be normalized backed by an
ObjectMap
and useRef
s to resolve the parent / child relationships of the folders and files. However when I try to deserialize the normalized store of folders I run into an error when trying to resolve the refs.Calling
fromSnapshot<FileStore>(JSON.parse(storeJsonString))
results in error:Uncaught Error: a reference of type '...' could not resolve an object with id ...
I assume this is because
fromSnapshot
is trying to deserialize the normalized collection, but the items in the collection contain references to other items in the same collection currently being deserialized.Here's what my models look like at a high level (sorry if all the abstractions make it difficult to follow):
My question: Is there a way to deserialize a model's snapshot JSON when it contains Refs to other objects in the same collection? Please let me know if my question is unclear. Thanks again for the great work!
The text was updated successfully, but these errors were encountered: