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

Add Serializable and Transferable interfaces #1

Merged
merged 2 commits into from
Apr 12, 2024

Conversation

kasperisager
Copy link
Contributor

@kasperisager kasperisager commented Apr 8, 2024

The Serializable interface has two methods defined by the well-known symbols Symbol.for('bare.serialize') and Symbol.for('bare.deserialize'). The former is an instance method that returns a serializable representation of the class instance. The latter is a class method that takes that same representation and creates a class instance.

Likewise, the Transferable interface two methods defined by the well-known symbols Symbol.for('bare.detach') and Symbol.for('bare.attach') as well as an optional named detached property. The former method is an instance method that detaches the class instance and returns a serializable representation of the data to transfer, such as an External pointer. The former method is a class method that takes that same data and creates a class instance with the data attached.

To actually use classes that implement these interfaces during serialization and deserialization, the classes must be registered. This happens via new function arguments:

class Foo extends Serializable {}

const serialized = serialize(new Foo(), false, [Foo])

deserialize(serialized, [Foo])
class Foo extends Transferable {}

const foo = new Foo()

const serialized = serializeWithTransfer(foo, [foo], [Foo])

deserializeWithTransfer(serialized, [Foo])

Attempts to serialize or transfer unregistered interfaces will throw.

@kasperisager kasperisager marked this pull request as ready for review April 12, 2024 08:04
@kasperisager kasperisager merged commit 22e77ba into main Apr 12, 2024
3 checks passed
@kasperisager kasperisager deleted the extensible-cloning-and-transfer branch April 12, 2024 08: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.

1 participant