Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
vscode
提交
e02573f0
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,发现更多精彩内容 >>
提交
e02573f0
编写于
9月 15, 2016
作者:
J
Joao Moreno
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
event buffer, nextTick, tests
上级
2a8bab87
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
159 addition
and
98 deletion
+159
-98
src/vs/base/common/event.ts
src/vs/base/common/event.ts
+11
-3
src/vs/base/test/common/event.test.ts
src/vs/base/test/common/event.test.ts
+148
-95
未找到文件。
src/vs/base/common/event.ts
浏览文件 @
e02573f0
...
@@ -375,7 +375,7 @@ export function stopwatch<T>(event: Event<T>): Event<number> {
...
@@ -375,7 +375,7 @@ export function stopwatch<T>(event: Event<T>): Event<number> {
* // 4
* // 4
* ```
* ```
*/
*/
export
function
buffer
<
T
>
(
event
:
Event
<
T
>
):
Event
<
T
>
{
export
function
buffer
<
T
>
(
event
:
Event
<
T
>
,
nextTick
=
false
):
Event
<
T
>
{
let
buffer
:
T
[]
=
[];
let
buffer
:
T
[]
=
[];
let
listener
=
event
(
e
=>
{
let
listener
=
event
(
e
=>
{
if
(
buffer
)
{
if
(
buffer
)
{
...
@@ -385,6 +385,11 @@ export function buffer<T>(event: Event<T>): Event<T> {
...
@@ -385,6 +385,11 @@ export function buffer<T>(event: Event<T>): Event<T> {
}
}
});
});
const
flush
=
()
=>
{
buffer
.
forEach
(
e
=>
emitter
.
fire
(
e
));
buffer
=
null
;
};
const
emitter
=
new
Emitter
<
T
>
({
const
emitter
=
new
Emitter
<
T
>
({
onFirstListenerAdd
()
{
onFirstListenerAdd
()
{
if
(
!
listener
)
{
if
(
!
listener
)
{
...
@@ -394,8 +399,11 @@ export function buffer<T>(event: Event<T>): Event<T> {
...
@@ -394,8 +399,11 @@ export function buffer<T>(event: Event<T>): Event<T> {
onFirstListenerDidAdd
()
{
onFirstListenerDidAdd
()
{
if
(
buffer
)
{
if
(
buffer
)
{
buffer
.
forEach
(
e
=>
emitter
.
fire
(
e
));
if
(
nextTick
)
{
buffer
=
null
;
setTimeout
(
flush
);
}
else
{
flush
();
}
}
}
},
},
...
...
src/vs/base/test/common/event.test.ts
浏览文件 @
e02573f0
...
@@ -5,7 +5,7 @@
...
@@ -5,7 +5,7 @@
'
use strict
'
;
'
use strict
'
;
import
*
as
assert
from
'
assert
'
;
import
*
as
assert
from
'
assert
'
;
import
Event
,
{
Emitter
,
fromEventEmitter
,
debounceEvent
,
EventBufferer
,
once
,
fromPromise
,
stopwatch
}
from
'
vs/base/common/event
'
;
import
Event
,
{
Emitter
,
fromEventEmitter
,
debounceEvent
,
EventBufferer
,
once
,
fromPromise
,
stopwatch
,
buffer
}
from
'
vs/base/common/event
'
;
import
{
IDisposable
}
from
'
vs/base/common/lifecycle
'
;
import
{
IDisposable
}
from
'
vs/base/common/lifecycle
'
;
import
{
EventEmitter
}
from
'
vs/base/common/eventEmitter
'
;
import
{
EventEmitter
}
from
'
vs/base/common/eventEmitter
'
;
import
Errors
=
require
(
'
vs/base/common/errors
'
);
import
Errors
=
require
(
'
vs/base/common/errors
'
);
...
@@ -211,144 +211,197 @@ suite('Event',function(){
...
@@ -211,144 +211,197 @@ suite('Event',function(){
});
});
});
});
suite
(
'
Event
Bufferer
'
,
()
=>
{
suite
(
'
Event
utils
'
,
()
=>
{
test
(
'
should not buffer when not wrapped
'
,
()
=>
{
suite
(
'
EventBufferer
'
,
()
=>
{
const
bufferer
=
new
EventBufferer
();
const
counter
=
new
Samples
.
EventCounter
();
const
emitter
=
new
Emitter
<
void
>
();
const
event
=
bufferer
.
wrapEvent
(
emitter
.
event
);
const
listener
=
event
(
counter
.
onEvent
,
counter
);
assert
.
equal
(
counter
.
count
,
0
);
test
(
'
should not buffer when not wrapped
'
,
()
=>
{
emitter
.
fire
();
const
bufferer
=
new
EventBufferer
();
assert
.
equal
(
counter
.
count
,
1
);
const
counter
=
new
Samples
.
EventCounter
();
emitter
.
fire
();
const
emitter
=
new
Emitter
<
void
>
();
assert
.
equal
(
counter
.
count
,
2
);
const
event
=
bufferer
.
wrapEvent
(
emitter
.
event
);
emitter
.
fire
();
const
listener
=
event
(
counter
.
onEvent
,
counter
);
assert
.
equal
(
counter
.
count
,
3
);
listener
.
dispose
();
assert
.
equal
(
counter
.
count
,
0
);
});
emitter
.
fire
();
assert
.
equal
(
counter
.
count
,
1
);
emitter
.
fire
();
assert
.
equal
(
counter
.
count
,
2
);
emitter
.
fire
();
assert
.
equal
(
counter
.
count
,
3
);
test
(
'
should buffer when wrapped
'
,
()
=>
{
listener
.
dispose
();
const
bufferer
=
new
EventBufferer
();
});
const
counter
=
new
Samples
.
EventCounter
();
const
emitter
=
new
Emitter
<
void
>
();
const
event
=
bufferer
.
wrapEvent
(
emitter
.
event
);
const
listener
=
event
(
counter
.
onEvent
,
counter
);
assert
.
equal
(
counter
.
count
,
0
);
test
(
'
should buffer when wrapped
'
,
()
=>
{
emitter
.
fire
();
const
bufferer
=
new
EventBufferer
();
assert
.
equal
(
counter
.
count
,
1
);
const
counter
=
new
Samples
.
EventCounter
();
const
emitter
=
new
Emitter
<
void
>
();
const
event
=
bufferer
.
wrapEvent
(
emitter
.
event
);
const
listener
=
event
(
counter
.
onEvent
,
counter
);
bufferer
.
bufferEvents
(()
=>
{
assert
.
equal
(
counter
.
count
,
0
);
emitter
.
fire
();
emitter
.
fire
();
assert
.
equal
(
counter
.
count
,
1
);
assert
.
equal
(
counter
.
count
,
1
);
bufferer
.
bufferEvents
(()
=>
{
emitter
.
fire
();
assert
.
equal
(
counter
.
count
,
1
);
emitter
.
fire
();
assert
.
equal
(
counter
.
count
,
1
);
});
assert
.
equal
(
counter
.
count
,
3
);
emitter
.
fire
();
emitter
.
fire
();
assert
.
equal
(
counter
.
count
,
1
);
assert
.
equal
(
counter
.
count
,
4
);
listener
.
dispose
();
});
});
assert
.
equal
(
counter
.
count
,
3
);
test
(
'
once
'
,
()
=>
{
emitter
.
fire
();
const
emitter
=
new
Emitter
<
void
>
();
assert
.
equal
(
counter
.
count
,
4
);
listener
.
dispose
();
let
counter1
=
0
,
counter2
=
0
,
counter3
=
0
;
});
test
(
'
once
'
,
()
=>
{
const
listener1
=
emitter
.
event
(()
=>
counter1
++
);
const
emitter
=
new
Emitter
<
void
>
();
const
listener2
=
once
(
emitter
.
event
)(()
=>
counter2
++
);
const
listener3
=
once
(
emitter
.
event
)(()
=>
counter3
++
);
let
counter1
=
0
,
counter2
=
0
,
counter3
=
0
;
assert
.
equal
(
counter1
,
0
);
assert
.
equal
(
counter2
,
0
);
assert
.
equal
(
counter3
,
0
);
const
listener1
=
emitter
.
event
(()
=>
counter1
++
);
listener3
.
dispose
();
const
listener2
=
once
(
emitter
.
event
)(()
=>
counter2
++
);
emitter
.
fire
();
const
listener3
=
once
(
emitter
.
event
)(()
=>
counter3
++
);
assert
.
equal
(
counter1
,
1
);
assert
.
equal
(
counter2
,
1
);
assert
.
equal
(
counter3
,
0
);
assert
.
equal
(
counter1
,
0
);
emitter
.
fire
();
assert
.
equal
(
counter2
,
0
);
assert
.
equal
(
counter1
,
2
);
assert
.
equal
(
counter3
,
0
);
assert
.
equal
(
counter2
,
1
);
assert
.
equal
(
counter3
,
0
);
listener3
.
dispose
();
listener1
.
dispose
();
emitter
.
fire
();
listener2
.
dispose
();
assert
.
equal
(
counter1
,
1
);
});
assert
.
equal
(
counter2
,
1
);
});
assert
.
equal
(
counter3
,
0
);
emitter
.
fire
();
suite
(
'
fromPromise
'
,
()
=>
{
assert
.
equal
(
counter1
,
2
);
assert
.
equal
(
counter2
,
1
);
assert
.
equal
(
counter3
,
0
);
listener1
.
dispose
();
test
(
'
should emit when done
'
,
()
=>
{
listener2
.
dispose
();
let
count
=
0
;
});
});
suite
(
'
fromPromise
'
,
()
=>
{
const
event
=
fromPromise
(
TPromise
.
as
(
null
));
event
(()
=>
count
++
);
test
(
'
should emit when done
'
,
()
=>
{
assert
.
equal
(
count
,
0
);
let
count
=
0
;
return
TPromise
.
timeout
(
10
).
then
(()
=>
{
assert
.
equal
(
count
,
1
);
});
});
const
event
=
fromPromise
(
TPromise
.
as
(
null
));
test
(
'
should emit when done - setTimeout
'
,
()
=>
{
event
(()
=>
count
++
)
;
let
count
=
0
;
assert
.
equal
(
count
,
0
);
const
event
=
fromPromise
(
TPromise
.
timeout
(
5
));
event
(()
=>
count
++
);
return
TPromise
.
timeout
(
10
).
then
(()
=>
{
assert
.
equal
(
count
,
0
);
assert
.
equal
(
count
,
1
);
return
TPromise
.
timeout
(
10
).
then
(()
=>
{
assert
.
equal
(
count
,
1
);
});
});
});
});
test
(
'
should emit when done - setTimeout
'
,
()
=>
{
test
(
'
should emit when done - setTimeout
'
,
()
=>
{
let
count
=
0
;
let
count
=
0
;
const
event
=
fromPromise
(
TPromise
.
timeout
(
5
));
const
event
=
fromPromise
(
TPromise
.
timeout
(
10
));
event
(()
=>
count
++
);
event
(()
=>
count
++
);
assert
.
equal
(
count
,
0
);
assert
.
equal
(
count
,
0
);
return
TPromise
.
timeout
(
0
).
then
(()
=>
{
assert
.
equal
(
count
,
0
);
return
TPromise
.
timeout
(
10
).
then
(()
=>
{
return
TPromise
.
timeout
(
10
).
then
(()
=>
{
assert
.
equal
(
count
,
1
);
assert
.
equal
(
count
,
1
);
});
});
});
});
});
});
test
(
'
should emit when done - setTimeout
'
,
()
=>
{
suite
(
'
stopwatch
'
,
()
=>
{
let
count
=
0
;
const
event
=
fromPromise
(
TPromise
.
timeout
(
10
));
test
(
'
should emit
'
,
()
=>
{
event
(()
=>
count
++
);
const
emitter
=
new
Emitter
<
void
>
();
const
event
=
stopwatch
(
emitter
.
event
);
assert
.
equal
(
count
,
0
);
return
new
TPromise
((
c
,
e
)
=>
{
event
(
duration
=>
{
try
{
assert
(
duration
>
0
);
}
catch
(
err
)
{
e
(
err
);
}
return
TPromise
.
timeout
(
0
).
then
(()
=>
{
c
(
null
);
assert
.
equal
(
count
,
0
);
}
);
return
TPromise
.
timeout
(
10
).
then
(()
=>
{
setTimeout
(()
=>
emitter
.
fire
(),
10
);
assert
.
equal
(
count
,
1
);
});
});
});
});
});
});
});
suite
(
'
stopwatch
'
,
()
=>
{
suite
(
'
buffer
'
,
()
=>
{
test
(
'
should emit
'
,
()
=>
{
test
(
'
should buffer events
'
,
()
=>
{
const
emitter
=
new
Emitter
<
void
>
();
const
result
=
[];
const
event
=
stopwatch
(
emitter
.
event
);
const
emitter
=
new
Emitter
<
number
>
();
const
event
=
emitter
.
event
;
const
bufferedEvent
=
buffer
(
event
);
return
new
TPromise
((
c
,
e
)
=>
{
emitter
.
fire
(
1
);
event
(
duration
=>
{
emitter
.
fire
(
2
);
try
{
emitter
.
fire
(
3
);
assert
(
duration
>
0
);
assert
.
deepEqual
(
result
,
[]);
}
catch
(
err
)
{
e
(
err
);
}
c
(
null
);
const
listener
=
bufferedEvent
(
num
=>
result
.
push
(
num
));
});
assert
.
deepEqual
(
result
,
[
1
,
2
,
3
]);
emitter
.
fire
(
4
);
assert
.
deepEqual
(
result
,
[
1
,
2
,
3
,
4
]);
setTimeout
(()
=>
emitter
.
fire
(),
10
);
listener
.
dispose
();
emitter
.
fire
(
5
);
assert
.
deepEqual
(
result
,
[
1
,
2
,
3
,
4
]);
});
test
(
'
should buffer events on next tick
'
,
()
=>
{
const
result
=
[];
const
emitter
=
new
Emitter
<
number
>
();
const
event
=
emitter
.
event
;
const
bufferedEvent
=
buffer
(
event
,
true
);
emitter
.
fire
(
1
);
emitter
.
fire
(
2
);
emitter
.
fire
(
3
);
assert
.
deepEqual
(
result
,
[]);
const
listener
=
bufferedEvent
(
num
=>
result
.
push
(
num
));
assert
.
deepEqual
(
result
,
[]);
return
TPromise
.
timeout
(
10
).
then
(()
=>
{
emitter
.
fire
(
4
);
assert
.
deepEqual
(
result
,
[
1
,
2
,
3
,
4
]);
listener
.
dispose
();
emitter
.
fire
(
5
);
assert
.
deepEqual
(
result
,
[
1
,
2
,
3
,
4
]);
});
});
});
});
});
});
});
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录