Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Feature request: Promise-based API for singleton services. #6

Open
bolinfest opened this issue Apr 17, 2015 · 2 comments
Open

Feature request: Promise-based API for singleton services. #6

bolinfest opened this issue Apr 17, 2015 · 2 comments

Comments

@bolinfest
Copy link

Because services are basically the only way to communicate between Atom packages, it is common that there is expected to be exactly one provider for a given service. As such, it would be convenient if there were a Promise-based API to subscribe to the first provider registered for a service in a given range:

var promise = global.services.consumeOnly('special-build-toolbar', '1.0.0');

If there are multiple providers for special-build-toolbar at the time consumeOnly is called, then it could return a Promise that rejects.

@thomasjo
Copy link

At first glance I'm 👍 on this feature request. Seems like it could reduce boilerplate code.

How would you handle the rejection gracefully?

try {
  var promise = global.services.consumeOnly('special-build-toolbar', '1.0.0');
} catch (error) {
  // ?
}

I'm sure as the package ecosystem continues to grow, we'll run into scenarios with multiple providers, so I think it's worth having a story for handling that within your suggested scheme.

@bolinfest
Copy link
Author

Here's what I'm doing right now:

'use babel';
/* @flow */

function consumeFirstProvider(keyPath: string, version = '0.0.0'): Promise {
  return new Promise((resolve, reject) => {
    var sub = atom.packages.serviceHub.consume(keyPath, version, provider => {
      resolve(provider);
      sub.dispose();
    });
  });
}

module.exports = {
  consumeFirstProvider,
};

In this implementation, the returned Promise may never resolve, but it will never reject.

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

No branches or pull requests

2 participants