add proper events and make the notifier class generic

Expand the event system to have explicit Event objects and potentially
many Notifiers. Previously they was only one notifier and one (implied)
event.

A lot of this is stolen from #358.
This commit is contained in:
Martin Grenfell 2014-07-17 20:16:57 +01:00
parent f9a933991d
commit fd14757c04
6 changed files with 53 additions and 25 deletions

View File

@ -74,10 +74,11 @@ function! nerdtree#loadClassFiles()
runtime lib/nerdtree/tree_dir_node.vim
runtime lib/nerdtree/opener.vim
runtime lib/nerdtree/creator.vim
runtime lib/nerdtree/refresh_notifier.vim
runtime lib/nerdtree/flag_set.vim
runtime lib/nerdtree/nerdtree.vim
runtime lib/nerdtree/ui.vim
runtime lib/nerdtree/event.vim
runtime lib/nerdtree/notifier.vim
endfunction
" FUNCTION: nerdtree#postSourceActions() {{{2

13
lib/nerdtree/event.vim Normal file
View File

@ -0,0 +1,13 @@
"CLASS: Event
"============================================================
let s:Event = {}
let g:NERDTreeEvent = s:Event
function! s:Event.New(nerdtree, subject, action, params) abort
let newObj = copy(self)
let newObj.nerdtree = a:nerdtree
let newObj.subject = a:subject
let newObj.action = a:action
let newObj.params = a:params
return newObj
endfunction

35
lib/nerdtree/notifier.vim Normal file
View File

@ -0,0 +1,35 @@
"CLASS: Notifier
"============================================================
let s:Notifier = {}
function! s:Notifier.AddListener(event, funcname)
let listeners = s:Notifier.GetListenersForEvent(a:event)
if listeners == []
let listenersMap = s:Notifier.GetListenersMap()
let listenersMap[a:event] = listeners
endif
call add(listeners, a:funcname)
endfunction
function! s:Notifier.NotifyListeners(event, path, params)
let event = g:NERDTreeEvent.New(b:NERDTree, a:path, a:event, a:params)
for listener in s:Notifier.GetListenersForEvent(a:event)
call {listener}(event)
endfor
endfunction
function! s:Notifier.GetListenersMap()
if !exists("s:refreshListenersMap")
let s:refreshListenersMap = {}
endif
return s:refreshListenersMap
endfunction
function! s:Notifier.GetListenersForEvent(name)
let listenersMap = s:Notifier.GetListenersMap()
return get(listenersMap, a:name, [])
endfunction
let g:NERDTreePathNotifier = deepcopy(s:Notifier)

View File

@ -549,13 +549,13 @@ endfunction
"FUNCTION: Path.refresh() {{{1
function! s:Path.refresh()
call self.readInfoFromDisk(self.str())
call g:NERDTreeRefreshNotifier.NotifyListeners(self)
call g:NERDTreePathNotifier.NotifyListeners('refresh', self, {})
call self.cacheDisplayString()
endfunction
"FUNCTION: Path.refreshFlags() {{{1
function! s:Path.refreshFlags()
call g:NERDTreeRefreshNotifier.NotifyListeners(self)
call g:NERDTreePathNotifier.NotifyListeners('refreshFlags', self, {})
call self.cacheDisplayString()
endfunction

View File

@ -1,21 +0,0 @@
"CLASS: RefreshNotifier
"============================================================
let s:RefreshNotifier = {}
let g:NERDTreeRefreshNotifier = s:RefreshNotifier
function! s:RefreshNotifier.AddListener(funcname)
call add(s:RefreshNotifier.GetListeners(), a:funcname)
endfunction
function! s:RefreshNotifier.NotifyListeners(refreshedPath)
for listener in s:RefreshNotifier.GetListeners()
call {listener}(a:refreshedPath)
endfor
endfunction
function! s:RefreshNotifier.GetListeners()
if !exists("s:refreshListeners")
let s:refreshListeners = []
endif
return s:refreshListeners
endfunction

View File

@ -252,7 +252,7 @@ function! s:TreeDirNode._initChildren(silent)
try
let path = g:NERDTreePath.New(i)
call self.createChild(path, 0)
call g:NERDTreeRefreshNotifier.NotifyListeners(path)
call g:NERDTreePathNotifier.NotifyListeners('init', path, {})
catch /^NERDTree.\(InvalidArguments\|InvalidFiletype\)Error/
let invalidFilesFound += 1
endtry