stub-react-java-application/stub-gui/source/js/KeepAliveWorkerManager.js

38 lines
1.1 KiB
JavaScript

import Rest from './webapi/Rest';
class KeepAliveWorkerManager {
worker = null;
keepAliveResultHandler = null;
init = (keepAliveResultHandler, minutes = 5, url = 'session/keep-alive') => {
if (window.Worker) {
this.keepAliveResultHandler = keepAliveResultHandler;
this.worker = new Worker('libs/KeepAliveWorker.js');
this.worker.postMessage({ type: "KEEP_ALIVE_TEST" });
const fullUrl = Rest.createRestLink(url);
this.worker.postMessage({
type: "START_KEEP_ALIVE",
data: {
url: fullUrl,
minutes
}
});
this.worker.onmessage = this.onmessage.bind(this);
}
}
onmessage = (e) => {
const { type, data } = e.data;
console.log('Message received from [KeepAliveWorker]', e.data);
if (this.keepAliveResultHandler && type === 'KEEP_ALIVE_RESULT') {
this.keepAliveResultHandler(data);
}
}
}
export default new KeepAliveWorkerManager();