Skip to content

Revive classes#36

Merged
merelinguist merged 9 commits intomainfrom
revive-classes
Aug 10, 2020
Merged

Revive classes#36
merelinguist merged 9 commits intomainfrom
revive-classes

Conversation

@Skn0tt
Copy link
Copy Markdown
Collaborator

@Skn0tt Skn0tt commented Jul 31, 2020

Closes #34

Implements revival of ES6 classes by attaching the original prototypes during revival. It requires registering all neccessary classes using SuperJSON.registerClass(...).
In the future, this could be done automatically in a compile-step (Which poses it's own issues, e.g. figuring out which classes are actually used and which classes to bundle therefore, but would make it opaque to Blitz users).

@Skn0tt Skn0tt requested a review from merelinguist July 31, 2020 20:16
@Skn0tt
Copy link
Copy Markdown
Collaborator Author

Skn0tt commented Jul 31, 2020

One thing I couldn't figure out is how to extend SuperJSONValue to any class-based values - do you have an idea on this @merelinguist? See the type errors introduced in 70f2912 for reference.

throw new Error('Trying to deserialize unknown class');
}

return Object.assign(Object.create(clazz.prototype), json);
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This is where the magic happens btw. Object.create(clazz.prototype) creates a new, empty instance of clazz, which is much faster than changing the prototype of the existing value using Object.setPrototypeOf

@merelinguist
Copy link
Copy Markdown
Contributor

@Skn0tt Hmm don’t really have any ideas about the types. Is it mergeable with looser typing?

@Skn0tt
Copy link
Copy Markdown
Collaborator Author

Skn0tt commented Aug 5, 2020

@merelinguist this is the best I could come up with: 29d10cc. This will effectively downgrade our types to any, but I don't see any way to make it better :(

@merelinguist merelinguist merged commit fc932c9 into main Aug 10, 2020
@merelinguist merelinguist deleted the revive-classes branch August 10, 2020 09:11
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.

Add support for serialising ES6 classes

2 participants