@haex-space/vault-sdk 4.7.1 → 4.8.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.
@@ -1596,22 +1596,45 @@ var ModelsAPI = class {
1596
1596
  * `cancelled` with the error. Errors of the start itself (an invalid job, a refused model or
1597
1597
  * consent, no fitting model) reject. The end can arrive before the start answers; it is kept
1598
1598
  * until then.
1599
- */
1600
- async runAsync(job) {
1599
+ *
1600
+ * Aborting `signal` asks to cancel the job, which then ends as `cancelled`; a job that had
1601
+ * already ended keeps its actual end. A signal that is already aborted rejects with its reason
1602
+ * and starts no job. If the cancel request fails, the run rejects with that error and the job
1603
+ * keeps running on the host; if it reports an unknown job, the run rejects with an error.
1604
+ */
1605
+ async runAsync(job, options = {}) {
1606
+ const { signal } = options;
1607
+ if (signal?.aborted) throw signal.reason;
1601
1608
  const ended = /* @__PURE__ */ new Map();
1602
1609
  let waiting = null;
1603
1610
  const unsubscribe = this.onCompleted((completion) => {
1604
1611
  if (waiting && completion.jobId === waiting.jobId) waiting.resolve(completion);
1605
1612
  else ended.set(completion.jobId, completion);
1606
1613
  });
1614
+ let detach = () => {
1615
+ };
1607
1616
  try {
1608
1617
  const { jobId } = await this.startAsync(job);
1609
- return await new Promise((resolve) => {
1618
+ return await new Promise((resolve, reject) => {
1610
1619
  const early = ended.get(jobId);
1611
- if (early) resolve(early);
1612
- else waiting = { jobId, resolve };
1620
+ if (early) return resolve(early);
1621
+ waiting = { jobId, resolve };
1622
+ if (!signal) return;
1623
+ const cancel = async () => {
1624
+ try {
1625
+ if (!await this.cancelAsync(jobId)) {
1626
+ reject(new Error("The model job could not be cancelled"));
1627
+ }
1628
+ } catch (error) {
1629
+ reject(error);
1630
+ }
1631
+ };
1632
+ if (signal.aborted) return cancel();
1633
+ signal.addEventListener("abort", cancel, { once: true });
1634
+ detach = () => signal.removeEventListener("abort", cancel);
1613
1635
  });
1614
1636
  } finally {
1637
+ detach();
1615
1638
  unsubscribe();
1616
1639
  }
1617
1640
  }