mobx-persist
mobx-persist copied to clipboard
MobX 6: Can I serialize array of primitives?
I have a test. The test passes.
import { makeAutoObservable, toJS } from 'mobx';
import { serialize, update } from 'serializr';
import { persist } from 'mobx-persist';
class StoreOneItem {
constructor(public name: string, public id: string) {
makeAutoObservable(this);
}
}
class StoreOne {
constructor(
public lines: string[], // mobx-persist can't serialize this
public items: StoreOneItem[],
) {
makeAutoObservable(this);
}
get linesCount(): number {
return this.lines.length;
}
public setLines(lines: string[]) {
this.lines = lines;
}
}
const schema = {
lines: false,
items: {
type: 'list',
schema: {
name: true,
id: true,
},
},
};
persist(schema)(StoreOne);
it('save', () => {
const targetStorage = new StoreOne(
['first', 'second'],
[new StoreOneItem('item-1', 'item-1-id')],
);
expect(serialize(targetStorage)).toEqual({
items: [
{
id: 'item-1-id',
name: 'item-1',
},
],
});
});
Case 1. But, if I change schema to:
const schema = {
lines: true,
items: {
type: 'list',
schema: {
name: true,
id: true,
},
},
};
I'll get an error:
Error: [serializr] this value is not primitive: first,second
Case 2. Or if I change schema to:
const schema = {
lines: {
type: 'list',
schema: true,
},
items: {
type: 'list',
schema: {
name: true,
id: true,
},
},
};
I'll get another error:
[serializr] No modelschema provided. If you are importing it from another file be aware of circular dependencies.
Can I serialize array of primitives?
Package versions:
"mobx": "6.0.1",
"mobx-persist": "^0.4.1",
"version": "1.5.4"