Skip to content

Instantly share code, notes, and snippets.

@jbrains
Created February 22, 2015 16:16
Show Gist options
  • Save jbrains/c2c7f2a3b1f8240013fc to your computer and use it in GitHub Desktop.
Save jbrains/c2c7f2a3b1f8240013fc to your computer and use it in GitHub Desktop.
How well do I understand how Atom activates/deactivates a package with/in spite of having an activation command configured?
{
"name": "status-stats",
"main": "./lib/status-stats",
"activationCommands": ["status-stats:toggle"],
"version": "0.1.0",
"private": false,
"description": "A derivative of @segphault's status-wordcount. Displays wordcount, grade level, and Flesh Kincaid scores, to help you write for your audience.",
"repository": "https://github.com/jbrains/status-stats",
"license": "MIT",
"engines": {
"atom": ">0.182.0"
},
"dependencies": {
"atom-space-pen-views": "^2.0.3",
"text-statistics": "0.1.1"
}
}
# SMELL relative path in require; how to configure LOAD PATH for libraries?
entryPoint = require '../lib/status-stats'
describe "integrating this plugin with Atom", ->
[workspaceElement] = []
beforeEach ->
workspaceElement = atom.views.getView(atom.workspace)
# We need this because as of 2015-02-22, PackageManager.activatePackage(packageName)
# does not always activate the named package. For example, when we configure
# an activation command, then .activatePackage() will wait for that command
# before activating the package.
# See https://discuss.atom.io/t/can-you-force-the-activation-of-another-package/10885/18
forceAtomToActivatePackage = (name) ->
pack = atom.packages.loadPackage(name)
expect(pack).toBeDefined() # otherwise the package didn't load correctly
pack.activationDeferred ?= resolve:(->), reject:(->)
pack.activateNow()
describe "activating the package", ->
it "does not handle commands when it has not yet been activated", ->
spyOn(entryPoint, "toggle")
# ASSUME that the package is not activated by default
expect(atom.packages.isPackageActive("status-stats")).toBe(false)
atom.commands.dispatch(workspaceElement, "status-stats:toggle")
expect(entryPoint.toggle).not.toHaveBeenCalled()
it "handles commands once activated", ->
spyOn(entryPoint, "toggle")
# activate directly, rather than simulate the activation command
forceAtomToActivatePackage("status-stats")
atom.commands.dispatch(workspaceElement, "status-stats:toggle")
expect(entryPoint.toggle).toHaveBeenCalled()
# I'm not actually certain that this spec's expectation is correct.
# Atom appears to behave this way, but I might have written the spec
# incorrectly.
it "Atom disables commands when the package is deactivated, even if the package doesn't disable them", ->
spyOn(entryPoint, "toggle")
# activate directly, rather than simulate the activation command
forceAtomToActivatePackage("status-stats")
atom.packages.deactivatePackage("status-stats")
atom.commands.dispatch(workspaceElement, "status-stats:toggle")
expect(entryPoint.toggle).not.toHaveBeenCalled()
it "does not activate until someone tries to toggle the status", ->
# ASSUME that the package is not activated by default
expect(atom.packages.isPackageActive("status-stats")).toBe(false)
# I've configured the corresponding activation command in package.json
atom.commands.dispatch(workspaceElement, "status-stats:toggle")
expect(atom.packages.isPackageActive("status-stats")).toBe(true)
module.exports =
activate: (state) ->
# The command name needs to match status-stats.cson
atom.commands.add("atom-workspace", "status-stats:toggle", => @toggle())
console.log("activating plugin with state #{state}")
deactivate: ->
console.log("deactivating plugin")
toggle: ->
console.log("toggle")
@dfkaye
Copy link

dfkaye commented Feb 23, 2015

Just offering this w/o real experience with it ~ from the atom package manager spec it appears that atom.packages.activatePackage and atom.packages.deactivatePackage are asynchronous and should wrapped in promises ~ see for example, https://github.com/atom/atom/blob/master/spec/package-manager-spec.coffee#L496 and forward...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment