34 lines
1.1 KiB
JavaScript
34 lines
1.1 KiB
JavaScript
import { Manager } from "socket.io-client";
|
|
import modes from "../modes.js";
|
|
import events from "../events.js";
|
|
|
|
export { default as events } from "../events.js";
|
|
export { default as modes } from "../modes.js";
|
|
|
|
export default class Initiator {
|
|
constructor(url, options = {}) {
|
|
this.url = url;
|
|
this.mode = modes.INIT;
|
|
this.onLog = options.onLog ?? ((d) => console.log(`job: ${d}`));
|
|
this.onClose = options.onClose ?? (() => {});
|
|
this.onCreate = options.onCreate ?? ((id) => console.log(`job id: ${id}`));
|
|
}
|
|
|
|
async newJob(jobRequest, onLog, onClose, onCreate) {
|
|
const mgr = new Manager(this.url, {
|
|
query: { mode: this.mode, job: JSON.stringify(jobRequest) },
|
|
});
|
|
onLog = onLog ?? this.onLog.bind(this);
|
|
onClose = onClose ?? this.onClose.bind(this);
|
|
onCreate = onCreate ?? this.onCreate.bind(this);
|
|
const sk = mgr.socket("/");
|
|
sk.on(events.JOB_LOG, onLog);
|
|
sk.on(events.JOB_CLS, onClose);
|
|
return new Promise((res) =>
|
|
sk.on(events.JOB_CRT, function onJobCreate(id) {
|
|
onCreate(id);
|
|
res({ ...jobRequest, id });
|
|
})
|
|
);
|
|
}
|
|
}
|