@ad-sdk/bgd 0.0.2 → 0.0.3
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/cancellation.d.ts +74 -0
- package/cancellation.js +185 -0
- package/mathlib/core.d.ts +1 -0
- package/mathlib/core.js +4 -0
- package/package.json +2 -2
- package/runtime.d.ts +2 -0
- package/runtime.js +26 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { type IDisposable } from "./lifecycle/disposable";
|
|
2
|
+
/**
|
|
3
|
+
* Type defining a listener function for cancellation requests.
|
|
4
|
+
*
|
|
5
|
+
* @callback CancellationRequestListener
|
|
6
|
+
* @param listener - A function to handle the cancellation event.
|
|
7
|
+
* @param thisArgs - Optional `this` context for the listener function.
|
|
8
|
+
* @param disposables - Optional array to collect IDisposable resources.
|
|
9
|
+
* @returns {IDisposable} A disposable object to cancel the listener.
|
|
10
|
+
*/
|
|
11
|
+
export type CancellationRequestListener = (listener: (e: unknown) => unknown, thisArgs?: unknown, disposables?: IDisposable[]) => IDisposable;
|
|
12
|
+
/**
|
|
13
|
+
* Interface representing a cancellation token that signals whether cancellation has been requested
|
|
14
|
+
* and includes an event listener for responding to cancellation requests.
|
|
15
|
+
*/
|
|
16
|
+
export interface ICancellationToken {
|
|
17
|
+
/**
|
|
18
|
+
* A flag signalling is cancellation has been requested.
|
|
19
|
+
*/
|
|
20
|
+
readonly isCancellationRequested: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* An event which fires when cancellation is requested. This event
|
|
23
|
+
* only ever fires `once` as cancellation can only happen once. Listeners
|
|
24
|
+
* that are registered after cancellation will be called (next event loop run),
|
|
25
|
+
* but also only once.
|
|
26
|
+
*
|
|
27
|
+
* @event
|
|
28
|
+
*/
|
|
29
|
+
readonly onCancellationRequested: CancellationRequestListener;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Checks if the provided argument is an ICancellationToken.
|
|
33
|
+
*
|
|
34
|
+
* @param arg - The object to check.
|
|
35
|
+
* @returns {boolean} `true` if `arg` is an ICancellationToken, otherwise `false`.
|
|
36
|
+
*/
|
|
37
|
+
export declare function isCancellationToken(arg: unknown): arg is ICancellationToken;
|
|
38
|
+
export declare namespace CancellationToken {
|
|
39
|
+
const None: Readonly<ICancellationToken>;
|
|
40
|
+
const Cancelled: Readonly<ICancellationToken>;
|
|
41
|
+
}
|
|
42
|
+
export declare class CancellationTokenSource {
|
|
43
|
+
private readonly _parent?;
|
|
44
|
+
private _token?;
|
|
45
|
+
private _parentListener?;
|
|
46
|
+
/**
|
|
47
|
+
* Constructs a CancellationTokenSource, optionally linked to a parent token to cascade cancellations.
|
|
48
|
+
*
|
|
49
|
+
* @param _parent - Optional parent cancellation token.
|
|
50
|
+
*/
|
|
51
|
+
constructor(_parent?: ICancellationToken | undefined);
|
|
52
|
+
/**
|
|
53
|
+
* Gets the associated cancellation token, creating it if necessary.
|
|
54
|
+
*/
|
|
55
|
+
get token(): ICancellationToken;
|
|
56
|
+
/**
|
|
57
|
+
* Gets the parent cancellation token, if any.
|
|
58
|
+
*/
|
|
59
|
+
get parent(): ICancellationToken | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* Requests cancellation for the token, optionally providing a reason and a stack trace location.
|
|
62
|
+
*
|
|
63
|
+
* @param reason - Optional reason for the cancellation.
|
|
64
|
+
* @param location - Optional stack trace location.
|
|
65
|
+
*/
|
|
66
|
+
cancel(reason?: unknown): void;
|
|
67
|
+
/**
|
|
68
|
+
* Disposes of the token source, optionally canceling it before disposal.
|
|
69
|
+
*
|
|
70
|
+
* @param cancel - If `true`, requests cancellation before disposal.
|
|
71
|
+
* @param cancellationReason - Optional reason for cancellation.
|
|
72
|
+
*/
|
|
73
|
+
dispose(cancel?: boolean, cancellationReason?: unknown): void;
|
|
74
|
+
}
|
package/cancellation.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.CancellationTokenSource = exports.CancellationToken = void 0;
|
|
7
|
+
exports.isCancellationToken = isCancellationToken;
|
|
8
|
+
var _nodeEvents = require("node:events");
|
|
9
|
+
var _runtime = require("./runtime");
|
|
10
|
+
var _disposable = require("./lifecycle/disposable");
|
|
11
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
12
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
13
|
+
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
14
|
+
/**
|
|
15
|
+
* Type defining a listener function for cancellation requests.
|
|
16
|
+
*
|
|
17
|
+
* @callback CancellationRequestListener
|
|
18
|
+
* @param listener - A function to handle the cancellation event.
|
|
19
|
+
* @param thisArgs - Optional `this` context for the listener function.
|
|
20
|
+
* @param disposables - Optional array to collect IDisposable resources.
|
|
21
|
+
* @returns {IDisposable} A disposable object to cancel the listener.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Interface representing a cancellation token that signals whether cancellation has been requested
|
|
26
|
+
* and includes an event listener for responding to cancellation requests.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
const shortcutEvent = Object.freeze(function (callback, context) {
|
|
30
|
+
return (0, _runtime.immediate)(callback.bind(context));
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Checks if the provided argument is an ICancellationToken.
|
|
35
|
+
*
|
|
36
|
+
* @param arg - The object to check.
|
|
37
|
+
* @returns {boolean} `true` if `arg` is an ICancellationToken, otherwise `false`.
|
|
38
|
+
*/
|
|
39
|
+
function isCancellationToken(arg) {
|
|
40
|
+
if (typeof arg !== "object" || !arg || Array.isArray(arg)) return false;
|
|
41
|
+
const candidate = arg;
|
|
42
|
+
return typeof candidate.isCancellationRequested === "boolean" && typeof candidate.onCancellationRequested === "function";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
46
|
+
let CancellationToken = exports.CancellationToken = void 0;
|
|
47
|
+
(function (_CancellationToken) {
|
|
48
|
+
const None = _CancellationToken.None = Object.freeze({
|
|
49
|
+
isCancellationRequested: false,
|
|
50
|
+
onCancellationRequested: () => Object.freeze({
|
|
51
|
+
dispose() {}
|
|
52
|
+
})
|
|
53
|
+
});
|
|
54
|
+
const Cancelled = _CancellationToken.Cancelled = Object.freeze({
|
|
55
|
+
isCancellationRequested: true,
|
|
56
|
+
onCancellationRequested: shortcutEvent
|
|
57
|
+
});
|
|
58
|
+
})(CancellationToken || (exports.CancellationToken = CancellationToken = {}));
|
|
59
|
+
class MutableToken extends _disposable.Disposable {
|
|
60
|
+
constructor(...args) {
|
|
61
|
+
super(...args);
|
|
62
|
+
_defineProperty(this, "_isCancelled", false);
|
|
63
|
+
_defineProperty(this, "_emitter", null);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Returns whether cancellation has been requested.
|
|
67
|
+
*/
|
|
68
|
+
get isCancellationRequested() {
|
|
69
|
+
return this._isCancelled;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Returns an event listener for cancellation requests. If cancellation has been requested, returns an immediate event.
|
|
74
|
+
*/
|
|
75
|
+
get onCancellationRequested() {
|
|
76
|
+
if (this._isCancelled) return shortcutEvent;
|
|
77
|
+
if (!this._emitter) {
|
|
78
|
+
this._emitter = new _nodeEvents.EventEmitter({
|
|
79
|
+
captureRejections: false
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return (listener, thisArgs, disposables) => {
|
|
83
|
+
if (!this._isCancelled) {
|
|
84
|
+
this._emitter?.addListener("cancellationrequest", thisArgs ? listener.bind(thisArgs) : listener);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (disposables && Array.isArray(disposables)) {
|
|
88
|
+
disposables.push(shortcutEvent(listener, thisArgs));
|
|
89
|
+
for (const d of disposables) {
|
|
90
|
+
super._register(d);
|
|
91
|
+
}
|
|
92
|
+
return void 0;
|
|
93
|
+
}
|
|
94
|
+
return listener.call(thisArgs, void 0);
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Requests cancellation, firing the cancellation event if applicable.
|
|
100
|
+
*
|
|
101
|
+
* @param reason - Optional reason for the cancellation.
|
|
102
|
+
*/
|
|
103
|
+
cancel(reason) {
|
|
104
|
+
if (this._isCancelled) return;
|
|
105
|
+
this._isCancelled = true;
|
|
106
|
+
if (!this._emitter) return this.dispose();
|
|
107
|
+
this._emitter.emit("cancellationrequest", reason ?? void 0);
|
|
108
|
+
this.dispose();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Disposes of the resources associated with the cancellation token.
|
|
113
|
+
*/
|
|
114
|
+
dispose() {
|
|
115
|
+
if (this._emitter instanceof _nodeEvents.EventEmitter) {
|
|
116
|
+
this._emitter?.removeAllListeners();
|
|
117
|
+
this._emitter = null;
|
|
118
|
+
}
|
|
119
|
+
super.dispose();
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
class CancellationTokenSource {
|
|
123
|
+
/**
|
|
124
|
+
* Constructs a CancellationTokenSource, optionally linked to a parent token to cascade cancellations.
|
|
125
|
+
*
|
|
126
|
+
* @param _parent - Optional parent cancellation token.
|
|
127
|
+
*/
|
|
128
|
+
constructor(_parent) {
|
|
129
|
+
_defineProperty(this, "_token", null);
|
|
130
|
+
_defineProperty(this, "_parentListener", null);
|
|
131
|
+
this._parent = _parent;
|
|
132
|
+
if (!_parent) return;
|
|
133
|
+
this._parentListener = _parent.onCancellationRequested(this.cancel, this);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Gets the associated cancellation token, creating it if necessary.
|
|
138
|
+
*/
|
|
139
|
+
get token() {
|
|
140
|
+
if (!this._token) {
|
|
141
|
+
this._token = new MutableToken();
|
|
142
|
+
}
|
|
143
|
+
return this._token;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Gets the parent cancellation token, if any.
|
|
148
|
+
*/
|
|
149
|
+
get parent() {
|
|
150
|
+
return this._parent;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Requests cancellation for the token, optionally providing a reason and a stack trace location.
|
|
155
|
+
*
|
|
156
|
+
* @param reason - Optional reason for the cancellation.
|
|
157
|
+
* @param location - Optional stack trace location.
|
|
158
|
+
*/
|
|
159
|
+
cancel(reason) {
|
|
160
|
+
if (!this._token) {
|
|
161
|
+
this._token = CancellationToken.Cancelled;
|
|
162
|
+
} else if (this._token instanceof MutableToken) {
|
|
163
|
+
this._token.cancel(reason);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Disposes of the token source, optionally canceling it before disposal.
|
|
169
|
+
*
|
|
170
|
+
* @param cancel - If `true`, requests cancellation before disposal.
|
|
171
|
+
* @param cancellationReason - Optional reason for cancellation.
|
|
172
|
+
*/
|
|
173
|
+
dispose(cancel = false, cancellationReason) {
|
|
174
|
+
if (cancel === true) {
|
|
175
|
+
this.cancel(cancellationReason);
|
|
176
|
+
}
|
|
177
|
+
this._parentListener?.dispose();
|
|
178
|
+
if (!this._token) {
|
|
179
|
+
this._token = CancellationToken.None;
|
|
180
|
+
} else if (this._token instanceof MutableToken) {
|
|
181
|
+
this._token.dispose();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
exports.CancellationTokenSource = CancellationTokenSource;
|
package/mathlib/core.d.ts
CHANGED
package/mathlib/core.js
CHANGED
package/package.json
CHANGED
package/runtime.d.ts
ADDED
package/runtime.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.immediate = immediate;
|
|
7
|
+
function immediate(callback, ...args) {
|
|
8
|
+
const hasNativeMethod = typeof setImmediate === "function";
|
|
9
|
+
const id = hasNativeMethod ? setImmediate(callback, ...args) : setTimeout(callback, 0, ...args);
|
|
10
|
+
return {
|
|
11
|
+
dispose() {
|
|
12
|
+
if (hasNativeMethod) {
|
|
13
|
+
clearImmediate(id);
|
|
14
|
+
} else {
|
|
15
|
+
clearTimeout(id);
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
[Symbol.dispose]() {
|
|
19
|
+
if (hasNativeMethod) {
|
|
20
|
+
clearImmediate(id);
|
|
21
|
+
} else {
|
|
22
|
+
clearTimeout(id);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|