Remove atom.services global

Signed-off-by: Nathan Sobo <nathan@github.com>
This commit is contained in:
Max Brunsfeld
2015-02-02 10:01:34 -08:00
committed by Nathan Sobo
parent cb8b254502
commit 5638c7fb6a
6 changed files with 18 additions and 114 deletions

View File

@@ -449,23 +449,23 @@ describe "PackageManager", ->
describe "service registration", ->
it "registers the package's service providers", ->
service1V3 = null
atom.services.consume "service-1", "^0.3", (service) ->
atom.packages.serviceHub.consume "service-1", "^0.3", (service) ->
service1V3 = service
new Disposable -> service1V3 = 'deactivated'
service1V4 = null
atom.services.consume "service-1", "^0.4", (service) ->
atom.packages.serviceHub.consume "service-1", "^0.4", (service) ->
service1V4 = service
new Disposable -> service1V4 = 'deactivated'
service2V5 = null
atom.services.consume "service-2", "^0.5", (service) ->
atom.packages.serviceHub.consume "service-2", "^0.5", (service) ->
service2V5 = service
new Disposable -> service2V5 = 'deactivated'
# Incompatible
service2V6 = null
atom.services.consume "service-2", "^0.6", (service) ->
atom.packages.serviceHub.consume "service-2", "^0.6", (service) ->
service2V6 = service
new Disposable -> service2V6 = 'deactivated'
@@ -495,10 +495,10 @@ describe "PackageManager", ->
service2V5Spy = jasmine.createSpy('service2V5')
service2V6Spy = jasmine.createSpy('service2V6')
atom.services.provide "service-1", "0.3.1", service1V3Spy
atom.services.provide "service-1", "0.4.1", service1V4Spy
atom.services.provide "service-2", "0.5.1", service2V5Spy
atom.services.provide "service-2", "0.6.1", service2V5Spy # incompatible
atom.packages.serviceHub.provide "service-1", "0.3.1", service1V3Spy
atom.packages.serviceHub.provide "service-1", "0.4.1", service1V4Spy
atom.packages.serviceHub.provide "service-2", "0.5.1", service2V5Spy
atom.packages.serviceHub.provide "service-2", "0.6.1", service2V5Spy # incompatible
expect(service1V3Spy).toHaveBeenCalledWith('first-service-v3-used')
expect(service1V4Spy).toHaveBeenCalledWith('first-service-v4-used')
@@ -512,10 +512,10 @@ describe "PackageManager", ->
service2V5Spy.reset()
service2V6Spy.reset()
atom.services.provide "service-1", "0.3.1", service1V3Spy
atom.services.provide "service-1", "0.4.1", service1V4Spy
atom.services.provide "service-2", "0.5.1", service2V5Spy
atom.services.provide "service-2", "0.6.1", service2V5Spy # incompatible
atom.packages.serviceHub.provide "service-1", "0.3.1", service1V3Spy
atom.packages.serviceHub.provide "service-1", "0.4.1", service1V4Spy
atom.packages.serviceHub.provide "service-2", "0.5.1", service2V5Spy
atom.packages.serviceHub.provide "service-2", "0.6.1", service2V5Spy # incompatible
expect(service1V3Spy).not.toHaveBeenCalled()
expect(service1V4Spy).not.toHaveBeenCalled()

View File

@@ -17,7 +17,7 @@ Config = require '../src/config'
{Point} = require 'text-buffer'
Project = require '../src/project'
Workspace = require '../src/workspace'
ServiceHub = require '../src/service-hub'
ServiceHub = require 'service-hub'
TextEditor = require '../src/text-editor'
TextEditorView = require '../src/text-editor-view'
TextEditorElement = require '../src/text-editor-element'
@@ -78,7 +78,7 @@ beforeEach ->
projectPath = specProjectPath ? path.join(@specDirectory, 'fixtures')
atom.project = new Project(paths: [projectPath])
atom.workspace = new Workspace()
atom.services = new ServiceHub
atom.packages.serviceHub = new ServiceHub
atom.keymaps.keyBindings = _.clone(keyBindingsToRestore)
atom.commands.restoreSnapshot(commandsToRestore)
atom.styles.restoreSnapshot(styleElementsToRestore)

View File

@@ -133,9 +133,6 @@ class Atom extends Model
# Public: A {Clipboard} instance
clipboard: null
# A {ServiceHub} instance
services: null
# Public: A {ContextMenuManager} instance
contextMenu: null
@@ -235,7 +232,6 @@ class Atom extends Model
NotificationManager = require './notification-manager'
PackageManager = require './package-manager'
Clipboard = require './clipboard'
ServiceHub = require './service-hub'
GrammarRegistry = require './grammar-registry'
ThemeManager = require './theme-manager'
StyleManager = require './style-manager'
@@ -271,7 +267,6 @@ class Atom extends Model
@contextMenu = new ContextMenuManager({resourcePath, devMode})
@menu = new MenuManager({resourcePath})
@clipboard = new Clipboard()
@services = new ServiceHub
@grammars = @deserializers.deserialize(@state.grammars ? @state.syntax) ? new GrammarRegistry()

