Trouble setting up consumed service


#1

I’m developing a package to provide some functionality between a custom grammar and the new atom-ide-terminal, and I’m having some trouble implementing a consumed service from the atom-ide package.

I got some helpful advice from the developers of atom-ide, they told me that I can put this in the console

atom.packages.serviceHub.consume(‘nuclide-terminal’, ‘0.0.0’, x => window.terminal = x);
terminal.open({initialInput: “hi world”});

This works perfectly! In the console.

Okay, so I’m not completely a noob at writing code but I have only spent about 2 days learning JS and I’m not super familiar with the Atom API. So that’s about where I’m coming from here…

I have, in my package.json file,

  "consumedServices": {
    "nuclide-terminal": {
      "versions": {
        "^0.0.0": "consumeTerminal"
      }
    }
  }

which matches up with the provided service in the atom-ide-terminal module, and I’m trying to figure out exactly what I need in my main.js file to finish it up.

I obviously need to include the

x => window.terminal = x

somewhere, do I need to make a function definition like

consumeTerminal(x) => window.terminal = x

in my activate() section? The flight manual doesn’t seem to give much detail on this…


#2

It might help to look at an existing package to see how it works. E.g.,

and

You put the consumedServices declaration in the package.json, and the string value to the version number is the name of the function in your main file that will be called when that provider activates. In your main file, put that function in the module.exports object (not inside the activate() function).


#3

I appreciate the response. I’ve tried to look a bit at some other packages, but I’m still trying to puzzle how exactly the provideService/consumeService functions work.

Let me ask based on your example code here:

  1. busy-signal has a provided function called providerRegistry. This is logged in its package.json as busy-signal:1.0.0:providerRegistry
  2. this is defined in the index.js as a function that returns an object of type SignalRegister.
  3. hyperclick consumes a service from busy-signal, this function is logged in the package.json as busy-signal:1.0.0:consumeSignal.
  4. this is defined in the main.js as a function that takes an argument registry and then does a bunch of stuff with it (creates a registry and adds some things to it).

So I don’t entirely understand how these interact, but what I think is going on:

  1. When busy-signal launches it calls providerRegistry, which returns an object x that just hangs around until someone needs it.
  2. When hyperclick launches it calls consumeSignal, the package.json points out busy-signal as providing x to use as argument, so consumeSignal(x) is the call.
  3. the consumeSignal function sets up references to use the object, so other functions can utilize this.disposables.busyProvider to access the busy-signal features they need.

Okay I’ve got it working now, but I’m leaving all of this to act as a paper trail for anyone else trying to set up something similar.

  "consumedServices": {
    "nuclide-terminal": {
      "versions": {
        "0.0.0": "consumeTerminal"
      }
    }
  }

goes in my package.json, and I defined my consumeTerminal function to take nuclide-terminal and bind its export to a command terminal using

  consumeTerminal(x) {
    window.terminal = x;
  }

in the export block of my main.js.


#4

I’ve created a minimal(ish) working example of providing and consuming a service between two packages for beginners’ reference.