fetchfox-sdk 1.0.27 → 1.0.29
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/package.json +1 -1
- package/src/detach.js +20 -76
package/package.json
CHANGED
package/src/detach.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { io } from 'socket.io-client';
|
|
2
1
|
import { ws, appHost } from './configure.js';
|
|
3
2
|
import { jobs } from './jobs.js';
|
|
4
3
|
|
|
@@ -10,9 +9,8 @@ export function getSocket() {}
|
|
|
10
9
|
|
|
11
10
|
export const Job = class {
|
|
12
11
|
#callbacks;
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
#socket3;
|
|
12
|
+
#wsUpdates;
|
|
13
|
+
#wsChildren;
|
|
16
14
|
#seen;
|
|
17
15
|
#completed;
|
|
18
16
|
#failed;
|
|
@@ -33,19 +31,33 @@ export const Job = class {
|
|
|
33
31
|
};
|
|
34
32
|
|
|
35
33
|
this.#seen = {};
|
|
36
|
-
this.#
|
|
37
|
-
this.#
|
|
34
|
+
this.#wsUpdates = new WebSocket(ws(options) + '/ws/jobs/' + id);
|
|
35
|
+
this.#wsUpdates.onmessage = (data) => {
|
|
38
36
|
let parsed;
|
|
39
37
|
try {
|
|
40
38
|
parsed = JSON.parse(data.data);
|
|
41
39
|
} catch {
|
|
42
40
|
return;
|
|
43
41
|
}
|
|
42
|
+
|
|
43
|
+
for (const key of Object.keys(parsed)) {
|
|
44
|
+
this[key] = parsed[key];
|
|
45
|
+
}
|
|
46
|
+
|
|
44
47
|
this.trigger('update', parsed);
|
|
48
|
+
|
|
49
|
+
if (!this.#completed && parsed.state == 'completed') {
|
|
50
|
+
this.#completed = true;
|
|
51
|
+
this.trigger('finished', parsed);
|
|
52
|
+
}
|
|
53
|
+
if (!this.#failed && parsed.state == 'failed') {
|
|
54
|
+
this.#failed = true;
|
|
55
|
+
this.trigger('finished', parsed);
|
|
56
|
+
}
|
|
45
57
|
};
|
|
46
58
|
|
|
47
|
-
this.#
|
|
48
|
-
this.#
|
|
59
|
+
this.#wsChildren = new WebSocket(ws(options) + '/ws/children/' + id);
|
|
60
|
+
this.#wsChildren.onmessage = (data) => {
|
|
49
61
|
let parsed;
|
|
50
62
|
try {
|
|
51
63
|
parsed = JSON.parse(data.data);
|
|
@@ -85,74 +97,6 @@ export const Job = class {
|
|
|
85
97
|
return s;
|
|
86
98
|
}
|
|
87
99
|
|
|
88
|
-
updateFromData(data) {
|
|
89
|
-
const s = this.#select(data);
|
|
90
|
-
for (const key of Object.keys(s)) {
|
|
91
|
-
this[key] = s[key];
|
|
92
|
-
}
|
|
93
|
-
if (['completed', 'failed'].includes(this.state)) {
|
|
94
|
-
if (this.progress?.children?.jobs) {
|
|
95
|
-
this.progress.children.jobs = this.progress.children.jobs.filter(
|
|
96
|
-
(it) => it.state != 'active'
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
async get() {
|
|
103
|
-
const data = await jobs.get(this.id);
|
|
104
|
-
this.updateFromData(data);
|
|
105
|
-
return this;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
handleProgress(data) {
|
|
109
|
-
const last = JSON.stringify(this);
|
|
110
|
-
|
|
111
|
-
this.updateFromData(data);
|
|
112
|
-
|
|
113
|
-
const didUpdate = JSON.stringify(this) != last;
|
|
114
|
-
if (didUpdate) {
|
|
115
|
-
this.trigger('progress', this);
|
|
116
|
-
|
|
117
|
-
for (const item of this.results?.items || []) {
|
|
118
|
-
const ser = JSON.stringify(item);
|
|
119
|
-
if (this.#seen[ser]) {
|
|
120
|
-
continue;
|
|
121
|
-
}
|
|
122
|
-
this.#seen[ser] = true;
|
|
123
|
-
this.trigger('item', item);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
if (this.state == 'completed') {
|
|
127
|
-
this.#completed = true;
|
|
128
|
-
}
|
|
129
|
-
if (this.state == 'failed') {
|
|
130
|
-
this.#failed = true;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
if (['completed', 'failed'].includes(this.state)) {
|
|
135
|
-
console.log('Got state:', this.state);
|
|
136
|
-
|
|
137
|
-
if (this.progress?.children?.jobs) {
|
|
138
|
-
this.progress.children.jobs = this.progress.children.jobs.filter(
|
|
139
|
-
(it) => it.state != 'active'
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
console.log('Set timeout before triggering completion');
|
|
144
|
-
setTimeout(() => {
|
|
145
|
-
this.get()
|
|
146
|
-
.then(() => {
|
|
147
|
-
this.trigger('progress', this);
|
|
148
|
-
this.trigger(this.state, this);
|
|
149
|
-
this.trigger('finished', this);
|
|
150
|
-
})
|
|
151
|
-
.catch((e) => console.error(e));
|
|
152
|
-
}, 500);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
100
|
checkEvent(event) {
|
|
157
101
|
if (!this.#callbacks[event]) {
|
|
158
102
|
throw new FetchFoxError(`Invalid event: ${event}`);
|