@quazardous/qdadm 2.47.0 → 2.48.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.
@@ -72,6 +72,13 @@ export interface TaskKind {
72
72
  slots?: TaskUiOptions['slots']
73
73
  /** A dialog of its own for this kind, instead of TaskDialog. */
74
74
  component?: TaskUiOptions['component']
75
+ /**
76
+ * Start a task of this kind from its meta alone. With it, `run({ kind, meta })`
77
+ * needs no `start`, and a sequence of such kinds goes on after a reload (#4934).
78
+ */
79
+ start?: (meta: Record<string, unknown> | null) => Promise<unknown>
80
+ /** A run's line in a sequence's list (#4934). Default: the error, else the step. */
81
+ summary?: (run: TaskRun) => string | null | undefined
75
82
  }
76
83
 
77
84
  export interface TaskConfirm {
@@ -84,11 +91,15 @@ export interface TaskConfirm {
84
91
  severity?: 'primary' | 'secondary' | 'success' | 'info' | 'warn' | 'danger' | 'contrast'
85
92
  }
86
93
 
87
- export interface RunOptions extends Omit<Partial<TaskKind>, 'label'> {
94
+ export interface RunOptions extends Omit<Partial<TaskKind>, 'label' | 'start'> {
88
95
  /** A string, or a function read each time it shows (tray, dialog, toast), so it follows the locale (#4692) */
89
96
  label: string | (() => string)
90
- /** Queue the task; returns its id, or `{ task_id | taskId | id }`. */
91
- start: () => Promise<unknown>
97
+ /**
98
+ * Queue the task; returns its id, or `{ task_id | taskId | id }`. In a
99
+ * sequence it gets the previous step's run (#4934). Optional when the kind
100
+ * has a `start`.
101
+ */
102
+ start?: (previous: TaskRun | null) => Promise<unknown>
92
103
  /** A registered kind; its settings are the defaults of this run, and the run resumes after a reload. */
93
104
  kind?: string
94
105
  confirm?: TaskConfirm
@@ -116,10 +127,59 @@ export interface TaskRun {
116
127
  cancelling: boolean
117
128
  /** What the run is about, as given to run() (#4687) */
118
129
  meta: Record<string, unknown> | null
130
+ /** The sequence this run is a step of, or null (#4934) */
131
+ sequenceId: string | null
119
132
  /** Settles with the run once it ends: succeeded, failed or cancelled (it never rejects). */
120
133
  finished: Promise<TaskRun>
121
134
  }
122
135
 
136
+ /** A step of a sequence: run options, or a function of the previous step's run that returns them (#4934). */
137
+ export type SequenceStep = RunOptions | ((previous: TaskRun | null) => RunOptions)
138
+
139
+ export interface SequenceOptions {
140
+ /** A failure stops the steps that follow (they show "not run"). Default true. */
141
+ stopOnError?: boolean
142
+ /** Open the sequence's window at once. Default true. */
143
+ open?: boolean
144
+ /** Asked once, before the first step. */
145
+ confirm?: TaskConfirm
146
+ /** What the sequence is about, for a page to find it again (sequenceFor). Plain values only. */
147
+ meta?: Record<string, unknown>
148
+ /** One toast when it ends. Default true. */
149
+ notify?: boolean
150
+ /** Called once the sequence ends. */
151
+ onSettled?: (sequence: TaskSequence) => void
152
+ }
153
+
154
+ /** A step that has no run: waiting its turn, or never run (and why). */
155
+ export type SequenceStepStatus = TaskStatus | 'pending' | 'skipped'
156
+
157
+ export interface TaskSequenceStep {
158
+ /** The run's label once started, else the declared one ("Step 2" for a step given as a function) */
159
+ label: string
160
+ runId: string | null
161
+ /** The state recorded: pending, skipped, or the run's once it ended. Live: `store.stepStatus(step)` */
162
+ status: SequenceStepStatus
163
+ /** Why a step was not run: an earlier step failed, the sequence was cancelled, the page was reloaded */
164
+ skipped: 'error' | 'cancelled' | 'reload' | null
165
+ }
166
+
167
+ export interface TaskSequence {
168
+ id: string
169
+ label: string
170
+ status: 'running' | 'succeeded' | 'failed' | 'cancelled'
171
+ steps: TaskSequenceStep[]
172
+ /** The index of the step running, or of the last one run */
173
+ current: number
174
+ stopOnError: boolean
175
+ cancelling: boolean
176
+ startedAt: number
177
+ endedAt: number | null
178
+ meta: Record<string, unknown> | null
179
+ /** Settles with the sequence once it ends (it never rejects). */
180
+ finished: Promise<TaskSequence>
181
+ }
182
+
123
183
  interface TaskHostDeps {
124
184
  orchestrator?: {
125
185
  get: (name: string) => unknown
@@ -136,6 +196,7 @@ interface PendingConfirm {
136
196
 
137
197
  const FINAL: TaskStatus[] = ['succeeded', 'failed', 'cancelled']
138
198
  const STORAGE_KEY = 'qdadm:tasks'
199
+ const SEQUENCES_KEY = 'qdadm:task-sequences'
139
200
  const KEEP_FINISHED = 20
140
201
 
141
202
  const USUAL: Record<string, Exclude<TaskStatus, 'starting'>> = {
@@ -173,6 +234,9 @@ function errorText(err: unknown): string {
173
234
  function createTaskStore() {
174
235
  const state = reactive({
175
236
  runs: [] as TaskRun[],
237
+ /** Runs chained in one window (#4934) */
238
+ sequences: [] as TaskSequence[],
239
+ /** The run or the sequence whose window is open */
176
240
  openId: null as string | null,
177
241
  pendingConfirm: null as PendingConfirm | null,
178
242
  hosts: 0,
@@ -191,7 +255,7 @@ function createTaskStore() {
191
255
  try {
192
256
  const saved = state.runs
193
257
  .filter((r) => r.kind && kinds.has(r.kind) && r.taskId !== null && !FINAL.includes(r.status))
194
- .map((r) => ({ id: r.id, kind: r.kind, label: r.label, taskId: r.taskId, startedAt: r.startedAt, meta: r.meta }))
258
+ .map((r) => ({ id: r.id, kind: r.kind, label: r.label, taskId: r.taskId, startedAt: r.startedAt, meta: r.meta, sequenceId: r.sequenceId }))
195
259
  if (saved.length) sessionStorage.setItem(STORAGE_KEY, JSON.stringify(saved))
196
260
  else sessionStorage.removeItem(STORAGE_KEY)
197
261
  } catch {
@@ -202,7 +266,7 @@ function createTaskStore() {
202
266
  function restore(): void {
203
267
  if (restored) return
204
268
  restored = true
205
- let saved: Array<Pick<TaskRun, 'id' | 'kind' | 'label' | 'taskId' | 'startedAt'> & { meta?: TaskRun['meta'] }> = []
269
+ let saved: Array<Pick<TaskRun, 'id' | 'kind' | 'label' | 'taskId' | 'startedAt'> & { meta?: TaskRun['meta']; sequenceId?: string | null }> = []
206
270
  try {
207
271
  saved = JSON.parse(sessionStorage.getItem(STORAGE_KEY) ?? '[]')
208
272
  } catch {
@@ -211,10 +275,12 @@ function createTaskStore() {
211
275
  for (const s of saved) {
212
276
  const kind = s.kind ? kinds.get(s.kind) : undefined
213
277
  if (!kind || s.taskId === null || state.runs.some((r) => r.id === s.id)) continue
214
- const run = addRun({ id: s.id, kind: s.kind, label: s.label, taskId: s.taskId, status: 'running', startedAt: s.startedAt, meta: s.meta ?? null })
215
- defs.set(run.id, kind)
278
+ const run = addRun({ id: s.id, kind: s.kind, label: s.label, taskId: s.taskId, status: 'running', startedAt: s.startedAt, meta: s.meta ?? null, sequenceId: s.sequenceId ?? null })
279
+ // A step of a sequence: the sequence gives the one toast
280
+ defs.set(run.id, s.sequenceId ? { ...kind, notify: false } : kind)
216
281
  follow(run.id)
217
282
  }
283
+ restoreSequences()
218
284
  }
219
285
 
220
286
  // --- Runs ---
@@ -235,6 +301,7 @@ function createTaskStore() {
235
301
  endedAt: null,
236
302
  cancelling: false,
237
303
  meta: partial.meta ?? null,
304
+ sequenceId: partial.sequenceId ?? null,
238
305
  // A promise is not made reactive: run.finished stays the raw promise.
239
306
  finished: new Promise<TaskRun>((resolve) => settles.set(id, resolve)),
240
307
  }
@@ -400,6 +467,10 @@ function createTaskStore() {
400
467
  * pass `onSettled`) for its outcome.
401
468
  */
402
469
  async function run(options: RunOptions): Promise<TaskRun | null> {
470
+ return startRun(options, {})
471
+ }
472
+
473
+ async function startRun(options: RunOptions, ctx: { previous?: TaskRun | null; sequenceId?: string }): Promise<TaskRun | null> {
403
474
  const base = options.kind ? kinds.get(options.kind) : undefined
404
475
  if (options.kind && !base && !options.task) {
405
476
  throw new Error(`TaskRunner: unknown task kind "${options.kind}"`)
@@ -407,13 +478,20 @@ function createTaskStore() {
407
478
  const def: TaskKind = { ...base, ...options, task: { ...base?.task, ...options.task } } as TaskKind
408
479
  const labelOf = typeof options.label === 'function' ? options.label : undefined
409
480
  const label = labelOf ? labelOf() : (options.label as string)
481
+ const meta = options.meta ? { ...options.meta } : null
482
+ const start = options.start
483
+ ? () => options.start!(ctx.previous ?? null)
484
+ : base?.start
485
+ ? () => base.start!(meta)
486
+ : null
487
+ if (!start) throw new Error(`TaskRunner: "${label}" has no start, and its kind none either`)
410
488
  if (options.confirm && !(await requestConfirm(label, options.confirm))) return null
411
489
 
412
- const r = addRun({ label, kind: options.kind ?? null, meta: options.meta ? { ...options.meta } : null }, labelOf)
490
+ const r = addRun({ label, kind: options.kind ?? null, meta, sequenceId: ctx.sequenceId ?? null }, labelOf)
413
491
  defs.set(r.id, def)
414
492
  if (options.open !== false) state.openId = r.id
415
493
  try {
416
- const taskId = taskIdOf(await options.start())
494
+ const taskId = taskIdOf(await start())
417
495
  if (taskId === null) throw new Error('the start handler returned no task id')
418
496
  const current = find(r.id)!
419
497
  current.taskId = taskId
@@ -454,6 +532,7 @@ function createTaskStore() {
454
532
 
455
533
  function clearFinished(): void {
456
534
  for (const r of state.runs.filter((x) => FINAL.includes(x.status))) dismiss(r.id)
535
+ for (const x of state.sequences.filter((x) => x.status !== 'running')) dismissSequence(x.id)
457
536
  }
458
537
 
459
538
  /** Wire the orchestrator (manager reads, toasts) and the signal bus (live). Called by useTaskRunner. */
@@ -465,6 +544,313 @@ function createTaskStore() {
465
544
 
466
545
  const active: ComputedRef<TaskRun[]> = computed(() => state.runs.filter((r) => !FINAL.includes(r.status)))
467
546
 
547
+ // --- Sequences: runs chained in one window (#4934) ---
548
+
549
+ interface SequenceInternals {
550
+ /** Each step's options; null for a step that cannot start again after a reload */
551
+ specs: Array<SequenceStep | null>
552
+ options: SequenceOptions
553
+ /** One step only: it is a plain run, with its own window and toast */
554
+ single: boolean
555
+ settle: (sequence: TaskSequence) => void
556
+ }
557
+ const sequenceInternals = new Map<string, SequenceInternals>()
558
+ let sequenceSeq = 0
559
+
560
+ const findSequence = (id: string) => state.sequences.find((s) => s.id === id)
561
+
562
+ /** A step's state: its run's while the run is in the store, else what was recorded */
563
+ function stepStatus(step: TaskSequenceStep): SequenceStepStatus {
564
+ const run = step.runId ? find(step.runId) : undefined
565
+ return run?.status ?? step.status ?? 'pending'
566
+ }
567
+
568
+ /** What the list shows on a step's line: the kind's summary, else the error, else the step reached */
569
+ function stepSummary(step: TaskSequenceStep): string | null {
570
+ const run = step.runId ? find(step.runId) : undefined
571
+ if (!run) return null
572
+ const custom = defs.get(run.id)?.summary
573
+ if (custom) {
574
+ try {
575
+ const text = custom(run)
576
+ if (text !== undefined) return text
577
+ } catch (err) {
578
+ console.error(`[qdadm] Task "${run.label}": summary threw`, err)
579
+ }
580
+ }
581
+ if (run.status === 'failed') return run.error
582
+ if (run.status === 'succeeded' && run.result !== null && typeof run.result !== 'object') return String(run.result)
583
+ return run.step
584
+ }
585
+
586
+ const declaredLabel = (spec: SequenceStep | null | undefined): string | (() => string) =>
587
+ !spec || typeof spec === 'function' ? '' : spec.label
588
+
589
+ function addSequence(
590
+ partial: Pick<TaskSequence, 'label' | 'stopOnError' | 'meta'> & Partial<Pick<TaskSequence, 'id' | 'startedAt' | 'current' | 'cancelling'>>,
591
+ steps: Array<{ label: string | (() => string); runId?: string | null; status?: SequenceStepStatus; skipped?: TaskSequenceStep['skipped'] }>,
592
+ internals: Omit<SequenceInternals, 'settle'>,
593
+ labelOf?: () => string
594
+ ): TaskSequence {
595
+ const id = partial.id ?? `seq-${Date.now().toString(36)}-${++sequenceSeq}`
596
+ let settle!: (sequence: TaskSequence) => void
597
+ const sequence: TaskSequence = {
598
+ id,
599
+ label: partial.label,
600
+ status: 'running',
601
+ steps: steps.map((st) => {
602
+ const declared = st.label
603
+ return {
604
+ runId: st.runId ?? null,
605
+ status: st.status ?? 'pending',
606
+ skipped: st.skipped ?? null,
607
+ // The run's label once started (it follows the locale), else the declared one
608
+ get label(): string {
609
+ const run = this.runId ? find(this.runId) : undefined
610
+ if (run) return run.label
611
+ return typeof declared === 'function' ? declared() : declared
612
+ },
613
+ } as TaskSequenceStep
614
+ }),
615
+ current: partial.current ?? 0,
616
+ stopOnError: partial.stopOnError,
617
+ cancelling: partial.cancelling ?? false,
618
+ startedAt: partial.startedAt ?? Date.now(),
619
+ endedAt: null,
620
+ meta: partial.meta,
621
+ finished: new Promise<TaskSequence>((resolve) => (settle = resolve)),
622
+ }
623
+ if (labelOf) Object.defineProperty(sequence, 'label', { get: labelOf, enumerable: true, configurable: true })
624
+ sequenceInternals.set(id, { ...internals, settle })
625
+ state.sequences.unshift(sequence)
626
+ const finished = state.sequences.filter((x) => FINAL.includes(x.status as TaskStatus))
627
+ for (const old of finished.slice(KEEP_FINISHED)) dismissSequence(old.id)
628
+ return findSequence(id)!
629
+ }
630
+
631
+ function persistSequences(): void {
632
+ try {
633
+ const saved = state.sequences
634
+ .filter((x) => x.status === 'running' && !sequenceInternals.get(x.id)?.single)
635
+ .map((x) => {
636
+ const specs = sequenceInternals.get(x.id)?.specs ?? []
637
+ return {
638
+ id: x.id,
639
+ label: x.label,
640
+ meta: x.meta,
641
+ stopOnError: x.stopOnError,
642
+ startedAt: x.startedAt,
643
+ current: x.current,
644
+ cancelling: x.cancelling,
645
+ steps: x.steps.map((st, i) => {
646
+ const spec = specs[i]
647
+ const plain = spec && typeof spec !== 'function' ? spec : null
648
+ return { label: st.label, runId: st.runId, status: stepStatus(st), skipped: st.skipped, kind: plain?.kind ?? null, meta: plain?.meta ?? null }
649
+ }),
650
+ }
651
+ })
652
+ if (saved.length) sessionStorage.setItem(SEQUENCES_KEY, JSON.stringify(saved))
653
+ else sessionStorage.removeItem(SEQUENCES_KEY)
654
+ } catch {
655
+ // no storage: a sequence does not survive a reload
656
+ }
657
+ }
658
+
659
+ /** A sequence that was running before a reload: its running step was restored with the runs; the steps after it start again when their kind can */
660
+ function restoreSequences(): void {
661
+ type Saved = Pick<TaskSequence, 'id' | 'label' | 'meta' | 'stopOnError' | 'startedAt' | 'current' | 'cancelling'> & {
662
+ steps: Array<{ label: string; runId: string | null; status: SequenceStepStatus; skipped: TaskSequenceStep['skipped']; kind: string | null; meta: Record<string, unknown> | null }>
663
+ }
664
+ let saved: Saved[] = []
665
+ try {
666
+ saved = JSON.parse(sessionStorage.getItem(SEQUENCES_KEY) ?? '[]')
667
+ } catch {
668
+ return
669
+ }
670
+ for (const x of saved) {
671
+ if (findSequence(x.id)) continue
672
+ const specs = x.steps.map((st) => {
673
+ const kind = st.kind ? kinds.get(st.kind) : undefined
674
+ return kind?.start ? ({ kind: st.kind!, label: st.label, meta: st.meta ?? undefined } as RunOptions) : null
675
+ })
676
+ const steps = x.steps.map((st) => ({
677
+ label: st.label,
678
+ // A run that is not back (its kind is not registered) cannot be followed
679
+ runId: st.runId && find(st.runId) ? st.runId : null,
680
+ status: st.runId && !find(st.runId) && !FINAL.includes(st.status as TaskStatus) ? ('skipped' as const) : st.status,
681
+ skipped: st.runId && !find(st.runId) && !FINAL.includes(st.status as TaskStatus) ? ('reload' as const) : st.skipped,
682
+ }))
683
+ const sequence = addSequence(x, steps, { specs, options: {}, single: false })
684
+ const current = sequence.steps[sequence.current]
685
+ const previous = current?.runId ? find(current.runId) ?? null : null
686
+ void drive(sequence.id, previous)
687
+ }
688
+ }
689
+
690
+ function skipFrom(sequence: TaskSequence, index: number, why: NonNullable<TaskSequenceStep['skipped']>): void {
691
+ for (const step of sequence.steps.slice(index)) {
692
+ if (step.runId) continue
693
+ step.status = 'skipped'
694
+ step.skipped = why
695
+ }
696
+ }
697
+
698
+ /** Run the steps one after another, from the sequence's current one */
699
+ async function drive(id: string, previous: TaskRun | null): Promise<void> {
700
+ const internals = sequenceInternals.get(id)
701
+ if (!internals) return
702
+ let failed = false
703
+ for (let i = findSequence(id)?.current ?? 0; i < (findSequence(id)?.steps.length ?? 0); i++) {
704
+ const sequence = findSequence(id)
705
+ if (!sequence) return
706
+ const step = sequence.steps[i]!
707
+ if (step.status === 'skipped') continue
708
+ if (step.runId && FINAL.includes(stepStatus(step) as TaskStatus)) {
709
+ // Already over before a reload
710
+ failed ||= stepStatus(step) === 'failed'
711
+ continue
712
+ }
713
+ if (sequence.cancelling) {
714
+ skipFrom(sequence, i, 'cancelled')
715
+ break
716
+ }
717
+ if (failed && sequence.stopOnError) {
718
+ skipFrom(sequence, i, 'error')
719
+ break
720
+ }
721
+ sequence.current = i
722
+ let run: TaskRun | null = step.runId ? find(step.runId) ?? null : null
723
+ if (!run) {
724
+ const spec = internals.specs[i]
725
+ if (!spec) {
726
+ skipFrom(sequence, i, 'reload')
727
+ break
728
+ }
729
+ let options: RunOptions
730
+ try {
731
+ options = typeof spec === 'function' ? spec(previous) : spec
732
+ } catch (err) {
733
+ options = { label: step.label || `#${i + 1}`, start: () => Promise.reject(err), task: {} }
734
+ }
735
+ const stepOptions: RunOptions = {
736
+ ...options,
737
+ confirm: undefined,
738
+ open: internals.single ? internals.options.open !== false : false,
739
+ ...(internals.single ? {} : { notify: false }),
740
+ }
741
+ try {
742
+ run = await startRun(stepOptions, { previous, sequenceId: id })
743
+ } catch (err) {
744
+ run = await startRun({ label: options.label, start: () => Promise.reject(err), task: {}, open: false, notify: internals.single ? undefined : false }, { sequenceId: id })
745
+ }
746
+ if (!run) break
747
+ step.runId = run.id
748
+ persistSequences()
749
+ }
750
+ const end = await run.finished
751
+ step.status = end.status
752
+ if (end.status === 'failed') failed = true
753
+ if (end.status === 'cancelled') findSequence(id)!.cancelling = true
754
+ previous = end
755
+ persistSequences()
756
+ }
757
+ finishSequence(id)
758
+ }
759
+
760
+ function finishSequence(id: string): void {
761
+ const sequence = findSequence(id)
762
+ const internals = sequenceInternals.get(id)
763
+ if (!sequence || !internals || sequence.status !== 'running') return
764
+ const statuses = sequence.steps.map((st) => stepStatus(st))
765
+ sequence.status = statuses.includes('failed')
766
+ ? 'failed'
767
+ : statuses.some((st) => st === 'cancelled' || st === 'skipped')
768
+ ? 'cancelled'
769
+ : 'succeeded'
770
+ sequence.endedAt = Date.now()
771
+ sequence.cancelling = false
772
+ persistSequences()
773
+ if (!internals.single && internals.options.notify !== false) notifySequenceEnd(sequence)
774
+ internals.settle(sequence)
775
+ try {
776
+ internals.options.onSettled?.(sequence)
777
+ } catch (err) {
778
+ console.error(`[qdadm] Task sequence "${sequence.label}": onSettled threw`, err)
779
+ }
780
+ }
781
+
782
+ const MARKS: Partial<Record<SequenceStepStatus, string>> = { succeeded: '✓', failed: '✗', cancelled: '⊘', skipped: '–' }
783
+
784
+ /** One toast for the whole sequence: each step marked, the failure's error after it */
785
+ function notifySequenceEnd(sequence: TaskSequence): void {
786
+ const toast = deps.orchestrator?.toast
787
+ if (!toast) return
788
+ const detail = sequence.steps
789
+ .map((st, i) => {
790
+ const status = stepStatus(st)
791
+ const label = st.label || `#${i + 1}`
792
+ const error = status === 'failed' && st.runId ? find(st.runId)?.error : null
793
+ return `${MARKS[status] ?? '…'} ${label}${error ? `: ${error}` : ''}`
794
+ })
795
+ .join(' · ')
796
+ const opts = { emitter: 'TaskRunner' }
797
+ if (sequence.status === 'succeeded') toast.success(sequence.label, detail, opts)
798
+ else if (sequence.status === 'failed') toast.error(sequence.label, detail, { ...opts, keep: 'long' })
799
+ else toast.warn(sequence.label, detail, opts)
800
+ }
801
+
802
+ /**
803
+ * Run tasks one after another, followed in one window: the list of steps,
804
+ * each step's detail on a click (#4934). Resolves once the sequence is
805
+ * under way (null when its confirmation is declined); await
806
+ * `sequence.finished` for its end. One step only is a plain run.
807
+ */
808
+ async function sequence(
809
+ label: string | (() => string),
810
+ steps: SequenceStep[],
811
+ options: SequenceOptions = {}
812
+ ): Promise<TaskSequence | null> {
813
+ if (!steps.length) throw new Error('TaskRunner: a sequence needs at least one step')
814
+ const labelOf = typeof label === 'function' ? label : undefined
815
+ const text = labelOf ? labelOf() : (label as string)
816
+ if (options.confirm && !(await requestConfirm(text, options.confirm))) return null
817
+ const single = steps.length === 1
818
+ const created = addSequence(
819
+ { label: text, stopOnError: options.stopOnError !== false, meta: options.meta ? { ...options.meta } : null },
820
+ steps.map((spec) => ({ label: declaredLabel(spec) })),
821
+ { specs: steps.slice(), options, single },
822
+ labelOf
823
+ )
824
+ if (!single && options.open !== false) state.openId = created.id
825
+ void drive(created.id, null)
826
+ return created
827
+ }
828
+
829
+ /** Stop a sequence: its running step is cancelled when its kind can, and the steps after it are not run. */
830
+ async function cancelSequence(id: string): Promise<void> {
831
+ const sequence = findSequence(id)
832
+ if (!sequence || sequence.status !== 'running') return
833
+ sequence.cancelling = true
834
+ persistSequences()
835
+ const runId = sequence.steps[sequence.current]?.runId
836
+ if (runId) await cancel(runId)
837
+ }
838
+
839
+ function dismissSequence(id: string): void {
840
+ const i = state.sequences.findIndex((x) => x.id === id)
841
+ if (i !== -1) state.sequences.splice(i, 1)
842
+ sequenceInternals.delete(id)
843
+ if (state.openId === id) state.openId = null
844
+ persistSequences()
845
+ }
846
+
847
+ /** The sequences matching a meta (each given key equal), newest first. */
848
+ function sequencesFor(meta: Record<string, unknown> = {}): TaskSequence[] {
849
+ return state.sequences
850
+ .filter((x) => Object.entries(meta).every(([k, v]) => x.meta?.[k] === v))
851
+ .sort((a, b) => b.startedAt - a.startedAt)
852
+ }
853
+
468
854
  // --- Finding a run again (#4687) ---
469
855
 
470
856
  /** The runs matching a kind and/or a meta (each given key equal), newest first. */
@@ -500,9 +886,20 @@ function createTaskStore() {
500
886
  definition: (id: string) => defs.get(id),
501
887
  runFor,
502
888
  runsFor,
503
- isFinal: (status: TaskStatus) => FINAL.includes(status),
504
- /** A run still going: starting, queued or running (#4695) */
505
- isActive: (run: TaskRun | null | undefined) => !!run && !FINAL.includes(run.status),
889
+ isFinal: (status: TaskStatus | SequenceStepStatus | TaskSequence['status']) => FINAL.includes(status as TaskStatus),
890
+ /** A run or a sequence still going (#4695, #4934) */
891
+ isActive: (run: TaskRun | TaskSequence | null | undefined) => !!run && !FINAL.includes(run.status as TaskStatus),
892
+ sequence,
893
+ cancelSequence,
894
+ dismissSequence,
895
+ /** The sequence a run is a step of, or a sequence by its id */
896
+ sequenceOf: (id: string | null | undefined): TaskSequence | null =>
897
+ !id ? null : (findSequence(id) ?? (find(id)?.sequenceId ? findSequence(find(id)!.sequenceId!) ?? null : null)),
898
+ sequencesFor,
899
+ /** The latest sequence about a record: `sequenceFor({ box: 'b1' })` */
900
+ sequenceFor: (meta: Record<string, unknown>) => sequencesFor(meta)[0] ?? null,
901
+ stepStatus,
902
+ stepSummary,
506
903
  /** For tests: stop everything and forget every run. */
507
904
  reset(): void {
508
905
  for (const stop of stops.values()) stop()
@@ -512,6 +909,8 @@ function createTaskStore() {
512
909
  defs.clear()
513
910
  kinds.clear()
514
911
  state.runs.splice(0)
912
+ state.sequences.splice(0)
913
+ sequenceInternals.clear()
515
914
  state.openId = null
516
915
  state.pendingConfirm = null
517
916
  state.hosts = 0
@@ -46,12 +46,16 @@ export function useTaskNotifications(): void {
46
46
  for (const { run } of entries) {
47
47
  const id = STATUS_PREFIX + run.id
48
48
  ids.add(id)
49
+ // A step of a sequence says which sequence, and opens its window (#4934)
50
+ const sequence = run.sequenceId ? store.sequenceOf(run.sequenceId) : null
51
+ const index = sequence ? sequence.steps.findIndex((st) => st.runId === run.id) : -1
52
+ const label = sequence && index !== -1 ? `${sequence.label} · ${run.label} (${index + 1}/${sequence.steps.length})` : run.label
49
53
  notifications.registerStatus({
50
54
  id,
51
- label: `${run.label} — ${stateText(run)}`,
55
+ label: `${label} — ${stateText(run)}`,
52
56
  severity: 'nominal',
53
57
  icon: 'pi pi-spin pi-spinner',
54
- onClick: () => store.open(run.id),
58
+ onClick: () => store.open(sequence ? sequence.id : run.id),
55
59
  })
56
60
  shown.add(id)
57
61
  if (!tracked.has(run.id)) {
@@ -24,6 +24,14 @@
24
24
  * tasks.run({ kind: 'stock-check', label, start, meta: { supplierId: id } })
25
25
  * const check = computed(() => tasks.runFor('stock-check', { supplierId: id }))
26
26
  *
27
+ * // Tasks chained in one window: the list of steps, each step's detail on a click (#4934).
28
+ * // A step's start gets the previous step's run; a failure stops the rest.
29
+ * const seq = await tasks.sequence('Verify', [
30
+ * { kind: 'check', label: 'Check', start: () => api.check(box) },
31
+ * { kind: 'conf-check', label: 'Conf check', start: (prev) => api.confCheck(box, prev?.result) },
32
+ * ], { meta: { box } })
33
+ * const busy = computed(() => tasks.isActive(tasks.sequenceFor({ box })))
34
+ *
27
35
  * // As a row action, with a typed confirmation
28
36
  * list.addAction('destroy', {
29
37
  * icon: 'pi pi-trash', severity: 'danger',
@@ -57,8 +65,14 @@ export function useTaskRunner() {
57
65
  runFor: store.runFor,
58
66
  /** The runs matching `{ kind, taskId, meta }`, newest first. */
59
67
  runsFor: store.runsFor,
60
- /** A run still going (starting, queued, running), false for none (#4695). */
68
+ /** A run or a sequence still going, false for none (#4695). */
61
69
  isActive: store.isActive,
70
+ /** Run tasks one after another, followed in one window (#4934). Resolves once under way; await `sequence.finished`. */
71
+ sequence: store.sequence,
72
+ cancelSequence: store.cancelSequence,
73
+ /** The latest sequence about a record (its meta), or null. */
74
+ sequenceFor: store.sequenceFor,
75
+ sequencesFor: store.sequencesFor,
62
76
  }
63
77
  }
64
78