mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2025-04-06 03:57:56 +08:00
fix(dep): get-tagert需要指定type
This commit is contained in:
parent
e50e332818
commit
70f2b11df5
@ -40,13 +40,8 @@ export default class Watcher {
|
||||
* @param id target id
|
||||
* @returns Target
|
||||
*/
|
||||
public getTarget(id: string | number) {
|
||||
const allTargets = Object.values(this.targetsList);
|
||||
for (const targets of allTargets) {
|
||||
if (targets[id]) {
|
||||
return targets[id];
|
||||
}
|
||||
}
|
||||
public getTarget(id: string | number, type: string = DepTargetType.DEFAULT) {
|
||||
return this.getTargets(type)[id];
|
||||
}
|
||||
|
||||
/**
|
||||
@ -54,21 +49,19 @@ export default class Watcher {
|
||||
* @param id target id
|
||||
* @returns boolean
|
||||
*/
|
||||
public hasTarget(id: string | number) {
|
||||
return Boolean(this.getTarget(id));
|
||||
public hasTarget(id: string | number, type: string = DepTargetType.DEFAULT) {
|
||||
return Boolean(this.getTarget(id, type));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除指定id的target
|
||||
* @param id target id
|
||||
*/
|
||||
public removeTarget(id: string | number) {
|
||||
const allTargets = Object.values(this.targetsList);
|
||||
for (const targets of allTargets) {
|
||||
if (targets[id]) {
|
||||
targets[id].destroy();
|
||||
delete targets[id];
|
||||
}
|
||||
public removeTarget(id: string | number, type: string = DepTargetType.DEFAULT) {
|
||||
const targets = this.getTargets(type);
|
||||
if (targets[id]) {
|
||||
targets[id].destroy();
|
||||
delete targets[id];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -19,8 +19,8 @@ describe('Watcher', () => {
|
||||
|
||||
watcher.addTarget(target);
|
||||
|
||||
expect(watcher.getTarget(1)).toBeUndefined();
|
||||
expect(watcher.getTarget('target')?.id).toBe('target');
|
||||
expect(watcher.getTarget(1, 'target')).toBeUndefined();
|
||||
expect(watcher.getTarget('target', 'target')?.id).toBe('target');
|
||||
expect(Object.keys(watcher.getTargets())).toHaveLength(0);
|
||||
expect(Object.keys(watcher.getTargets('target'))).toHaveLength(1);
|
||||
});
|
||||
@ -92,11 +92,11 @@ describe('Watcher', () => {
|
||||
watcher.addTarget(defaultTarget);
|
||||
watcher.addTarget(target);
|
||||
expect(watcher.hasTarget('defaultTarget')).toBeTruthy();
|
||||
expect(watcher.hasTarget('target')).toBeTruthy();
|
||||
expect(watcher.hasTarget('target', 'targetType')).toBeTruthy();
|
||||
|
||||
watcher.removeTargets('targetType');
|
||||
expect(watcher.hasTarget('defaultTarget')).toBeTruthy();
|
||||
expect(watcher.hasTarget('target')).toBeFalsy();
|
||||
expect(watcher.hasTarget('target', 'targetType')).toBeFalsy();
|
||||
});
|
||||
|
||||
test('collect', () => {
|
||||
@ -141,8 +141,8 @@ describe('Watcher', () => {
|
||||
},
|
||||
]);
|
||||
|
||||
const target1 = watcher.getTarget('collect_1');
|
||||
const target2 = watcher.getTarget('collect_2');
|
||||
const target1 = watcher.getTarget('collect_1', 'target');
|
||||
const target2 = watcher.getTarget('collect_2', 'target');
|
||||
|
||||
expect(target1?.deps?.node_1.name).toBe('node');
|
||||
expect(target2?.deps?.node_1.name).toBe('node');
|
||||
@ -226,7 +226,7 @@ describe('Watcher', () => {
|
||||
true,
|
||||
);
|
||||
|
||||
const target1 = watcher.getTarget('collect_1');
|
||||
const target1 = watcher.getTarget('collect_1', 'target');
|
||||
|
||||
expect(target1?.deps?.node_1.name).toBe('node');
|
||||
expect(target1?.deps?.node_2.name).toBe('node2');
|
||||
|
Loading…
x
Reference in New Issue
Block a user