Skip to content

Commit

Permalink
feat(task): 新的 Task 准备替换旧的 Task
Browse files Browse the repository at this point in the history
  • Loading branch information
KonghaYao committed Aug 2, 2021
1 parent 7d07804 commit 8b22238
Show file tree
Hide file tree
Showing 19 changed files with 489 additions and 157 deletions.
File renamed without changes.
14 changes: 14 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# JSpider 项目看板

JSpider 项目看板中主要有很多关于 JSpider 的未来目标

### Todo

- [ ]

### In Progress


### Done ✓


6 changes: 3 additions & 3 deletions VERSION.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@

- 完整的 JSpider 编程逻辑框架出图

- [ ] JSpider 中的数据流系统
- [x] JSpider 中的数据流系统

- [ ] JSpider 中的事件系统
- [x] JSpider 中的事件系统

- [ ] JSpider 需要实现 MVVM 模型来实现爬虫系统的可视化

- 重构所有的核心代码

- [x] Control Panel 暂停功能

- [ ] Control Panel 重试功能(非流内自动重试)
- [x] Control Panel 重试功能(非流内自动重试)

- 重构 plugins
- [ ] plugins 分包导入
Expand Down
10 changes: 5 additions & 5 deletions src/index.js → index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* Copyright 2021 KonghaYao 江夏尧 <dongzhongzhidong@qq.com>
* SPDX-License-Identifier: Apache-2.0
*/
import { Spider } from './Spider/index';
import * as plugins from '../plugins/index.js';
import * as tools from '../tools/index.js'; // 工具都是 $ 开头的函数
import { Plugin } from './Pipeline/PluginSystem';
import { Task, TaskGroup } from './TaskSystem/index';
import { Spider } from '@src/Spider/index';
import * as plugins from '@plugins/index.js';
import * as tools from '@tools/index.js'; // 工具都是 $ 开头的函数
import { Plugin } from '@src/Pipeline/PluginSystem';
import { Task, TaskGroup } from '@src/TaskSystem/index';
export default Object.assign(Spider, tools, {
plugins,
Plugin,
Expand Down
160 changes: 160 additions & 0 deletions jspider.drawio

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
"chalk": "^4.1.1",
"consola": "^2.15.3",
"lodash-es": "^4.17.21",
"mobx": "^6.3.2",
"mobx-state-tree": "^5.0.2",
"mockjs": "^1.1.0",
"rxjs": "6.6.6",
"uuid": "^8.3.2"
Expand Down
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions rollup.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ export default {
resolve({
browser: true,
}),
// babel({
// exclude: 'node_modules/**', // 仅仅转译我们的源码
// include: 'src',
// }),
babel({
exclude: 'node_modules/**', // 仅仅转译我们的源码
include: 'src',
babelHelpers: 'bundled',
}),
commonjs(), // 将 CommonJS 转换成 ES2015 模块供 Rollup 处理
livereload({ watch: 'dist' }),
serve({
Expand Down
4 changes: 2 additions & 2 deletions src/ControlPanel/ControlPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { pauseToggle } from '../utils/pauseToggle';

export class ControlPanel {
state = 'free'; // 'free' 'preparing'
#runningQueue = new functionQueue(); // init 阶段的 Queue 队列
#runningQueue = new functionQueue(); // 异步 Queue 队列
_stop = false; // 用于直接切断 spiderSource$ 的流
spiderSource$ = null;
_pipeline = null;
Expand Down Expand Up @@ -61,7 +61,7 @@ export class ControlPanel {
}
}

// startInfo --TaskManager.createTask--> Task --emit--EventHub--> Flow
// |startInfo| - TaskManager.createTask -> |Task| - emit EventHub -> |Flow|
createFlow(infos) {
return this.#runningQueue.enQueue(() => {
infos.forEach((info) => {
Expand Down
14 changes: 7 additions & 7 deletions src/TaskSystem/StaticEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
// 这些事件一般通过 $commit(eventName,payload) 进行执行
// ! this 被绑定为 Task 的实例
export const staticEvent = {
start(UUID) {
start(uuid) {
this._status = 'busy';
// ! 这里会有一个清空这个 key 的 value 的情况,这是因为 start 是强制的
this._progress.set(UUID, { process: 'start' });
this._progress.set(uuid, { uuid, process: 'start' });

return this._output || this._originData;
},
success(output, UUID) {
success(output, uuid) {
this._status = 'free';
this._progress.set(UUID, { process: 'success', output });
this._progress.set(uuid, { uuid, process: 'success', output });
this._output = output;
},
complete(UUID) {
complete(uuid) {
this._status = 'complete';
},
error(err, UUID) {
error(err, uuid) {
this._status = 'error';
this._progress.set(UUID, { process: 'error', err });
this._progress.set(uuid, { uuid, process: 'error', err });
},
destroy() {
this._process = [];
Expand Down
123 changes: 0 additions & 123 deletions src/jspider.drawio

This file was deleted.

40 changes: 40 additions & 0 deletions test/TaskState/Task.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { staticEvent } from './TaskEvent.js';
import { createTaskStore } from './TaskState.js';
export class Task {
_belongTo = null; // 当有 TaskGroup 时,指向 Group
constructor(_originData, _spiderUUID) {
this.$EventHub = new EventHub(staticEvent, this);

// 由 store 验证相关的正确性
this.$store = createTaskStore({ spiderUUID, originData });
}

// Plugin 的汇报口
$commit(type, ...payload) {
const result = this.$store[type](...payload);
this.$EventHub.emit(type, ...payload);
return result;
}

// 外部系统的监控口
$on(...args) {
return this.$EventHub.on(...args);
}
$off(...args) {
return this.$EventHub.off(...args);
}
$isSameTask(task) {
return task.$store.spiderUUID === this.$store.spiderUUID && task.$store.uuid === this.$store.uuid;
}
$checkRepeat(uuid) {
return this.$store.dataSlide.includes(uuid);
}
$destroy() {
this._belongTo = null;
this.$off('*');
this.$commit('destroy'); // 通知外部,该 Task 被销毁
}
get [Symbol.toStringTag]() {
return 'Task';
}
}
21 changes: 21 additions & 0 deletions test/TaskState/TaskProps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @license
* Copyright 2021 KonghaYao 江夏尧 <dongzhongzhidong@qq.com>
* SPDX-License-Identifier: Apache-2.0
*/
import { v4 as uuidv4 } from 'uuid';
export function createProps({ spiderUUID, originData }) {
// 创建初始化的参数
return {
uuid: uuidv4(),
spiderUUID, // JSpider 的实例的 UUID
createdAt: new Date(),
error: '',
status: 'free', // 这个位置是为了让 Plugin 能识别的一个标识
updatedAt: new Date(),
dataSlideUUID: spiderUUID,
dataSlide: [],
originData,
output: null, // 每个中间件传出的数据
};
}
Loading

0 comments on commit 8b22238

Please sign in to comment.