Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
vscode
提交
d393578d
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,体验更适合开发者的 AI 搜索 >>
提交
d393578d
编写于
10月 29, 2018
作者:
B
Benjamin Pasero
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
storage - switch to throttled delayer
上级
3fb99698
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
27 addition
and
56 deletion
+27
-56
src/vs/base/common/async.ts
src/vs/base/common/async.ts
+4
-2
src/vs/base/node/storage.ts
src/vs/base/node/storage.ts
+23
-54
未找到文件。
src/vs/base/common/async.ts
浏览文件 @
d393578d
...
...
@@ -242,8 +242,10 @@ export class Delayer<T> implements IDisposable {
* A helper to delay execution of a task that is being requested often, while
* preventing accumulation of consecutive executions, while the task runs.
*
* Simply combine the two mail men's strategies from the Throttler and Delayer
* helpers, for an analogy.
* The mail man is clever and waits for a certain amount of time, before going
* out to deliver letters. While the mail man is going out, more letters arrive
* and can only be delivered once he is back. Once he is back the mail man will
* do one more trip to deliver the letters that have accumulated while he was out.
*/
export
class
ThrottledDelayer
<
T
>
extends
Delayer
<
TPromise
<
T
>>
{
...
...
src/vs/base/node/storage.ts
浏览文件 @
d393578d
...
...
@@ -6,7 +6,7 @@
import
{
Database
,
Statement
,
OPEN_READWRITE
,
OPEN_CREATE
}
from
'
vscode-sqlite3
'
;
import
{
Disposable
,
IDisposable
}
from
'
vs/base/common/lifecycle
'
;
import
{
Emitter
,
Event
}
from
'
vs/base/common/event
'
;
import
{
RunOnceScheduler
,
Queue
}
from
'
vs/base/common/async
'
;
import
{
ThrottledDelayer
}
from
'
vs/base/common/async
'
;
import
{
isUndefinedOrNull
}
from
'
vs/base/common/types
'
;
import
{
mapToString
,
setToString
}
from
'
vs/base/common/map
'
;
import
{
basename
}
from
'
path
'
;
...
...
@@ -50,8 +50,8 @@ export interface IStorage extends IDisposable {
getInteger
(
key
:
string
,
fallbackValue
:
number
):
number
;
getInteger
(
key
:
string
,
fallbackValue
?:
number
):
number
|
undefined
;
set
(
key
:
string
,
value
:
any
):
Promis
e
<
void
>
;
delete
(
key
:
string
):
Promis
e
<
void
>
;
set
(
key
:
string
,
value
:
any
):
Thenabl
e
<
void
>
;
delete
(
key
:
string
):
Thenabl
e
<
void
>
;
close
():
Thenable
<
void
>
;
...
...
@@ -72,19 +72,17 @@ export class Storage extends Disposable implements IStorage {
private
storage
:
SQLiteStorageImpl
;
private
cache
:
Map
<
string
,
string
>
=
new
Map
<
string
,
string
>
();
private
pendingQueue
:
Queue
<
void
>
;
private
pendingScheduler
:
RunOnceScheduler
;
private
flushDelayer
:
ThrottledDelayer
<
void
>
;
private
pendingDeletes
:
Set
<
string
>
=
new
Set
<
string
>
();
private
pendingInserts
:
Map
<
string
,
string
>
=
new
Map
();
private
pendingPromises
:
{
resolve
:
Function
,
reject
:
Function
}[]
=
[];
constructor
(
options
:
IStorageOptions
)
{
super
();
this
.
storage
=
new
SQLiteStorageImpl
(
options
);
this
.
pendingQueue
=
this
.
_register
(
new
Queue
());
this
.
pendingScheduler
=
this
.
_register
(
new
RunOnceScheduler
(()
=>
this
.
flushPending
(),
Storage
.
FLUSH_DELAY
));
this
.
flushDelayer
=
this
.
_register
(
new
ThrottledDelayer
(
Storage
.
FLUSH_DELAY
));
}
get
size
():
number
{
...
...
@@ -139,7 +137,7 @@ export class Storage extends Disposable implements IStorage {
return
parseInt
(
value
,
10
);
}
set
(
key
:
string
,
value
:
any
):
Promis
e
<
void
>
{
set
(
key
:
string
,
value
:
any
):
Thenabl
e
<
void
>
{
if
(
this
.
state
===
StorageState
.
Closed
)
{
return
Promise
.
resolve
();
// Return early if we are already closed
}
...
...
@@ -166,10 +164,11 @@ export class Storage extends Disposable implements IStorage {
// Event
this
.
_onDidChangeStorage
.
fire
(
key
);
return
this
.
update
();
// Accumulate work by scheduling after timeout
return
this
.
flushDelayer
.
trigger
(()
=>
this
.
flushPending
());
}
delete
(
key
:
string
):
Promis
e
<
void
>
{
delete
(
key
:
string
):
Thenabl
e
<
void
>
{
if
(
this
.
state
===
StorageState
.
Closed
)
{
return
Promise
.
resolve
();
// Return early if we are already closed
}
...
...
@@ -189,17 +188,8 @@ export class Storage extends Disposable implements IStorage {
// Event
this
.
_onDidChangeStorage
.
fire
(
key
);
return
this
.
update
();
}
private
update
():
Promise
<
void
>
{
// Schedule
if
(
!
this
.
pendingScheduler
.
isScheduled
())
{
this
.
pendingScheduler
.
schedule
();
}
return
new
Promise
((
resolve
,
reject
)
=>
this
.
pendingPromises
.
push
({
resolve
,
reject
}));
// Accumulate work by scheduling after timeout
return
this
.
flushDelayer
.
trigger
(()
=>
this
.
flushPending
());
}
close
():
Thenable
<
void
>
{
...
...
@@ -210,44 +200,23 @@ export class Storage extends Disposable implements IStorage {
// Update state
this
.
state
=
StorageState
.
Closed
;
// Dispose scheduler (no more scheduling possible)
this
.
pendingScheduler
.
dispose
();
// Flush & close
return
this
.
flushPending
().
then
(()
=>
{
return
this
.
storage
.
close
();
});
// Trigger new flush to ensure data is persisted and then close
// even if there is an error flushing. We must always ensure
// the DB is closed to avoid corruption.
const
onDone
=
()
=>
this
.
storage
.
close
();
return
this
.
flushDelayer
.
trigger
(()
=>
this
.
flushPending
()).
then
(
onDone
,
onDone
);
}
private
flushPending
():
Thenable
<
void
>
{
// We use a Queue to ensure that:
// - there is only ever one call to storage.updateItems() at the same time
// - upon close() we are certain that all calls to storage.updateItems()
// have finished. Otherwise there is a risk that we close() the DB while
// a transaction is active.
return
this
.
pendingQueue
.
queue
(()
=>
{
// Get pending data
const
pendingPromises
=
this
.
pendingPromises
;
const
pendingDeletes
=
this
.
pendingDeletes
;
const
pendingInserts
=
this
.
pendingInserts
;
// Reset pending data for next run
this
.
pendingPromises
=
[];
this
.
pendingDeletes
=
new
Set
<
string
>
();
this
.
pendingInserts
=
new
Map
<
string
,
string
>
();
// Get pending data
const
updateRequest
:
IUpdateRequest
=
{
insert
:
this
.
pendingInserts
,
delete
:
this
.
pendingDeletes
};
return
this
.
storage
.
updateItems
({
insert
:
pendingInserts
,
delete
:
pendingDeletes
}).
then
(()
=>
{
// Reset pending data for next run
this
.
pendingDeletes
=
new
Set
<
string
>
();
this
.
pendingInserts
=
new
Map
<
string
,
string
>
();
// Resolve pending
pendingPromises
.
forEach
(
promise
=>
promise
.
resolve
());
},
error
=>
{
// Forward error to pending
pendingPromises
.
forEach
(
promise
=>
promise
.
reject
(
error
));
});
});
return
this
.
storage
.
updateItems
(
updateRequest
);
}
getItems
():
Promise
<
Map
<
string
,
string
>>
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录