@embedpdf/models 2.6.0 → 2.6.2

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.
package/dist/index.d.ts CHANGED
@@ -16,6 +16,7 @@ export * from './date';
16
16
  export * from './helpers';
17
17
  export * from './uuid';
18
18
  export * from './compound-task';
19
+ export * from './task-sequence';
19
20
  export * from './fonts';
20
21
  /**
21
22
  * ignore will do nothing when called.
package/dist/index.js CHANGED
@@ -2083,6 +2083,109 @@ class CompoundTask extends Task {
2083
2083
  return compound;
2084
2084
  }
2085
2085
  }
2086
+ class TaskSequence {
2087
+ constructor(parentTask) {
2088
+ this.parentTask = parentTask;
2089
+ this.activeChild = null;
2090
+ this.disposed = false;
2091
+ const origAbort = parentTask.abort.bind(parentTask);
2092
+ parentTask.abort = (reason) => {
2093
+ var _a;
2094
+ this.disposed = true;
2095
+ (_a = this.activeChild) == null ? void 0 : _a.abort(reason);
2096
+ origAbort(reason);
2097
+ };
2098
+ }
2099
+ /**
2100
+ * Execute a child Task and return its result as a Promise.
2101
+ *
2102
+ * If the parent task has been aborted, throws `TaskAbortedError` immediately.
2103
+ * If the parent task is aborted while the child is running, the child is aborted too.
2104
+ */
2105
+ run(factory) {
2106
+ return new Promise((resolve, reject) => {
2107
+ if (this.disposed || this.parentTask.state.stage !== TaskStage.Pending) {
2108
+ reject(new TaskAbortedError("Sequence aborted"));
2109
+ return;
2110
+ }
2111
+ const child = factory();
2112
+ this.activeChild = child;
2113
+ child.wait(
2114
+ (result) => {
2115
+ this.activeChild = null;
2116
+ resolve(result);
2117
+ },
2118
+ (error) => {
2119
+ this.activeChild = null;
2120
+ if (error.type === "abort") {
2121
+ reject(new TaskAbortedError(error.reason));
2122
+ } else {
2123
+ reject(new TaskRejectedError(error.reason));
2124
+ }
2125
+ }
2126
+ );
2127
+ });
2128
+ }
2129
+ /**
2130
+ * Execute a child Task and return its result as a Promise,
2131
+ * forwarding the child's progress events to the parent task
2132
+ * through the provided mapper function.
2133
+ *
2134
+ * If the parent task has been aborted, throws `TaskAbortedError` immediately.
2135
+ * If the parent task is aborted while the child is running, the child is aborted too.
2136
+ */
2137
+ runWithProgress(factory, mapProgress) {
2138
+ return new Promise((resolve, reject) => {
2139
+ if (this.disposed || this.parentTask.state.stage !== TaskStage.Pending) {
2140
+ reject(new TaskAbortedError("Sequence aborted"));
2141
+ return;
2142
+ }
2143
+ const child = factory();
2144
+ this.activeChild = child;
2145
+ child.onProgress((p) => {
2146
+ this.parentTask.progress(mapProgress(p));
2147
+ });
2148
+ child.wait(
2149
+ (result) => {
2150
+ this.activeChild = null;
2151
+ resolve(result);
2152
+ },
2153
+ (error) => {
2154
+ this.activeChild = null;
2155
+ if (error.type === "abort") {
2156
+ reject(new TaskAbortedError(error.reason));
2157
+ } else {
2158
+ reject(new TaskRejectedError(error.reason));
2159
+ }
2160
+ }
2161
+ );
2162
+ });
2163
+ }
2164
+ /**
2165
+ * Execute an async function body that uses `run()` / `runWithProgress()`,
2166
+ * automatically handling abort and error routing to the parent task.
2167
+ *
2168
+ * - If the body throws `TaskAbortedError`, it is silently ignored
2169
+ * (the parent task was already aborted via the abort override).
2170
+ * - If the body throws `TaskRejectedError` (from a child task rejection
2171
+ * via `run()` / `runWithProgress()`), its `.reason` is forwarded directly
2172
+ * to the parent task, bypassing `mapError`.
2173
+ * - Any other thrown error is mapped through `mapError` and used to
2174
+ * reject the parent task. This handles unexpected runtime exceptions
2175
+ * in the async body itself.
2176
+ * - On success, the body is responsible for calling `parentTask.resolve()`.
2177
+ */
2178
+ execute(fn, mapError) {
2179
+ fn().catch((err) => {
2180
+ if (err instanceof TaskAbortedError) return;
2181
+ if (err instanceof TaskRejectedError) {
2182
+ this.parentTask.reject(err.reason);
2183
+ return;
2184
+ }
2185
+ this.parentTask.reject(mapError(err));
2186
+ });
2187
+ }
2188
+ }
2086
2189
  function ignore() {
2087
2190
  }
2088
2191
  export {
@@ -2145,6 +2248,7 @@ export {
2145
2248
  Task,
2146
2249
  TaskAbortedError,
2147
2250
  TaskRejectedError,
2251
+ TaskSequence,
2148
2252
  TaskStage,
2149
2253
  blendModeLabel,
2150
2254
  blendModeSelectOptions,