Skip to content

Busy Signal API

Lukas Beranek edited this page Jan 2, 2019 · 3 revisions

The Busy Signal API

The busy-signal package provides 2 ways that allows you to tell the user you are doing something in your package

Provider

// The class exposed via the API
class Registry {
  create(): Provider
}

class Provider {
  add(message: string)
  remove(message: string)
  clear()
  dispose()
}

Defining a provider

You'll need to add this in your package.json

"consumedServices": {
  "busy-signal": {
    "versions": {
      "1.0.0": "consumeSignal"
    }
  }
},

Your package's consumeSignal function will be called with Registry. Here's an example package using the signal API to show/hide the signal every 5 seconds

'use babel'

import { CompositeDisposable } from 'atom'

module.exports = {
  activate() {
    this.subscriptions = new CompositeDisposable()
    this.interval = null
  },
  consumeSignal(registry) {
    const provider = registry.create()
    this.subscriptions.add(provider)

    let isShown = false
    this.interval = setInterval(function() {
      if (isShown) {
        provider.clear()
      } else {
        provider.add('Building project')
        provider.add('Linter(s) executing on: main.js')
      }
      isShown = !isShown
    }, 5000)
  },
  deactivate() {
    this.subscriptions.dispose()
    clearInterval(this.interval)
  }
}

The above package produces this

Screenshot

Atom IDE

class API {
  reportBusy(title: string): BusyMessage
  reportBusyWhile(title: string, f: () => Promise<T>): Promise<T>
}

class BusyMessage {
  setTitle(title: string)
  dispose()
}

You'll need to add this in your package.json

"consumedServices": {
  "atom-ide-busy-signal": {
    "versions": {
      "0.1.0": "consumeBusySignal"
    }
  }
},

Your package's consumeBusySignal function will be called with API. Here are two examples that demonstrate both reportBusy and reportBusyWhile.

'use babel'

import { CompositeDisposable } from 'atom'

module.exports = {
  // activate() {}, deactivate() {}, ...
  consumeBusySignal(api) {
    const response = await api.reportBusyWhile('Downloading data ...', () => fetch('https://...'))
    // deal with fetch response, e.g. response.json()

    // imagine saving a file that runs through formatting, writing to disk and linting
    const message = api.reportBusy('Formatting ...')
    await format()
    message.setTitle('Writing to disk ...')
    await writeToDisk()
    message.setTitle('Linting ...')
    await lint()
    message.setTitle('Saved!')
    message.dispose()
  }
}
Clone this wiki locally