Skip to content

Commit 45cea5d

Browse files
committed
Async.Promise: Refactor state check in _publish() and wait_group in all()
1 parent 05e02b4 commit 45cea5d

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

autoload/vital/__vital__/Async/Promise.vim

+14-10
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,20 @@ endfunction
6464
" ... is added to use this function as a callback of timer_start()
6565
function! s:_publish(promise, ...) abort
6666
let settled = a:promise._state
67+
if settled == s:PENDING
68+
throw 'vital: Async.Promise: Cannot publish a pending promise'
69+
endif
70+
6771
if empty(a:promise._children)
6872
return
6973
endif
74+
7075
for i in range(len(a:promise._children))
7176
if settled == s:FULFILLED
7277
let l:CB = a:promise._fulfillments[i]
73-
elseif settled == s:REJECTED
74-
let l:CB = a:promise._rejections[i]
7578
else
76-
throw 'vital: Async.Promise: Cannot publish a pending promise'
79+
" When rejected
80+
let l:CB = a:promise._rejections[i]
7781
endif
7882
let child = a:promise._children[i]
7983
if type(child) != v:t_none
@@ -82,6 +86,7 @@ function! s:_publish(promise, ...) abort
8286
call l:CB(a:promise._result)
8387
endif
8488
endfor
89+
8590
let a:promise._children = []
8691
let a:promise._fulfillments = []
8792
let a:promise._rejections = []
@@ -142,10 +147,10 @@ function! s:_reject(promise, ...) abort
142147
endfunction
143148

144149
function! s:_notify_done(wg, index, value) abort
145-
let a:wg.done[a:index] = a:value
146-
let a:wg.resolved += 1
147-
if a:wg.resolved == a:wg.total
148-
call a:wg.resolve(a:wg.done)
150+
let a:wg.results[a:index] = a:value
151+
let a:wg.remaining -= 1
152+
if a:wg.remaining == 0
153+
call a:wg.resolve(a:wg.results)
149154
endif
150155
endfunction
151156

@@ -157,10 +162,9 @@ function! s:_all(promises, resolve, reject) abort
157162
endif
158163

159164
let wait_group = {
160-
\ 'done': repeat([v:null], total),
165+
\ 'results': repeat([v:null], total),
161166
\ 'resolve': a:resolve,
162-
\ 'resolved': 0,
163-
\ 'total': total,
167+
\ 'remaining': total,
164168
\ }
165169

166170
" 'for' statement is not available here because iteration variable is captured into lambda

0 commit comments

Comments
 (0)