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