Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
掘金者说
vscode
提交
1162228b
V
vscode
项目概览
掘金者说
/
vscode
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
V
vscode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
1162228b
编写于
5月 23, 2016
作者:
J
Joao Moreno
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
cleanup async
上级
7fec80c8
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
32 addition
and
25 deletion
+32
-25
src/vs/base/common/async.ts
src/vs/base/common/async.ts
+32
-25
未找到文件。
src/vs/base/common/async.ts
浏览文件 @
1162228b
...
...
@@ -2,13 +2,14 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'
use strict
'
;
import
*
as
errors
from
'
vs/base/common/errors
'
;
import
{
Promise
,
TPromise
,
ValueCallback
,
ErrorCallback
,
ProgressCallback
}
from
'
vs/base/common/winjs.base
'
;
import
*
as
platform
from
'
vs/base/common/platform
'
;
import
{
CancellationToken
,
CancellationTokenSource
}
from
'
vs/base/common/cancellation
'
;
import
{
Disposable
}
from
'
vs/base/common/lifecycle
'
;
import
{
Promise
,
TPromise
,
ValueCallback
,
ErrorCallback
,
ProgressCallback
}
from
'
vs/base/common/winjs.base
'
;
import
{
CancellationToken
,
CancellationTokenSource
}
from
'
vs/base/common/cancellation
'
;
import
{
Disposable
}
from
'
vs/base/common/lifecycle
'
;
function
isThenable
<
T
>
(
obj
:
any
):
obj
is
Thenable
<
T
>
{
return
obj
&&
typeof
(
<
Thenable
<
any
>>
obj
).
then
===
'
function
'
;
...
...
@@ -63,9 +64,15 @@ export interface ITask<T> {
* var throttler = new Throttler();
* var letters = [];
*
* function letterReceived(l) {
* function deliver() {
* const lettersToDeliver = letters;
* letters = [];
* return makeTheTrip(lettersToDeliver);
* }
*
* function onLetterReceived(l) {
* letters.push(l);
* throttler.queue(
() => { return makeTheTrip(); }
);
* throttler.queue(
deliver
);
* }
*/
export
class
Throttler
{
...
...
@@ -80,7 +87,7 @@ export class Throttler {
this
.
queuedPromiseFactory
=
null
;
}
public
queue
<
T
>
(
promiseFactory
:
ITask
<
TPromise
<
T
>>
):
TPromise
<
T
>
{
queue
<
T
>
(
promiseFactory
:
ITask
<
TPromise
<
T
>>
):
TPromise
<
T
>
{
if
(
this
.
activePromise
)
{
this
.
queuedPromiseFactory
=
promiseFactory
;
...
...
@@ -161,7 +168,7 @@ export class Delayer<T> {
this
.
task
=
null
;
}
public
trigger
(
task
:
ITask
<
T
>
,
delay
:
number
=
this
.
defaultDelay
):
TPromise
<
T
>
{
trigger
(
task
:
ITask
<
T
>
,
delay
:
number
=
this
.
defaultDelay
):
TPromise
<
T
>
{
this
.
task
=
task
;
this
.
cancelTimeout
();
...
...
@@ -188,11 +195,11 @@ export class Delayer<T> {
return
this
.
completionPromise
;
}
public
isTriggered
():
boolean
{
isTriggered
():
boolean
{
return
this
.
timeout
!==
null
;
}
public
cancel
():
void
{
cancel
():
void
{
this
.
cancelTimeout
();
if
(
this
.
completionPromise
)
{
...
...
@@ -226,7 +233,7 @@ export class ThrottledDelayer<T> extends Delayer<TPromise<T>> {
this
.
throttler
=
new
Throttler
();
}
public
trigger
(
promiseFactory
:
ITask
<
TPromise
<
T
>>
,
delay
?:
number
):
Promise
{
trigger
(
promiseFactory
:
ITask
<
TPromise
<
T
>>
,
delay
?:
number
):
Promise
{
return
super
.
trigger
(()
=>
this
.
throttler
.
queue
(
promiseFactory
),
delay
);
}
}
...
...
@@ -247,7 +254,7 @@ export class PeriodThrottledDelayer<T> extends ThrottledDelayer<T> {
this
.
periodThrottler
=
new
Throttler
();
}
public
trigger
(
promiseFactory
:
ITask
<
TPromise
<
T
>>
,
delay
?:
number
):
Promise
{
trigger
(
promiseFactory
:
ITask
<
TPromise
<
T
>>
,
delay
?:
number
):
Promise
{
return
super
.
trigger
(()
=>
{
return
this
.
periodThrottler
.
queue
(()
=>
{
return
Promise
.
join
([
...
...
@@ -415,8 +422,8 @@ export class Limiter<T> {
this
.
runningPromises
=
0
;
}
public
queue
(
promiseFactory
:
ITask
<
Promise
>
):
Promise
;
public
queue
(
promiseFactory
:
ITask
<
TPromise
<
T
>>
):
TPromise
<
T
>
{
queue
(
promiseFactory
:
ITask
<
Promise
>
):
Promise
;
queue
(
promiseFactory
:
ITask
<
TPromise
<
T
>>
):
TPromise
<
T
>
{
return
new
TPromise
<
T
>
((
c
,
e
,
p
)
=>
{
this
.
outstandingPromises
.
push
({
factory
:
promiseFactory
,
...
...
@@ -454,19 +461,19 @@ export class TimeoutTimer extends Disposable {
this
.
_token
=
-
1
;
}
public
dispose
():
void
{
dispose
():
void
{
this
.
cancel
();
super
.
dispose
();
}
public
cancel
():
void
{
cancel
():
void
{
if
(
this
.
_token
!==
-
1
)
{
platform
.
clearTimeout
(
this
.
_token
);
this
.
_token
=
-
1
;
}
}
public
cancelAndSet
(
runner
:
()
=>
void
,
timeout
:
number
):
void
{
cancelAndSet
(
runner
:
()
=>
void
,
timeout
:
number
):
void
{
this
.
cancel
();
this
.
_token
=
platform
.
setTimeout
(()
=>
{
this
.
_token
=
-
1
;
...
...
@@ -474,7 +481,7 @@ export class TimeoutTimer extends Disposable {
},
timeout
);
}
public
setIfNotSet
(
runner
:
()
=>
void
,
timeout
:
number
):
void
{
setIfNotSet
(
runner
:
()
=>
void
,
timeout
:
number
):
void
{
if
(
this
.
_token
!==
-
1
)
{
// timer is already set
return
;
...
...
@@ -495,19 +502,19 @@ export class IntervalTimer extends Disposable {
this
.
_token
=
-
1
;
}
public
dispose
():
void
{
dispose
():
void
{
this
.
cancel
();
super
.
dispose
();
}
public
cancel
():
void
{
cancel
():
void
{
if
(
this
.
_token
!==
-
1
)
{
platform
.
clearInterval
(
this
.
_token
);
this
.
_token
=
-
1
;
}
}
public
cancelAndSet
(
runner
:
()
=>
void
,
interval
:
number
):
void
{
cancelAndSet
(
runner
:
()
=>
void
,
interval
:
number
):
void
{
this
.
cancel
();
this
.
_token
=
platform
.
setInterval
(()
=>
{
runner
();
...
...
@@ -532,7 +539,7 @@ export class RunOnceScheduler {
/**
* Dispose RunOnceScheduler
*/
public
dispose
():
void
{
dispose
():
void
{
this
.
cancel
();
this
.
runner
=
null
;
}
...
...
@@ -540,7 +547,7 @@ export class RunOnceScheduler {
/**
* Cancel current scheduled runner (if any).
*/
public
cancel
():
void
{
cancel
():
void
{
if
(
this
.
isScheduled
())
{
platform
.
clearTimeout
(
this
.
timeoutToken
);
this
.
timeoutToken
=
-
1
;
...
...
@@ -550,14 +557,14 @@ export class RunOnceScheduler {
/**
* Replace runner. If there is a runner already scheduled, the new runner will be called.
*/
public
setRunner
(
runner
:
()
=>
void
):
void
{
setRunner
(
runner
:
()
=>
void
):
void
{
this
.
runner
=
runner
;
}
/**
* Cancel previous runner (if any) & schedule a new runner.
*/
public
schedule
(
delay
=
this
.
timeout
):
void
{
schedule
(
delay
=
this
.
timeout
):
void
{
this
.
cancel
();
this
.
timeoutToken
=
platform
.
setTimeout
(
this
.
timeoutHandler
,
this
.
timeout
);
}
...
...
@@ -565,7 +572,7 @@ export class RunOnceScheduler {
/**
* Returns true if scheduled.
*/
public
isScheduled
():
boolean
{
isScheduled
():
boolean
{
return
this
.
timeoutToken
!==
-
1
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录