Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

Mobx-state-tree, create a types.custom from a protobuf message

$
0
0

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.


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>