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
|
* @param id target id
|
||||||
* @returns Target
|
* @returns Target
|
||||||
*/
|
*/
|
||||||
public getTarget(id: string | number) {
|
public getTarget(id: string | number, type: string = DepTargetType.DEFAULT) {
|
||||||
const allTargets = Object.values(this.targetsList);
|
return this.getTargets(type)[id];
|
||||||
for (const targets of allTargets) {
|
|
||||||
if (targets[id]) {
|
|
||||||
return targets[id];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -54,23 +49,21 @@ export default class Watcher {
|
|||||||
* @param id target id
|
* @param id target id
|
||||||
* @returns boolean
|
* @returns boolean
|
||||||
*/
|
*/
|
||||||
public hasTarget(id: string | number) {
|
public hasTarget(id: string | number, type: string = DepTargetType.DEFAULT) {
|
||||||
return Boolean(this.getTarget(id));
|
return Boolean(this.getTarget(id, type));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除指定id的target
|
* 删除指定id的target
|
||||||
* @param id target id
|
* @param id target id
|
||||||
*/
|
*/
|
||||||
public removeTarget(id: string | number) {
|
public removeTarget(id: string | number, type: string = DepTargetType.DEFAULT) {
|
||||||
const allTargets = Object.values(this.targetsList);
|
const targets = this.getTargets(type);
|
||||||
for (const targets of allTargets) {
|
|
||||||
if (targets[id]) {
|
if (targets[id]) {
|
||||||
targets[id].destroy();
|
targets[id].destroy();
|
||||||
delete targets[id];
|
delete targets[id];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除指定分类的所有target
|
* 删除指定分类的所有target
|
||||||
|
@ -19,8 +19,8 @@ describe('Watcher', () => {
|
|||||||
|
|
||||||
watcher.addTarget(target);
|
watcher.addTarget(target);
|
||||||
|
|
||||||
expect(watcher.getTarget(1)).toBeUndefined();
|
expect(watcher.getTarget(1, 'target')).toBeUndefined();
|
||||||
expect(watcher.getTarget('target')?.id).toBe('target');
|
expect(watcher.getTarget('target', 'target')?.id).toBe('target');
|
||||||
expect(Object.keys(watcher.getTargets())).toHaveLength(0);
|
expect(Object.keys(watcher.getTargets())).toHaveLength(0);
|
||||||
expect(Object.keys(watcher.getTargets('target'))).toHaveLength(1);
|
expect(Object.keys(watcher.getTargets('target'))).toHaveLength(1);
|
||||||
});
|
});
|
||||||
@ -92,11 +92,11 @@ describe('Watcher', () => {
|
|||||||
watcher.addTarget(defaultTarget);
|
watcher.addTarget(defaultTarget);
|
||||||
watcher.addTarget(target);
|
watcher.addTarget(target);
|
||||||
expect(watcher.hasTarget('defaultTarget')).toBeTruthy();
|
expect(watcher.hasTarget('defaultTarget')).toBeTruthy();
|
||||||
expect(watcher.hasTarget('target')).toBeTruthy();
|
expect(watcher.hasTarget('target', 'targetType')).toBeTruthy();
|
||||||
|
|
||||||
watcher.removeTargets('targetType');
|
watcher.removeTargets('targetType');
|
||||||
expect(watcher.hasTarget('defaultTarget')).toBeTruthy();
|
expect(watcher.hasTarget('defaultTarget')).toBeTruthy();
|
||||||
expect(watcher.hasTarget('target')).toBeFalsy();
|
expect(watcher.hasTarget('target', 'targetType')).toBeFalsy();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('collect', () => {
|
test('collect', () => {
|
||||||
@ -141,8 +141,8 @@ describe('Watcher', () => {
|
|||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const target1 = watcher.getTarget('collect_1');
|
const target1 = watcher.getTarget('collect_1', 'target');
|
||||||
const target2 = watcher.getTarget('collect_2');
|
const target2 = watcher.getTarget('collect_2', 'target');
|
||||||
|
|
||||||
expect(target1?.deps?.node_1.name).toBe('node');
|
expect(target1?.deps?.node_1.name).toBe('node');
|
||||||
expect(target2?.deps?.node_1.name).toBe('node');
|
expect(target2?.deps?.node_1.name).toBe('node');
|
||||||
@ -226,7 +226,7 @@ describe('Watcher', () => {
|
|||||||
true,
|
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_1.name).toBe('node');
|
||||||
expect(target1?.deps?.node_2.name).toBe('node2');
|
expect(target1?.deps?.node_2.name).toBe('node2');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user