mobx-downloader 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ReadMe.md +185 -0
- package/dist/index.d.ts +53 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.esm.js +861 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +76 -0
- package/source/HTTP.ts +58 -0
- package/source/Task.ts +127 -0
- package/source/index.ts +73 -0
- package/tsconfig.json +22 -0
package/source/index.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { computed, observable } from 'mobx';
|
|
2
|
+
import { persist, restore } from 'mobx-restful';
|
|
3
|
+
import { Constructor } from 'web-utility';
|
|
4
|
+
|
|
5
|
+
import { HTTPDownloadTask } from './HTTP';
|
|
6
|
+
import { DownloadTask } from './Task';
|
|
7
|
+
|
|
8
|
+
export * from './HTTP';
|
|
9
|
+
export * from './Task';
|
|
10
|
+
|
|
11
|
+
export class Downloader {
|
|
12
|
+
static protocolMap: Record<string, Constructor<DownloadTask>> = {
|
|
13
|
+
http: HTTPDownloadTask,
|
|
14
|
+
https: HTTPDownloadTask
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
static createTask(path: string, name?: string) {
|
|
18
|
+
const [protocol] = path.split(':');
|
|
19
|
+
const ProtocolTask = Downloader.protocolMap[protocol];
|
|
20
|
+
|
|
21
|
+
if (!ProtocolTask)
|
|
22
|
+
throw new URIError(
|
|
23
|
+
`Protocol "${protocol} has not been registered"`
|
|
24
|
+
);
|
|
25
|
+
return new ProtocolTask(path, name);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
constructor() {
|
|
29
|
+
restore(this, 'downloader');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
@persist({
|
|
33
|
+
set: tasks => tasks.map(({ name, path }) => ({ name, path })),
|
|
34
|
+
get: list =>
|
|
35
|
+
list?.map(({ name, path }) => Downloader.createTask(path, name))
|
|
36
|
+
})
|
|
37
|
+
@observable
|
|
38
|
+
accessor tasks: DownloadTask[] = [];
|
|
39
|
+
|
|
40
|
+
@computed
|
|
41
|
+
get unfinishedCount() {
|
|
42
|
+
return this.tasks.filter(({ percent }) => percent < 100).length;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
@computed
|
|
46
|
+
get executingCount() {
|
|
47
|
+
return this.tasks.filter(({ executing }) => executing).length;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
createTask(path: string, name?: string) {
|
|
51
|
+
const { tasks } = this;
|
|
52
|
+
const sameTask = tasks.find(task => task.path === path);
|
|
53
|
+
|
|
54
|
+
if (sameTask) return sameTask;
|
|
55
|
+
|
|
56
|
+
const task = Downloader.createTask(path, name);
|
|
57
|
+
|
|
58
|
+
this.tasks = [...tasks, task];
|
|
59
|
+
|
|
60
|
+
return task;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async destroyTask(name: string) {
|
|
64
|
+
const { tasks } = this;
|
|
65
|
+
const index = tasks.findIndex(({ name: N }) => N === name);
|
|
66
|
+
|
|
67
|
+
if (index < 0) throw new ReferenceError(`${name} isn't found`);
|
|
68
|
+
|
|
69
|
+
await tasks[index].destroy();
|
|
70
|
+
|
|
71
|
+
this.tasks = [...tasks.slice(0, index), ...tasks.slice(index + 1)];
|
|
72
|
+
}
|
|
73
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES6",
|
|
4
|
+
"module": "UMD",
|
|
5
|
+
"moduleResolution": "Node",
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"downlevelIteration": true,
|
|
8
|
+
"useDefineForClassFields": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"lib": ["ESNext", "DOM", "DOM.Iterable"],
|
|
11
|
+
"declaration": true,
|
|
12
|
+
"outDir": "dist"
|
|
13
|
+
},
|
|
14
|
+
"include": ["source/*"],
|
|
15
|
+
"typedocOptions": {
|
|
16
|
+
"name": "MobX downloader",
|
|
17
|
+
"excludeExternals": true,
|
|
18
|
+
"excludePrivate": true,
|
|
19
|
+
"readme": "./ReadMe.md",
|
|
20
|
+
"plugin": ["typedoc-plugin-mdn-links"]
|
|
21
|
+
}
|
|
22
|
+
}
|