Atom 通過服務(wù)和其它包交互

2018-08-12 21:50 更新

通過服務(wù)和其它包交互

Atom包可以通過叫做服務(wù)的帶有版本控制的APi,和其它包進行交互。在你的package.json文件中指定一個或者多個版本號來提供服務(wù),每個版本號都要帶有一個包的主模塊中的方法。

{
  "providedServices": {
    "my-service": {
      "description": "Does a useful thing",
      "versions": {
        "1.2.3": "provideMyServiceV1",
        "2.3.4": "provideMyServiceV2",
      }
    }
  }
}

在你的包的主模塊中實現(xiàn)上面的方法。這些方法會在一個包被激活的任何時候調(diào)用,它們會使用它們的通信服務(wù)。它們應(yīng)該返回實現(xiàn)了服務(wù)API的一個值。

module.exports =
  activate: -> # ...

  provideMyServiceV1: ->
    adaptToLegacyAPI(myService)

  provideMyServiceV2: ->
    myService

與之相似,指定一個或多個版本范圍來使用一個服務(wù),每個都帶有一個包的主模塊中的方法。

{
  "consumedServices": {
    "another-service": {
      "versions": {
        "^1.2.3": "consumeAnotherServiceV1",
        ">=2.3.4 <2.5": "consumeAnotherServiceV2",
      }
    }
  }
}

這些方法會在一個包被激活的任何時候調(diào)用,它們會提供它們的通信服務(wù)。它們會接受到一個通信對象作為一個參數(shù)。你通常需要在包提供的服務(wù)失效的時間中,進行同種類型的清除工作。從你使用服務(wù)的方法中返回一個Disposable來完成它:

{Disposable} = require 'atom'

module.exports =
  activate: -> # ...

  consumeAnotherServiceV1: (service) ->
    useService(adaptServiceFromLegacyAPI(service))
    new Disposable -> stopUsingService(service)

  consumeAnotherServiceV2: (service) ->
    useService(service)
    new Disposable -> stopUsingService(service)
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號