Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
🎨 Cleanup IME Event Subscription
Browse files Browse the repository at this point in the history
- Handle dispose better, prevent undefined @suggestionList calls
  • Loading branch information
joefitzgerald committed Feb 6, 2015
1 parent f4173b3 commit 9e287c5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
47 changes: 22 additions & 25 deletions lib/autocomplete-manager.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{Range, TextEditor, CompositeDisposable, Disposable} = require('atom')
$ = require 'jquery'
_ = require('underscore-plus')
minimatch = require('minimatch')
path = require('path')
Expand All @@ -13,6 +12,7 @@ class AutocompleteManager
backspaceTriggersAutocomplete: true
buffer: null
compositionInProgress: false
disposed: false
editor: null
editorSubscriptions: null
editorView: null
Expand All @@ -34,6 +34,7 @@ class AutocompleteManager

@handleEvents()
@handleCommands()
@subscriptions.add(@suggestionList) # We're adding this last so it is disposed after events
@ready = true

updateCurrentEditor: (currentPaneItem) =>
Expand All @@ -60,21 +61,15 @@ class AutocompleteManager
@editorSubscriptions.add(@buffer.onDidSave(@bufferSaved))
@editorSubscriptions.add(@buffer.onDidChange(@bufferChanged))

# This's a workaround, we should ask Atom to emit events like `onCompositionDidStart`
editorelement = $('atom-text-editor')
# Watch IME Events To Allow IME To Function Without The Suggestion List Showing
startsub = editorelement.on 'compositionstart', =>
@compositionInProgress = true
null
compositionStart = => @compositionInProgress = true
compositionEnd = => @compositionInProgress = false

endsub = editorelement.on 'compositionend', =>
@compositionInProgress = false
null

@editorSubscriptions.add(new Disposable(->
startsub.off()
endsub.off()
))
@editorView.addEventListener('compositionstart', compositionStart)
@editorView.addEventListener('compositionend', compositionEnd)
@editorSubscriptions.add new Disposable ->
@editorView?.removeEventListener('compositionstart', compositionStart)
@editorView?.removeEventListener('compositionend', compositionEnd)

# Subscribe to editor events:
# Close the overlay when the cursor moved without changing any text
Expand Down Expand Up @@ -106,9 +101,8 @@ class AutocompleteManager
# Private: Finds suggestions for the current prefix, sets the list items,
# positions the overlay and shows it
findSuggestions: =>
return unless @providerManager?
return unless @editor?
return unless @buffer?
return if @disposed
return unless @providerManager? and @editor? and @buffer?
return if @isCurrentFileBlackListed()
cursor = @editor.getLastCursor()
return unless cursor?
Expand Down Expand Up @@ -164,7 +158,7 @@ class AutocompleteManager
#
# match - An {Object} representing the confirmed suggestion
confirm: (match) =>
return unless @editor? and match?
return unless @editor? and match? and not @disposed

match.onWillConfirm?()

Expand All @@ -181,12 +175,13 @@ class AutocompleteManager
match.onDidConfirm?()

showSuggestionList: (suggestions) ->
return unless @suggestionList?
return if @disposed
@suggestionList.changeItems(suggestions)
@suggestionList.show(@editor)

hideSuggestionList: =>
@suggestionList?.hide()
return if @disposed
@suggestionList.hide()
@shouldDisplaySuggestions = false

requestHideSuggestionList: (command) ->
Expand Down Expand Up @@ -228,7 +223,7 @@ class AutocompleteManager
requestNewSuggestions: =>
delay = atom.config.get('autocomplete-plus.autoActivationDelay')
clearTimeout(@delayTimeout)
delay = @suggestionDelay if @suggestionList?.isActive()
delay = @suggestionDelay if @suggestionList.isActive()
@delayTimeout = setTimeout(@findSuggestions, delay)
@shouldDisplaySuggestions = true

Expand Down Expand Up @@ -259,9 +254,10 @@ class AutocompleteManager
#
# event - The change {Event}
bufferChanged: ({newText, oldText}) =>
return if @compositionInProgress
return if @disposed
return @hideSuggestionList() if @compositionInProgress
autoActivationEnabled = atom.config.get('autocomplete-plus.enableAutoActivation')
wouldAutoActivate = newText.trim().length is 1 or ((@backspaceTriggersAutocomplete or @suggestionList?.isActive()) and oldText.trim().length is 1)
wouldAutoActivate = newText.trim().length is 1 or ((@backspaceTriggersAutocomplete or @suggestionList.isActive()) and oldText.trim().length is 1)

if autoActivationEnabled and wouldAutoActivate
@cancelHideSuggestionListRequest()
Expand All @@ -272,11 +268,12 @@ class AutocompleteManager

# Public: Clean up, stop listening to events
dispose: =>
@hideSuggestionList()
@disposed = true
@ready = false
@editorSubscriptions?.dispose()
@editorSubscriptions = null
@suggestionList?.destroy()
@suggestionList = null
@subscriptions?.dispose()
@subscriptions = null
@suggestionList = null
@providerManager = null
2 changes: 1 addition & 1 deletion lib/suggestion-list-element.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SuggestionListElement extends HTMLElement
@subscriptions.add(@model.onDidSelectNext(@moveSelectionDown.bind(this)))
@subscriptions.add(@model.onDidSelectPrevious(@moveSelectionUp.bind(this)))
@subscriptions.add(@model.onDidConfirmSelection(@confirmSelection.bind(this)))
@subscriptions.add(@model.onDidDestroy(@dispose.bind(this)))
@subscriptions.add(@model.onDidDispose(@dispose.bind(this)))
this

# This should be unnecessary but the events we need to override
Expand Down
8 changes: 4 additions & 4 deletions lib/suggestion-list.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ class SuggestionList
@emitter.on('did-change-items', fn)

# Public: Clean up, stop listening to events
destroy: ->
dispose: ->
@subscriptions.dispose()
@emitter.emit('did-destroy')
@emitter.emit('did-dispose')
@emitter.dispose()

onDidDestroy: (fn) ->
@emitter.on('did-destroy', fn)
onDidDispose: (fn) ->
@emitter.on('did-dispose', fn)
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"async": ">=0.9.0",
"fuzzaldrin": ">=1.0.0",
"grim": ">=0.12.0",
"jquery": ">=2.1.3",
"minimatch": ">=0.2.14",
"node-uuid": ">=1.4.1",
"scoped-property-store": ">=0.16.2",
Expand Down

0 comments on commit 9e287c5

Please sign in to comment.