Skip to content

Commit eba5caa

Browse files
authored
Merge pull request #24 from antvis/cpu-usage
Cpu usage
2 parents 758f511 + 15b6367 commit eba5caa

9 files changed

Lines changed: 82 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# ChangeLog
2+
#### 0.1.8
3+
4+
- fix: CPU usage increases due to 0.1.3-beta ~ 0.1.3 with publicPath configuration;
5+
- fix: CPU usage increases due to 0.1.6 ~ 0.17 with browser output;
6+
- feat: export fix: export detectAllCycles, detectAllDirectedCycle, detectAllUndirectedCycle;
27

38
#### 0.1.6
49

@@ -8,6 +13,7 @@
813

914
- fix: worker async problem;
1015
- chore: unify allPath and allPaths;
16+
1117
#### 0.1.2
1218

1319
- fix: failed to find result problem in dijkstra;

packages/graph/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@antv/algorithm",
3-
"version": "0.1.6",
3+
"version": "0.1.8",
44
"description": "graph algorithm",
55
"keywords": [
66
"graph",

packages/graph/src/index.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import breadthFirstSearch from './bfs';
33
import connectedComponent from './connected-component';
44
import getDegree from './degree';
55
import { getInDegree, getOutDegree } from './degree';
6-
import detectCycle from './detect-cycle';
6+
import detectCycle, { detectAllCycles, detectAllDirectedCycle, detectAllUndirectedCycle } from './detect-cycle';
77
import depthFirstSearch from './dfs';
88
import dijkstra from './dijkstra';
99
import { findAllPath, findShortestPath } from './find-path';
@@ -16,13 +16,19 @@ import GADDI from './gaddi';
1616
import Stack from './structs/stack';
1717
import { getNeighbors } from './util';
1818

19+
const detectDirectedCycle = detectCycle;
20+
const detectDirectedCycleAsync = detectCycleAsync;
21+
1922
import {
2023
getAdjMatrixAsync,
2124
connectedComponentAsync,
2225
getDegreeAsync,
2326
getInDegreeAsync,
2427
getOutDegreeAsync,
2528
detectCycleAsync,
29+
detectAllCyclesAsync,
30+
detectAllDirectedCycleAsync,
31+
detectAllUndirectedCycleAsync,
2632
dijkstraAsync,
2733
findAllPathAsync,
2834
findShortestPathAsync,
@@ -43,6 +49,10 @@ export {
4349
getInDegree,
4450
getOutDegree,
4551
detectCycle,
52+
detectDirectedCycle,
53+
detectAllCycles,
54+
detectAllDirectedCycle,
55+
detectAllUndirectedCycle,
4656
depthFirstSearch,
4757
dijkstra,
4858
findAllPath,
@@ -61,6 +71,10 @@ export {
6171
getInDegreeAsync,
6272
getOutDegreeAsync,
6373
detectCycleAsync,
74+
detectDirectedCycleAsync,
75+
detectAllCyclesAsync,
76+
detectAllDirectedCycleAsync,
77+
detectAllUndirectedCycleAsync,
6478
dijkstraAsync,
6579
findAllPathAsync,
6680
findShortestPathAsync,
@@ -81,6 +95,10 @@ export default {
8195
getInDegree,
8296
getOutDegree,
8397
detectCycle,
98+
detectDirectedCycle,
99+
detectAllCycles,
100+
detectAllDirectedCycle,
101+
detectAllUndirectedCycle,
84102
depthFirstSearch,
85103
dijkstra,
86104
findAllPath,
@@ -99,6 +117,10 @@ export default {
99117
getInDegreeAsync,
100118
getOutDegreeAsync,
101119
detectCycleAsync,
120+
detectDirectedCycleAsync,
121+
detectAllCyclesAsync,
122+
detectAllDirectedCycleAsync,
123+
detectAllUndirectedCycleAsync,
102124
dijkstraAsync,
103125
findAllPathAsync,
104126
findShortestPathAsync,

packages/graph/src/workers/constant.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ export const ALGORITHM = {
44
connectedComponent: 'connectedComponent',
55
depthFirstSearch: 'depthFirstSearch',
66
detectCycle: 'detectCycle',
7+
detectDirectedCycle: 'detectDirectedCycle',
8+
detectAllCycles: 'detectAllCycles',
9+
detectAllDirectedCycle: 'detectAllDirectedCycle',
10+
detectAllUndirectedCycle: 'detectAllUndirectedCycle',
711
dijkstra: 'dijkstra',
812
findAllPath: 'findAllPath',
913
findShortestPath: 'findShortestPath',

packages/graph/src/workers/createWorker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ const createWorker = <R>(type: string) => (...data) =>
1414
new Promise<R>((resolve, reject) => {
1515
const worker = new Worker();
1616
worker.postMessage({
17-
type,
17+
_algorithmType:type,
1818
data,
1919
});
2020

2121
worker.onmessage = (event: Event) => {
22-
const { data, type } = event.data;
23-
if (MESSAGE.SUCCESS === type) {
22+
const { data, _algorithmType } = event.data;
23+
if (MESSAGE.SUCCESS === _algorithmType) {
2424
resolve(data);
2525
} else {
2626
reject();

packages/graph/src/workers/index.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
GraphData,
33
DegreeType,
44
Matrix,
5-
IAlgorithmCallbacks,
65
ClusterData,
76
EdgeConfig,
87
NodeConfig,
@@ -49,14 +48,41 @@ const getOutDegreeAsync = (graphData: GraphData, nodeId: string) =>
4948
createWorker<DegreeType>(ALGORITHM.getOutDegree)(graphData, nodeId);
5049

5150
/**
52-
* 检测图中的 Cycle
51+
* 检测图中的(有向) Cycle
5352
* @param graphData 图数据
5453
*/
5554
const detectCycleAsync = (graphData: GraphData) =>
5655
createWorker<{
5756
[key: string]: string;
5857
}>(ALGORITHM.detectCycle)(graphData);
5958

59+
/**
60+
* 检测图中的(无向) Cycle
61+
* @param graphData 图数据
62+
*/
63+
const detectAllCyclesAsync = (graphData: GraphData) =>
64+
createWorker<{
65+
[key: string]: string;
66+
}>(ALGORITHM.detectAllCycles)(graphData);
67+
68+
/**
69+
* 检测图中的所有(有向) Cycle
70+
* @param graphData 图数据
71+
*/
72+
const detectAllDirectedCycleAsync = (graphData: GraphData) =>
73+
createWorker<{
74+
[key: string]: string;
75+
}>(ALGORITHM.detectAllDirectedCycle)(graphData);
76+
77+
/**
78+
* 检测图中的所有(无向) Cycle
79+
* @param graphData 图数据
80+
*/
81+
const detectAllUndirectedCycleAsync = (graphData: GraphData) =>
82+
createWorker<{
83+
[key: string]: string;
84+
}>(ALGORITHM.detectAllUndirectedCycle)(graphData);
85+
6086
/**
6187
* Dijkstra's algorithm, See {@link https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm}
6288
* @param graphData 图数据
@@ -211,6 +237,9 @@ export {
211237
getInDegreeAsync,
212238
getOutDegreeAsync,
213239
detectCycleAsync,
240+
detectAllCyclesAsync,
241+
detectAllDirectedCycleAsync,
242+
detectAllUndirectedCycleAsync,
214243
dijkstraAsync,
215244
findAllPathAsync,
216245
findShortestPathAsync,

packages/graph/src/workers/index.worker.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ interface Event {
99
}
1010

1111
ctx.onmessage = (event: Event) => {
12-
const { type, data } = event.data;
13-
if (typeof algorithm[type] === 'function') {
14-
const result = algorithm[type](...data);
15-
ctx.postMessage({ type: MESSAGE.SUCCESS, data: result });
12+
const { _algorithmType, data } = event.data;
13+
// 如果发送内容没有私有类型。说明不是自己发的。不管
14+
// fix: https://github.com/antvis/algorithm/issues/25
15+
if(!_algorithmType){
1616
return;
1717
}
18-
19-
ctx.postMessage({ type: MESSAGE.FAILURE });
18+
if (typeof algorithm[_algorithmType] === 'function') {
19+
const result = algorithm[_algorithmType](...data);
20+
ctx.postMessage({ _algorithmType: MESSAGE.SUCCESS, data: result });
21+
return;
22+
}
23+
ctx.postMessage({ _algorithmType: MESSAGE.FAILURE });
2024
};
2125

2226
// https://stackoverflow.com/questions/50210416/webpack-worker-loader-fails-to-compile-typescript-worker

packages/graph/webpack.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ module.exports = {
1414
globalObject: 'this',
1515
publicPath: './dist',
1616
},
17+
watchOptions: {
18+
ignored: /node_modules/
19+
},
1720
resolve: {
1821
extensions: ['.ts', '.js'],
1922
},

packages/graph/webpack.dev.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = Object.assign(
77
watchOptions: {
88
aggregateTimeout: 300,
99
poll: 1000,
10+
ignored: /node_modules/
1011
},
1112
},
1213
webpackConfig,

0 commit comments

Comments
 (0)