Replies: 1 comment 2 replies
-
If you aren't actually going to deploy this in a multi-threaded enviromment, you might as well make a wrapper that pretends You can also use |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I recently wrote JsArc, which is a wrapper around JsValue that is Send + Sync. It accomplishes this by keeping JsValues on one thread (in syntax only) and sending closures from other threads to be executed on it. The purpose for writing it was to more easily pass trait restrictions.
I think it's examples are mostly self explanatory
JsValues are wrapped with closures, then those wrapped JsValues can be used with each other. It then makes it possible to do the following even though JsValue itself is not Send + Sync
I arrived at writing this type after writing an abstraction for bindings to WalletConnect. When writing those bindings, I needed to implement a trait for a transport that interacted with a JsValue. (And that trait required the type to be Send + Sync). In the library, the type that implemented the trait used a
async_channel::Sender
which sent messages to runtime/server running in a promise.Solution: Message passing to runtime/server
I realized afterwards that the abstraction could likely been less complex with the closure wrapping type
Solution: Using a JsArc to abstractly include a JsValue
I think the pattern of using message passing to circumvent the trait restrictions has likely been used in other libraries. Are there any other variations of the created type that already exist? Are there any more obvious solutions I should have considered instead?
edit: Sorry for the bump. My account was on a shadow ban and this post disappeared from GitHub. I still want to keep this question open, because I think there are likely better possible versions. (but maybe they'll be written unrelated to WASM)
I also removed the WalletConnect library because I didn't really like it, but I think the discussion is still relevant.
Beta Was this translation helpful? Give feedback.
All reactions