From ab96e5b5fdfd57f942d6e9ff5b21368b30068257 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 9 Sep 2014 13:58:01 -0700 Subject: [PATCH 01/21] :lipstick: Fix doc string --- src/decoration.coffee | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/decoration.coffee b/src/decoration.coffee index cd5035e5f..7216aee18 100644 --- a/src/decoration.coffee +++ b/src/decoration.coffee @@ -61,13 +61,16 @@ class Decoration # Essential: When the {Decoration} is updated via {Decoration::update}. # - # * `event` {Object} - # * `oldProperties` {Object} the old parameters the decoration used to have - # * `newProperties` {Object} the new parameters the decoration now has + # * `callback` {Function} + # * `event` {Object} + # * `oldProperties` {Object} the old parameters the decoration used to have + # * `newProperties` {Object} the new parameters the decoration now has onDidChangeProperties: (callback) -> @emitter.on 'did-change-properties', callback # Essential: Invoke the given callback when the {Decoration} is destroyed + # + # * `callback` {Function} onDidDestroy: (callback) -> @emitter.on 'did-destroy', callback From ed4acb02d8d07eb12b36a7e4d4d5be9db322112b Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 9 Sep 2014 13:58:23 -0700 Subject: [PATCH 02/21] Add Project::onDidChangePath() --- src/project.coffee | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/project.coffee b/src/project.coffee index a34a442a5..4cf76cf94 100644 --- a/src/project.coffee +++ b/src/project.coffee @@ -6,7 +6,9 @@ fs = require 'fs-plus' Q = require 'q' {deprecate} = require 'grim' {Model} = require 'theorist' -{Emitter, Subscriber} = require 'emissary' +{Subscriber} = require 'emissary' +EmitterMixin = require('emissary').Emitter +{Emitter} = require 'event-kit' Serializable = require 'serializable' TextBuffer = require 'text-buffer' {Directory} = require 'pathwatcher' @@ -46,6 +48,7 @@ class Project extends Model constructor: ({path, @buffers}={}) -> @buffers ?= [] + @emitter = new Emitter for buffer in @buffers do (buffer) => @@ -53,6 +56,25 @@ class Project extends Model @setPath(path) + ### + Section: Events + ### + + # Essential: Invoke the given callback when the project's path has changed. + # + # * `callback` {Function} + # * `path` the new path + onDidChangePath: (callback) -> + @emitter.on 'did-change-path', callback + + on: (eventName) -> + switch eventName + when 'path-changed' + deprecate 'Use Project::onDidChangePath instead' + else + deprecate 'Project::on is deprecated. Use event subscription methods instead.' + EmitterMixin::on.apply(this, arguments) + serializeParams: -> path: @path buffers: _.compact(@buffers.map (buffer) -> buffer.serialize() if buffer.isRetained()) @@ -98,7 +120,8 @@ class Project extends Model else @rootDirectory = null - @emit "path-changed" + @emit 'path-changed' + @emitter.emit 'did-change-path', @getPath() # Public: Returns the root {Directory} object for this project. getRootDirectory: -> From 4070e5fb25a1c001ee781c28a932c105a5ab1598 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 9 Sep 2014 14:12:49 -0700 Subject: [PATCH 03/21] Add Project::onDidCreateBuffer --- spec/project-spec.coffee | 2 +- src/project.coffee | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/spec/project-spec.coffee b/spec/project-spec.coffee index 06e3ee29d..b1fed60ee 100644 --- a/spec/project-spec.coffee +++ b/spec/project-spec.coffee @@ -58,7 +58,7 @@ describe "Project", -> beforeEach -> absolutePath = require.resolve('./fixtures/dir/a') newBufferHandler = jasmine.createSpy('newBufferHandler') - atom.project.on 'buffer-created', newBufferHandler + atom.project.onDidCreateBuffer newBufferHandler describe "when given an absolute path that isn't currently open", -> it "returns a new edit session for the given path and emits 'buffer-created'", -> diff --git a/src/project.coffee b/src/project.coffee index 4cf76cf94..fb6cc6cfe 100644 --- a/src/project.coffee +++ b/src/project.coffee @@ -67,10 +67,21 @@ class Project extends Model onDidChangePath: (callback) -> @emitter.on 'did-change-path', callback + + # Extended: Invoke the given callback when a new buffer has been created. For + # example, when {::open} is called, this is fired. + # + # * `callback` {Function} + # * `buffer` {TextBuffer} the new buffer + onDidCreateBuffer: (callback) -> + @emitter.on 'did-create-buffer', callback + on: (eventName) -> switch eventName when 'path-changed' deprecate 'Use Project::onDidChangePath instead' + when 'buffer-created' + deprecate 'Use Project::onDidCreateBuffer instead' else deprecate 'Project::on is deprecated. Use event subscription methods instead.' EmitterMixin::on.apply(this, arguments) @@ -245,6 +256,7 @@ class Project extends Model @buffers.splice(index, 0, buffer) buffer.onDidDestroy => @removeBuffer(buffer) @emit 'buffer-created', buffer + @emitter.emit 'did-create-buffer', buffer buffer # Removes a {TextBuffer} association from the project. From b6fe72ef9e416723f66c775049381c8b28c927f3 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 9 Sep 2014 14:13:26 -0700 Subject: [PATCH 04/21] Add Project::observeBuffers --- spec/project-spec.coffee | 6 +++--- src/git.coffee | 2 +- src/project.coffee | 20 +++++++++++++++----- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/spec/project-spec.coffee b/spec/project-spec.coffee index b1fed60ee..7c705b8a4 100644 --- a/spec/project-spec.coffee +++ b/spec/project-spec.coffee @@ -479,7 +479,7 @@ describe "Project", -> runs -> expect(results).toHaveLength 0 - describe ".eachBuffer(callback)", -> + describe ".observeBuffers(callback)", -> beforeEach -> atom.project.bufferForPathSync('a') @@ -490,7 +490,7 @@ describe "Project", -> callback = (buffer) -> callbackBuffer = buffer count++ - atom.project.eachBuffer(callback) + atom.project.observeBuffers(callback) expect(count).toBe 1 expect(callbackBuffer).toBe atom.project.getBuffers()[0] @@ -501,7 +501,7 @@ describe "Project", -> callbackBuffer = buffer count++ - atom.project.eachBuffer(callback) + atom.project.observeBuffers(callback) count = 0 callbackBuffer = null atom.project.bufferForPathSync(require.resolve('./fixtures/sample.txt')) diff --git a/src/git.coffee b/src/git.coffee index 0e5273aa9..d3c811440 100644 --- a/src/git.coffee +++ b/src/git.coffee @@ -86,7 +86,7 @@ class Git @refreshStatus() if @project? - @subscribe @project.eachBuffer (buffer) => @subscribeToBuffer(buffer) + @subscribe @project.observeBuffers (buffer) => @subscribeToBuffer(buffer) # Subscribes to buffer events. subscribeToBuffer: (buffer) -> diff --git a/src/project.coffee b/src/project.coffee index fb6cc6cfe..f21e65c5e 100644 --- a/src/project.coffee +++ b/src/project.coffee @@ -67,6 +67,15 @@ class Project extends Model onDidChangePath: (callback) -> @emitter.on 'did-change-path', callback + # Extended: Immediately invoke the given callback for all existing buffers. + # Then invoke the given callback when a new buffer has been created. For + # example, when {::open} is called, this is fired. + # + # * `callback` {Function} + # * `buffer` {TextBuffer} the new buffer + observeBuffers: (callback) -> + callback(buffer) for buffer in @getBuffers() + @onDidCreateBuffer(callback) # Extended: Invoke the given callback when a new buffer has been created. For # example, when {::open} is called, this is fired. @@ -363,11 +372,12 @@ class Project extends Model subscriber = args.shift() if args.length > 1 callback = args.shift() - callback(buffer) for buffer in @getBuffers() - if subscriber - subscriber.subscribe this, 'buffer-created', (buffer) -> callback(buffer) - else - @on 'buffer-created', (buffer) -> callback(buffer) + message = 'Use Project::observeBuffers instead' + message += '; Project::observeBuffers no longer accepts a subscriber parameter. Call `subscriber.subscribe(atom.project.observeBuffers(cb))`' if subscriber? + + disposable = observeBuffers(callback) + subscriber.subscribe(disposable) if subscriber? + disposable # Deprecated: delegate registerOpener: (opener) -> From ffbb18a0f8750064271bc2494089f52b3cd4371b Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 9 Sep 2014 14:16:10 -0700 Subject: [PATCH 05/21] Remove event comments, add method section comment --- src/project.coffee | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/project.coffee b/src/project.coffee index f21e65c5e..ddf09ab6e 100644 --- a/src/project.coffee +++ b/src/project.coffee @@ -20,19 +20,6 @@ Git = require './git' # Extended: Represents a project that's opened in Atom. # # An instance of this class is always available as the `atom.project` global. -# -# ## Events -# -# ### path-changed -# -# Extended: Emit when the project's path has changed. Use {::getPath} to get the new path -# -# ### buffer-created -# -# Extended: Emit when a buffer is created. For example, when {::open} is called, this is fired. -# -# * `buffer` {TextBuffer} the new buffer that was created. -# module.exports = class Project extends Model atom.deserializers.add(this) @@ -95,6 +82,10 @@ class Project extends Model deprecate 'Project::on is deprecated. Use event subscription methods instead.' EmitterMixin::on.apply(this, arguments) + ### + Section: Methods + ### + serializeParams: -> path: @path buffers: _.compact(@buffers.map (buffer) -> buffer.serialize() if buffer.isRetained()) From 05f54d427d4d8ed2e1dac2bec7ead8d7adcdc9ba Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 9 Sep 2014 14:28:30 -0700 Subject: [PATCH 06/21] Add Git::onDidChangeStatus --- spec/git-spec.coffee | 24 ++++++++++++------------ src/git.coffee | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/spec/git-spec.coffee b/spec/git-spec.coffee index 2de1d2e16..4e3bb7de3 100644 --- a/spec/git-spec.coffee +++ b/spec/git-spec.coffee @@ -111,10 +111,10 @@ describe "Git", -> fs.writeFileSync(filePath, 'ch ch changes') repo.getPathStatus(filePath) statusHandler = jasmine.createSpy('statusHandler') - repo.on 'status-changed', statusHandler + repo.onDidChangeStatus statusHandler repo.checkoutHead(filePath) expect(statusHandler.callCount).toBe 1 - expect(statusHandler.argsForCall[0][0..1]).toEqual [filePath, 0] + expect(statusHandler.argsForCall[0][0]).toEqual {path: filePath, pathStatus: 0} repo.checkoutHead(filePath) expect(statusHandler.callCount).toBe 1 @@ -167,11 +167,11 @@ describe "Git", -> it "trigger a status-changed event when the new status differs from the last cached one", -> statusHandler = jasmine.createSpy("statusHandler") - repo.on 'status-changed', statusHandler + repo.onDidChangeStatus statusHandler fs.writeFileSync(filePath, '') status = repo.getPathStatus(filePath) expect(statusHandler.callCount).toBe 1 - expect(statusHandler.argsForCall[0][0..1]).toEqual [filePath, status] + expect(statusHandler.argsForCall[0][0]).toEqual {path: filePath, pathStatus: status} fs.writeFileSync(filePath, 'abc') status = repo.getPathStatus(filePath) @@ -232,19 +232,19 @@ describe "Git", -> editor.insertNewline() statusHandler = jasmine.createSpy('statusHandler') - atom.project.getRepo().on 'status-changed', statusHandler + atom.project.getRepo().onDidChangeStatus statusHandler editor.save() expect(statusHandler.callCount).toBe 1 - expect(statusHandler).toHaveBeenCalledWith editor.getPath(), 256 + expect(statusHandler).toHaveBeenCalledWith {path: editor.getPath(), pathStatus: 256} it "emits a status-changed event when a buffer is reloaded", -> fs.writeFileSync(editor.getPath(), 'changed') statusHandler = jasmine.createSpy('statusHandler') - atom.project.getRepo().on 'status-changed', statusHandler + atom.project.getRepo().onDidChangeStatus statusHandler editor.getBuffer().reload() expect(statusHandler.callCount).toBe 1 - expect(statusHandler).toHaveBeenCalledWith editor.getPath(), 256 + expect(statusHandler).toHaveBeenCalledWith {path: editor.getPath(), pathStatus: 256} editor.getBuffer().reload() expect(statusHandler.callCount).toBe 1 @@ -252,10 +252,10 @@ describe "Git", -> fs.writeFileSync(editor.getPath(), 'changed') statusHandler = jasmine.createSpy('statusHandler') - atom.project.getRepo().on 'status-changed', statusHandler + atom.project.getRepo().onDidChangeStatus statusHandler editor.getBuffer().emitter.emit 'did-change-path' expect(statusHandler.callCount).toBe 1 - expect(statusHandler).toHaveBeenCalledWith editor.getPath(), 256 + expect(statusHandler).toHaveBeenCalledWith {path: editor.getPath(), pathStatus: 256} editor.getBuffer().emitter.emit 'did-change-path' expect(statusHandler.callCount).toBe 1 @@ -283,7 +283,7 @@ describe "Git", -> buffer.append('changes') statusHandler = jasmine.createSpy('statusHandler') - project2.getRepo().on 'status-changed', statusHandler + project2.getRepo().onDidChangeStatus statusHandler buffer.save() expect(statusHandler.callCount).toBe 1 - expect(statusHandler).toHaveBeenCalledWith buffer.getPath(), 256 + expect(statusHandler).toHaveBeenCalledWith {path: buffer.getPath(), pathStatus: 256} diff --git a/src/git.coffee b/src/git.coffee index d3c811440..bfabcf829 100644 --- a/src/git.coffee +++ b/src/git.coffee @@ -1,9 +1,12 @@ {basename, join} = require 'path' _ = require 'underscore-plus' -{Emitter, Subscriber} = require 'emissary' +{Subscriber} = require 'emissary' +EmitterMixin = require('emissary').Emitter +{Emitter} = require 'event-kit' fs = require 'fs-plus' GitUtils = require 'git-utils' +{deprecate} = require 'grim' Task = require './task' @@ -41,7 +44,7 @@ Task = require './task' # ``` module.exports = class Git - Emitter.includeInto(this) + EmitterMixin.includeInto(this) Subscriber.includeInto(this) # Public: Creates a new Git instance. @@ -67,6 +70,7 @@ class Git false constructor: (path, options={}) -> + @emitter = new Emitter @repo = GitUtils.open(path) unless @repo? throw new Error("No Git repository found searching path: #{path}") @@ -88,6 +92,32 @@ class Git if @project? @subscribe @project.observeBuffers (buffer) => @subscribeToBuffer(buffer) + ### + Section: Events + ### + + # Essential: When the {Decoration} is updated via {Decoration::update}. + # + # * `callback` {Function} + # * `event` {Object} + # * `path` {String} the old parameters the decoration used to have + # * `pathStatus` {Number} representing the status. This value can be passed to + # {::isStatusModified} or {::isStatusNew} to get more information. + onDidChangeStatus: (callback) -> + @emitter.on 'did-change-status', callback + + on: (eventName) -> + switch eventName + when 'status-changed' + deprecate 'Use Git::onDidChangeStatus instead' + else + deprecate 'Git::on is deprecated. Use event subscription methods instead.' + EmitterMixin::on.apply(this, arguments) + + ### + Section: Methods + ### + # Subscribes to buffer events. subscribeToBuffer: (buffer) -> getBufferPathStatus = => @@ -171,6 +201,8 @@ class Git delete @statuses[relativePath] if currentPathStatus isnt pathStatus @emit 'status-changed', path, pathStatus + @emitter.emit 'did-change-status', {path, pathStatus} + pathStatus # Public: Is the given path ignored? From dddd17c11b02d8e4214a9684cfb3e6a968317085 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 9 Sep 2014 14:29:14 -0700 Subject: [PATCH 07/21] Fix observeBuffers --- src/project.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/project.coffee b/src/project.coffee index ddf09ab6e..1fe38577f 100644 --- a/src/project.coffee +++ b/src/project.coffee @@ -366,7 +366,7 @@ class Project extends Model message = 'Use Project::observeBuffers instead' message += '; Project::observeBuffers no longer accepts a subscriber parameter. Call `subscriber.subscribe(atom.project.observeBuffers(cb))`' if subscriber? - disposable = observeBuffers(callback) + disposable = @observeBuffers(callback) subscriber.subscribe(disposable) if subscriber? disposable From ae4f92cc5253ea3b79ea9193fcd51048e7aa3779 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 9 Sep 2014 14:45:31 -0700 Subject: [PATCH 08/21] Add onDidChangeStatuses --- spec/git-spec.coffee | 2 +- src/git.coffee | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/spec/git-spec.coffee b/spec/git-spec.coffee index 4e3bb7de3..2cc0a6239 100644 --- a/spec/git-spec.coffee +++ b/spec/git-spec.coffee @@ -208,7 +208,7 @@ describe "Git", -> it "returns status information for all new and modified files", -> fs.writeFileSync(modifiedPath, 'making this path modified') statusHandler = jasmine.createSpy('statusHandler') - repo.on 'statuses-changed', statusHandler + repo.onDidChangeStatuses statusHandler repo.refreshStatus() waitsFor -> diff --git a/src/git.coffee b/src/git.coffee index bfabcf829..34beb474e 100644 --- a/src/git.coffee +++ b/src/git.coffee @@ -106,10 +106,21 @@ class Git onDidChangeStatus: (callback) -> @emitter.on 'did-change-status', callback + # Essential: Invoke the given callback when a multiple files' statuses have + # changed. For example, on window focus, the status of all the paths in the + # repo is checked. If any of them have changed, this will be fired. Call + # {::getPathStatus(path)} to get the status for your path of choice. + # + # * `callback` {Function} + onDidChangeStatuses: (callback) -> + @emitter.on 'did-change-statuses', callback + on: (eventName) -> switch eventName when 'status-changed' deprecate 'Use Git::onDidChangeStatus instead' + when 'statuses-changed' + deprecate 'Use Git::onDidChangeStatuses instead' else deprecate 'Git::on is deprecated. Use event subscription methods instead.' EmitterMixin::on.apply(this, arguments) @@ -423,4 +434,6 @@ class Git for submodulePath, submoduleRepo of @getRepo().submodules submoduleRepo.upstream = submodules[submodulePath]?.upstream ? {ahead: 0, behind: 0} - @emit 'statuses-changed' unless statusesUnchanged + unless statusesUnchanged + @emit 'statuses-changed' + @emitter.emit 'did-change-statuses' From af1b0b5736a8884581763b3a5028dc63779e7c6d Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 9 Sep 2014 14:45:41 -0700 Subject: [PATCH 09/21] Fix Doc String --- src/git.coffee | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/git.coffee b/src/git.coffee index 34beb474e..7f9d28007 100644 --- a/src/git.coffee +++ b/src/git.coffee @@ -96,7 +96,9 @@ class Git Section: Events ### - # Essential: When the {Decoration} is updated via {Decoration::update}. + # Essential: Invoke the given callback when a specific file's status has + # changed. When a file is updated, reloaded, etc, and the status changes, this + # will be fired. # # * `callback` {Function} # * `event` {Object} From 229277f764c1e9c35206eaf5d2cc63ea98e60006 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 9 Sep 2014 15:03:45 -0700 Subject: [PATCH 10/21] Add PackageManager::onDidActivateAll() --- src/atom.coffee | 2 +- src/menu-manager.coffee | 2 +- src/package-manager.coffee | 30 +++++++++++++++++++++++++++--- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/atom.coffee b/src/atom.coffee index 4fb113302..697a92c4b 100644 --- a/src/atom.coffee +++ b/src/atom.coffee @@ -187,7 +187,7 @@ class Atom extends Model @syntax = @deserializers.deserialize(@state.syntax) ? new Syntax() - @subscribe @packages, 'activated', => @watchThemes() + @subscribe @packages.onDidActivateAll => @watchThemes() Project = require './project' TextBuffer = require 'text-buffer' diff --git a/src/menu-manager.coffee b/src/menu-manager.coffee index b9f64a2fc..5c7d955d0 100644 --- a/src/menu-manager.coffee +++ b/src/menu-manager.coffee @@ -15,7 +15,7 @@ class MenuManager @pendingUpdateOperation = null @template = [] atom.keymaps.on 'bundled-keymaps-loaded', => @loadPlatformItems() - atom.packages.on 'activated', => @sortPackagesMenu() + atom.packages.onDidActivateAll => @sortPackagesMenu() # Public: Adds the given items to the application menu. # diff --git a/src/package-manager.coffee b/src/package-manager.coffee index 14da123a7..98416806f 100644 --- a/src/package-manager.coffee +++ b/src/package-manager.coffee @@ -1,14 +1,16 @@ path = require 'path' _ = require 'underscore-plus' -{Emitter} = require 'emissary' +EmitterMixin = require('emissary').Emitter +{Emitter} = require 'event-kit' fs = require 'fs-plus' Q = require 'q' +{deprecate} = require 'grim' Package = require './package' ThemePackage = require './theme-package' -# Public: Package manager for coordinating the lifecycle of Atom packages. +# Extended: Package manager for coordinating the lifecycle of Atom packages. # # An instance of this class is always available as the `atom.packages` global. # @@ -25,9 +27,10 @@ ThemePackage = require './theme-package' # settings and also by calling `enablePackage()/disablePackage()`. module.exports = class PackageManager - Emitter.includeInto(this) + EmitterMixin.includeInto(this) constructor: ({configDirPath, devMode, safeMode, @resourcePath}) -> + @emitter = new Emitter @packageDirPaths = [] unless safeMode if devMode @@ -41,6 +44,26 @@ class PackageManager @packageActivators = [] @registerPackageActivator(this, ['atom', 'textmate']) + # Essential: Invoke the given callback when all packages have been activated. + # + # * `callback` {Function} + onDidActivateAll: (callback) -> + @emitter.on 'did-activate-all', callback + + on: (eventName) -> + switch eventName + when 'loaded' + deprecate 'Use PackageManager::onDidLoadAll instead' + when 'activated' + deprecate 'Use PackageManager::onDidActivateAll instead' + else + deprecate 'PackageManager::on is deprecated. Use event subscription methods instead.' + EmitterMixin::on.apply(this, arguments) + + ### + Section: Methods + ### + # Extended: Get the path to the apm command. # # Return a {String} file path to apm. @@ -83,6 +106,7 @@ class PackageManager packages = @getLoadedPackagesForTypes(types) activator.activatePackages(packages) @emit 'activated' + @emitter.emit 'did-activate-all' # another type of package manager can handle other package types. # See ThemeManager From 55b5debd7f1042353cfa8d091b915b88efeac860 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 9 Sep 2014 15:04:13 -0700 Subject: [PATCH 11/21] Add PackageManager::onDidLoadAll --- src/package-manager.coffee | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/package-manager.coffee b/src/package-manager.coffee index 98416806f..7fd2bcd16 100644 --- a/src/package-manager.coffee +++ b/src/package-manager.coffee @@ -44,6 +44,16 @@ class PackageManager @packageActivators = [] @registerPackageActivator(this, ['atom', 'textmate']) + ### + Section: Events + ### + + # Essential: Invoke the given callback when all packages have been activated. + # + # * `callback` {Function} + onDidLoadAll: (callback) -> + @emitter.on 'did-load-all', callback + # Essential: Invoke the given callback when all packages have been activated. # # * `callback` {Function} @@ -183,6 +193,7 @@ class PackageManager packagePaths = _.uniq packagePaths, (packagePath) -> path.basename(packagePath) @loadPackage(packagePath) for packagePath in packagePaths @emit 'loaded' + @emitter.emit 'did-load-all' loadPackage: (nameOrPath) -> return pack if pack = @getLoadedPackage(nameOrPath) From 0afd8a139231e69df83fc182f6ac7fa06e146bff Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 9 Sep 2014 15:09:18 -0700 Subject: [PATCH 12/21] Add Package::onDidDeactivate --- spec/package-spec.coffee | 2 +- src/package.coffee | 32 +++++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/spec/package-spec.coffee b/spec/package-spec.coffee index bbb00ca81..b94f86a0f 100644 --- a/spec/package-spec.coffee +++ b/spec/package-spec.coffee @@ -102,6 +102,6 @@ describe "Package", -> theme.activate() it "deactivated event fires on .deactivate()", -> - theme.on 'deactivated', spy = jasmine.createSpy() + theme.onDidDeactivate spy = jasmine.createSpy() theme.deactivate() expect(spy).toHaveBeenCalled() diff --git a/src/package.coffee b/src/package.coffee index 707e7b591..7a8133466 100644 --- a/src/package.coffee +++ b/src/package.coffee @@ -4,8 +4,10 @@ _ = require 'underscore-plus' async = require 'async' CSON = require 'season' fs = require 'fs-plus' -{Emitter} = require 'emissary' +EmitterMixin = require('emissary').Emitter +{Emitter} = require 'event-kit' Q = require 'q' +{deprecate} = require 'grim' $ = null # Defer require in case this is in the window-less browser process ScopedProperties = require './scoped-properties' @@ -14,7 +16,7 @@ ScopedProperties = require './scoped-properties' # stylesheets, keymaps, grammar, editor properties, and menus. module.exports = class Package - Emitter.includeInto(this) + EmitterMixin.includeInto(this) @stylesheetsDir: 'stylesheets' @@ -38,10 +40,33 @@ class Package mainModule: null constructor: (@path, @metadata) -> + @emitter = new Emitter @metadata ?= Package.loadMetadata(@path) @name = @metadata?.name ? path.basename(@path) @reset() + ### + Section: Events + ### + + # Essential: Invoke the given callback when all packages have been activated. + # + # * `callback` {Function} + onDidDeactivate: (callback) -> + @emitter.on 'did-deactivate', callback + + on: (eventName) -> + switch eventName + when 'deactivated' + deprecate 'Use Package::onDidDeactivate instead' + else + deprecate 'Package::on is deprecated. Use event subscription methods instead.' + EmitterMixin::on.apply(this, arguments) + + ### + Section: Methods + ### + enable: -> atom.config.removeAtKeyPath('core.disabledPackages', @name) @@ -243,7 +268,8 @@ class Package @deactivateResources() @deactivateConfig() @mainModule?.deactivate?() if @mainActivated - @emit('deactivated') + @emit 'deactivated' + @emitter.emit 'did-deactivate' deactivateConfig: -> @mainModule?.deactivateConfig?() From 3bf348e51feadaf194889a83c82ab3a1a37cbddb Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 9 Sep 2014 15:24:55 -0700 Subject: [PATCH 13/21] Add ThemeManager::onDidReloadAll --- spec/theme-manager-spec.coffee | 2 +- src/theme-manager.coffee | 30 ++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/spec/theme-manager-spec.coffee b/spec/theme-manager-spec.coffee index d3a30c589..ebb8e1c6e 100644 --- a/spec/theme-manager-spec.coffee +++ b/spec/theme-manager-spec.coffee @@ -69,7 +69,7 @@ describe "ThemeManager", -> describe "when the core.themes config value changes", -> it "add/removes stylesheets to reflect the new config value", -> - themeManager.on 'reloaded', reloadHandler = jasmine.createSpy() + themeManager.onDidReloadAll reloadHandler = jasmine.createSpy() spyOn(themeManager, 'getUserStylesheetPath').andCallFake -> null waitsForPromise -> diff --git a/src/theme-manager.coffee b/src/theme-manager.coffee index 6ed3e764a..e3da5377b 100644 --- a/src/theme-manager.coffee +++ b/src/theme-manager.coffee @@ -1,10 +1,12 @@ path = require 'path' _ = require 'underscore-plus' -{Emitter} = require 'emissary' +EmitterMixin = require('emissary').Emitter +{Emitter} = require 'event-kit' {File} = require 'pathwatcher' fs = require 'fs-plus' Q = require 'q' +{deprecate} = require 'grim' Package = require './package' @@ -36,12 +38,35 @@ Package = require './package' # module.exports = class ThemeManager - Emitter.includeInto(this) + EmitterMixin.includeInto(this) constructor: ({@packageManager, @resourcePath, @configDirPath, @safeMode}) -> + @emitter = new Emitter @lessCache = null @packageManager.registerPackageActivator(this, ['theme']) + ### + Section: Events + ### + + # Essential: Invoke `callback` when all styles have been reloaded. + # + # * `callback` {Function} + onDidReloadAll: (callback) -> + @emitter.on 'did-reload-all', callback + + on: (eventName) -> + switch eventName + when 'reloaded' + deprecate 'Use ThemeManager::onDidReloadAll instead' + else + deprecate 'ThemeManager::on is deprecated. Use event subscription methods instead.' + EmitterMixin::on.apply(this, arguments) + + ### + Section: Methods + ### + getAvailableNames: -> # TODO: Maybe should change to list all the available themes out there? @getLoadedNames() @@ -121,6 +146,7 @@ class ThemeManager @loadUserStylesheet() @reloadBaseStylesheets() @emit 'reloaded' + @emitter.emit 'did-reload-all' deferred.resolve() deferred.promise From 5ba5215f5d53305e73c75d0fc7e315d67ecebc95 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 9 Sep 2014 15:35:00 -0700 Subject: [PATCH 14/21] Add event subscription methods to ThemeManager --- spec/theme-manager-spec.coffee | 24 +++++++++++++----------- src/theme-manager.coffee | 31 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/spec/theme-manager-spec.coffee b/spec/theme-manager-spec.coffee index ebb8e1c6e..7028d7c95 100644 --- a/spec/theme-manager-spec.coffee +++ b/spec/theme-manager-spec.coffee @@ -131,8 +131,8 @@ describe "ThemeManager", -> describe "requireStylesheet(path)", -> it "synchronously loads css at the given path and installs a style tag for it in the head", -> - themeManager.on 'stylesheets-changed', stylesheetsChangedHandler = jasmine.createSpy("stylesheetsChangedHandler") - themeManager.on 'stylesheet-added', stylesheetAddedHandler = jasmine.createSpy("stylesheetAddedHandler") + themeManager.onDidChangeStylesheets stylesheetsChangedHandler = jasmine.createSpy("stylesheetsChangedHandler") + themeManager.onDidAddStylesheet stylesheetAddedHandler = jasmine.createSpy("stylesheetAddedHandler") cssPath = atom.project.resolve('css.css') lengthBefore = $('head style').length @@ -193,8 +193,8 @@ describe "ThemeManager", -> themeManager.requireStylesheet(cssPath) expect($(document.body).css('font-weight')).toBe("bold") - themeManager.on 'stylesheet-removed', stylesheetRemovedHandler = jasmine.createSpy("stylesheetRemovedHandler") - themeManager.on 'stylesheets-changed', stylesheetsChangedHandler = jasmine.createSpy("stylesheetsChangedHandler") + themeManager.onDidRemoveStylesheet stylesheetRemovedHandler = jasmine.createSpy("stylesheetRemovedHandler") + themeManager.onDidChangeStylesheets stylesheetsChangedHandler = jasmine.createSpy("stylesheetsChangedHandler") themeManager.removeStylesheet(cssPath) @@ -217,7 +217,7 @@ describe "ThemeManager", -> themeManager.activateThemes() it "loads the correct values from the theme's ui-variables file", -> - themeManager.on 'reloaded', reloadHandler = jasmine.createSpy() + themeManager.onDidReloadAll reloadHandler = jasmine.createSpy() atom.config.set('core.themes', ['theme-with-ui-variables']) waitsFor -> @@ -234,7 +234,7 @@ describe "ThemeManager", -> describe "when there is a theme with incomplete variables", -> it "loads the correct values from the fallback ui-variables", -> - themeManager.on 'reloaded', reloadHandler = jasmine.createSpy() + themeManager.onDidReloadAll reloadHandler = jasmine.createSpy() atom.config.set('core.themes', ['theme-with-incomplete-ui-variables']) waitsFor -> @@ -251,7 +251,7 @@ describe "ThemeManager", -> it 'adds theme-* classes to the workspace for each active theme', -> expect(atom.workspaceView).toHaveClass 'theme-atom-dark-ui' - themeManager.on 'reloaded', reloadHandler = jasmine.createSpy() + themeManager.onDidReloadAll reloadHandler = jasmine.createSpy() atom.config.set('core.themes', ['theme-with-ui-variables']) waitsFor -> @@ -273,9 +273,9 @@ describe "ThemeManager", -> themeManager.activateThemes() runs -> - themeManager.on 'stylesheets-changed', stylesheetsChangedHandler = jasmine.createSpy("stylesheetsChangedHandler") - themeManager.on 'stylesheet-removed', stylesheetRemovedHandler = jasmine.createSpy("stylesheetRemovedHandler") - themeManager.on 'stylesheet-added', stylesheetAddedHandler = jasmine.createSpy("stylesheetAddedHandler") + themeManager.onDidChangeStylesheets stylesheetsChangedHandler = jasmine.createSpy("stylesheetsChangedHandler") + themeManager.onDidRemoveStylesheet stylesheetRemovedHandler = jasmine.createSpy("stylesheetRemovedHandler") + themeManager.onDidAddStylesheet stylesheetAddedHandler = jasmine.createSpy("stylesheetAddedHandler") spyOn(themeManager, 'loadUserStylesheet').andCallThrough() expect($(document.body).css('border-style')).toBe 'dotted' @@ -316,7 +316,9 @@ describe "ThemeManager", -> themeManager.activateThemes() runs -> - themeManager.once 'reloaded', -> reloaded = true + disposable = themeManager.onDidReloadAll -> + disposable.dispose() + reloaded = true spyOn(console, 'warn') expect(-> atom.config.set('core.themes', ['atom-light-ui', 'theme-really-does-not-exist'])).not.toThrow() diff --git a/src/theme-manager.coffee b/src/theme-manager.coffee index e3da5377b..ee48d555f 100644 --- a/src/theme-manager.coffee +++ b/src/theme-manager.coffee @@ -55,10 +55,36 @@ class ThemeManager onDidReloadAll: (callback) -> @emitter.on 'did-reload-all', callback + # Essential: Invoke `callback` when a stylesheet has been added to the dom. + # + # * `callback` {Function} + # * `stylesheet` {HTMLElement} the style node + onDidAddStylesheet: (callback) -> + @emitter.on 'did-add-stylesheet', callback + + # Essential: Invoke `callback` when a stylesheet has been removed from the dom. + # + # * `callback` {Function} + # * `stylesheet` {HTMLElement} the style node + onDidRemoveStylesheet: (callback) -> + @emitter.on 'did-remove-stylesheet', callback + + # Essential: Invoke `callback` when any stylesheet has been updated, added, or removed. + # + # * `callback` {Function} + onDidChangeStylesheets: (callback) -> + @emitter.on 'did-change-stylesheets', callback + on: (eventName) -> switch eventName when 'reloaded' deprecate 'Use ThemeManager::onDidReloadAll instead' + when 'stylesheet-added' + deprecate 'Use ThemeManager::onDidAddStylesheet instead' + when 'stylesheet-removed' + deprecate 'Use ThemeManager::onDidRemoveStylesheet instead' + when 'stylesheets-changed' + deprecate 'Use ThemeManager::onDidChangeStylesheets instead' else deprecate 'ThemeManager::on is deprecated. Use event subscription methods instead.' EmitterMixin::on.apply(this, arguments) @@ -286,7 +312,9 @@ class ThemeManager {sheet} = element element.remove() @emit 'stylesheet-removed', sheet + @emitter.emit 'did-remove-stylesheet', sheet @emit 'stylesheets-changed' + @emitter.emit 'did-change-stylesheets' applyStylesheet: (path, text, type='bundled') -> styleId = @stringToId(path) @@ -294,6 +322,7 @@ class ThemeManager if styleElement? @emit 'stylesheet-removed', styleElement.sheet + @emitter.emit 'did-remove-stylesheet', styleElement.sheet styleElement.textContent = text else styleElement = document.createElement('style') @@ -308,4 +337,6 @@ class ThemeManager document.head.appendChild(styleElement) @emit 'stylesheet-added', styleElement.sheet + @emitter.emit 'did-add-stylesheet', styleElement.sheet @emit 'stylesheets-changed' + @emitter.emit 'did-change-stylesheets' From 10ceb344266b33099b68aa0d7fbafbdb8b748e2b Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 9 Sep 2014 15:43:12 -0700 Subject: [PATCH 15/21] Fix docs --- src/theme-manager.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/theme-manager.coffee b/src/theme-manager.coffee index ee48d555f..e4ae4c626 100644 --- a/src/theme-manager.coffee +++ b/src/theme-manager.coffee @@ -58,14 +58,14 @@ class ThemeManager # Essential: Invoke `callback` when a stylesheet has been added to the dom. # # * `callback` {Function} - # * `stylesheet` {HTMLElement} the style node + # * `stylesheet` {StyleSheet} the style node onDidAddStylesheet: (callback) -> @emitter.on 'did-add-stylesheet', callback # Essential: Invoke `callback` when a stylesheet has been removed from the dom. # # * `callback` {Function} - # * `stylesheet` {HTMLElement} the style node + # * `stylesheet` {StyleSheet} the style node onDidRemoveStylesheet: (callback) -> @emitter.on 'did-remove-stylesheet', callback From 09fbd468695c252bc2d964b3f0065f81c7a48d4f Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 9 Sep 2014 15:43:35 -0700 Subject: [PATCH 16/21] Remove event docs --- src/theme-manager.coffee | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/src/theme-manager.coffee b/src/theme-manager.coffee index e4ae4c626..7ada908f9 100644 --- a/src/theme-manager.coffee +++ b/src/theme-manager.coffee @@ -13,29 +13,6 @@ Package = require './package' # Extended: Handles loading and activating available themes. # # An instance of this class is always available as the `atom.themes` global. -# -# ## Events -# -# ### reloaded -# -# Extended: Emitted when all styles have been reloaded. -# -# ### stylesheet-added -# -# Extended: Emitted when a stylesheet has been added. -# -# * `stylesheet` {StyleSheet} object that was removed -# -# ### stylesheet-removed -# -# Extended: Emitted when a stylesheet has been removed. -# -# * `stylesheet` {StyleSheet} object that was removed -# -# ### stylesheets-changed -# -# Extended: Emitted anytime any style sheet is added or removed from the editor -# module.exports = class ThemeManager EmitterMixin.includeInto(this) From 2e219f288d5b926a5dd1f1d952d84cbc99d668d1 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 9 Sep 2014 15:55:47 -0700 Subject: [PATCH 17/21] Add sections for docs clarity --- src/cursor.coffee | 2 +- src/decoration.coffee | 2 +- src/editor.coffee | 2 +- src/git.coffee | 10 +++++++++- src/package-manager.coffee | 4 ++-- src/package.coffee | 12 ++++++++++-- src/project.coffee | 2 +- src/selection.coffee | 2 +- src/theme-manager.coffee | 2 +- 9 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/cursor.coffee b/src/cursor.coffee index 1126ad790..a165ebfef 100644 --- a/src/cursor.coffee +++ b/src/cursor.coffee @@ -55,7 +55,7 @@ class Cursor extends Model @needsAutoscroll = true ### - Section: Events + Section: Event Subscription ### # Essential: Calls your `callback` when the cursor has been moved. diff --git a/src/decoration.coffee b/src/decoration.coffee index 7216aee18..3cf4822d2 100644 --- a/src/decoration.coffee +++ b/src/decoration.coffee @@ -56,7 +56,7 @@ class Decoration @markerDestroyDisposable = @marker.onDidDestroy => @destroy() ### - Section: Events + Section: Event Subscription ### # Essential: When the {Decoration} is updated via {Decoration::update}. diff --git a/src/editor.coffee b/src/editor.coffee index bf40518bf..a1b86a0c4 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -173,7 +173,7 @@ class Editor extends Model @languageMode.destroy() ### - Section: Events + Section: Event Subscription ### # Essential: Calls your `callback` when the buffer's title has changed. diff --git a/src/git.coffee b/src/git.coffee index 7f9d28007..b2907eefc 100644 --- a/src/git.coffee +++ b/src/git.coffee @@ -47,6 +47,10 @@ class Git EmitterMixin.includeInto(this) Subscriber.includeInto(this) + ### + Section: Class Methods + ### + # Public: Creates a new Git instance. # # * `path` The {String} path to the Git repository to open. @@ -69,6 +73,10 @@ class Git else false + ### + Section: Construction + ### + constructor: (path, options={}) -> @emitter = new Emitter @repo = GitUtils.open(path) @@ -93,7 +101,7 @@ class Git @subscribe @project.observeBuffers (buffer) => @subscribeToBuffer(buffer) ### - Section: Events + Section: Event Subscription ### # Essential: Invoke the given callback when a specific file's status has diff --git a/src/package-manager.coffee b/src/package-manager.coffee index 7fd2bcd16..e3b9d9f3b 100644 --- a/src/package-manager.coffee +++ b/src/package-manager.coffee @@ -45,7 +45,7 @@ class PackageManager @registerPackageActivator(this, ['atom', 'textmate']) ### - Section: Events + Section: Event ### # Essential: Invoke the given callback when all packages have been activated. @@ -71,7 +71,7 @@ class PackageManager EmitterMixin::on.apply(this, arguments) ### - Section: Methods + Section: Instance Methods ### # Extended: Get the path to the apm command. diff --git a/src/package.coffee b/src/package.coffee index 7a8133466..bb443329a 100644 --- a/src/package.coffee +++ b/src/package.coffee @@ -20,6 +20,10 @@ class Package @stylesheetsDir: 'stylesheets' + ### + Section: Class Methods + ### + @loadMetadata: (packagePath, ignoreErrors=false) -> if metadataPath = CSON.resolve(path.join(packagePath, 'package')) try @@ -39,6 +43,10 @@ class Package resolvedMainModulePath: false mainModule: null + ### + Section: Construction + ### + constructor: (@path, @metadata) -> @emitter = new Emitter @metadata ?= Package.loadMetadata(@path) @@ -46,7 +54,7 @@ class Package @reset() ### - Section: Events + Section: Event Subscription ### # Essential: Invoke the given callback when all packages have been activated. @@ -64,7 +72,7 @@ class Package EmitterMixin::on.apply(this, arguments) ### - Section: Methods + Section: Instance Methods ### enable: -> diff --git a/src/project.coffee b/src/project.coffee index 1fe38577f..f2743be18 100644 --- a/src/project.coffee +++ b/src/project.coffee @@ -44,7 +44,7 @@ class Project extends Model @setPath(path) ### - Section: Events + Section: Event Subscription ### # Essential: Invoke the given callback when the project's path has changed. diff --git a/src/selection.coffee b/src/selection.coffee index 5e2118bee..47f87b31c 100644 --- a/src/selection.coffee +++ b/src/selection.coffee @@ -31,7 +31,7 @@ class Selection extends Model @emitter.dispose() ### - Section: Events + Section: Event Subscription ### # Extended: Calls your `callback` when the selection was moved. diff --git a/src/theme-manager.coffee b/src/theme-manager.coffee index 7ada908f9..562bcdd5a 100644 --- a/src/theme-manager.coffee +++ b/src/theme-manager.coffee @@ -23,7 +23,7 @@ class ThemeManager @packageManager.registerPackageActivator(this, ['theme']) ### - Section: Events + Section: Event Subscription ### # Essential: Invoke `callback` when all styles have been reloaded. From 6a0a842de440fa88981d9fa0e1065b1a0f5d80ab Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Wed, 10 Sep 2014 11:52:57 -0700 Subject: [PATCH 18/21] Update section comments --- src/package-manager.coffee | 2 +- src/package.coffee | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/package-manager.coffee b/src/package-manager.coffee index e3b9d9f3b..4151869ec 100644 --- a/src/package-manager.coffee +++ b/src/package-manager.coffee @@ -45,7 +45,7 @@ class PackageManager @registerPackageActivator(this, ['atom', 'textmate']) ### - Section: Event + Section: Event Subscription ### # Essential: Invoke the given callback when all packages have been activated. diff --git a/src/package.coffee b/src/package.coffee index bb443329a..e21182f0b 100644 --- a/src/package.coffee +++ b/src/package.coffee @@ -20,10 +20,6 @@ class Package @stylesheetsDir: 'stylesheets' - ### - Section: Class Methods - ### - @loadMetadata: (packagePath, ignoreErrors=false) -> if metadataPath = CSON.resolve(path.join(packagePath, 'package')) try From 61fa1c4230527849406be6212dc5b00cb5ce65bf Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Wed, 10 Sep 2014 11:55:26 -0700 Subject: [PATCH 19/21] Revert "Add Project::observeBuffers" This reverts commit b6fe72ef9e416723f66c775049381c8b28c927f3. Conflicts: src/project.coffee --- spec/project-spec.coffee | 6 +++--- src/git.coffee | 2 +- src/project.coffee | 20 +++++--------------- 3 files changed, 9 insertions(+), 19 deletions(-) diff --git a/spec/project-spec.coffee b/spec/project-spec.coffee index 7c705b8a4..b1fed60ee 100644 --- a/spec/project-spec.coffee +++ b/spec/project-spec.coffee @@ -479,7 +479,7 @@ describe "Project", -> runs -> expect(results).toHaveLength 0 - describe ".observeBuffers(callback)", -> + describe ".eachBuffer(callback)", -> beforeEach -> atom.project.bufferForPathSync('a') @@ -490,7 +490,7 @@ describe "Project", -> callback = (buffer) -> callbackBuffer = buffer count++ - atom.project.observeBuffers(callback) + atom.project.eachBuffer(callback) expect(count).toBe 1 expect(callbackBuffer).toBe atom.project.getBuffers()[0] @@ -501,7 +501,7 @@ describe "Project", -> callbackBuffer = buffer count++ - atom.project.observeBuffers(callback) + atom.project.eachBuffer(callback) count = 0 callbackBuffer = null atom.project.bufferForPathSync(require.resolve('./fixtures/sample.txt')) diff --git a/src/git.coffee b/src/git.coffee index b2907eefc..fb458c995 100644 --- a/src/git.coffee +++ b/src/git.coffee @@ -98,7 +98,7 @@ class Git @refreshStatus() if @project? - @subscribe @project.observeBuffers (buffer) => @subscribeToBuffer(buffer) + @subscribe @project.eachBuffer (buffer) => @subscribeToBuffer(buffer) ### Section: Event Subscription diff --git a/src/project.coffee b/src/project.coffee index f2743be18..96da3da5e 100644 --- a/src/project.coffee +++ b/src/project.coffee @@ -54,15 +54,6 @@ class Project extends Model onDidChangePath: (callback) -> @emitter.on 'did-change-path', callback - # Extended: Immediately invoke the given callback for all existing buffers. - # Then invoke the given callback when a new buffer has been created. For - # example, when {::open} is called, this is fired. - # - # * `callback` {Function} - # * `buffer` {TextBuffer} the new buffer - observeBuffers: (callback) -> - callback(buffer) for buffer in @getBuffers() - @onDidCreateBuffer(callback) # Extended: Invoke the given callback when a new buffer has been created. For # example, when {::open} is called, this is fired. @@ -363,12 +354,11 @@ class Project extends Model subscriber = args.shift() if args.length > 1 callback = args.shift() - message = 'Use Project::observeBuffers instead' - message += '; Project::observeBuffers no longer accepts a subscriber parameter. Call `subscriber.subscribe(atom.project.observeBuffers(cb))`' if subscriber? - - disposable = @observeBuffers(callback) - subscriber.subscribe(disposable) if subscriber? - disposable + callback(buffer) for buffer in @getBuffers() + if subscriber + subscriber.subscribe this, 'buffer-created', (buffer) -> callback(buffer) + else + @on 'buffer-created', (buffer) -> callback(buffer) # Deprecated: delegate registerOpener: (opener) -> From 40b32930cf497030eb8d97dafd09dc5721a3d213 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Wed, 10 Sep 2014 11:55:41 -0700 Subject: [PATCH 20/21] Revert "Add Project::onDidCreateBuffer" This reverts commit 4070e5fb25a1c001ee781c28a932c105a5ab1598. --- spec/project-spec.coffee | 2 +- src/project.coffee | 12 ------------ 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/spec/project-spec.coffee b/spec/project-spec.coffee index b1fed60ee..06e3ee29d 100644 --- a/spec/project-spec.coffee +++ b/spec/project-spec.coffee @@ -58,7 +58,7 @@ describe "Project", -> beforeEach -> absolutePath = require.resolve('./fixtures/dir/a') newBufferHandler = jasmine.createSpy('newBufferHandler') - atom.project.onDidCreateBuffer newBufferHandler + atom.project.on 'buffer-created', newBufferHandler describe "when given an absolute path that isn't currently open", -> it "returns a new edit session for the given path and emits 'buffer-created'", -> diff --git a/src/project.coffee b/src/project.coffee index 96da3da5e..3f8d7ce37 100644 --- a/src/project.coffee +++ b/src/project.coffee @@ -54,21 +54,10 @@ class Project extends Model onDidChangePath: (callback) -> @emitter.on 'did-change-path', callback - - # Extended: Invoke the given callback when a new buffer has been created. For - # example, when {::open} is called, this is fired. - # - # * `callback` {Function} - # * `buffer` {TextBuffer} the new buffer - onDidCreateBuffer: (callback) -> - @emitter.on 'did-create-buffer', callback - on: (eventName) -> switch eventName when 'path-changed' deprecate 'Use Project::onDidChangePath instead' - when 'buffer-created' - deprecate 'Use Project::onDidCreateBuffer instead' else deprecate 'Project::on is deprecated. Use event subscription methods instead.' EmitterMixin::on.apply(this, arguments) @@ -247,7 +236,6 @@ class Project extends Model @buffers.splice(index, 0, buffer) buffer.onDidDestroy => @removeBuffer(buffer) @emit 'buffer-created', buffer - @emitter.emit 'did-create-buffer', buffer buffer # Removes a {TextBuffer} association from the project. From 49937b2956dd62b756fb9029b994bd059feae2e0 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Wed, 10 Sep 2014 11:56:38 -0700 Subject: [PATCH 21/21] Revert "Add Project::onDidChangePath()" This reverts commit ed4acb02d8d07eb12b36a7e4d4d5be9db322112b. Conflicts: src/project.coffee --- src/project.coffee | 31 ++----------------------------- 1 file changed, 2 insertions(+), 29 deletions(-) diff --git a/src/project.coffee b/src/project.coffee index 3f8d7ce37..371a7af3e 100644 --- a/src/project.coffee +++ b/src/project.coffee @@ -6,9 +6,7 @@ fs = require 'fs-plus' Q = require 'q' {deprecate} = require 'grim' {Model} = require 'theorist' -{Subscriber} = require 'emissary' -EmitterMixin = require('emissary').Emitter -{Emitter} = require 'event-kit' +{Emitter, Subscriber} = require 'emissary' Serializable = require 'serializable' TextBuffer = require 'text-buffer' {Directory} = require 'pathwatcher' @@ -35,7 +33,6 @@ class Project extends Model constructor: ({path, @buffers}={}) -> @buffers ?= [] - @emitter = new Emitter for buffer in @buffers do (buffer) => @@ -43,29 +40,6 @@ class Project extends Model @setPath(path) - ### - Section: Event Subscription - ### - - # Essential: Invoke the given callback when the project's path has changed. - # - # * `callback` {Function} - # * `path` the new path - onDidChangePath: (callback) -> - @emitter.on 'did-change-path', callback - - on: (eventName) -> - switch eventName - when 'path-changed' - deprecate 'Use Project::onDidChangePath instead' - else - deprecate 'Project::on is deprecated. Use event subscription methods instead.' - EmitterMixin::on.apply(this, arguments) - - ### - Section: Methods - ### - serializeParams: -> path: @path buffers: _.compact(@buffers.map (buffer) -> buffer.serialize() if buffer.isRetained()) @@ -111,8 +85,7 @@ class Project extends Model else @rootDirectory = null - @emit 'path-changed' - @emitter.emit 'did-change-path', @getPath() + @emit "path-changed" # Public: Returns the root {Directory} object for this project. getRootDirectory: ->