Merge pull request #11671 from atom/disentangle-text-editors-global

Remove TextEditor's dependency on the textEditors global
This commit is contained in:
Josh Abernathy
2016-05-03 10:33:41 -04:00
3 changed files with 26 additions and 2 deletions

View File

@@ -10,6 +10,7 @@ describe "TextEditorRegistry", ->
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)
@@ -19,6 +20,16 @@ describe "TextEditorRegistry", ->
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", ->

View File

@@ -26,8 +26,20 @@ class TextEditorRegistry
# editor is destroyed.
add: (editor) ->
@editors.add(editor)
editor.registered = true
@emitter.emit 'did-add-editor', editor
new Disposable => @editors.delete(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`.

View File

@@ -62,6 +62,7 @@ class TextEditor extends Model
selectionFlashDuration: 500
gutterContainer: null
editorElement: null
registered: false
Object.defineProperty @prototype, "element",
get: -> @getElement()
@@ -157,7 +158,7 @@ class TextEditor extends Model
firstVisibleScreenColumn: @getFirstVisibleScreenColumn()
displayBuffer: @displayBuffer.serialize()
selectionsMarkerLayerId: @selectionsMarkerLayer.id
registered: atom.textEditors.editors.has this
registered: @registered
subscribeToBuffer: ->
@buffer.retain()