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

custom asynchronous deserialization #85

Merged
merged 9 commits into from
Jul 23, 2018
Merged

Conversation

1R053
Copy link
Collaborator

@1R053 1R053 commented Jul 6, 2018

I've had the need to wait for the resolution of references during a custom polymorphic deserialization step. The custom propSchema only allowed a synced process as far as I have seen. Therefore, I've added the customAsync propSchema, which allows more control over the process. It should also allow other async deserialization logic, as it is kept as generic and close to the original interface as possible.

I assume, the next version should be 1.2.1, but please check the correctness of the changelog and package.json.

Thanks

Jörg Rose added 2 commits July 4, 2018 22:46
…deserialization. Especially, it allows to wait for resolution of references inside custom properties.
…deserialization. Especially, it allows to wait for resolution of references inside custom properties.
@coveralls
Copy link

coveralls commented Jul 6, 2018

Coverage Status

Coverage increased (+0.03%) to 92.227% when pulling 42343a2 on evoye:master into 61b2ce6 on mobxjs:master.

@alexggordon
Copy link
Member

Offhand this looks pretty good when looking over it! Let me look over this locally. Ideally, every serializr type would return a promise, such that everything would be async, but unfortunately that is not the case right now.

I was kind of just thinking that it might be good to combine this with custom, instead of having a customAsync. Something that would just test if a function is returned, just so people wouldn't have to think about which custom they need.

@1R053
Copy link
Collaborator Author

1R053 commented Jul 7, 2018

Initially, I was having the thought of combining it as well. To do this you would probably require the propSchema Function to know the parameter types of the serializer / deserializer function. I wasn't aware of a way to do this in VanillaJS. Even in TS this is probably not possible at runtime. Alternatively, you could pass additional options to the propSchema, however this would increase verbosity. Since I thought that explicitly showing somehow the async nature of the schema is important in the class declaration, I thought that giving it a new propSchema would be best. The existing core schemas like object, list etc. don't require async callback imho, or do you have good use case for it that should not be better covered with custom code?

@1R053
Copy link
Collaborator Author

1R053 commented Jul 7, 2018

issue #77 we should be also thinking about.
Imo it is either:
a) callback as last argument and all other args (context + oldValue) required or
b) callback after last mandatory arg and context + oldValue optional

internally it was a) which is why I made the PR this way but maybe b) is the more convenient form overall

@1R053
Copy link
Collaborator Author

1R053 commented Jul 7, 2018

how about going with option b) and using deserializer.length to detect whether there are 4 parameters in the function definition? This would be the certain indicator that the function expects a callback and serializr could pass the callback to the function instead of running async. Not sure however, whether this works with promisify.

@1R053
Copy link
Collaborator Author

1R053 commented Jul 17, 2018

the simple solution with checking for a 4th callback parameter worked fine for me. Ready to pull imo

@alexggordon
Copy link
Member

One last thing, can you bump the version back down to 1.2.0? The version will get bumped when we push a new release to NPM

@1R053
Copy link
Collaborator Author

1R053 commented Jul 18, 2018

sure, done

@alexggordon
Copy link
Member

Awesome. I’ll merge and deploy the new version later today.

Copy link
Member

@chengjianhua chengjianhua left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent ~ Just some tiny changes required.

@@ -295,7 +295,7 @@ When deserializing a model elememt / property, the following fields are availabl

## ModelSchema

[src/serializr.js:52-52](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/serializr.js#L52-L52 "Source code on GitHub")
[src/serializr.js:52-52](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/serializr.js#L52-L52 "Source code on GitHub")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The update of README.md is caused by the prepublish script declared in the package.json. All the links have been replaced with which related to you. You should discard the update of this. I will keep the readme being changed accidentally someday.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since I updated the docs for the custom propschema, the prepublish scripts needs to run to generate the respective readme chapter. Since I cannot do this for the official repo, maybe you can pull, run prepublish and commit the final readme before publishing to npm.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah ~ I and @alexggordon can do this after your PR merged.

@@ -71,8 +71,9 @@ export function map(propSchema: PropSchema): PropSchema;

export function mapAsArray(propSchema: PropSchema, keyPropertyName: string): PropSchema;

export function custom(serializer: (value: any) => any, deserializer: (jsonValue: any) => any): PropSchema;
export function custom(serializer: (value: any) => any, deserializer: (jsonValue: any, context?: any, oldValue?: any) => any): PropSchema;
export function custom(serializer: (value: any) => any, deserializer: (jsonValue: any, context: any, oldValue: any, callback: (err: any, result: any) => void) => any): PropSchema;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An empty line break for readability?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently, line breaks are only between different functions, not signature alternatives. We just introduced the latter with the final version.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Your explanation is reasonable.

@alexggordon alexggordon merged commit ef97799 into mobxjs:master Jul 23, 2018
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

Successfully merging this pull request may close these issues.

4 participants