Skip to content

Commit

Permalink
fix(dep): get-tagert需要指定type
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen committed Dec 8, 2023
1 parent e50e332 commit 70f2b11
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
25 changes: 9 additions & 16 deletions packages/dep/src/Watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,28 @@ 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];
}

/**
* 判断是否存在指定id的target
* @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];
}
}

Expand Down
14 changes: 7 additions & 7 deletions packages/dep/tests/Watch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 70f2b11

Please sign in to comment.