Skip to content

Commit

Permalink
依赖关系调整
Browse files Browse the repository at this point in the history
  • Loading branch information
pearone committed Jan 2, 2024
1 parent aead030 commit 27c417b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 42 deletions.
14 changes: 13 additions & 1 deletion packages/event/__tests__/dag.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import DAG from '../src/graph';

test('dag', () => {
const dag = new DAG({
nodes: [1, 2, 3, 4, 5],
nodes: [1, 2, 3, 4, 5, 6],
links: [
{ parent_id: 1, child_id: 2, type: 'link' },
{ parent_id: 2, child_id: 3, type: 'drill' },
Expand All @@ -17,11 +17,23 @@ test('dag', () => {
}
});

/**
* 1 => 2
* 2 => 3,4
* 3 => 1,
* 4 => 5,6
*/
console.log(dag.children);

// true
console.log(dag.hasCycle());
// 5, 4, 2, 1, 3
console.log(dag.findParents(5));
// 4, 2, 1, 3
console.log(dag.findParents(5, { include_self: false }));
// 4, 5, 6
console.log(dag.findChildren(4));
// 5, 6
console.log(dag.findChildren(4, { include_self: false }));
dag.removeNode(4);
console.log(dag.children, dag.parents);
Expand Down
4 changes: 2 additions & 2 deletions packages/event/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dpdfe/event-utils",
"version": "0.0.30",
"version": "0.0.32",
"description": "通用方法",
"author": "pearone",
"homepage": "https://github.com/DPDFE/react-layout/tree/main/packages/event",
Expand All @@ -24,7 +24,7 @@
"dev": "tsc -w",
"build": "rm -rf dist && tsc --build",
"test": "jest",
"test:single": "jest ./__tests__/community/epoll_queue.test.ts",
"test:single": "jest ./__tests__/dag.test.ts",
"prepublishOnly": "npm run build"
},
"bugs": {
Expand Down
31 changes: 11 additions & 20 deletions packages/event/src/graph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ export const DEFAULT_END = 'end';
* 可以帮助快速找到当前节点的父节点、子节点、全量父节点、全量子节点,
* 处理删除和添加关系时候的同步逻辑、判断成环。
*/
class DAG {
class DAG<T extends OriginLink> {
/** 所有节点 */
nodes: Set<OriginGraphNodeType> = new Set();

/** 所有关系 */
links: OriginLink[] = [];
links: T[] = [];

link_key: OriginKey = { start: DEFAULT_START, end: DEFAULT_END };

Expand All @@ -56,17 +56,13 @@ class DAG {
link_key
}: {
nodes: OriginGraphNodeType[];
links: OriginLink[];
links: T[];
link_key: OriginKey;
}) {
this.init(nodes, links, link_key);
}

init(
nodes: OriginGraphNodeType[],
links: OriginLink[],
link_key: OriginKey
) {
init(nodes: OriginGraphNodeType[], links: T[], link_key: OriginKey) {
this.fillNodes(nodes);
this.fillLinks(links, link_key);
}
Expand All @@ -84,7 +80,7 @@ class DAG {
}

/** 填充link */
fillLinks(links: OriginLink[], link_key: OriginKey) {
fillLinks(links: T[], link_key: OriginKey) {
this.links = links;
if (link_key) {
this.link_key = link_key;
Expand Down Expand Up @@ -119,18 +115,13 @@ class DAG {
}

/** 添加一条边 */
addLink(
start: OriginGraphNodeType,
end: OriginGraphNodeType,
data: OriginLink
) {
addLink(start: OriginGraphNodeType, end: OriginGraphNodeType, data: T) {
this._children.addLink(start, end, data);
this._parents.addLink(end, start, data);
}

/** 删除一条边 */
removeLink(start: OriginGraphNodeType, end: OriginGraphNodeType) {
console.log(start, end);
this._children.removeLink(start, end);
this._parents.removeLink(end, start);
}
Expand Down Expand Up @@ -176,7 +167,7 @@ class DAG {
const children = this._children.links.get(node);

if (children) {
for (const child of children.target.values()) {
for (const child of children.keys()) {
if (this.detectCycle(child, visited, stack)) {
return true;
}
Expand Down Expand Up @@ -223,10 +214,10 @@ class DAG {
visited.add(node);
stack.add(node);

const children = this._children.links.get(node);
const children = this.children.get(node);

if (children) {
for (const parent of children.target.values()) {
for (const parent of children.keys()) {
if (this.detectChild(parent, visited, stack)) {
return true;
}
Expand Down Expand Up @@ -273,10 +264,10 @@ class DAG {
visited.add(node);
stack.add(node);

const parents = this._parents.links.get(node);
const parents = this.parents.get(node);

if (parents) {
for (const parent of parents.target.values()) {
for (const parent of parents.keys()) {
if (this.detectParent(parent, visited, stack)) {
return true;
}
Expand Down
29 changes: 11 additions & 18 deletions packages/event/src/graph/link.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import { OriginGraphNodeType, OriginLink } from '.';

class Link {
links: Map<
OriginGraphNodeType,
{
target: Set<OriginGraphNodeType>;
data: Record<OriginGraphNodeType, OriginLink>;
}
> = new Map();
links: Map<OriginGraphNodeType, Map<OriginGraphNodeType, OriginLink>> =
new Map();

addNode(node: OriginGraphNodeType) {
if (!this.links.get(node)) {
this.links.set(node, {
target: new Set<OriginGraphNodeType>(),
data: {}
});
this.links.set(node, new Map());
}
}

Expand All @@ -27,18 +19,19 @@ class Link {
end: OriginGraphNodeType,
data: OriginLink
) {
const link = this.links.get(start);
if (link && this.links.get(end)) {
link.target.add(end);
link.data[end] = data;
const start_link = this.links.get(start);
const end_link = this.links.get(end);
if (start_link && end_link) {
start_link.set(end, data);
} else {
console.warn(`有未知节点: ${start}${end}`);
}
}
removeLink(start: OriginGraphNodeType, end: OriginGraphNodeType) {
const link = this.links.get(start);
if (link && this.links.get(end)) {
link.target.delete(end);
const start_link = this.links.get(start);
const end_link = this.links.get(end);
if (start_link && end_link) {
start_link.delete(end);
} else {
console.warn(`有未知节点: ${start}${end}`);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/event/src/lifecycle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** 这是一个生命周期Hooks
/** 这是一个生命周期Hooks 状态机管理
*
*/

Expand Down

0 comments on commit 27c417b

Please sign in to comment.