Skip to content

Commit 75a4f7f

Browse files
authored
Merge pull request #8 from zuiidea/feature-webworker
图算法webworker支持
2 parents 9203008 + e397427 commit 75a4f7f

23 files changed

+1459
-39
lines changed

packages/graph/.babelrc.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ module.exports = api => {
77
{
88
loose: true,
99
modules: false,
10+
targets: { node: 'current' },
1011
},
1112
],
13+
'@babel/preset-typescript',
1214
{
13-
"plugins": [
14-
"@babel/plugin-proposal-class-properties"
15-
]
16-
}
17-
]
15+
plugins: ['@babel/plugin-proposal-class-properties'],
16+
},
17+
],
1818
};
19-
};
19+
};

packages/graph/.fatherrc.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
export default {
22
entry: './src/index.ts',
33
esm: 'babel',
4-
cjs: 'babel',
5-
umd: {
6-
minFile: true,
7-
file: 'index'
8-
}
4+
cjs: 'babel'
95
};

packages/graph/.prettierrc.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const fabric = require('@umijs/fabric');
2+
3+
module.exports = {
4+
...fabric.prettier,
5+
};

packages/graph/package.json

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,18 @@
2020
"module": "es/index.js",
2121
"browser": "dist/index.min.js",
2222
"scripts": {
23-
"build": "npm run clean && father build",
23+
"build": "npm run clean && father build && npm run build:umd",
24+
"build:umd": "webpack --config webpack.config.js --mode production",
25+
"dev:umd": "webpack --config webpack.dev.config.js --mode development",
2426
"ci": "npm run build && npm run coverage",
2527
"clean": "rimraf es esm lib",
2628
"coverage": "jest --coverage",
2729
"lint": "eslint --ext .js,.jsx,.ts,.tsx --format=pretty \"./\"",
2830
"lint:src": "eslint --ext .ts --format=pretty \"./src\"",
2931
"prettier": "prettier -c --write \"**/*\"",
30-
"test": "jest",
31-
"test-live": "DEBUG_MODE=1 jest --watch ./tests/unit/find-path-spec.ts",
32+
"test": "npm run build:umd && jest",
33+
"test-live": "npm run build:umd && DEBUG_MODE=1 jest --watch ./tests/unit/louvain-spec.ts",
34+
"test-live:async": "npm run build:umd && DEBUG_MODE=1 jest --watch ./tests/unit/*-async-spec.ts",
3235
"lint-staged:js": "eslint --ext .js,.jsx,.ts,.tsx",
3336
"cdn": "antv-bin upload -n @antv/algorithm"
3437
},
@@ -43,16 +46,23 @@
4346
"license": "MIT",
4447
"author": "https://github.com/orgs/antvis/people",
4548
"devDependencies": {
46-
"@babel/core": "^7.12.9",
49+
"@babel/core": "^7.12.10",
4750
"@babel/plugin-proposal-class-properties": "^7.12.1",
4851
"@babel/preset-env": "^7.12.7",
52+
"@babel/preset-typescript": "^7.12.7",
4953
"@types/jest": "^26.0.18",
54+
"@umijs/fabric": "^2.5.6",
55+
"babel-loader": "^8.2.2",
5056
"father": "^2.30.0",
5157
"jest": "^26.6.3",
5258
"jest-electron": "^0.1.11",
5359
"rimraf": "^3.0.2",
5460
"ts-jest": "^26.4.4",
61+
"ts-loader": "^8.0.14",
5562
"tslint": "^6.1.3",
56-
"typescript": "^4.1.2"
63+
"typescript": "^4.1.3",
64+
"webpack": "^5.17.0",
65+
"webpack-cli": "^4.4.0",
66+
"worker-loader": "^3.0.7"
5767
}
5868
}

packages/graph/src/index.ts

