My goal is to create a custom type for mobx-state-tree where the type is based on my protobuf message.
Here is what I have.
// create my custom typeconst myMessagePrimitive= types.custom<Uint8Array, SomeProtobufMessage>({ name: "myMessage", fromSnapshot(value: Uint8Array) : SomeProtobufMessage { return SomeProtobufMessage.deserializeBinary(value) }, toSnapshot(value: SomeProtobufMessage) : Uint8Array { return value.serializeBinary() }, isTargetType(value: Uint8Array| SomeProtobufMessage): boolean { return value instanceof SomeProtobufMessage }, getValidationMessage(value: string): string { return '' }})// modelconst MyModel = types.model({ myMessage: myMessagePrimitive})// Create my modelconst instanceOfMyModel = MyModel.create({ myMessage: new Uint8Array() // lets be explicit that its a Uint8Array})
I expect MyModel.create to call my fromSnapshot under the hood. However using console.log I can see that toSnapshot is actually being called. The result is I get an error that .serializeBinary is not a function of a Uint8Array.
This is the example I have worked from..https://mobx-state-tree.js.org/API/#customReally seems like fromSnapshot should be called by MyModel.create.
To me this should be so simple as I have an object that is serializable. Any better resources on types.custom for Mobx-state-tree much appreciated.