W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
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)
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: