Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
vscode
提交
a41ac457
V
vscode
项目概览
xxadev
/
vscode
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
V
vscode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a41ac457
编写于
6月 06, 2018
作者:
I
isidor
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
queue.cancel
上级
78d7d64c
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
46 addition
and
8 deletion
+46
-8
src/vs/base/common/async.ts
src/vs/base/common/async.ts
+19
-8
src/vs/base/test/common/async.test.ts
src/vs/base/test/common/async.test.ts
+27
-0
未找到文件。
src/vs/base/common/async.ts
浏览文件 @
a41ac457
...
...
@@ -5,6 +5,7 @@
'
use strict
'
;
import
*
as
nls
from
'
vs/nls
'
;
import
*
as
errors
from
'
vs/base/common/errors
'
;
import
{
TPromise
,
ValueCallback
,
ErrorCallback
,
ProgressCallback
}
from
'
vs/base/common/winjs.base
'
;
import
{
CancellationToken
,
CancellationTokenSource
}
from
'
vs/base/common/cancellation
'
;
...
...
@@ -486,7 +487,7 @@ interface ILimitedTaskFactory {
* ensures that at any time no more than M promises are running at the same time.
*/
export
class
Limiter
<
T
>
{
private
runningPromises
:
number
;
private
runningPromises
:
TPromise
<
T
>
[]
;
private
maxDegreeOfParalellism
:
number
;
private
outstandingPromises
:
ILimitedTaskFactory
[];
private
readonly
_onFinished
:
Emitter
<
void
>
;
...
...
@@ -494,7 +495,7 @@ export class Limiter<T> {
constructor
(
maxDegreeOfParalellism
:
number
)
{
this
.
maxDegreeOfParalellism
=
maxDegreeOfParalellism
;
this
.
outstandingPromises
=
[];
this
.
runningPromises
=
0
;
this
.
runningPromises
=
[]
;
this
.
_onFinished
=
new
Emitter
<
void
>
();
}
...
...
@@ -503,7 +504,7 @@ export class Limiter<T> {
}
public
get
size
():
number
{
return
this
.
runningPromises
+
this
.
outstandingPromises
.
length
;
return
this
.
runningPromises
.
length
+
this
.
outstandingPromises
.
length
;
}
queue
(
promiseFactory
:
ITask
<
TPromise
>
):
TPromise
;
...
...
@@ -520,19 +521,29 @@ export class Limiter<T> {
});
}
cancel
():
void
{
const
outstanding
=
this
.
outstandingPromises
;
const
running
=
this
.
runningPromises
;
this
.
outstandingPromises
=
[];
this
.
runningPromises
=
[];
outstanding
.
forEach
(
o
=>
o
.
e
(
new
Error
(
nls
.
localize
(
'
canceled
'
,
"
Canceled
"
))));
running
.
forEach
(
p
=>
p
.
cancel
());
}
private
consume
():
void
{
while
(
this
.
outstandingPromises
.
length
&&
this
.
runningPromises
<
this
.
maxDegreeOfParalellism
)
{
while
(
this
.
outstandingPromises
.
length
&&
this
.
runningPromises
.
length
<
this
.
maxDegreeOfParalellism
)
{
const
iLimitedTask
=
this
.
outstandingPromises
.
shift
();
this
.
runningPromises
++
;
const
promise
=
iLimitedTask
.
factory
();
this
.
runningPromises
.
push
(
promise
);
promise
.
done
(
iLimitedTask
.
c
,
iLimitedTask
.
e
,
iLimitedTask
.
p
);
promise
.
done
(()
=>
this
.
consumed
(
),
()
=>
this
.
consumed
(
));
promise
.
done
(()
=>
this
.
consumed
(
promise
),
()
=>
this
.
consumed
(
promise
));
}
}
private
consumed
():
void
{
this
.
runningPromises
--
;
private
consumed
(
promise
:
TPromise
<
T
>
):
void
{
this
.
runningPromises
=
this
.
runningPromises
.
filter
(
p
=>
p
!==
promise
)
;
if
(
this
.
outstandingPromises
.
length
>
0
)
{
this
.
consume
();
...
...
src/vs/base/test/common/async.test.ts
浏览文件 @
a41ac457
...
...
@@ -507,6 +507,31 @@ suite('Async', () => {
});
});
test
(
'
Queue - cancel
'
,
function
()
{
let
queue
=
new
Async
.
Queue
();
let
res
:
number
[]
=
[];
let
f1
=
()
=>
TPromise
.
timeout
(
0
).
then
(()
=>
res
.
push
(
1
));
let
f2
=
()
=>
TPromise
.
timeout
(
10
).
then
(()
=>
res
.
push
(
2
));
let
f3
=
()
=>
TPromise
.
as
(
true
).
then
(()
=>
res
.
push
(
3
));
let
f4
=
()
=>
TPromise
.
timeout
(
20
).
then
(()
=>
res
.
push
(
4
));
let
f5
=
()
=>
TPromise
.
timeout
(
0
).
then
(()
=>
res
.
push
(
5
));
queue
.
queue
(
f1
).
then
(()
=>
assert
.
fail
());
queue
.
queue
(
f2
).
then
(()
=>
assert
.
fail
());
queue
.
queue
(
f3
).
then
(()
=>
assert
.
fail
());
assert
.
equal
(
queue
.
size
,
3
);
queue
.
cancel
();
assert
.
equal
(
queue
.
size
,
0
);
queue
.
queue
(
f4
).
then
(
undefined
,
()
=>
assert
.
fail
());
return
queue
.
queue
(
f5
).
then
(()
=>
{
assert
.
equal
(
res
.
length
,
2
);
assert
.
equal
(
res
[
0
],
4
);
assert
.
equal
(
res
[
1
],
5
);
});
});
test
(
'
Queue - errors bubble individually but not cause stop
'
,
function
()
{
let
queue
=
new
Async
.
Queue
();
...
...
@@ -547,6 +572,8 @@ suite('Async', () => {
return
queue
.
queue
(
f2
).
then
(()
=>
{
return
queue
.
queue
(
f3
).
then
(()
=>
{
return
queue
.
queue
(
f4
).
then
(()
=>
{
// Queue is empty, cancel should be a noop
queue
.
cancel
();
return
queue
.
queue
(
f5
).
then
(()
=>
{
assert
.
equal
(
res
[
0
],
1
);
assert
.
equal
(
res
[
1
],
2
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录