question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reduce boilerplate for firing events

See original GitHub issue

Instead 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:open
  • Created 4 years ago
  • Reactions:14
  • Comments:20 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
Conduitrycommented, Aug 31, 2019

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 appropriate import and call createEventDispatcher() 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.

2reactions
rixocommented, Sep 23, 2020

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 like onMount and onDestroy, 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found