mobxjs / mobx-state-tree

Full-featured reactive state management without the boilerplate
https://mobx-state-tree.js.org/
MIT License
6.94k stars 640 forks source link

How to store Buffer type in state #1964

Closed msaraac closed 2 years ago

msaraac commented 2 years ago

Question I would like to store Buffer in the state but I could not find any solution. I tried frozen with default sized buffer. But when I try to change this frozen property I'm getting errors. Is there a way to store buffers ?

.props({
    type: types.optional(types.number, 0),
    fc: types.optional(types.number, 0),
    seq: types.optional(types.number, 0),
    len: types.optional(types.number, 0),
    data: types.optional(types.frozen<Buffer>(), Buffer.alloc(5)),
  })
BATCOH commented 2 years ago

Frozen type accepts values that immutable and JSON-serializable. Buffer afaik is mutable and not serializable.

You can create your own custom type for Buffer if you need some advanced features (like snapshots/patches), or just use volatile state.