deno icon indicating copy to clipboard operation
deno copied to clipboard

Add default implementation for WorkerOptions

Open cd-work opened this issue 3 years ago • 1 comments

This adds an implementation of Default for both WorkerOptions and BootstrapOptions. Since both of these structs are rather big, this should make it easier for people unfamiliar with the internals to focus on the options relevant to them.

As a user of deno_runtime I feel like these should serve as good defaults, getting people them started without having to tweak the runtime. Additionally even if some changes are made, the usage of ..Default::default() will significantly help with code clarity and verbosity.


Some of these defaults might be somewhat questionable, but I think the convenience of ..Default::default() still outweighs that. However there is the option of using a builder pattern, which would allow to still have some mandatory options while the rest could be relied upon as fallbacks.

I think the majority of deno_runtime consumers would rely on the defaults of most WorkerOptions, so having some methodology that doesn't require setting every single one of them makes sense to me and would likely clean up consumer code significantly.

cd-work avatar Jun 13 '22 16:06 cd-work

Interesting PR, I think we could land as is (or with some minor changes). Please make sure your code your code passes lint and format.

bartlomieju avatar Jun 26 '22 22:06 bartlomieju

Personally, I'd love to see this being merged. 👍

Having no default makes creating workers relatively cumbersome. As an example, in the case of this demo, it makes up over half of the code:

https://github.com/denoland/roll-your-own-javascript-runtime/compare/main...mlafeldt:roll-your-own-javascript-runtime:deno_runtime

All defaults except for runtime_version make sense to me. I don't know how it's being used, but an empty string or "n/a" might be less confusing than a wrong version (Deno vs deno_runtime)?

mlafeldt avatar Aug 11 '22 11:08 mlafeldt

Would also reduce the boilerplate in tests and examples, e.g.

  • https://github.com/denoland/deno/blob/25a1cc1b28ee2f37deb6305c0feb66d4eec50804/runtime/worker.rs#L402
  • https://github.com/denoland/deno/blob/25a1cc1b28ee2f37deb6305c0feb66d4eec50804/runtime/examples/hello_runtime.rs#L29

mlafeldt avatar Aug 11 '22 11:08 mlafeldt

@cd-work could you please rebase this PR? I think we should land it after all

bartlomieju avatar Sep 02 '22 20:09 bartlomieju

I've rebased it, I'm not sure the CI failure is related to this PR?

cd-work avatar Sep 06 '22 20:09 cd-work

I've rebased it, I'm not sure the CI failure is related to this PR?

It's not, CI should pass now

bartlomieju avatar Sep 07 '22 12:09 bartlomieju