diff --git a/spec/text-editor-registry-spec.coffee b/spec/text-editor-registry-spec.coffee deleted file mode 100644 index 80f29f897..000000000 --- a/spec/text-editor-registry-spec.coffee +++ /dev/null @@ -1,49 +0,0 @@ -TextEditorRegistry = require '../src/text-editor-registry' - -describe "TextEditorRegistry", -> - [registry, editor] = [] - - beforeEach -> - registry = new TextEditorRegistry - - describe "when a TextEditor is added", -> - it "gets added to the list of registered editors", -> - editor = {} - registry.add(editor) - expect(editor.registered).toBe true - expect(registry.editors.size).toBe 1 - expect(registry.editors.has(editor)).toBe(true) - - it "returns a Disposable that can unregister the editor", -> - editor = {} - disposable = registry.add(editor) - expect(registry.editors.size).toBe 1 - disposable.dispose() - expect(registry.editors.size).toBe 0 - expect(editor.registered).toBe false - - it "can be removed", -> - editor = {} - registry.add(editor) - expect(registry.editors.size).toBe 1 - success = registry.remove(editor) - expect(success).toBe true - expect(registry.editors.size).toBe 0 - expect(editor.registered).toBe false - - describe "when the registry is observed", -> - it "calls the callback for current and future editors until unsubscribed", -> - [editor1, editor2, editor3] = [{}, {}, {}] - - registry.add(editor1) - subscription = registry.observe spy = jasmine.createSpy() - expect(spy.calls.length).toBe 1 - - registry.add(editor2) - expect(spy.calls.length).toBe 2 - expect(spy.argsForCall[0][0]).toBe editor1 - expect(spy.argsForCall[1][0]).toBe editor2 - - subscription.dispose() - registry.add(editor3) - expect(spy.calls.length).toBe 2 diff --git a/spec/text-editor-registry-spec.js b/spec/text-editor-registry-spec.js new file mode 100644 index 000000000..b58907378 --- /dev/null +++ b/spec/text-editor-registry-spec.js @@ -0,0 +1,59 @@ +/** @babel */ + +import TextEditorRegistry from '../src/text-editor-registry' + +describe('TextEditorRegistry', function () { + let registry, editor + + beforeEach(function () { + registry = new TextEditorRegistry() + }) + + describe('when a TextEditor is added', function () { + it('gets added to the list of registered editors', function () { + editor = {} + registry.add(editor) + expect(editor.registered).toBe(true) + expect(registry.editors.size).toBe(1) + expect(registry.editors.has(editor)).toBe(true) + }) + + it('returns a Disposable that can unregister the editor', function () { + editor = {} + const disposable = registry.add(editor) + expect(registry.editors.size).toBe(1) + disposable.dispose() + expect(registry.editors.size).toBe(0) + expect(editor.registered).toBe(false) + }) + + it('can be removed', function () { + editor = {} + registry.add(editor) + expect(registry.editors.size).toBe(1) + const success = registry.remove(editor) + expect(success).toBe(true) + expect(registry.editors.size).toBe(0) + expect(editor.registered).toBe(false) + }) + }) + + describe('when the registry is observed', function () { + it('calls the callback for current and future editors until unsubscribed', function () { + const spy = jasmine.createSpy() + const [editor1, editor2, editor3] = [{}, {}, {}] + registry.add(editor1) + const subscription = registry.observe(spy) + expect(spy.calls.length).toBe(1) + + registry.add(editor2) + expect(spy.calls.length).toBe(2) + expect(spy.argsForCall[0][0]).toBe(editor1) + expect(spy.argsForCall[1][0]).toBe(editor2) + subscription.dispose() + + registry.add(editor3) + expect(spy.calls.length).toBe(2) + }) + }) +}) diff --git a/src/text-editor-registry.coffee b/src/text-editor-registry.coffee deleted file mode 100644 index e31630fee..000000000 --- a/src/text-editor-registry.coffee +++ /dev/null @@ -1,52 +0,0 @@ -{Emitter, Disposable} = require 'event-kit' - -# Experimental: This global registry tracks registered `TextEditors`. -# -# If you want to add functionality to a wider set of text editors than just -# those appearing within workspace panes, use `atom.textEditors.observe` to -# invoke a callback for all current and future registered text editors. -# -# If you want packages to be able to add functionality to your non-pane text -# editors (such as a search field in a custom user interface element), register -# them for observation via `atom.textEditors.add`. **Important:** When you're -# done using your editor, be sure to call `dispose` on the returned disposable -# to avoid leaking editors. -module.exports = -class TextEditorRegistry - constructor: -> - @editors = new Set - @emitter = new Emitter - - # Register a `TextEditor`. - # - # * `editor` The editor to register. - # - # Returns a {Disposable} on which `.dispose()` can be called to remove the - # added editor. To avoid any memory leaks this should be called when the - # editor is destroyed. - add: (editor) -> - @editors.add(editor) - editor.registered = true - - @emitter.emit 'did-add-editor', editor - new Disposable => @remove(editor) - - # Remove a `TextEditor`. - # - # * `editor` The editor to remove. - # - # Returns a {Boolean} indicating whether the editor was successfully removed. - remove: (editor) -> - removed = @editors.delete(editor) - editor.registered = false - removed - - # Invoke the given callback with all the current and future registered - # `TextEditors`. - # - # * `callback` {Function} to be called with current and future text editors. - # - # Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. - observe: (callback) -> - @editors.forEach(callback) - @emitter.on 'did-add-editor', callback diff --git a/src/text-editor-registry.js b/src/text-editor-registry.js new file mode 100644 index 000000000..c9ce3cb35 --- /dev/null +++ b/src/text-editor-registry.js @@ -0,0 +1,58 @@ +/** @babel */ + +import {Emitter, Disposable} from "event-kit" + +// Experimental: This global registry tracks registered `TextEditors`. +// +// If you want to add functionality to a wider set of text editors than just +// those appearing within workspace panes, use `atom.textEditors.observe` to +// invoke a callback for all current and future registered text editors. +// +// If you want packages to be able to add functionality to your non-pane text +// editors (such as a search field in a custom user interface element), register +// them for observation via `atom.textEditors.add`. **Important:** When you're +// done using your editor, be sure to call `dispose` on the returned disposable +// to avoid leaking editors. +export default class TextEditorRegistry { + constructor () { + this.editors = new Set() + this.emitter = new Emitter() + } + + // Register a `TextEditor`. + // + // * `editor` The editor to register. + // + // Returns a {Disposable} on which `.dispose()` can be called to remove the + // added editor. To avoid any memory leaks this should be called when the + // editor is destroyed. + add (editor) { + this.editors.add(editor) + editor.registered = true + this.emitter.emit("did-add-editor", editor) + + return new Disposable(() => this.remove(editor)) + } + + // Remove a `TextEditor`. + // + // * `editor` The editor to remove. + // + // Returns a {Boolean} indicating whether the editor was successfully removed. + remove (editor) { + var removed = this.editors.delete(editor) + editor.registered = false + return removed + } + + // Invoke the given callback with all the current and future registered + // `TextEditors`. + // + // * `callback` {Function} to be called with current and future text editors. + // + // Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. + observe (callback) { + this.editors.forEach(callback) + return this.emitter.on("did-add-editor", callback) + } +}