Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MobX 6: Can I serialize array of primitives? #91

Open
RusovVladimir opened this issue Nov 20, 2020 · 0 comments
Open

MobX 6: Can I serialize array of primitives? #91

RusovVladimir opened this issue Nov 20, 2020 · 0 comments

Comments

@RusovVladimir
Copy link

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"
@RusovVladimir RusovVladimir changed the title MobX 6: Can I serialize array of primitives MobX 6: Can I serialize array of primitives? Nov 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant