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

Implement a managed API for bind, on, after and every #107

Closed
kasper opened this issue Jun 23, 2016 · 5 comments
Closed

Implement a managed API for bind, on, after and every #107

kasper opened this issue Jun 23, 2016 · 5 comments
Assignees
Milestone

Comments

@kasper
Copy link
Owner

kasper commented Jun 23, 2016

It seems that those users who don’t want to dynamically control handlers seem to get confused for the requirement to retain handlers. Lets abstract this in the JavaScript scope. Implement a managed API for bind, on, after and every.

a.) Implement them to the global scope:

bind('q', [ 'ctrl', 'shift' ], function () {});
on('screensDidChange', function () {});
after(double interval, function () {});
every(double interval, function () {});

b.) Create a prettier API with counter actions:

Key.bind('q', [ 'ctrl', 'shift' ], function () {});
Key.unbind(handler);

Event.on('screensDidChange', function () {});
Event.off(handler);

Timer.after(1, function () {});
Timer.every(1, function () {});
Timer.stop(handler);

This will not change or break existing behaviour and advanced contexts can and should still manually manage handlers. We should also change all examples to use the managed API.

@mafredri
Copy link
Contributor

mafredri commented Jun 24, 2016

I like a) for being concise and to the point, but also b) for being more clear. In favor of b), it might introduce new possibilities as well, e.g. Key.availableKeys() (could be used to bind all available keys temporarily) or what else could be dreamed up.

Also, since option a) doesn't have a way to unbind, we could return a callback function that unbinds, e.g.:

var change = on('screensDidChange', function () {});
change(); // unbind

This pattern at least used to be pretty common in JS, not sure about where it stands today.

PS. Hyvää juhannusta! 😄 🎉

@kasper
Copy link
Owner Author

kasper commented Jun 24, 2016

@mafredri Kiitos, sitä samaa!

@kasper
Copy link
Owner Author

kasper commented Jun 27, 2016

This has been now implemented and will be released in 2.1.2. I hope the new API is more consistent than the previous examples. This should also be a more familiar concept for new users. 😄

Key.on('q', [ 'ctrl', 'shift' ], function () {});
Key.off(identifier);

Event.on('screensDidChange', function () {});
Event.off(identifier);

Timer.after(1, function () {});
Timer.every(1, function () {});
Timer.off(identifier);

@kasper kasper closed this as completed Jun 27, 2016
@mafredri
Copy link
Contributor

Nice effort! :)

@kasper
Copy link
Owner Author

kasper commented Jun 27, 2016

@mafredri Thanks! Obviously to get the full effect for keys, this also needs the KeyHandler change for binding (#99).

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