Lines changed: 103 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,103 @@
1-
export { default as getAdjMatrix } from './adjacent-matrix'
2-
export { default as breadthFirstSearch } from './bfs'
3-
export { default as connectedComponent } from './connected-component'
4-
export { default as getDegree } from './degree'
5-
export { getInDegree, getOutDegree } from './degree'
6-
export { default as detectCycle } from './detect-cycle'
7-
export { default as depthFirstSearch } from './dfs'
8-
export { default as dijkstra } from './dijkstra'
9-
export { findAllPath, findShortestPath } from './find-path'
10-
export { default as floydWarshall } from './floydWarshall'
11-
export { default as labelPropagation } from './label-propagation'
12-
export { default as louvain } from './louvain'
13-
export { default as minimumSpanningTree } from './mts'
14-
export { default as pageRank } from './pageRank'
15-
export { getNeighbors } from './util'
16-
export { default as Stack } from './structs/stack'
1+
import getAdjMatrix from './adjacent-matrix';
2+
import breadthFirstSearch from './bfs';
3+
import connectedComponent from './connected-component';
4+
import getDegree from './degree';
5+
import { getInDegree, getOutDegree } from './degree';
6+
import detectCycle from './detect-cycle';
7+
import depthFirstSearch from './dfs';
8+
import dijkstra from './dijkstra';
9+
import { findAllPath, findShortestPath } from './find-path';
10+
import floydWarshall from './floydWarshall';
11+
import labelPropagation from './label-propagation';
12+
import louvain from './louvain';
13+
import minimumSpanningTree from './mts';
14+
import pageRank from './pageRank';
15+
import { getNeighbors } from './util';
16+
import Stack from './structs/stack';
17+
import {
18+
getAdjMatrixAsync,
19+
connectedComponentAsync,
20+
getDegreeAsync,
21+
getInDegreeAsync,
22+
getOutDegreeAsync,
23+
detectCycleAsync,
24+
dijkstraAsync,
25+
findAllPathAsync,
26+
findShortestPathAsync,
27+
floydWarshallAsync,
28+
labelPropagationAsync,
29+
louvainAsync,
30+
minimumSpanningTreeAsync,
31+
pageRankAsync,
32+
getNeighborsAsync,
33+
} from './workers/index';
34+
35+
export {
36+
getAdjMatrix,
37+
breadthFirstSearch,
38+
connectedComponent,
39+
getDegree,
40+
getInDegree,
41+
getOutDegree,
42+
detectCycle,
43+
depthFirstSearch,
44+
dijkstra,
45+
findAllPath,
46+
findShortestPath,
47+
floydWarshall,
48+
labelPropagation,
49+
louvain,
50+
minimumSpanningTree,
51+
pageRank,
52+
getNeighbors,
53+
getAdjMatrixAsync,
54+
connectedComponentAsync,
55+
getDegreeAsync,
56+
getInDegreeAsync,
57+
getOutDegreeAsync,
58+
detectCycleAsync,
59+
dijkstraAsync,
60+
findAllPathAsync,
61+
findShortestPathAsync,
62+
floydWarshallAsync,
63+
labelPropagationAsync,
64+
louvainAsync,
65+
minimumSpanningTreeAsync,
66+
pageRankAsync,
67+
getNeighborsAsync,
68+
};
69+
70+
export default {
71+
getAdjMatrix,
72+
breadthFirstSearch,
73+
connectedComponent,
74+
getDegree,
75+
getInDegree,
76+
getOutDegree,
77+
detectCycle,
78+
depthFirstSearch,
79+
dijkstra,
80+
findAllPath,
81+
findShortestPath,
82+
floydWarshall,
83+
labelPropagation,
84+
louvain,
85+
minimumSpanningTree,
86+
pageRank,
87+
getNeighbors,
88+
getAdjMatrixAsync,
89+
connectedComponentAsync,
90+
getDegreeAsync,
91+
getInDegreeAsync,
92+
getOutDegreeAsync,
93+
detectCycleAsync,
94+
dijkstraAsync,
95+
findAllPathAsync,
96+
findShortestPathAsync,
97+
floydWarshallAsync,
98+
labelPropagationAsync,
99+
louvainAsync,
100+
minimumSpanningTreeAsync,
101+
pageRankAsync,
102+
getNeighborsAsync,
103+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export { default as getAdjMatrix } from '../adjacent-matrix';
2+
export { default as breadthFirstSearch } from '../bfs';
3+
export { default as connectedComponent } from '../connected-component';
4+
export { default as getDegree } from '../degree';
5+
export { getInDegree, getOutDegree } from '../degree';
6+
export { default as detectCycle } from '../detect-cycle';
7+
export { default as depthFirstSearch } from '../dfs';
8+
export { default as dijkstra } from '../dijkstra';
9+
export { findAllPath, findShortestPath } from '../find-path';
10+
export { default as floydWarshall } from '../floydWarshall';
11+
export { default as labelPropagation } from '../label-propagation';
12+
export { default as louvain } from '../louvain';
13+
export { default as minimumSpanningTree } from '../mts';
14+
export { default as pageRank } from '../pageRank';
15+
export { getNeighbors } from '../util';
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export const ALGORITHM = {
2+
pageRank: 'pageRank',
3+
breadthFirstSearch: 'breadthFirstSearch',
4+
connectedComponent: 'connectedComponent',
5+
depthFirstSearch: 'depthFirstSearch',
6+
detectCycle: 'detectCycle',
7+
dijkstra: 'dijkstra',
8+
findAllPath: 'findAllPath',
9+
findShortestPath: 'findShortestPath',
10+
floydWarshall: 'floydWarshall',
11+
getAdjMatrix: 'getAdjMatrix',
12+
getDegree: 'getDegree',
13+
getInDegree: 'getInDegree',
14+
getNeighbors: 'getNeighbors',
15+
getOutDegree: 'getOutDegree',
16+
labelPropagation: 'labelPropagation',
17+
louvain: 'louvain',
18+
minimumSpanningTree: 'minimumSpanningTree',
19+
SUCCESS: 'SUCCESS',
20+
FAILURE: 'FAILURE',
21+
};
22+
23+
export const MESSAGE = {
24+
SUCCESS: 'SUCCESS',
25+
FAILURE: 'FAILURE',
26+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { MESSAGE } from './constant';
2+
3+
interface Event {
4+
type: string;
5+
data: any;
6+
}
7+
8+
/**
9+
* 创建一个在worker中运行的算法
10+
* @param type 算法类型
11+
*/
12+
const createWorker = <R>(type: string) => (...data) =>
13+
new Promise<R>((resolve, reject) => {
14+
import('./index.worker').then(Worker => {
15+
const worker = new Worker.default();
16+
17+
worker.postMessage({
18+
type,
19+
data,
20+
});
21+
22+
worker.onmessage = (event: Event) => {
23+
const { data, type } = event.data;
24+
if (MESSAGE.SUCCESS === type) {
25+
resolve(data);
26+
} else {
27+
reject();
28+
}
29+
30+
worker.terminate();
31+
};
32+
});
33+
});
34+
35+
export default createWorker;

0 commit comments

Comments
 (0)