@tstdl/base 0.83.28 → 0.84.1
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/application/application.d.ts +1 -1
- package/application/application.js +3 -3
- package/browser/browser-context-controller.d.ts +32 -0
- package/browser/browser-context-controller.js +88 -0
- package/browser/browser-controller.d.ts +30 -11
- package/browser/browser-controller.js +40 -30
- package/browser/browser.service.d.ts +9 -7
- package/browser/browser.service.js +31 -34
- package/browser/index.d.ts +1 -1
- package/browser/index.js +1 -1
- package/browser/page-controller.d.ts +33 -35
- package/browser/page-controller.js +58 -81
- package/browser/pdf-options.d.ts +5 -1
- package/browser/pdf-options.js +9 -5
- package/browser/utils.d.ts +6 -0
- package/browser/utils.js +60 -0
- package/data-structures/cache.d.ts +11 -0
- package/data-structures/cache.js +69 -0
- package/data-structures/index.d.ts +1 -0
- package/data-structures/index.js +1 -0
- package/disposable/async-disposer.d.ts +2 -1
- package/error/index.d.ts +1 -0
- package/error/index.js +1 -0
- package/error/timeout.error.d.ts +5 -0
- package/{browser/types.js → error/timeout.error.js} +16 -2
- package/examples/browser/basic.js +2 -2
- package/examples/pdf/basic.js +10 -6
- package/http/client/adapters/undici-http-client.adapter.js +1 -1
- package/http/client/http-client-request.js +1 -1
- package/http/server/node/node-http-server.js +1 -1
- package/lock/web/web-lock.js +2 -2
- package/package.json +8 -6
- package/pdf/pdf.service.d.ts +15 -21
- package/pdf/pdf.service.js +23 -62
- package/pool/pool.d.ts +1 -0
- package/pool/pool.js +3 -0
- package/queue/mongo/queue.js +2 -2
- package/types.d.ts +2 -2
- package/utils/cancellation-token.d.ts +11 -16
- package/utils/cancellation-token.js +10 -39
- package/utils/timing.d.ts +7 -1
- package/utils/timing.js +23 -3
- package/browser/types.d.ts +0 -3
package/pdf/pdf.service.d.ts
CHANGED
|
@@ -1,20 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BrowserContextController } from '../browser/browser-context-controller.js';
|
|
2
|
+
import type { BrowserControllerArgument } from '../browser/browser-controller.js';
|
|
3
|
+
import { BrowserController } from '../browser/browser-controller.js';
|
|
2
4
|
import { PdfRenderOptions } from '../browser/pdf-options.js';
|
|
3
|
-
import type {
|
|
4
|
-
import {
|
|
5
|
-
import type { AsyncDisposable } from '../disposable/disposable.js';
|
|
6
|
-
import { disposeAsync } from '../disposable/disposable.js';
|
|
7
|
-
import { Logger } from '../logger/index.js';
|
|
5
|
+
import type { Injectable } from '../container/index.js';
|
|
6
|
+
import { resolveArgumentType } from '../container/index.js';
|
|
8
7
|
import type { TemplateField } from '../templates/index.js';
|
|
9
8
|
import { Template, TemplateService } from '../templates/index.js';
|
|
10
9
|
export declare class PdfServiceRenderOptions extends PdfRenderOptions {
|
|
11
|
-
|
|
10
|
+
browserContext?: BrowserContextController;
|
|
11
|
+
locale?: string;
|
|
12
12
|
waitForNetworkIdle?: boolean;
|
|
13
|
-
/**
|
|
14
|
-
* Timeout for closing render context in case something went wrong.
|
|
15
|
-
* @default 60000 (1 minute)
|
|
16
|
-
*/
|
|
17
|
-
timeout?: number;
|
|
18
13
|
}
|
|
19
14
|
export type PdfTemplateOptions = PdfRenderOptions;
|
|
20
15
|
export declare class PdfTemplate<Context extends object = any> extends Template<{
|
|
@@ -25,18 +20,17 @@ export declare class PdfTemplate<Context extends object = any> extends Template<
|
|
|
25
20
|
options?: PdfTemplateOptions;
|
|
26
21
|
}
|
|
27
22
|
export type PdfServiceOptions = {
|
|
28
|
-
|
|
23
|
+
locale?: string;
|
|
24
|
+
};
|
|
25
|
+
export type PdfServiceArgument = PdfServiceOptions & {
|
|
26
|
+
browserControllerArgument: BrowserControllerArgument;
|
|
29
27
|
};
|
|
30
|
-
export
|
|
31
|
-
export declare class PdfService implements AsyncDisposable, AfterResolve, Injectable<PdfServiceArgument> {
|
|
28
|
+
export declare class PdfService implements Injectable<PdfServiceArgument> {
|
|
32
29
|
private readonly templateService;
|
|
33
|
-
private readonly
|
|
34
|
-
private readonly
|
|
30
|
+
private readonly browserController;
|
|
31
|
+
private readonly defaultLocale;
|
|
35
32
|
readonly [resolveArgumentType]: PdfServiceArgument;
|
|
36
|
-
constructor(templateService: TemplateService,
|
|
37
|
-
[afterResolve](): void;
|
|
38
|
-
[disposeAsync](): Promise<void>;
|
|
39
|
-
dispose(): Promise<void>;
|
|
33
|
+
constructor(templateService: TemplateService, browserController: BrowserController, options?: PdfServiceOptions);
|
|
40
34
|
/**
|
|
41
35
|
* Renders HTML to pdf stream
|
|
42
36
|
* @param html html to render
|
package/pdf/pdf.service.js
CHANGED
|
@@ -24,20 +24,16 @@ __export(pdf_service_exports, {
|
|
|
24
24
|
pdfTemplate: () => pdfTemplate
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(pdf_service_exports);
|
|
27
|
-
var
|
|
27
|
+
var import_browser_context_controller = require("../browser/browser-context-controller.js");
|
|
28
|
+
var import_browser_controller = require("../browser/browser-controller.js");
|
|
28
29
|
var import_pdf_options = require("../browser/pdf-options.js");
|
|
29
30
|
var import_container = require("../container/index.js");
|
|
30
|
-
var import_core = require("../core.js");
|
|
31
|
-
var import_disposable = require("../disposable/disposable.js");
|
|
32
|
-
var import_logger = require("../logger/index.js");
|
|
33
|
-
var import_pool = require("../pool/pool.js");
|
|
34
31
|
var import_schema = require("../schema/index.js");
|
|
35
32
|
var import_templates = require("../templates/index.js");
|
|
36
33
|
var import_finalize_stream = require("../utils/stream/finalize-stream.js");
|
|
37
34
|
var import_readable_stream_from_promise = require("../utils/stream/readable-stream-from-promise.js");
|
|
38
35
|
var import_stream_reader = require("../utils/stream/stream-reader.js");
|
|
39
36
|
var import_type_guards = require("../utils/type-guards.js");
|
|
40
|
-
var import_units = require("../utils/units.js");
|
|
41
37
|
var __decorate = function(decorators, target, key, desc) {
|
|
42
38
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
43
39
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
@@ -58,26 +54,22 @@ var __param = function(paramIndex, decorator) {
|
|
|
58
54
|
};
|
|
59
55
|
};
|
|
60
56
|
class PdfServiceRenderOptions extends import_pdf_options.PdfRenderOptions {
|
|
61
|
-
|
|
57
|
+
browserContext;
|
|
58
|
+
locale;
|
|
62
59
|
waitForNetworkIdle;
|
|
63
|
-
/**
|
|
64
|
-
* Timeout for closing render context in case something went wrong.
|
|
65
|
-
* @default 60000 (1 minute)
|
|
66
|
-
*/
|
|
67
|
-
timeout;
|
|
68
60
|
}
|
|
61
|
+
__decorate([
|
|
62
|
+
(0, import_schema.Optional)(),
|
|
63
|
+
__metadata("design:type", import_browser_context_controller.BrowserContextController)
|
|
64
|
+
], PdfServiceRenderOptions.prototype, "browserContext", void 0);
|
|
69
65
|
__decorate([
|
|
70
66
|
(0, import_schema.Optional)(),
|
|
71
67
|
__metadata("design:type", String)
|
|
72
|
-
], PdfServiceRenderOptions.prototype, "
|
|
68
|
+
], PdfServiceRenderOptions.prototype, "locale", void 0);
|
|
73
69
|
__decorate([
|
|
74
70
|
(0, import_schema.Optional)(),
|
|
75
71
|
__metadata("design:type", Boolean)
|
|
76
72
|
], PdfServiceRenderOptions.prototype, "waitForNetworkIdle", void 0);
|
|
77
|
-
__decorate([
|
|
78
|
-
(0, import_schema.Optional)(),
|
|
79
|
-
__metadata("design:type", Number)
|
|
80
|
-
], PdfServiceRenderOptions.prototype, "timeout", void 0);
|
|
81
73
|
class PdfTemplate extends import_templates.Template {
|
|
82
74
|
}
|
|
83
75
|
__decorate([
|
|
@@ -87,21 +79,12 @@ __decorate([
|
|
|
87
79
|
const browserArguments = ["--font-render-hinting=none", "--disable-web-security", "--disable-features=IsolateOrigins", "--disable-site-isolation-trials"];
|
|
88
80
|
let PdfService = class PdfService2 {
|
|
89
81
|
templateService;
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
constructor(templateService,
|
|
82
|
+
browserController;
|
|
83
|
+
defaultLocale;
|
|
84
|
+
constructor(templateService, browserController, options = {}) {
|
|
93
85
|
this.templateService = templateService;
|
|
94
|
-
this.
|
|
95
|
-
this.
|
|
96
|
-
}
|
|
97
|
-
[import_container.afterResolve]() {
|
|
98
|
-
import_core.disposer.add(this);
|
|
99
|
-
}
|
|
100
|
-
async [import_disposable.disposeAsync]() {
|
|
101
|
-
return this.dispose();
|
|
102
|
-
}
|
|
103
|
-
async dispose() {
|
|
104
|
-
return this.pool.dispose();
|
|
86
|
+
this.browserController = browserController;
|
|
87
|
+
this.defaultLocale = options.locale;
|
|
105
88
|
}
|
|
106
89
|
/**
|
|
107
90
|
* Renders HTML to pdf stream
|
|
@@ -110,7 +93,7 @@ let PdfService = class PdfService2 {
|
|
|
110
93
|
* @returns pdf stream
|
|
111
94
|
*/
|
|
112
95
|
renderHtmlStream(html, options) {
|
|
113
|
-
return this.renderStream(async (page) => page.setContent(html, { waitUntil: options?.waitForNetworkIdle == true ? "
|
|
96
|
+
return this.renderStream(async (page) => page.setContent(html, { waitUntil: options?.waitForNetworkIdle == true ? "networkidle" : "load" }), options);
|
|
114
97
|
}
|
|
115
98
|
/**
|
|
116
99
|
* Renders HTML to pdf
|
|
@@ -130,7 +113,7 @@ let PdfService = class PdfService2 {
|
|
|
130
113
|
*/
|
|
131
114
|
renderUrlStream(url, options) {
|
|
132
115
|
return this.renderStream(async (controller) => {
|
|
133
|
-
await controller.navigate(url, { waitUntil: options?.waitForNetworkIdle == true ? "
|
|
116
|
+
await controller.navigate(url, { waitUntil: options?.waitForNetworkIdle == true ? "networkidle" : "load" });
|
|
134
117
|
}, options);
|
|
135
118
|
}
|
|
136
119
|
/**
|
|
@@ -153,7 +136,7 @@ let PdfService = class PdfService2 {
|
|
|
153
136
|
renderTemplateStream(keyOrTemplate, templateContext, options) {
|
|
154
137
|
return this.renderStream(async (page) => {
|
|
155
138
|
const { fields: { header, body, footer }, options: optionsFromTemplate } = await this.templateService.render(keyOrTemplate, templateContext);
|
|
156
|
-
await page.setContent(body, { timeout: options?.timeout, waitUntil: options?.waitForNetworkIdle == true ? "
|
|
139
|
+
await page.setContent(body, { timeout: options?.timeout, waitUntil: options?.waitForNetworkIdle == true ? "networkidle" : "load" });
|
|
157
140
|
return { ...optionsFromTemplate, headerTemplate: header, footerTemplate: footer };
|
|
158
141
|
}, options);
|
|
159
142
|
}
|
|
@@ -170,45 +153,23 @@ let PdfService = class PdfService2 {
|
|
|
170
153
|
}
|
|
171
154
|
renderStream(handler, options = {}) {
|
|
172
155
|
return (0, import_readable_stream_from_promise.readableStreamFromPromise)(async () => {
|
|
173
|
-
const
|
|
174
|
-
let page;
|
|
175
|
-
try {
|
|
176
|
-
page = await browserController.newPage();
|
|
177
|
-
} catch (error) {
|
|
178
|
-
await this.pool.disposeInstance(browserController);
|
|
179
|
-
throw error;
|
|
180
|
-
}
|
|
181
|
-
if ((0, import_type_guards.isDefined)(options.language)) {
|
|
182
|
-
await page.setExtraHttpHeaders({ "Accept-Language": options.language });
|
|
183
|
-
}
|
|
156
|
+
const page = (0, import_type_guards.isDefined)(options.browserContext) ? await options.browserContext.newPage() : await this.browserController.newPage({ locale: options.locale ?? this.defaultLocale });
|
|
184
157
|
const optionsFromHandler = await handler(page);
|
|
185
158
|
const pdfStream = page.renderPdfStream({ ...optionsFromHandler, ...options });
|
|
186
|
-
const
|
|
187
|
-
const close = async () => {
|
|
188
|
-
try {
|
|
189
|
-
clearTimeout(timeoutRef);
|
|
190
|
-
await page.close();
|
|
191
|
-
} catch (error) {
|
|
192
|
-
await this.pool.disposeInstance(browserController);
|
|
193
|
-
this.logger.error(error);
|
|
194
|
-
}
|
|
195
|
-
};
|
|
159
|
+
const close = async () => page.close();
|
|
196
160
|
return (0, import_finalize_stream.finalizeStream)(pdfStream, {
|
|
197
161
|
beforeDone: close,
|
|
198
162
|
beforeCancel: close,
|
|
199
|
-
error:
|
|
200
|
-
clearTimeout(timeoutRef);
|
|
201
|
-
await this.pool.disposeInstance(browserController);
|
|
202
|
-
}
|
|
163
|
+
error: close
|
|
203
164
|
});
|
|
204
165
|
});
|
|
205
166
|
}
|
|
206
167
|
};
|
|
207
168
|
PdfService = __decorate([
|
|
208
169
|
(0, import_container.singleton)(),
|
|
209
|
-
__param(
|
|
210
|
-
__param(
|
|
211
|
-
__metadata("design:paramtypes", [import_templates.TemplateService,
|
|
170
|
+
__param(1, (0, import_container.forwardArg)((argument) => argument?.browserControllerArgument ?? { browserArguments })),
|
|
171
|
+
__param(2, (0, import_container.injectArg)()),
|
|
172
|
+
__metadata("design:paramtypes", [import_templates.TemplateService, import_browser_controller.BrowserController, Object])
|
|
212
173
|
], PdfService);
|
|
213
174
|
function pdfTemplate(name, fields, options) {
|
|
214
175
|
return {
|
package/pool/pool.d.ts
CHANGED
|
@@ -34,6 +34,7 @@ export declare class Pool<T extends object> implements AsyncDisposable {
|
|
|
34
34
|
private disposed;
|
|
35
35
|
get length(): number;
|
|
36
36
|
constructor(factory: PoolInstanceFactory<T>, disposer: PoolInstanceDisposer<T>, logger: Logger, options?: PoolOptions);
|
|
37
|
+
owns(instance: T): boolean;
|
|
37
38
|
get(): Promise<T>;
|
|
38
39
|
release(instance: T): Promise<void>;
|
|
39
40
|
/**
|
package/pool/pool.js
CHANGED
|
@@ -50,6 +50,9 @@ class Pool {
|
|
|
50
50
|
this.placeholderInstances = 0;
|
|
51
51
|
this.disposed = false;
|
|
52
52
|
}
|
|
53
|
+
owns(instance) {
|
|
54
|
+
return this.usedInstances.includes(instance) || this.freeInstances.includes(instance);
|
|
55
|
+
}
|
|
53
56
|
async get() {
|
|
54
57
|
if (this.disposed) {
|
|
55
58
|
throw new Error("Pool was disposed.");
|
package/queue/mongo/queue.js
CHANGED
|
@@ -216,7 +216,7 @@ let MongoQueue = class MongoQueue2 extends import_queue.Queue {
|
|
|
216
216
|
return this.cancelMany(jobIds);
|
|
217
217
|
}
|
|
218
218
|
async *getConsumer(cancellationToken) {
|
|
219
|
-
const continueToken = import_cancellation_token.CancellationToken.
|
|
219
|
+
const continueToken = import_cancellation_token.CancellationToken.from(this.messageBus.allMessages$);
|
|
220
220
|
for await (const backoff of (0, import_backoff.backoffGenerator)(backoffOptions, cancellationToken)) {
|
|
221
221
|
const job = await this.dequeue();
|
|
222
222
|
if (job != void 0) {
|
|
@@ -228,7 +228,7 @@ let MongoQueue = class MongoQueue2 extends import_queue.Queue {
|
|
|
228
228
|
continueToken.complete();
|
|
229
229
|
}
|
|
230
230
|
async *getBatchConsumer(size, cancellationToken) {
|
|
231
|
-
const continueToken = import_cancellation_token.CancellationToken.
|
|
231
|
+
const continueToken = import_cancellation_token.CancellationToken.from(this.messageBus.allMessages$);
|
|
232
232
|
for await (const backoff of (0, import_backoff.backoffGenerator)(backoffOptions, cancellationToken)) {
|
|
233
233
|
const jobs = await this.dequeueMany(size);
|
|
234
234
|
if (jobs.length > 0) {
|
package/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Except,
|
|
1
|
+
import type { Except, UnionToIntersection } from 'type-fest';
|
|
2
2
|
export type ObjectLiteral = {};
|
|
3
3
|
export type PrimitiveTypeMap = {
|
|
4
4
|
'string': string;
|
|
@@ -113,7 +113,7 @@ export type OmitBy<T, V> = Omit<T, {
|
|
|
113
113
|
*/
|
|
114
114
|
export type Optionalize<T extends object> = OmitBy<T, undefined> & Partial<PickBy<T, undefined>>;
|
|
115
115
|
export type Unoptionalize<T extends object> = Simplify<OmitBy<T, undefined> & {
|
|
116
|
-
[P in
|
|
116
|
+
[P in PropertiesOfType<T, undefined>]: T[P] | undefined;
|
|
117
117
|
}>;
|
|
118
118
|
export type SimplifiedOptionalize<T extends object> = Simplify<Optionalize<T>>;
|
|
119
119
|
export type Merge<T1, T2> = SimplifyObject<Except<T1, Extract<keyof T1, keyof T2>> & T2>;
|
|
@@ -67,7 +67,7 @@ export interface ReadonlyCancellationToken extends PromiseLike<void>, Subscribab
|
|
|
67
67
|
/**
|
|
68
68
|
* Returns an AbortSignal.
|
|
69
69
|
*/
|
|
70
|
-
asAbortSignal: AbortSignal;
|
|
70
|
+
asAbortSignal(): AbortSignal;
|
|
71
71
|
/**
|
|
72
72
|
* Create a new token and connect it to this instance.
|
|
73
73
|
* @see {@link connect}
|
|
@@ -81,7 +81,6 @@ export interface ReadonlyCancellationToken extends PromiseLike<void>, Subscribab
|
|
|
81
81
|
}
|
|
82
82
|
export declare class CancellationToken implements ReadonlyCancellationToken {
|
|
83
83
|
private readonly stateSubject;
|
|
84
|
-
private abortController;
|
|
85
84
|
readonly state$: Observable<boolean>;
|
|
86
85
|
readonly set$: Observable<void>;
|
|
87
86
|
readonly unset$: Observable<void>;
|
|
@@ -90,8 +89,6 @@ export declare class CancellationToken implements ReadonlyCancellationToken {
|
|
|
90
89
|
get $set(): Promise<void>;
|
|
91
90
|
get $unset(): Promise<void>;
|
|
92
91
|
get $state(): Promise<boolean>;
|
|
93
|
-
get asReadonly(): ReadonlyCancellationToken;
|
|
94
|
-
get asAbortSignal(): AbortSignal;
|
|
95
92
|
/**
|
|
96
93
|
* @param initialState which state to initialze this token to
|
|
97
94
|
* - `false`: unset
|
|
@@ -100,23 +97,21 @@ export declare class CancellationToken implements ReadonlyCancellationToken {
|
|
|
100
97
|
*/
|
|
101
98
|
constructor(initialState?: boolean);
|
|
102
99
|
/**
|
|
103
|
-
* Creates a token and sets it whenever the
|
|
104
|
-
* @param
|
|
105
|
-
* @param complete complete token after
|
|
100
|
+
* Creates a token and sets it whenever the source signals
|
|
101
|
+
* @param source source to listen to
|
|
102
|
+
* @param complete complete token after signal
|
|
106
103
|
*/
|
|
107
|
-
static
|
|
104
|
+
static from(source: AbortSignal | PromiseLike<any> | Observable<void>, options?: {
|
|
105
|
+
complete?: boolean;
|
|
106
|
+
}): CancellationToken;
|
|
108
107
|
/**
|
|
109
|
-
* Creates a token and
|
|
110
|
-
* @param promise promise to await
|
|
111
|
-
* @param complete complete token after resolve
|
|
112
|
-
*/
|
|
113
|
-
static fromPromise(promise: PromiseLike<any>, complete?: boolean): CancellationToken;
|
|
114
|
-
/**
|
|
115
|
-
* Creates a token and connets its next, error and complete.
|
|
108
|
+
* Creates a token and connects the source to its next, error and complete.
|
|
116
109
|
* @param observable observable to subscribe. Takes emitted value as state if type is boolean otherwise sets state to true.
|
|
117
110
|
*/
|
|
118
|
-
static
|
|
111
|
+
static from(source: Observable<boolean>, config?: ConnectConfig): CancellationToken;
|
|
119
112
|
private static connect;
|
|
113
|
+
asAbortSignal(): AbortSignal;
|
|
114
|
+
asReadonly(): ReadonlyCancellationToken;
|
|
120
115
|
createChild(config?: ConnectConfig): CancellationToken;
|
|
121
116
|
connect(child: CancellationToken, config?: ConnectConfig): void;
|
|
122
117
|
/**
|
|
@@ -27,7 +27,6 @@ var import_noop2 = require("./noop.js");
|
|
|
27
27
|
var import_type_guards = require("./type-guards.js");
|
|
28
28
|
class CancellationToken {
|
|
29
29
|
stateSubject;
|
|
30
|
-
abortController;
|
|
31
30
|
state$;
|
|
32
31
|
set$;
|
|
33
32
|
unset$;
|
|
@@ -46,20 +45,6 @@ class CancellationToken {
|
|
|
46
45
|
get $state() {
|
|
47
46
|
return (0, import_rxjs.firstValueFrom)(this.state$.pipe((0, import_rxjs.skip)(1)));
|
|
48
47
|
}
|
|
49
|
-
get asReadonly() {
|
|
50
|
-
return this;
|
|
51
|
-
}
|
|
52
|
-
get asAbortSignal() {
|
|
53
|
-
if ((0, import_type_guards.isUndefined)(this.abortController)) {
|
|
54
|
-
const abortController = new AbortController();
|
|
55
|
-
this.abortController = abortController;
|
|
56
|
-
this.set$.pipe((0, import_rxjs.first)()).subscribe(() => {
|
|
57
|
-
abortController.abort();
|
|
58
|
-
this.unset$.pipe((0, import_rxjs.first)()).subscribe(() => this.abortController = void 0);
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
return this.abortController.signal;
|
|
62
|
-
}
|
|
63
48
|
/**
|
|
64
49
|
* @param initialState which state to initialze this token to
|
|
65
50
|
* - `false`: unset
|
|
@@ -72,31 +57,9 @@ class CancellationToken {
|
|
|
72
57
|
this.set$ = this.state$.pipe((0, import_rxjs.filter)((state) => state), (0, import_rxjs.map)(() => void 0));
|
|
73
58
|
this.unset$ = this.state$.pipe((0, import_rxjs.filter)((state) => !state), (0, import_rxjs.map)(() => void 0));
|
|
74
59
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
* @param signal abort signal to listen to
|
|
78
|
-
* @param complete complete token after resolve
|
|
79
|
-
*/
|
|
80
|
-
static fromAbortSignal(signal, complete = true) {
|
|
81
|
-
const signal$ = (0, import_rxjs.fromEvent)(signal, "abort", () => true);
|
|
82
|
-
return CancellationToken.fromObservable(signal$, { complete });
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Creates a token and sets it whenever the promise is resolved.
|
|
86
|
-
* @param promise promise to await
|
|
87
|
-
* @param complete complete token after resolve
|
|
88
|
-
*/
|
|
89
|
-
static fromPromise(promise, complete = true) {
|
|
90
|
-
const signal$ = (0, import_rxjs.from)(promise).pipe((0, import_rxjs.map)(() => true));
|
|
91
|
-
return CancellationToken.fromObservable(signal$, { complete });
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Creates a token and connets its next, error and complete.
|
|
95
|
-
* @param observable observable to subscribe. Takes emitted value as state if type is boolean otherwise sets state to true.
|
|
96
|
-
*/
|
|
97
|
-
static fromObservable(observable, config) {
|
|
60
|
+
static from(source, config) {
|
|
61
|
+
const source$ = source instanceof AbortSignal ? (0, import_rxjs.fromEvent)(source, "abort", () => true) : (0, import_rxjs.isObservable)(source) ? source.pipe((0, import_rxjs.map)((state) => (0, import_type_guards.isBoolean)(state) ? state : true)) : (0, import_rxjs.from)(source).pipe((0, import_rxjs.map)(() => true));
|
|
98
62
|
const token = new CancellationToken();
|
|
99
|
-
const source$ = observable.pipe((0, import_rxjs.map)((state) => (0, import_type_guards.isBoolean)(state) ? state : true));
|
|
100
63
|
CancellationToken.connect(source$, token, config);
|
|
101
64
|
return token;
|
|
102
65
|
}
|
|
@@ -113,6 +76,14 @@ class CancellationToken {
|
|
|
113
76
|
complete: () => subscription.unsubscribe()
|
|
114
77
|
});
|
|
115
78
|
}
|
|
79
|
+
asAbortSignal() {
|
|
80
|
+
const abortController = new AbortController();
|
|
81
|
+
this.set$.pipe((0, import_rxjs.first)()).subscribe(() => abortController.abort());
|
|
82
|
+
return abortController.signal;
|
|
83
|
+
}
|
|
84
|
+
asReadonly() {
|
|
85
|
+
return this;
|
|
86
|
+
}
|
|
116
87
|
createChild(config) {
|
|
117
88
|
const child = new CancellationToken();
|
|
118
89
|
this.connect(child, config);
|
package/utils/timing.d.ts
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
import type { ReadonlyCancellationToken } from './cancellation-token.js';
|
|
2
|
+
import type { ValueOrProvider } from './value-or-provider.js';
|
|
2
3
|
/** timeout for specified duration */
|
|
3
|
-
export declare function timeout(milliseconds?: number
|
|
4
|
+
export declare function timeout(milliseconds?: number, options?: {
|
|
5
|
+
abortSignal?: AbortSignal;
|
|
6
|
+
}): Promise<void>;
|
|
4
7
|
/** timeout until specified time */
|
|
5
8
|
export declare function timeoutUntil(timestamp: number | Date): Promise<void>;
|
|
6
9
|
/** timeout for specified duration */
|
|
7
10
|
export declare function cancelableTimeout(milliseconds: number, cancelToken: ReadonlyCancellationToken): Promise<boolean>;
|
|
8
11
|
/** timeout until specified time */
|
|
9
12
|
export declare function cancelableTimeoutUntil(timestamp: number | Date, cancelToken: ReadonlyCancellationToken): Promise<boolean>;
|
|
13
|
+
export declare function withTimeout<T>(milliseconds: number, promiseOrProvider: ValueOrProvider<Promise<T>>, options?: {
|
|
14
|
+
errorMessage?: string;
|
|
15
|
+
}): Promise<T>;
|
|
10
16
|
export declare function immediate(): Promise<void>;
|
|
11
17
|
export declare function nextTick(): Promise<void>;
|
|
12
18
|
export declare function animationFrame(): Promise<number>;
|
package/utils/timing.js
CHANGED
|
@@ -25,12 +25,23 @@ __export(timing_exports, {
|
|
|
25
25
|
immediate: () => immediate,
|
|
26
26
|
nextTick: () => nextTick,
|
|
27
27
|
timeout: () => timeout,
|
|
28
|
-
timeoutUntil: () => timeoutUntil
|
|
28
|
+
timeoutUntil: () => timeoutUntil,
|
|
29
|
+
withTimeout: () => withTimeout
|
|
29
30
|
});
|
|
30
31
|
module.exports = __toCommonJS(timing_exports);
|
|
32
|
+
var import_timeout_error = require("../error/timeout.error.js");
|
|
31
33
|
var import_rxjs = require("rxjs");
|
|
32
|
-
|
|
33
|
-
|
|
34
|
+
var import_throw = require("./throw.js");
|
|
35
|
+
var import_value_or_provider = require("./value-or-provider.js");
|
|
36
|
+
async function timeout(milliseconds = 0, options) {
|
|
37
|
+
return new Promise((resolve) => {
|
|
38
|
+
const abortListener = () => clearTimeout(timeoutRef);
|
|
39
|
+
const timeoutRef = setTimeout(() => {
|
|
40
|
+
options?.abortSignal?.removeEventListener("abort", abortListener);
|
|
41
|
+
resolve();
|
|
42
|
+
}, milliseconds);
|
|
43
|
+
options?.abortSignal?.addEventListener("abort", abortListener);
|
|
44
|
+
});
|
|
34
45
|
}
|
|
35
46
|
async function timeoutUntil(timestamp) {
|
|
36
47
|
const left = timestamp.valueOf() - Date.now();
|
|
@@ -47,6 +58,15 @@ async function cancelableTimeoutUntil(timestamp, cancelToken) {
|
|
|
47
58
|
const left = timestamp.valueOf() - Date.now();
|
|
48
59
|
return cancelableTimeout(left, cancelToken);
|
|
49
60
|
}
|
|
61
|
+
async function withTimeout(milliseconds, promiseOrProvider, options) {
|
|
62
|
+
const abortController = new AbortController();
|
|
63
|
+
const promise = (0, import_value_or_provider.resolveValueOrProvider)(promiseOrProvider);
|
|
64
|
+
void promise.then(() => abortController.abort());
|
|
65
|
+
return Promise.race([
|
|
66
|
+
promise,
|
|
67
|
+
timeout(milliseconds, { abortSignal: abortController.signal }).then(() => (0, import_throw._throw)(new import_timeout_error.TimeoutError(options?.errorMessage)))
|
|
68
|
+
]);
|
|
69
|
+
}
|
|
50
70
|
async function immediate() {
|
|
51
71
|
return new Promise(setImmediate);
|
|
52
72
|
}
|
package/browser/types.d.ts
DELETED