Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
vscode
提交
4e2dfa60
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,发现更多精彩内容 >>
提交
4e2dfa60
编写于
8月 18, 2016
作者:
A
Alex Dima
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Clean up IResourceService
上级
b673a645
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
55 addition
and
140 deletion
+55
-140
src/vs/editor/common/services/resourceService.ts
src/vs/editor/common/services/resourceService.ts
+2
-31
src/vs/editor/common/services/resourceServiceImpl.ts
src/vs/editor/common/services/resourceServiceImpl.ts
+45
-62
src/vs/editor/test/common/services/resourceService.test.ts
src/vs/editor/test/common/services/resourceService.test.ts
+8
-47
未找到文件。
src/vs/editor/common/services/resourceService.ts
浏览文件 @
4e2dfa60
...
...
@@ -4,47 +4,18 @@
*--------------------------------------------------------------------------------------------*/
'
use strict
'
;
import
{
EmitterEvent
,
ListenerCallback
}
from
'
vs/base/common/eventEmitter
'
;
import
{
IDisposable
}
from
'
vs/base/common/lifecycle
'
;
import
URI
from
'
vs/base/common/uri
'
;
import
{
createDecorator
}
from
'
vs/platform/instantiation/common/instantiation
'
;
import
{
IMirrorModel
}
from
'
vs/editor/common/editorCommon
'
;
// Resource Service
export
var
ResourceEvents
=
{
ADDED
:
'
resource.added
'
,
REMOVED
:
'
resource.removed
'
,
CHANGED
:
'
resource.changed
'
};
export
interface
IResourceAddedEvent
{
url
:
URI
;
addedElement
:
IMirrorModel
;
}
export
interface
IResourceRemovedEvent
{
url
:
URI
;
removedElement
:
IMirrorModel
;
}
export
interface
IResourceChangedEvent
{
url
:
URI
;
originalEvents
:
EmitterEvent
[];
}
export
var
IResourceService
=
createDecorator
<
IResourceService
>
(
'
resourceService
'
);
export
let
IResourceService
=
createDecorator
<
IResourceService
>
(
'
resourceService
'
);
export
interface
IResourceService
{
_serviceBrand
:
any
;
insert
(
url
:
URI
,
element
:
IMirrorModel
):
void
;
get
(
url
:
URI
):
IMirrorModel
;
all
():
IMirrorModel
[];
contains
(
url
:
URI
):
boolean
;
remove
(
url
:
URI
):
void
;
addListener2_
(
eventType
:
'
resource.added
'
,
listener
:
(
event
:
IResourceAddedEvent
)
=>
void
):
IDisposable
;
addListener2_
(
eventType
:
'
resource.removed
'
,
listener
:
(
event
:
IResourceRemovedEvent
)
=>
void
):
IDisposable
;
addListener2_
(
eventType
:
'
resource.changed
'
,
listener
:
(
event
:
IResourceChangedEvent
)
=>
void
):
IDisposable
;
addListener2_
(
eventType
:
string
,
listener
:
ListenerCallback
):
IDisposable
;
}
src/vs/editor/common/services/resourceServiceImpl.ts
浏览文件 @
4e2dfa60
...
...
@@ -4,31 +4,48 @@
*--------------------------------------------------------------------------------------------*/
'
use strict
'
;
import
{
EventEmitter
,
EmitterEvent
,
ListenerCallback
}
from
'
vs/base/common/eventEmitter
'
;
import
{
IDisposable
,
dispose
}
from
'
vs/base/common/lifecycle
'
;
import
URI
from
'
vs/base/common/uri
'
;
import
{
IMirrorModel
}
from
'
vs/editor/common/editorCommon
'
;
import
{
IResource
AddedEvent
,
IResourceChangedEvent
,
IResourceRemovedEvent
,
IResourceService
,
ResourceEvents
}
from
'
vs/editor/common/services/resourceService
'
;
import
{
IResource
Service
}
from
'
vs/editor/common/services/resourceService
'
;
export
class
ResourceService
extends
EventEmitter
implements
IResourceService
{
public
_serviceBrand
:
any
;
private
data
:{[
url
:
string
]:
IMirrorModel
;};
private
unbinds
:{[
url
:
string
]:
IDisposable
[];};
class
MirrorModelMap
{
private
_data
:
{[
key
:
string
]:
IMirrorModel
};
constructor
()
{
super
();
this
.
data
=
{};
this
.
unbinds
=
{};
this
.
_data
=
{};
}
public
set
(
key
:
string
,
data
:
IMirrorModel
):
void
{
this
.
_data
[
key
]
=
data
;
}
public
addListener2_
(
eventType
:
string
,
listener
:
ListenerCallback
):
IDisposable
{
return
super
.
addListener2
(
eventType
,
listener
);
public
get
(
key
:
string
):
IMirrorModel
{
return
this
.
_data
[
key
]
||
null
;
}
public
contains
(
key
:
string
):
boolean
{
return
!!
this
.
_data
[
key
];
}
public
remove
(
key
:
string
):
void
{
delete
this
.
_data
[
key
];
}
}
export
class
ResourceService
implements
IResourceService
{
public
_serviceBrand
:
any
;
private
_map
:
MirrorModelMap
;
constructor
()
{
this
.
_map
=
new
MirrorModelMap
();
}
private
_anonymousModelId
(
input
:
string
):
string
{
var
r
=
''
;
for
(
var
i
=
0
;
i
<
input
.
length
;
i
++
)
{
var
ch
=
input
[
i
];
private
static
_anonymousModelId
(
input
:
string
):
string
{
let
r
=
''
;
for
(
let
i
=
0
;
i
<
input
.
length
;
i
++
)
{
let
ch
=
input
[
i
];
if
(
ch
>=
'
0
'
&&
ch
<=
'
9
'
)
{
r
+=
'
0
'
;
continue
;
...
...
@@ -46,59 +63,25 @@ export class ResourceService extends EventEmitter implements IResourceService {
return
r
;
}
public
insert
(
url
:
URI
,
element
:
IMirrorModel
):
void
{
// console.log('INSERT: ' + url.toString());
if
(
this
.
contains
(
url
))
{
// There already exists a model with this id => this is a programmer error
throw
new
Error
(
'
ResourceService: Cannot add model
'
+
this
.
_anonymousModelId
(
url
.
toString
())
+
'
because it already exists!
'
);
}
// add resource
var
key
=
url
.
toString
();
this
.
data
[
key
]
=
element
;
this
.
unbinds
[
key
]
=
[];
this
.
unbinds
[
key
].
push
(
element
.
addBulkListener2
((
value
:
EmitterEvent
[])
=>
{
this
.
emit
(
ResourceEvents
.
CHANGED
,
<
IResourceChangedEvent
>
{
url
:
url
,
originalEvents
:
value
});
}));
// event
this
.
emit
(
ResourceEvents
.
ADDED
,
<
IResourceAddedEvent
>
{
url
:
url
,
addedElement
:
element
});
}
public
insert
(
uri
:
URI
,
element
:
IMirrorModel
):
void
{
let
key
=
uri
.
toString
();
public
get
(
url
:
URI
):
IMirrorModel
{
if
(
!
this
.
data
[
url
.
toString
()])
{
return
null
;
if
(
this
.
_map
.
contains
(
key
))
{
// There already exists a model with this id => this is a programmer error
throw
new
Error
(
'
ResourceService: Cannot add model
'
+
ResourceService
.
_anonymousModelId
(
key
)
+
'
because it already exists!
'
)
;
}
return
this
.
data
[
url
.
toString
()]
;
this
.
_map
.
set
(
key
,
element
)
;
}
public
all
():
IMirrorModel
[]
{
return
Object
.
keys
(
this
.
data
).
map
((
key
)
=>
{
return
this
.
data
[
key
];
});
}
public
get
(
uri
:
URI
):
IMirrorModel
{
let
key
=
uri
.
toString
();
public
contains
(
url
:
URI
):
boolean
{
return
!!
this
.
data
[
url
.
toString
()];
return
this
.
_map
.
get
(
key
);
}
public
remove
(
url
:
URI
):
void
{
// console.log('REMOVE: ' + url.toString());
if
(
!
this
.
contains
(
url
))
{
return
;
}
var
key
=
url
.
toString
(),
element
=
this
.
data
[
key
];
// stop listen
this
.
unbinds
[
key
]
=
dispose
(
this
.
unbinds
[
key
]);
// removal
delete
this
.
unbinds
[
key
];
delete
this
.
data
[
key
];
public
remove
(
uri
:
URI
):
void
{
let
key
=
uri
.
toString
();
// event
this
.
emit
(
ResourceEvents
.
REMOVED
,
<
IResourceRemovedEvent
>
{
url
:
url
,
removedElement
:
element
});
this
.
_map
.
remove
(
key
);
}
}
src/vs/editor/test/common/services/resourceService.test.ts
浏览文件 @
4e2dfa60
...
...
@@ -7,72 +7,33 @@
import
*
as
assert
from
'
assert
'
;
import
URI
from
'
vs/base/common/uri
'
;
import
{
createTestMirrorModelFromString
}
from
'
vs/editor/common/model/mirrorModel
'
;
import
{
ResourceEvents
}
from
'
vs/editor/common/services/resourceService
'
;
import
{
ResourceService
}
from
'
vs/editor/common/services/resourceServiceImpl
'
;
suite
(
'
Editor Services - ResourceService
'
,
()
=>
{
test
(
'
insert, remove, all
'
,
()
=>
{
var
service
=
new
ResourceService
();
let
service
=
new
ResourceService
();
service
.
insert
(
URI
.
parse
(
'
test://1
'
),
createTestMirrorModelFromString
(
'
hi
'
));
assert
.
equal
(
service
.
all
().
length
,
1
);
assert
.
equal
(
service
.
get
(
URI
.
parse
(
'
test://1
'
)).
getValue
(),
'
hi
'
);
service
.
insert
(
URI
.
parse
(
'
test://2
'
),
createTestMirrorModelFromString
(
'
hi
'
));
assert
.
equal
(
service
.
all
().
length
,
2
);
assert
.
ok
(
service
.
contains
(
URI
.
parse
(
'
test://1
'
)));
assert
.
ok
(
service
.
contains
(
URI
.
parse
(
'
test://2
'
)));
service
.
remove
(
URI
.
parse
(
'
test://1
'
));
service
.
remove
(
URI
.
parse
(
'
test://1
'
));
service
.
remove
(
URI
.
parse
(
'
test://2
'
));
assert
.
equal
(
service
.
all
().
length
,
0
);
});
test
(
'
event - add, remove
'
,
()
=>
{
var
eventCnt
=
0
;
var
url
=
URI
.
parse
(
'
far
'
);
var
element
=
createTestMirrorModelFromString
(
'
hi
'
);
var
service
=
new
ResourceService
();
service
.
addListener2
(
ResourceEvents
.
ADDED
,
()
=>
{
eventCnt
++
;
assert
.
ok
(
true
);
});
service
.
addListener2
(
ResourceEvents
.
REMOVED
,
()
=>
{
eventCnt
++
;
assert
.
ok
(
true
);
});
service
.
insert
(
url
,
element
);
service
.
remove
(
url
);
assert
.
equal
(
eventCnt
,
2
,
'
events
'
);
assert
.
equal
(
service
.
get
(
URI
.
parse
(
'
test://1
'
)),
null
);
});
test
(
'
event - propagation
'
,
()
=>
{
var
eventCnt
=
0
;
test
(
'
inserting the same resource twice throws
'
,
()
=>
{
let
service
=
new
ResourceService
();
var
url
=
URI
.
parse
(
'
far
'
);
var
element
=
createTestMirrorModelFromString
(
'
hi
'
);
var
event
=
{};
service
.
insert
(
URI
.
parse
(
'
test://path/some-N1ce-name
'
),
createTestMirrorModelFromString
(
'
hello
'
));
var
service
=
new
ResourceService
();
service
.
insert
(
url
,
element
);
service
.
addBulkListener2
((
events
)
=>
{
eventCnt
++
;
assert
.
equal
(
events
.
length
,
1
);
assert
.
equal
(
events
[
0
].
getData
().
originalEvents
.
length
,
1
);
assert
.
ok
(
events
[
0
].
getData
().
originalEvents
[
0
].
getData
()
===
event
);
assert
.
throws
(()
=>
{
service
.
insert
(
URI
.
parse
(
'
test://path/some-N1ce-name
'
),
createTestMirrorModelFromString
(
'
hello again
'
));
});
element
.
emit
(
'
changed
'
,
event
);
assert
.
equal
(
eventCnt
,
1
,
'
events
'
);
});
});
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录