Reduce boilerplate for firing events
See original GitHub issueInstead of requiring this boilerplate:
import { createEventDispatcher } from 'svelte';
const dispatch = createEventDispatcher();
with the warning that the call to createEventDispatcher
must happen at the top level, just provide a global variable dispatch
(maybe there can be a project-level option for the name), and if you detect that it is used, emit the boilerplate during compilation, otherwise omit it.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:14
- Comments:20 (8 by maintainers)
Top Results From Across the Web
Flutter BLoC: Transformers - Reduce boilerplate and… - Medium
Transformers dictate how your event will be processed within a bloc. They can help reduce boilerplate code and keep your code organized.
Read more >How to write less boilerplate for property changed notifications?
Am I missing some simpler solution? You could fire the PropertyChanged event with a null or empty property name to notify about a...
Read more >Reduce Boilerplate Code for DAO's -- Hades Introduction
There is a problem with the approach: this boiler plate code becomes part of your application source code and you will have to...
Read more >How to use Bloc with streams and concurrency
With this change comes several benefits, including reduced boilerplate, better consistency with cubit, and, most of all, concurrent event ...
Read more >Java Programming Skills – Boilerplate Code - Alibaba Cloud
Some boilerplate codes cannot be reduced just because we have not found suitable solutions. Typically, you can reduce boilerplate codes in ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m not sold on this being a great idea, but technically I don’t think this would be much of a hassle.
There’s precedent for special
$$
-prefix variables (although currently it’s a precedent of one,$$props
). It’d be pretty easy to inject the appropriateimport
and callcreateEventDispatcher()
and assign it to$$dispatch
if that variable were used.I can’t speak to other tooling, but there’s a mechanism already in place that the compiler is using to tell eslint-plugin-svelte3 about variables that it’s injecting (used for
$$props
as well as the$
-prefixed store autosubscriptions), and this could be used to tell the plugin about$$dispatch
without any more work needed in the plugin.But, again, none of this is necessarily an endorsement of this being a good idea. Just that it’s reasonable from a technical standpoint.
It seems it would spite @antony, so I turned this feature into a preprocessor: https://github.com/rixo/svelte-preprocess-autoimport.
This way, everyone can try it and choose for themselves.
I too have always been bothered by the boilerplaty syntax of
createEventDispatcher
and, increasingly, also by the micro management of lifecycle functions imports likeonMount
andonDestroy
, that often come and go as a component evolves…On the other hand, I’ve lived through the “everything global” and “libraries attach to default prototypes” era of JS, and so I’m pretty wary of magic imports and magic in general. And I guess that the lack of imports in the source code will surely trip up tooling like Typescript.
Yet, as long as it is limited to the framework’s own helpers, I think the added magic vs reduced boilerplate & micro management trade off might be worth it. I’m personally going to try this in my code, and see if I encounter any major setbacks that would make me reverse course.