@ad-sdk/bgd 0.0.2 → 0.0.4

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.
@@ -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
+ }
@@ -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/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  export * from "./allocators";
2
2
  export * from "./lifecycle";
3
3
  export * from "./util";
4
+ export * from "./cancellation";
4
5
  export * from "./errors";
6
+ export * from "./runtime";
5
7
  export * from "./types";
package/index.js CHANGED
@@ -36,6 +36,17 @@ Object.keys(_util).forEach(function (key) {
36
36
  }
37
37
  });
38
38
  });
39
+ var _cancellation = require("./cancellation");
40
+ Object.keys(_cancellation).forEach(function (key) {
41
+ if (key === "default" || key === "__esModule") return;
42
+ if (key in exports && exports[key] === _cancellation[key]) return;
43
+ Object.defineProperty(exports, key, {
44
+ enumerable: true,
45
+ get: function () {
46
+ return _cancellation[key];
47
+ }
48
+ });
49
+ });
39
50
  var _errors = require("./errors");
40
51
  Object.keys(_errors).forEach(function (key) {
41
52
  if (key === "default" || key === "__esModule") return;
@@ -47,6 +58,17 @@ Object.keys(_errors).forEach(function (key) {
47
58
  }
48
59
  });
49
60
  });
61
+ var _runtime = require("./runtime");
62
+ Object.keys(_runtime).forEach(function (key) {
63
+ if (key === "default" || key === "__esModule") return;
64
+ if (key in exports && exports[key] === _runtime[key]) return;
65
+ Object.defineProperty(exports, key, {
66
+ enumerable: true,
67
+ get: function () {
68
+ return _runtime[key];
69
+ }
70
+ });
71
+ });
50
72
  var _types = require("./types");
51
73
  Object.keys(_types).forEach(function (key) {
52
74
  if (key === "default" || key === "__esModule") return;
package/mathlib/core.d.ts CHANGED
@@ -13,5 +13,6 @@ export declare namespace MathLib {
13
13
  [K in keyof T]: T[K] extends (infer U)[] ? U : never;
14
14
  }[];
15
15
  export function zip<T extends unknown[][]>(...args: T): ZipResult<T>;
16
+ export function clamp(x: number, min: number, max: number): number;
16
17
  export {};
17
18
  }
package/mathlib/core.js CHANGED
@@ -26,4 +26,8 @@ let MathLib = exports.MathLib = void 0;
26
26
  return result;
27
27
  }
28
28
  _MathLib.zip = zip;
29
+ function clamp(x, min, max) {
30
+ return Math.min(Math.max(x, min), max);
31
+ }
32
+ _MathLib.clamp = clamp;
29
33
  })(MathLib || (exports.MathLib = MathLib = {}));
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ad-sdk/bgd",
3
- "version": "0.0.2",
4
- "main": "dist/index.js",
3
+ "version": "0.0.4",
4
+ "main": "index.js",
5
5
  "license": "BSD-3-Clause",
6
6
  "repository": {
7
7
  "type": "git",
package/runtime.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { type IDisposable } from "./lifecycle";
2
+ export declare function immediate<TArgs extends unknown[]>(callback: (...args: TArgs) => void, ...args: TArgs): IDisposable & Disposable;
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
+ }