Using a service from another package


#1

I am trying to expose a series of services on one of my packages for consumption by a series of other packages. However I am struggling to get it to the point where I can call the services at any given time, rather than when initialized.

Basically there are two issues.

  1. The service is called immediately
  2. I am unsure how to call the service with parameters

Here is the JSON for the provider

"providedServices": {
    "getView": {
      "description": "Gets a view Object",
      "versions": {
        "0.0.1": "getViewObject001"
      }
    }
  }

Here is a mock implementation of the service.

getViewObject001: (param1, param2) ->
    return "magic"

Here is my consumer.

 "consumedServices": {
    "getView": {
      "versions": {
        "^0.0.1": "getViewObject"
      }
    }
  }

Here is how I am calling it initially.

  getViewObject: (getView) ->
    console.log("blah")

#2

The best example there is for consuming services is probably in the various status bar indicator packages such as the encoding-selector. Basically, the status bar service just hands a reference to the status bar object to the consumer. Then the consumer uses the status bar object to add its view or views to the status bar. It could do it right away (which for the status bar makes sense) or wait until a more appropriate time by holding on to the reference.

You may want to try that pattern for what you’re trying to do?


Cross Package Events
#3

took a little digging through both packages but that got me close. However I am having an issue.

getViewObject: this.getViewObject.bind(@connectorView) when I tried to use the @connectorView.getViewObject it gave me a cannot read property bind of undefined. I think this is because @connectorView is not being initialized. Thoughts?


#4

Perhaps the issue is that you’re trying to manually call your service stuff? The way services are designed, you declare the provider function and you declare the consumer function and the framework does the calling, much like the activate and deactivate functions for a package.

If you look at the status-bar package’s provideStatusBar function, it returns an object:

that is then handed, by the framework, to the consumeStatusBar function of the encoding-selector package:

You can see the mechanics of the system in the README of the service-hub package. The code that actually performs all of this stuff that you see in the README though is in the PackageManager class, I believe.