kasper / phoenix

A lightweight macOS window and app manager scriptable with JavaScript
https://kasper.github.io/phoenix/
Other
4.38k stars 127 forks source link

Add a `Event.once` and `Key.once` #201

Closed ngryman closed 6 years ago

ngryman commented 6 years ago

Hi @kasper, first and foremost, this is awesome tool! Thanks for your work.

Description

This is a small feature request for convenience, it would be nice to be able to subscribe to an event only once so when it's called, the event is automatically unsubscribed. Most of event managers out there provide this feature and I think it could be cool.

As a bonus, if the handler explicitly returns false the event is not unsubscribed. So it lets the user filter the event he wants to be executed one.

Example

const filteredCallback = (appName, callback) =>
  (app) =>
    app.name() === appName
      ? callback(app)
      : _.stubFalse()

Event.on('appDidLaunch', filteredCallback('Code', () => {
  Event.once('appDidActivate', filteredCallback('Code', () => {
    // do stuff only once
  })
})
kasper commented 6 years ago

Hi @ngryman! Thanks for the kind words! This definitely seems like a good addition to the API and is easy to implement in the JavaScript library. I will look into it!

kasper commented 6 years ago

Implemented!