Convert text-editor-registry and tests to JS

Signed-off-by: Max Brunsfeld <maxbrunsfeld@github.com>
This commit is contained in:
Nathan Sobo 2016-07-06 12:54:30 -06:00 committed by Max Brunsfeld
parent 98fc8db3da
commit a5613cd7e4
4 changed files with 117 additions and 101 deletions

View File

@ -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

View File

@ -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)
})
})
})

View File

@ -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

View File

@ -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)
}
}