Skip to content
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

Allow setting up emitter #59

Open
Richienb opened this issue Jul 22, 2020 · 6 comments
Open

Allow setting up emitter #59

Richienb opened this issue Jul 22, 2020 · 6 comments

Comments

@Richienb
Copy link
Contributor

Just like how Cold Observables set up their emitter when they are first subscribed to, something similar should be done here. Using Emittery.listenerAdded, it is possible to detect when events are first listened to so it should be easy to invoke a callback from there.

For example:

const Emittery = require('emittery');

const emitter = new Emittery();

emitter.setup('name', () => {
    // Prepare emitter...
});

// Invoke preparation logic
emitter.on('name', () => {
    //...
})

// @sindresorhus

@sindresorhus
Copy link
Owner

Do you actually need this for something? If so, what?

@Richienb
Copy link
Contributor Author

This is useful for emitters that require network access for polling or have to subscribe to other event emitters. Here's my use case:

const Emittery = require("emittery")

const emitter = new Emittery()

emitter.setup("mouse", () => {
	terminal.grabInput({
		mouse: "button"
	})

	terminal.on("mouse", (name, data) => {
		// Handle data...

		emitter.emit("mouse", data)
	})
})

module.exports = emitter

@sindresorhus
Copy link
Owner

Alright. I think it needs a "unsetup" hook too then. And a better name than "setup". What naming do other similar packages use?

@Richienb
Copy link
Contributor Author

@sindresorhus How about activate and deactivate?

@sindresorhus
Copy link
Owner

That's not better. I guess setup/teardown is fine. But how about init/deinit?

@Richienb
Copy link
Contributor Author

But how about init/deinit

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants