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

Generic Writer Support #10

Closed
wants to merge 1 commit into from

Conversation

Meorawr
Copy link
Contributor

@Meorawr Meorawr commented Jan 16, 2022

This mirrors recent changes submitted for the deserialization API (#8) and provides a system for custom writer support, allowing control of how and where the byte stream generated during serialization is written. The change as-is is fully backwards compatible and passes the existing and expanded test cases - which can be used as examples.

Primary use cases would align with existing async serialization attempts (#4, #7) but don't put the complexity of thread management or throttling logic into the library, or risk breaking backwards compatibility. Instead the minutia of how those aspects are handled are left the responsibility of callers. It also enables some other advanced use cases such
as streaming the data to a destination without buffering it fully beforehand, which could enable efficiencies elsewhere.

This PR will likely conflict with #8 slightly if either is merged as some documentation and utility related changes are common between the two; otherwise however I'm happy with this PR as-is and can resolve those conflicts if either is merged.


As with #8 this defines a "Writer" protocol which expects that - at minimum - an object be supplied via the options table to SerializeEx() which provides a WriteString function. If this object is not supplied, or if the object supplied does not meet this requirement, it will be ignored and the existing approach of writing strings to a temporary buffer will continue to be used.

WriteString: function(writer, str) (required)

This function will be called each time the library submits a byte string that was created as result of serializing data.

Flush: function(writer) (optional)

If specified, this function will be called at the end of the serialization process. It may return any number of values - including zero - all of which will be passed through to the caller of SerializeEx() verbatim.

The default behavior if this function is not specified - and if the writer is otherwise valid - is a no-op that returns no values. In such a case it is assumed that the WriteString method will have either passed the strings it received on elsewhere, which seems a reasonable default given the advanced nature of this feature.


As with the deserialization/reader PR, this requires relatively minimal changes to the internals of the library - just a few changes to how a couple of operations are delegated.

This mirrors recent changes submitted for the deserialization API and
provides a generic system for custom writer support to control how and
where the byte stream generated during serialization is written. The
change as-is is fully backwards compatible and passes the existing and
expanded test cases.

Primary use cases would align with existing async serialization attempts
but don't put the complexity of thread management or throttling logic
into the library, instead leaving the minutia of how those aspects are
handled to callers. It also enables some other advanced use cases such
as streaming the data to a destination without buffering it fully
beforehand, which could enable efficiencies elsewhere.
@Meorawr Meorawr changed the title Implement support for custom writers Generic Writer Support Jan 16, 2022
@rossnichols
Copy link
Owner

@Meorawr sorry, I don't think I ever saw this because it wasn't assigned to me, and I just noticed it in the active PRs :(. I just finished going through #4 to try to make it ship-worthy (see #11). I'll take a look at this one and #8, any comments in the meantime?

@Meorawr
Copy link
Contributor Author

Meorawr commented May 12, 2024

No comments come to mind other than it's nice to see you again :)

@rossnichols
Copy link
Owner

You as well :). I just created a branch named readwrite-staging, what do you think about combining your two PRs and targeting that branch?

I really like the flexibility your approach provides here. I do like having at least the async/throttled aspect be part of the API surface directly, though, rather than requiring everyone to implement a custom reader/writer, but maybe the API surface could be implemented on top of this infrastructure rather than the slightly more invasive approach in #11. If you combine and retarget the PRs, I'll merge it and then spend some time looking at how to combine them.

@Meorawr
Copy link
Contributor Author

Meorawr commented May 12, 2024

Sounds good - I'll look at doing that some point this week.

@Meorawr
Copy link
Contributor Author

Meorawr commented May 15, 2024

Closing in favor of the merged #12 as discussed.

@Meorawr Meorawr closed this May 15, 2024
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.

2 participants