View File

@@ -7,6 +7,7 @@ fs = require 'fs-plus'
Q = require 'q'
Grim = require 'grim'
ServiceHub = require 'service-hub'
Package = require './package'
ThemePackage = require './theme-package'
@@ -40,6 +41,7 @@ class PackageManager
@loadedPackages = {}
@activePackages = {}
@packageStates = {}
@serviceHub = new ServiceHub
@packageActivators = []
@registerPackageActivator(this, ['atom', 'textmate'])

View File

@@ -213,11 +213,11 @@ class Package
activateServices: ->
for name, {versions} of @metadata.serviceProvisions
for version, methodName of versions
@activationDisposables.add atom.services.provide(name, version, @mainModule[methodName]())
@activationDisposables.add atom.packages.serviceHub.provide(name, version, @mainModule[methodName]())
for name, {versions} of @metadata.serviceDependencies
for version, methodName of versions
@activationDisposables.add atom.services.consume(name, version, @mainModule[methodName].bind(@mainModule))
@activationDisposables.add atom.packages.serviceHub.consume(name, version, @mainModule[methodName].bind(@mainModule))
loadKeymaps: ->
if @bundledPackage and packagesCache[@name]?

View File

@@ -1,93 +0,0 @@
_ServiceHub = require('service-hub')
# Experimental: This class facilitates communication between Atom packages
# through semantically-versioned services. If you want your package to provide
# an API for other packages to interact with, provide or consume a service via
# the global instance of this class available as `atom.services`.
#
# If you're providing an API for other packages, the most straightforward is to
# `provide` a module namespaced under your package's name as follows.
#
# ```coffee
# atom.services.provide "status-bar", "1.0.0",
# addRightItem: (item) -> # ...
# addLeftItem: (item) -> # ...
# ```
#
# Then other packages can interact with your package by consuming the provided
# service. Note that a service consumer can provide an npm-style version range
# string to express the required API version of the consumed service. The
# callback will be invoked with the service immediately or when the service
# becomes available. If multiple services match the provided key-path and
# version range, the callback will be invoked multiple times.
#
# ```coffee
# atom.services.consume "status-bar", "^1.0.0", (statusBar) ->
# statusBar.addLeftItem(new GrammarChanger)
# ```
#
# You can also provide multiple services end-points under the same namespace by
# passing a dot-separated key path. In this example, we also provide a global
# reference to the status bar's DOM element so other packages can modify it
# directly. Doing this via `atom.services` is superior to querying from the DOM
# manually because you can use semantic versioning to indicate when the DOM
# structure changes in a breaking way.
#
# ```coffee
# atom.services.provide "status-bar.view", "1.0.0", statusBarElement
# ```
#
# By convention, every package owns its package name in the services namespace.
# Your package can provide a service under another package's namespace, but you
# should always conform to that package's API. If you want to make additions to
# the API, add them under your own namespace.
#
# When upgrading your package's API, consider retaining previous versions with
# shims if at all possible to minimize breakage and to give the ecosystem time
# to catch up with your changes.
#
# You can also apply an inverted pattern, where your package consumes services
# under its own namespace. In this pattern, you would define a contract for
# services that other packages provide and your package consumes. For example,
# say we were adding the ability to add custom completion providers to
# autocomplete:
#
# ```coffee
# atom.services.consume "autocomplete", "1.0.0", (provider) ->
# addCompletionProvider(provider)
# ```
#
# In this use case, you would want to consume a specific version number rather
# than a range. You could consume multiple version numbers to provide backward
# compatibility.
module.exports =
class ServiceHub extends _ServiceHub
# Experimental: Provide a service by invoking the callback of all current and
# future consumers matching the given key path and version range.
#
# * `keyPath` A {String} of `.` separated keys indicating the services's
# location in the namespace of all services.
# * `version` A {String} containing a [semantic version](http://semver.org/)
# for the service's API.
# * `service` An object exposing the service API.
#
# Returns a {Disposable} on which `.dispose()` can be called to remove the
# provided service.
provide: (keyPath, version, service) ->
super
# Experimental: Consume a service by invoking the given callback for all
# current and future provided services matching the given key path and version
# range.
#
# * `keyPath` A {String} of `.` separated keys indicating the services's
# location in the namespace of all services.
# * `versionRange` A {String} containing a [semantic version range](https://www.npmjs.org/doc/misc/semver.html)
# that any provided services for the given key path must satisfy.
# * `callback` A {Function} to be called with current and future matching
# service objects.
#
# Returns a {Disposable} on which `.dispose()` can be called to remove the
# consumer.
consume: (keyPath, versionRange, callback) ->
super