-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: allow serialization/deserialization of custom data types #13125
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
Conversation
|
|
preview: https://svelte-dev-git-preview-kit-13125-svelte.vercel.app/ this is an automated message |
|
I'd love an api where They have a strict relation to one another and changing one without the other can lead to bugs. It would also simplify implementing tests that ensure serialize(deserialize(x)) equals x and deserialize(serialize(y)) equals y Users can possibly do that in userland by importing them from such a file, but just lik param matchers i think we could provide that from a known location. |
So I tried to do something like that, but I gave up. I couldn't figure out how how we could safely take the function on the server and use it on the client, given the function references imports, and generate client code with the same logic without leaking server stuff. This is my first Kit PR, so maybe I just need someone to help me understand how that sort of thing can be done. |
|
its a pair of pure functions tied together by a name, so shouldn't it work in a similar way as //src/serializers/foo.js
export const is(foo: any): boolean;
export const serialize(foo: Foo): string;
export const deserialize(str: string): Foo;due to treeshaking only one of serialize and deserialize should make it to the output of client or server. |
The body of the functions will require importing the class though. So when it runs on the client, the import will be missing? |
It's not done like that because currently we're not using the serialize function on the client / the deserialize function on the server. |
dummdidumm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good so far! I think this doesn't handle the client side navigation case yet though, i.e. when the data loading happens via __data.json. E.g. if you start on /foo and that contains a link to /serialization-basic, so it (de)serialize the data correctly?
| else fulfil(data); | ||
| const try_to_resolve = () => { | ||
| if (!deferred.has(id)) { | ||
| setTimeout(try_to_resolve, 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was is necessary after all due to the dynamic imports? If so we should add a comment here stating why this is necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this feels suuuuuper hacky, surely we can find another approach?
|
@dummdidumm Thanks for the feedback. Fixed! |
Co-authored-by: Rich Harris <[email protected]>
|
Opened an alternative PR that puts the encoding/decoding logic in a single hook: #13149 |
|
Closing in favor of #13149 |
Enables serialization and deserialization of custom data types in the form of hooks.
Closes #9401.