|
| 1 | +/* ************************************************************************ |
| 2 | +
|
| 3 | + osparc - the simcore frontend |
| 4 | +
|
| 5 | + https://osparc.io |
| 6 | +
|
| 7 | + Copyright: |
| 8 | + 2025 IT'IS Foundation, https://itis.swiss |
| 9 | +
|
| 10 | + License: |
| 11 | + MIT: https://opensource.org/licenses/MIT |
| 12 | +
|
| 13 | + Authors: |
| 14 | + * Odei Maiz (odeimaiz) |
| 15 | +
|
| 16 | +************************************************************************ */ |
| 17 | + |
| 18 | +qx.Class.define("osparc.store.StreamTasks", { |
| 19 | + extend: qx.core.Object, |
| 20 | + type: "singleton", |
| 21 | + |
| 22 | + properties: { |
| 23 | + tasks: { |
| 24 | + check: "Object", |
| 25 | + init: {}, |
| 26 | + nullable: false, |
| 27 | + } |
| 28 | + }, |
| 29 | + |
| 30 | + members: { |
| 31 | + createStreamTask: function(streamPromise, interval) { |
| 32 | + return new Promise((resolve, reject) => { |
| 33 | + streamPromise |
| 34 | + .then(streamData => { |
| 35 | + if ("status_href" in streamData) { |
| 36 | + const task = this.__addTask(streamData, interval); |
| 37 | + resolve(task); |
| 38 | + } else { |
| 39 | + throw Error("Status missing"); |
| 40 | + } |
| 41 | + }) |
| 42 | + .catch(err => reject(err)); |
| 43 | + }); |
| 44 | + }, |
| 45 | + |
| 46 | + __removeTask: function(task) { |
| 47 | + const tasks = this.getTasks(); |
| 48 | + const index = tasks.findIndex(t => t.getTaskId() === task.getTaskId()); |
| 49 | + if (index > -1) { |
| 50 | + tasks.splice(index, 1); |
| 51 | + } |
| 52 | + }, |
| 53 | + |
| 54 | + __addTask: function(streamData, interval) { |
| 55 | + const tasks = this.getTasks(); |
| 56 | + if (streamData["task_id"] in tasks) { |
| 57 | + return tasks[streamData["task_id"]]; |
| 58 | + } |
| 59 | + |
| 60 | + const stream = new osparc.data.StreamTask(streamData, interval); |
| 61 | + stream.addListener("resultReceived", () => this.__removeTask(stream), this); |
| 62 | + stream.addListener("taskAborted", () => this.__removeTask(stream), this); |
| 63 | + tasks[stream.getTaskId()] = stream; |
| 64 | + return stream; |
| 65 | + }, |
| 66 | + |
| 67 | + fetchStream: function(taskId) { |
| 68 | + const tasks = this.getTasks(); |
| 69 | + if (taskId in tasks) { |
| 70 | + const task = tasks[taskId]; |
| 71 | + task.fetchStream(); |
| 72 | + } |
| 73 | + }, |
| 74 | + } |
| 75 | +}); |
0 commit comments