-
Notifications
You must be signed in to change notification settings - Fork 5
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
Need preset DOM or StructuredClone #2
Comments
I'm thinking that I suggest having a structured cloning type instead of |
(Btw, my recent test addition to typeson-registry will not work until the new version of |
Hmm, the 'post-message' preset name is a little misleading. It's there to complement the structured clone as structured clone has limitations for practical reasons (marshaling Error objects for example) |
It's meant for using together with postMessage. Since postMessage() already supports structured clone, this one just need to encapsulate Error objects and leave the rest to the browser to marshall. |
Oh, yeah, I c... I'm thinking also from the Node perspective where we have to shim
|
I've pushed an initial implementation of SCA... Let me know what you think... I am probably missing some others that should throw such as As far as my use of |
Very nice! I've released this as version 1.0.0-alpha.1 on npm. Keeping the issue open for complementing missing types and exceptions. |
The cloning algorithm mentions (non-array) exotic objects as failing. I realized I could add some more error checking (built-in exotic objects we were missing, i.e., While I think we have covered those exotics defined in the ES spec, I couldn't find explicit reference to DOM objects as being such (and thus failing the algorithm), though I did add duck-typing
|
Good. Released it as 1.0.0-alpha.2 |
Thanks! I've elaborated on remaining questions and asked for help at http://stackoverflow.com/questions/42170826/categories-for-rejection-by-the-structured-cloning-algorithm |
Added one more tweak with docs (but will also rely on a typeson update)... |
And now I've removed the frame/module-dependent I think there is a problem with building typeson-registry with Webpack now though. I'm guessing it may be due to the cyclic dependencies. I don't know much about Webpack but think browserify would be fine with this kind of thing if it is no difference to you. |
There is one breaking change which I think ought to be made. The replacers do their var typeson = new Typeson().register([
{specificClassFinder: [(x) => x instanceof MyClass, () => {...}]},
{genericClassFinder: [(x) => x && typeof x === 'object', () => {...}]}
]); Would you be open to such a change? (I've documented the current behavior and added a test for it for the time being, but it can easily be reversed.) |
My thought has been to have generic rules first. Let's say someone creates a module that extends Typeson and pre-registers a preset in its constructor. The consumer of that libraries should then be able to call register() to override general rules to more specific ones. |
Makes sense... But one could still keep specific-generic ordering within the array and allow subsequent calls to splice onto the beginning of the array. From a linear programmatic point of view it seems to me the specific ones in a block ought to catch first even if subsequent calls get higher priority. Of course whatever way can work. |
Hi, I've pushed a new change to Although I thought it would be best to have a breaking version which forced the async API at least by default (in case a type relied on async behavior) and so that insistence on using the sync API could be done via While there may be some use cases for leveraging |
Hm. It's nice you start dealing with this architectural necessity. I've no concerns making a breaking change and bump the major version. However, for keeping the analogy with JSON.parse() and JSON.stringify() I prefer keeping the method behaviour synchronous and instead add *Async() versions to support async versions. This also indicates that the sync version of the API will continue to be a first class citizen in future and will not be deprecated. Do we really need a custom Promise here? It's a lot of code just because we need to follow W3C's spec and throw when a standard Promise is encapsulated. Couldn't we just let the registered encapsulator function return a std Promise that we put in an internal representation with less code (the internal representation wouldn't need to follow the entire Promise API). My suggestions would be the following. Note however, that I may have missed a point. If so, please correct or question me ;) Synchrounous APIstringify() - synchronous, as now Asynchronous APIstringifyAsync() - returns Promise Registered types
W3C ComplianceTrying to stringify a Promise should throw (if imported SCA preset). Therefore we need to have a built-in handling of whenever a registered reviver or encapsulator returns a Promise, it will be converted to an internal representation of that Promise (much like your Typeson.Promise, but won't need all the bells and wistles of it). The sync versions should through when hitting an async type. Future support for streamsA future version may add support for streams as well instead of promises (postfixing methods with *Stream). If it would have been possible to construct a Blob without having its entire content in memory, we would be better off with a stream-based API (based on either Async Iterables or ES-Observabe). Now as Blob cannot be constructed from a stream, a simpler API is probably Promise based. |
Thanks for the prompt and thorough response and consideration. Good point on having async for the reviver too. I had considered this, but dismissed it initially in thinking that the user would know it was async if a promise were returned, but on further consideration, in using async, they'd be able to immediately introspect on the final resolved type, whether async or not. I also like the idea of throwing (though perhaps optionally, see below) when encountering an async type in using a sync method. Sounds good about stream support. I've added dfahlander/typeson#3 for tracking this. Preference of Async over SyncAs far as keeping the analogy with JSON.parse/JSON.stringify, that makes sense. However, I do think we ought to give some strong suggestions to users to prefer the async methods.
Besides this, the browsers are deprecating things like sync XHR for a reason I must begrudgingly admit is important, namely preventing degraded performance from sites hogging resources, so if types can be implemented with both, the async ought to be encouraged.
And in such a case where both sync and async are possible, it would also be desirable to normally encourage the async method for UX reasons.
|
I've pushed a bunch of changes to
Would be glad to get your feedback. But even more urgently, from my perspective, I found a problem (which pre-existed my changes) for a certain type of recursive array (possibly objects too) for which I would particularly appreciate any help: dfahlander/typeson#4 |
Oh, and I just pushed changes to |
Ok, thanks! I'm not dead-set against the Promise thing. I'm ok with that. Having two sync APIs may confuse the user though. Is it really nescessary to have a *Sync version? Couln't you do those checks in the non-postfixed versions? |
No, I can drop the |
I think this can be closed now? The Stream API concept discussed in this thread has an issue already at dfahlander/typeson#3 . |
I'll go ahead and close, but feel free to reopen if there is something missing here, @dfahlander |
Go ahead! Sorry for missing out to respond |
'builtin' preset only contains ecmascript types. Other types from StructuredClone algorithm (except Blob, which cannot be serialized synchronically) should be defined and available as a preset.
The text was updated successfully, but these errors were encountered: