Created
February 9, 2019 04:38
Star
You must be signed in to star a gist
Cancellation.vim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let s:SYMBOL = '@@cancelSignal' | |
function! s:_vital_created(module) abort | |
let a:module.Symbol = s:SYMBOL | |
endfunction | |
function! s:cancelable() abort | |
let subscribers = [] | |
let signal = { | |
\ 'signaled': v:false, | |
\ 'subscribe': funcref('s:_signal_subscribe', [subscribers]), | |
\} | |
let cancelable = { | |
\ 'cancel': funcref('s:_cancelable_cancel', [subscribers, signal]), | |
\ 'signal': signal, | |
\} | |
let cancelable[s:SYMBOL] = { -> signal } | |
return cancelable | |
endfunction | |
function! s:_signal_subscribe(subscribers, fn) abort | |
call add(a:subscribers, a:fn) | |
return { | |
\ 'unsubscribe': funcref('s:_signal_unsubscribe', [a:subscribers, a:fn]) | |
\} | |
endfunction | |
function! s:_signal_unsubscribe(subscribers, fn) abort | |
let index = index(a:subscribers, a:fn) | |
if index isnot# -1 | |
call remove(a:subscribers, index) | |
endif | |
endfunction | |
function! s:_cancelable_cancel(subscribers, signal) abort dict | |
if a:signal.signaled | |
return | |
endif | |
let a:signal.signaled = v:true | |
for Subscriber in a:subscribers | |
call Subscriber() | |
endfor | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment