@itwin/core-bentley 4.0.0-dev.52 → 4.0.0-dev.55
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/lib/cjs/AccessToken.d.ts +10 -10
- package/lib/cjs/AccessToken.d.ts.map +1 -1
- package/lib/cjs/AccessToken.js +9 -9
- package/lib/cjs/Assert.d.ts +25 -25
- package/lib/cjs/Assert.js +45 -45
- package/lib/cjs/BeEvent.d.ts +81 -81
- package/lib/cjs/BeEvent.d.ts.map +1 -1
- package/lib/cjs/BeEvent.js +156 -156
- package/lib/cjs/BeSQLite.d.ts +172 -172
- package/lib/cjs/BeSQLite.js +185 -185
- package/lib/cjs/BentleyError.d.ts +378 -378
- package/lib/cjs/BentleyError.d.ts.map +1 -1
- package/lib/cjs/BentleyError.js +703 -703
- package/lib/cjs/BentleyLoggerCategory.d.ts +11 -11
- package/lib/cjs/BentleyLoggerCategory.js +19 -19
- package/lib/cjs/ByteStream.d.ts +110 -110
- package/lib/cjs/ByteStream.js +159 -159
- package/lib/cjs/ClassUtils.d.ts +14 -14
- package/lib/cjs/ClassUtils.js +27 -27
- package/lib/cjs/Compare.d.ts +47 -47
- package/lib/cjs/Compare.d.ts.map +1 -1
- package/lib/cjs/Compare.js +75 -75
- package/lib/cjs/CompressedId64Set.d.ts +134 -134
- package/lib/cjs/CompressedId64Set.d.ts.map +1 -1
- package/lib/cjs/CompressedId64Set.js +428 -428
- package/lib/cjs/CompressedId64Set.js.map +1 -1
- package/lib/cjs/Dictionary.d.ts +125 -125
- package/lib/cjs/Dictionary.js +203 -203
- package/lib/cjs/Disposable.d.ts +80 -80
- package/lib/cjs/Disposable.d.ts.map +1 -1
- package/lib/cjs/Disposable.js +120 -120
- package/lib/cjs/Id.d.ts +285 -285
- package/lib/cjs/Id.d.ts.map +1 -1
- package/lib/cjs/Id.js +643 -643
- package/lib/cjs/IndexMap.d.ts +65 -65
- package/lib/cjs/IndexMap.js +91 -91
- package/lib/cjs/JsonSchema.d.ts +77 -77
- package/lib/cjs/JsonSchema.d.ts.map +1 -1
- package/lib/cjs/JsonSchema.js +9 -9
- package/lib/cjs/JsonUtils.d.ts +78 -78
- package/lib/cjs/JsonUtils.js +151 -151
- package/lib/cjs/LRUMap.d.ts +129 -129
- package/lib/cjs/LRUMap.js +333 -333
- package/lib/cjs/LRUMap.js.map +1 -1
- package/lib/cjs/Logger.d.ts +143 -143
- package/lib/cjs/Logger.d.ts.map +1 -1
- package/lib/cjs/Logger.js +256 -256
- package/lib/cjs/Logger.js.map +1 -1
- package/lib/cjs/ObservableSet.d.ts +23 -23
- package/lib/cjs/ObservableSet.js +51 -51
- package/lib/cjs/OneAtATimeAction.d.ts +31 -31
- package/lib/cjs/OneAtATimeAction.js +94 -94
- package/lib/cjs/OrderedId64Iterable.d.ts +74 -74
- package/lib/cjs/OrderedId64Iterable.d.ts.map +1 -1
- package/lib/cjs/OrderedId64Iterable.js +235 -235
- package/lib/cjs/OrderedSet.d.ts +40 -40
- package/lib/cjs/OrderedSet.js +64 -64
- package/lib/cjs/PriorityQueue.d.ts +70 -70
- package/lib/cjs/PriorityQueue.d.ts.map +1 -1
- package/lib/cjs/PriorityQueue.js +140 -140
- package/lib/cjs/ProcessDetector.d.ts +59 -59
- package/lib/cjs/ProcessDetector.js +71 -71
- package/lib/cjs/SortedArray.d.ts +236 -236
- package/lib/cjs/SortedArray.d.ts.map +1 -1
- package/lib/cjs/SortedArray.js +315 -315
- package/lib/cjs/StatusCategory.d.ts +30 -30
- package/lib/cjs/StatusCategory.d.ts.map +1 -1
- package/lib/cjs/StatusCategory.js +460 -460
- package/lib/cjs/StatusCategory.js.map +1 -1
- package/lib/cjs/StringUtils.d.ts +22 -22
- package/lib/cjs/StringUtils.js +148 -148
- package/lib/cjs/Time.d.ts +122 -122
- package/lib/cjs/Time.js +152 -152
- package/lib/cjs/Time.js.map +1 -1
- package/lib/cjs/Tracing.d.ts +40 -40
- package/lib/cjs/Tracing.js +127 -127
- package/lib/cjs/TupleKeyedMap.d.ts +36 -36
- package/lib/cjs/TupleKeyedMap.js +102 -102
- package/lib/cjs/TypedArrayBuilder.d.ts +155 -155
- package/lib/cjs/TypedArrayBuilder.d.ts.map +1 -1
- package/lib/cjs/TypedArrayBuilder.js +206 -206
- package/lib/cjs/UnexpectedErrors.d.ts +43 -43
- package/lib/cjs/UnexpectedErrors.d.ts.map +1 -1
- package/lib/cjs/UnexpectedErrors.js +68 -68
- package/lib/cjs/UnexpectedErrors.js.map +1 -1
- package/lib/cjs/UtilityTypes.d.ts +112 -112
- package/lib/cjs/UtilityTypes.d.ts.map +1 -1
- package/lib/cjs/UtilityTypes.js +40 -40
- package/lib/cjs/YieldManager.d.ts +18 -18
- package/lib/cjs/YieldManager.js +34 -34
- package/lib/cjs/core-bentley.d.ts +74 -74
- package/lib/cjs/core-bentley.js +94 -90
- package/lib/cjs/core-bentley.js.map +1 -1
- package/lib/cjs/partitionArray.d.ts +21 -21
- package/lib/cjs/partitionArray.js +43 -43
- package/lib/esm/AccessToken.d.ts +10 -10
- package/lib/esm/AccessToken.d.ts.map +1 -1
- package/lib/esm/AccessToken.js +8 -8
- package/lib/esm/Assert.d.ts +25 -25
- package/lib/esm/Assert.js +41 -41
- package/lib/esm/BeEvent.d.ts +81 -81
- package/lib/esm/BeEvent.d.ts.map +1 -1
- package/lib/esm/BeEvent.js +150 -150
- package/lib/esm/BeSQLite.d.ts +172 -172
- package/lib/esm/BeSQLite.js +182 -182
- package/lib/esm/BentleyError.d.ts +378 -378
- package/lib/esm/BentleyError.d.ts.map +1 -1
- package/lib/esm/BentleyError.js +699 -699
- package/lib/esm/BentleyLoggerCategory.d.ts +11 -11
- package/lib/esm/BentleyLoggerCategory.js +16 -16
- package/lib/esm/ByteStream.d.ts +110 -110
- package/lib/esm/ByteStream.js +155 -155
- package/lib/esm/ClassUtils.d.ts +14 -14
- package/lib/esm/ClassUtils.js +22 -22
- package/lib/esm/Compare.d.ts +47 -47
- package/lib/esm/Compare.d.ts.map +1 -1
- package/lib/esm/Compare.js +63 -63
- package/lib/esm/CompressedId64Set.d.ts +134 -134
- package/lib/esm/CompressedId64Set.d.ts.map +1 -1
- package/lib/esm/CompressedId64Set.js +423 -423
- package/lib/esm/CompressedId64Set.js.map +1 -1
- package/lib/esm/Dictionary.d.ts +125 -125
- package/lib/esm/Dictionary.js +199 -199
- package/lib/esm/Disposable.d.ts +80 -80
- package/lib/esm/Disposable.d.ts.map +1 -1
- package/lib/esm/Disposable.js +112 -112
- package/lib/esm/Id.d.ts +285 -285
- package/lib/esm/Id.d.ts.map +1 -1
- package/lib/esm/Id.js +639 -639
- package/lib/esm/IndexMap.d.ts +65 -65
- package/lib/esm/IndexMap.js +86 -86
- package/lib/esm/JsonSchema.d.ts +77 -77
- package/lib/esm/JsonSchema.d.ts.map +1 -1
- package/lib/esm/JsonSchema.js +8 -8
- package/lib/esm/JsonUtils.d.ts +78 -78
- package/lib/esm/JsonUtils.js +148 -148
- package/lib/esm/LRUMap.d.ts +129 -129
- package/lib/esm/LRUMap.js +326 -326
- package/lib/esm/LRUMap.js.map +1 -1
- package/lib/esm/Logger.d.ts +143 -143
- package/lib/esm/Logger.d.ts.map +1 -1
- package/lib/esm/Logger.js +253 -251
- package/lib/esm/Logger.js.map +1 -1
- package/lib/esm/ObservableSet.d.ts +23 -23
- package/lib/esm/ObservableSet.js +47 -47
- package/lib/esm/OneAtATimeAction.d.ts +31 -31
- package/lib/esm/OneAtATimeAction.js +89 -89
- package/lib/esm/OrderedId64Iterable.d.ts +74 -74
- package/lib/esm/OrderedId64Iterable.d.ts.map +1 -1
- package/lib/esm/OrderedId64Iterable.js +232 -232
- package/lib/esm/OrderedSet.d.ts +40 -40
- package/lib/esm/OrderedSet.js +59 -59
- package/lib/esm/PriorityQueue.d.ts +70 -70
- package/lib/esm/PriorityQueue.d.ts.map +1 -1
- package/lib/esm/PriorityQueue.js +136 -136
- package/lib/esm/ProcessDetector.d.ts +59 -59
- package/lib/esm/ProcessDetector.js +67 -67
- package/lib/esm/SortedArray.d.ts +236 -236
- package/lib/esm/SortedArray.d.ts.map +1 -1
- package/lib/esm/SortedArray.js +308 -308
- package/lib/esm/StatusCategory.d.ts +30 -30
- package/lib/esm/StatusCategory.d.ts.map +1 -1
- package/lib/esm/StatusCategory.js +455 -454
- package/lib/esm/StatusCategory.js.map +1 -1
- package/lib/esm/StringUtils.d.ts +22 -22
- package/lib/esm/StringUtils.js +142 -142
- package/lib/esm/Time.d.ts +122 -122
- package/lib/esm/Time.js +146 -146
- package/lib/esm/Time.js.map +1 -1
- package/lib/esm/Tracing.d.ts +40 -40
- package/lib/esm/Tracing.js +123 -123
- package/lib/esm/TupleKeyedMap.d.ts +36 -36
- package/lib/esm/TupleKeyedMap.js +98 -98
- package/lib/esm/TypedArrayBuilder.d.ts +155 -155
- package/lib/esm/TypedArrayBuilder.d.ts.map +1 -1
- package/lib/esm/TypedArrayBuilder.js +198 -198
- package/lib/esm/UnexpectedErrors.d.ts +43 -43
- package/lib/esm/UnexpectedErrors.d.ts.map +1 -1
- package/lib/esm/UnexpectedErrors.js +65 -64
- package/lib/esm/UnexpectedErrors.js.map +1 -1
- package/lib/esm/UtilityTypes.d.ts +112 -112
- package/lib/esm/UtilityTypes.d.ts.map +1 -1
- package/lib/esm/UtilityTypes.js +34 -34
- package/lib/esm/YieldManager.d.ts +18 -18
- package/lib/esm/YieldManager.js +30 -30
- package/lib/esm/core-bentley.d.ts +74 -74
- package/lib/esm/core-bentley.js +78 -78
- package/lib/esm/partitionArray.d.ts +21 -21
- package/lib/esm/partitionArray.js +39 -39
- package/package.json +4 -4
|
@@ -1,199 +1,199 @@
|
|
|
1
|
-
/*---------------------------------------------------------------------------------------------
|
|
2
|
-
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
|
|
3
|
-
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
4
|
-
*--------------------------------------------------------------------------------------------*/
|
|
5
|
-
/** @packageDocumentation
|
|
6
|
-
* @module Collections
|
|
7
|
-
*/
|
|
8
|
-
import { assert } from "./Assert";
|
|
9
|
-
/** Incrementally builds a [TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) of unsigned 8-, 16-, or 32-bit integers.
|
|
10
|
-
* Sometimes you wish to populate a `TypedArray`, but you don't know how many elements you will need.
|
|
11
|
-
* `TypedArray` requires you to specify the size upon construction, and does not permit you to change the size later.
|
|
12
|
-
*
|
|
13
|
-
* `TypedArrayBuilder` manages a `TypedArray`, permitting you to [[push]] and [[append]] elements to it. It exposes two "size" properties":
|
|
14
|
-
* - [[capacity]], the number of elements it has currently allocated space for - i.e., the length of the underlying TypedArray; and
|
|
15
|
-
* - [[length]], the number of elements that have so far been added to it, which is never bigger than [[capacity]].
|
|
16
|
-
* When [[capacity]] is exceeded, a new, bigger TypedArray is allocated and the contents of the previous array are copied over to it.
|
|
17
|
-
*
|
|
18
|
-
* Once you've finished adding elements, you can obtain the finished `TypedArray` via [[toTypedArray]].
|
|
19
|
-
* @see [[Uint8ArrayBuilder]], [[Uint16ArrayBuilder]], and [[Uint32ArrayBuilder]] to build specific types of arrays.
|
|
20
|
-
* @see [[UintArrayBuilder]] when you don't know the maximum number of bytes required for each element in the array.
|
|
21
|
-
* @public
|
|
22
|
-
*/
|
|
23
|
-
export class TypedArrayBuilder {
|
|
24
|
-
/** Constructs a new builder from the specified options, with a [[length]] of zero. */
|
|
25
|
-
constructor(constructor, options) {
|
|
26
|
-
this._constructor = constructor;
|
|
27
|
-
this._data = new constructor(options?.initialCapacity ?? 0);
|
|
28
|
-
this.growthFactor = Math.max(1.0, options?.growthFactor ?? 1.5);
|
|
29
|
-
this._length = 0;
|
|
30
|
-
}
|
|
31
|
-
/** The number of elements currently in the array. */
|
|
32
|
-
get length() {
|
|
33
|
-
return this._length;
|
|
34
|
-
}
|
|
35
|
-
/** The number of elements that can fit into the memory currently allocated for the array. */
|
|
36
|
-
get capacity() {
|
|
37
|
-
return this._data.length;
|
|
38
|
-
}
|
|
39
|
-
/** Like [TypedArray.at](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/at),
|
|
40
|
-
* returns the element at the specified index, with negative integers counting back from the end of the array.
|
|
41
|
-
* @note It is your responsibility to ensure the index falls within the bounds of the array.
|
|
42
|
-
*/
|
|
43
|
-
at(index) {
|
|
44
|
-
if (index < 0)
|
|
45
|
-
index = this.length - index;
|
|
46
|
-
const value = this._data[index];
|
|
47
|
-
assert(value !== undefined, "index out of bounds");
|
|
48
|
-
return value;
|
|
49
|
-
}
|
|
50
|
-
/** Ensure that [[capacity]] is at least equal to `newCapacity`.
|
|
51
|
-
* This is used internally by methods like [[push]] and [[append]] to ensure the array has room for the element(s) to be added.
|
|
52
|
-
* It can also be useful when you know you intend to add some number of additional elements, to minimize reallocations.
|
|
53
|
-
*
|
|
54
|
-
* If `newCapacity` is not greater than the current [[capacity]], this function does nothing.
|
|
55
|
-
* Otherwise, it allocates a new `TypedArray` with room for `newCapacity * growthFactor` elements, and copies the contents of the previous `TypedArray` into it.
|
|
56
|
-
* [[length]] remains unchanged; [[capacity]] reflects the size of the new TypeArray.
|
|
57
|
-
*/
|
|
58
|
-
ensureCapacity(newCapacity) {
|
|
59
|
-
if (this.capacity >= newCapacity)
|
|
60
|
-
return this.capacity;
|
|
61
|
-
assert(this.growthFactor >= 1.0);
|
|
62
|
-
newCapacity = Math.ceil(newCapacity * this.growthFactor);
|
|
63
|
-
const prevData = this._data;
|
|
64
|
-
this._data = new this._constructor(newCapacity);
|
|
65
|
-
this._data.set(prevData, 0);
|
|
66
|
-
assert(this.capacity === newCapacity);
|
|
67
|
-
return this.capacity;
|
|
68
|
-
}
|
|
69
|
-
/** Append the specified value, resizing if necessary. */
|
|
70
|
-
push(value) {
|
|
71
|
-
this.ensureCapacity(this.length + 1);
|
|
72
|
-
this._data[this.length] = value;
|
|
73
|
-
++this._length;
|
|
74
|
-
}
|
|
75
|
-
/** Append an array of values, resizing (at most once) if necessary. */
|
|
76
|
-
append(values) {
|
|
77
|
-
const newLength = this.length + values.length;
|
|
78
|
-
this.ensureCapacity(newLength);
|
|
79
|
-
this._data.set(values, this.length);
|
|
80
|
-
this._length = newLength;
|
|
81
|
-
}
|
|
82
|
-
/** Obtain the finished array.
|
|
83
|
-
* @param includeUnusedCapacity If true, the length of the returned array will be equal to [[capacity]], with extra bytes initialized to zero; otherwise, the
|
|
84
|
-
* returned array's length will be equal to [[length]].
|
|
85
|
-
*/
|
|
86
|
-
toTypedArray(includeUnusedCapacity = false) {
|
|
87
|
-
if (includeUnusedCapacity)
|
|
88
|
-
return this._data;
|
|
89
|
-
const subarray = this._data.subarray(0, this.length);
|
|
90
|
-
assert(subarray instanceof this._constructor);
|
|
91
|
-
assert(subarray.buffer === this._data.buffer);
|
|
92
|
-
return subarray;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
/** A [[TypedArrayBuilder]] for producing a [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array).
|
|
96
|
-
* @public
|
|
97
|
-
*/
|
|
98
|
-
export class Uint8ArrayBuilder extends TypedArrayBuilder {
|
|
99
|
-
/** See [[TypedArrayBuilder]] constructor. */
|
|
100
|
-
constructor(options) {
|
|
101
|
-
super(Uint8Array, options);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
/** A [[TypedArrayBuilder]] for producing a [Uint16Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array).
|
|
105
|
-
* @public
|
|
106
|
-
*/
|
|
107
|
-
export class Uint16ArrayBuilder extends TypedArrayBuilder {
|
|
108
|
-
/** See [[TypedArrayBuilder]] constructor. */
|
|
109
|
-
constructor(options) {
|
|
110
|
-
super(Uint16Array, options);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
/** A [[TypedArrayBuilder]] for producing a [Uint32Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array).
|
|
114
|
-
* @public
|
|
115
|
-
*/
|
|
116
|
-
export class Uint32ArrayBuilder extends TypedArrayBuilder {
|
|
117
|
-
/** See [[TypedArrayBuilder]] constructor. */
|
|
118
|
-
constructor(options) {
|
|
119
|
-
super(Uint32Array, options);
|
|
120
|
-
}
|
|
121
|
-
/** Obtain a view of the finished array as an array of bytes. */
|
|
122
|
-
toUint8Array(includeUnusedCapacity = false) {
|
|
123
|
-
if (includeUnusedCapacity)
|
|
124
|
-
return new Uint8Array(this._data.buffer);
|
|
125
|
-
return new Uint8Array(this._data.buffer, 0, this.length * 4);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
/** A [[TypedArrayBuilder]] that can populate a [[UintArray]] with the minimum
|
|
129
|
-
* [bytes per element](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/BYTES_PER_ELEMENT) required.
|
|
130
|
-
*
|
|
131
|
-
* By default, the underlying array is a `Uint8Array`, though this can be configured via [[UintArrayBuilderOptions.initialType]].
|
|
132
|
-
* As values are added to the array, if the bytes per element supported by the underlying array is too small to hold one of the new values, the array is
|
|
133
|
-
* reallocated to a type large enough to hold all of the new values. For example, the following produces a `Uint8Array` because all values are less than 256:
|
|
134
|
-
*
|
|
135
|
-
* ```ts
|
|
136
|
-
* const builder = new UintArrayBuilder();
|
|
137
|
-
* builder.append([1, 2, 254, 255]);
|
|
138
|
-
* const array = builder.toTypedArray();
|
|
139
|
-
* assert(array instanceof Uint8Array);
|
|
140
|
-
* ```
|
|
141
|
-
*
|
|
142
|
-
* However, the following produces a `Uint16Array` because one of the values is larger than 255 but none are larger than 65,535:
|
|
143
|
-
*
|
|
144
|
-
* ```ts
|
|
145
|
-
* const builder = new UintArrayBuilder();
|
|
146
|
-
* builder.append([1, 255, 257, 65535]);
|
|
147
|
-
* const array = builder.toTypedArray();
|
|
148
|
-
* assert(array instanceof Uint16Array);
|
|
149
|
-
* ```
|
|
150
|
-
*
|
|
151
|
-
* @see [[Uint8ArrayBuilder]], [[Uint16ArrayBuilder]], or [[Uint32ArrayBuilder]] if you know the number of bytes you want to allocate for each element in the array.
|
|
152
|
-
* @public
|
|
153
|
-
*/
|
|
154
|
-
export class UintArrayBuilder extends TypedArrayBuilder {
|
|
155
|
-
constructor(options) {
|
|
156
|
-
super(options?.initialType ?? Uint8Array, options);
|
|
157
|
-
}
|
|
158
|
-
/** The number of bytes (1, 2, or 4) currently allocated per element by the underlying array.
|
|
159
|
-
* This may change as larger values are added to the array.
|
|
160
|
-
*/
|
|
161
|
-
get bytesPerElement() {
|
|
162
|
-
return this._data.BYTES_PER_ELEMENT;
|
|
163
|
-
}
|
|
164
|
-
/** Ensures that the underlying array is of a type that can contain the largest value in `newValues`.
|
|
165
|
-
* For example, if `_data` is a `Uint16Array` and `newValues` contains any value(s) larger than 65,535, it will be replaced with a `Uint32Array`.
|
|
166
|
-
* This method is invoked by [[push]] and [[append]].
|
|
167
|
-
*/
|
|
168
|
-
ensureBytesPerElement(newValues) {
|
|
169
|
-
const curBytesPerElem = this.bytesPerElement;
|
|
170
|
-
assert(curBytesPerElem === 1 || curBytesPerElem === 2 || curBytesPerElem === 4);
|
|
171
|
-
if (curBytesPerElem >= 4)
|
|
172
|
-
return;
|
|
173
|
-
let neededBytesPerElem = curBytesPerElem;
|
|
174
|
-
for (const value of newValues) {
|
|
175
|
-
if (value > 0xffff) {
|
|
176
|
-
neededBytesPerElem = 4;
|
|
177
|
-
break;
|
|
178
|
-
}
|
|
179
|
-
else if (value > 0xff) {
|
|
180
|
-
neededBytesPerElem = 2;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
if (neededBytesPerElem <= curBytesPerElem)
|
|
184
|
-
return;
|
|
185
|
-
this._constructor = neededBytesPerElem === 1 ? Uint8Array : (neededBytesPerElem === 2 ? Uint16Array : Uint32Array);
|
|
186
|
-
this._data = new this._constructor(this._data);
|
|
187
|
-
}
|
|
188
|
-
/** See [[TypedArrayBuilder.push]]. */
|
|
189
|
-
push(value) {
|
|
190
|
-
this.ensureBytesPerElement([value]);
|
|
191
|
-
super.push(value);
|
|
192
|
-
}
|
|
193
|
-
/** See [[TypedArrayBuilder.append]]. */
|
|
194
|
-
append(values) {
|
|
195
|
-
this.ensureBytesPerElement(values);
|
|
196
|
-
super.append(values);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
1
|
+
/*---------------------------------------------------------------------------------------------
|
|
2
|
+
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
|
|
3
|
+
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
4
|
+
*--------------------------------------------------------------------------------------------*/
|
|
5
|
+
/** @packageDocumentation
|
|
6
|
+
* @module Collections
|
|
7
|
+
*/
|
|
8
|
+
import { assert } from "./Assert";
|
|
9
|
+
/** Incrementally builds a [TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) of unsigned 8-, 16-, or 32-bit integers.
|
|
10
|
+
* Sometimes you wish to populate a `TypedArray`, but you don't know how many elements you will need.
|
|
11
|
+
* `TypedArray` requires you to specify the size upon construction, and does not permit you to change the size later.
|
|
12
|
+
*
|
|
13
|
+
* `TypedArrayBuilder` manages a `TypedArray`, permitting you to [[push]] and [[append]] elements to it. It exposes two "size" properties":
|
|
14
|
+
* - [[capacity]], the number of elements it has currently allocated space for - i.e., the length of the underlying TypedArray; and
|
|
15
|
+
* - [[length]], the number of elements that have so far been added to it, which is never bigger than [[capacity]].
|
|
16
|
+
* When [[capacity]] is exceeded, a new, bigger TypedArray is allocated and the contents of the previous array are copied over to it.
|
|
17
|
+
*
|
|
18
|
+
* Once you've finished adding elements, you can obtain the finished `TypedArray` via [[toTypedArray]].
|
|
19
|
+
* @see [[Uint8ArrayBuilder]], [[Uint16ArrayBuilder]], and [[Uint32ArrayBuilder]] to build specific types of arrays.
|
|
20
|
+
* @see [[UintArrayBuilder]] when you don't know the maximum number of bytes required for each element in the array.
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
export class TypedArrayBuilder {
|
|
24
|
+
/** Constructs a new builder from the specified options, with a [[length]] of zero. */
|
|
25
|
+
constructor(constructor, options) {
|
|
26
|
+
this._constructor = constructor;
|
|
27
|
+
this._data = new constructor(options?.initialCapacity ?? 0);
|
|
28
|
+
this.growthFactor = Math.max(1.0, options?.growthFactor ?? 1.5);
|
|
29
|
+
this._length = 0;
|
|
30
|
+
}
|
|
31
|
+
/** The number of elements currently in the array. */
|
|
32
|
+
get length() {
|
|
33
|
+
return this._length;
|
|
34
|
+
}
|
|
35
|
+
/** The number of elements that can fit into the memory currently allocated for the array. */
|
|
36
|
+
get capacity() {
|
|
37
|
+
return this._data.length;
|
|
38
|
+
}
|
|
39
|
+
/** Like [TypedArray.at](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/at),
|
|
40
|
+
* returns the element at the specified index, with negative integers counting back from the end of the array.
|
|
41
|
+
* @note It is your responsibility to ensure the index falls within the bounds of the array.
|
|
42
|
+
*/
|
|
43
|
+
at(index) {
|
|
44
|
+
if (index < 0)
|
|
45
|
+
index = this.length - index;
|
|
46
|
+
const value = this._data[index];
|
|
47
|
+
assert(value !== undefined, "index out of bounds");
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
/** Ensure that [[capacity]] is at least equal to `newCapacity`.
|
|
51
|
+
* This is used internally by methods like [[push]] and [[append]] to ensure the array has room for the element(s) to be added.
|
|
52
|
+
* It can also be useful when you know you intend to add some number of additional elements, to minimize reallocations.
|
|
53
|
+
*
|
|
54
|
+
* If `newCapacity` is not greater than the current [[capacity]], this function does nothing.
|
|
55
|
+
* Otherwise, it allocates a new `TypedArray` with room for `newCapacity * growthFactor` elements, and copies the contents of the previous `TypedArray` into it.
|
|
56
|
+
* [[length]] remains unchanged; [[capacity]] reflects the size of the new TypeArray.
|
|
57
|
+
*/
|
|
58
|
+
ensureCapacity(newCapacity) {
|
|
59
|
+
if (this.capacity >= newCapacity)
|
|
60
|
+
return this.capacity;
|
|
61
|
+
assert(this.growthFactor >= 1.0);
|
|
62
|
+
newCapacity = Math.ceil(newCapacity * this.growthFactor);
|
|
63
|
+
const prevData = this._data;
|
|
64
|
+
this._data = new this._constructor(newCapacity);
|
|
65
|
+
this._data.set(prevData, 0);
|
|
66
|
+
assert(this.capacity === newCapacity);
|
|
67
|
+
return this.capacity;
|
|
68
|
+
}
|
|
69
|
+
/** Append the specified value, resizing if necessary. */
|
|
70
|
+
push(value) {
|
|
71
|
+
this.ensureCapacity(this.length + 1);
|
|
72
|
+
this._data[this.length] = value;
|
|
73
|
+
++this._length;
|
|
74
|
+
}
|
|
75
|
+
/** Append an array of values, resizing (at most once) if necessary. */
|
|
76
|
+
append(values) {
|
|
77
|
+
const newLength = this.length + values.length;
|
|
78
|
+
this.ensureCapacity(newLength);
|
|
79
|
+
this._data.set(values, this.length);
|
|
80
|
+
this._length = newLength;
|
|
81
|
+
}
|
|
82
|
+
/** Obtain the finished array.
|
|
83
|
+
* @param includeUnusedCapacity If true, the length of the returned array will be equal to [[capacity]], with extra bytes initialized to zero; otherwise, the
|
|
84
|
+
* returned array's length will be equal to [[length]].
|
|
85
|
+
*/
|
|
86
|
+
toTypedArray(includeUnusedCapacity = false) {
|
|
87
|
+
if (includeUnusedCapacity)
|
|
88
|
+
return this._data;
|
|
89
|
+
const subarray = this._data.subarray(0, this.length);
|
|
90
|
+
assert(subarray instanceof this._constructor);
|
|
91
|
+
assert(subarray.buffer === this._data.buffer);
|
|
92
|
+
return subarray;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/** A [[TypedArrayBuilder]] for producing a [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array).
|
|
96
|
+
* @public
|
|
97
|
+
*/
|
|
98
|
+
export class Uint8ArrayBuilder extends TypedArrayBuilder {
|
|
99
|
+
/** See [[TypedArrayBuilder]] constructor. */
|
|
100
|
+
constructor(options) {
|
|
101
|
+
super(Uint8Array, options);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/** A [[TypedArrayBuilder]] for producing a [Uint16Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array).
|
|
105
|
+
* @public
|
|
106
|
+
*/
|
|
107
|
+
export class Uint16ArrayBuilder extends TypedArrayBuilder {
|
|
108
|
+
/** See [[TypedArrayBuilder]] constructor. */
|
|
109
|
+
constructor(options) {
|
|
110
|
+
super(Uint16Array, options);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/** A [[TypedArrayBuilder]] for producing a [Uint32Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array).
|
|
114
|
+
* @public
|
|
115
|
+
*/
|
|
116
|
+
export class Uint32ArrayBuilder extends TypedArrayBuilder {
|
|
117
|
+
/** See [[TypedArrayBuilder]] constructor. */
|
|
118
|
+
constructor(options) {
|
|
119
|
+
super(Uint32Array, options);
|
|
120
|
+
}
|
|
121
|
+
/** Obtain a view of the finished array as an array of bytes. */
|
|
122
|
+
toUint8Array(includeUnusedCapacity = false) {
|
|
123
|
+
if (includeUnusedCapacity)
|
|
124
|
+
return new Uint8Array(this._data.buffer);
|
|
125
|
+
return new Uint8Array(this._data.buffer, 0, this.length * 4);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/** A [[TypedArrayBuilder]] that can populate a [[UintArray]] with the minimum
|
|
129
|
+
* [bytes per element](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/BYTES_PER_ELEMENT) required.
|
|
130
|
+
*
|
|
131
|
+
* By default, the underlying array is a `Uint8Array`, though this can be configured via [[UintArrayBuilderOptions.initialType]].
|
|
132
|
+
* As values are added to the array, if the bytes per element supported by the underlying array is too small to hold one of the new values, the array is
|
|
133
|
+
* reallocated to a type large enough to hold all of the new values. For example, the following produces a `Uint8Array` because all values are less than 256:
|
|
134
|
+
*
|
|
135
|
+
* ```ts
|
|
136
|
+
* const builder = new UintArrayBuilder();
|
|
137
|
+
* builder.append([1, 2, 254, 255]);
|
|
138
|
+
* const array = builder.toTypedArray();
|
|
139
|
+
* assert(array instanceof Uint8Array);
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* However, the following produces a `Uint16Array` because one of the values is larger than 255 but none are larger than 65,535:
|
|
143
|
+
*
|
|
144
|
+
* ```ts
|
|
145
|
+
* const builder = new UintArrayBuilder();
|
|
146
|
+
* builder.append([1, 255, 257, 65535]);
|
|
147
|
+
* const array = builder.toTypedArray();
|
|
148
|
+
* assert(array instanceof Uint16Array);
|
|
149
|
+
* ```
|
|
150
|
+
*
|
|
151
|
+
* @see [[Uint8ArrayBuilder]], [[Uint16ArrayBuilder]], or [[Uint32ArrayBuilder]] if you know the number of bytes you want to allocate for each element in the array.
|
|
152
|
+
* @public
|
|
153
|
+
*/
|
|
154
|
+
export class UintArrayBuilder extends TypedArrayBuilder {
|
|
155
|
+
constructor(options) {
|
|
156
|
+
super(options?.initialType ?? Uint8Array, options);
|
|
157
|
+
}
|
|
158
|
+
/** The number of bytes (1, 2, or 4) currently allocated per element by the underlying array.
|
|
159
|
+
* This may change as larger values are added to the array.
|
|
160
|
+
*/
|
|
161
|
+
get bytesPerElement() {
|
|
162
|
+
return this._data.BYTES_PER_ELEMENT;
|
|
163
|
+
}
|
|
164
|
+
/** Ensures that the underlying array is of a type that can contain the largest value in `newValues`.
|
|
165
|
+
* For example, if `_data` is a `Uint16Array` and `newValues` contains any value(s) larger than 65,535, it will be replaced with a `Uint32Array`.
|
|
166
|
+
* This method is invoked by [[push]] and [[append]].
|
|
167
|
+
*/
|
|
168
|
+
ensureBytesPerElement(newValues) {
|
|
169
|
+
const curBytesPerElem = this.bytesPerElement;
|
|
170
|
+
assert(curBytesPerElem === 1 || curBytesPerElem === 2 || curBytesPerElem === 4);
|
|
171
|
+
if (curBytesPerElem >= 4)
|
|
172
|
+
return;
|
|
173
|
+
let neededBytesPerElem = curBytesPerElem;
|
|
174
|
+
for (const value of newValues) {
|
|
175
|
+
if (value > 0xffff) {
|
|
176
|
+
neededBytesPerElem = 4;
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
else if (value > 0xff) {
|
|
180
|
+
neededBytesPerElem = 2;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (neededBytesPerElem <= curBytesPerElem)
|
|
184
|
+
return;
|
|
185
|
+
this._constructor = neededBytesPerElem === 1 ? Uint8Array : (neededBytesPerElem === 2 ? Uint16Array : Uint32Array);
|
|
186
|
+
this._data = new this._constructor(this._data);
|
|
187
|
+
}
|
|
188
|
+
/** See [[TypedArrayBuilder.push]]. */
|
|
189
|
+
push(value) {
|
|
190
|
+
this.ensureBytesPerElement([value]);
|
|
191
|
+
super.push(value);
|
|
192
|
+
}
|
|
193
|
+
/** See [[TypedArrayBuilder.append]]. */
|
|
194
|
+
append(values) {
|
|
195
|
+
this.ensureBytesPerElement(values);
|
|
196
|
+
super.append(values);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
199
|
//# sourceMappingURL=TypedArrayBuilder.js.map
|
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
/** @packageDocumentation
|
|
2
|
-
* @module Errors
|
|
3
|
-
*/
|
|
4
|
-
/// <reference types="node" />
|
|
5
|
-
/** A function to be notified when an unexpected error happens
|
|
6
|
-
* @public
|
|
7
|
-
*/
|
|
8
|
-
export
|
|
9
|
-
/**
|
|
10
|
-
* Utility for handling/reporting unexpected runtime errors. This class establishes a global handler for
|
|
11
|
-
* unexpected errors, and programmers should use its `handle` method when they occur. Generally, unexpected
|
|
12
|
-
* errors should not cause program termination, and should instead be logged and swallowed. However, for
|
|
13
|
-
* development/debugging, it can be helpful to re-throw exceptions so they are not missed.
|
|
14
|
-
* @public
|
|
15
|
-
*/
|
|
16
|
-
export declare class UnexpectedErrors {
|
|
17
|
-
/** handler for re-throwing exceptions directly */
|
|
18
|
-
static readonly reThrowImmediate: (e: any) => never;
|
|
19
|
-
/** handler for re-throwing exceptions from an asynchronous interval (so the current call stack is not aborted) */
|
|
20
|
-
static readonly reThrowDeferred: (e: any) => NodeJS.Timeout;
|
|
21
|
-
/** handler for logging exception to console */
|
|
22
|
-
static readonly consoleLog: (e: any) => void;
|
|
23
|
-
/** handler for logging exception with [[Logger]] */
|
|
24
|
-
static readonly errorLog: (e: any) => void;
|
|
25
|
-
private static _telemetry;
|
|
26
|
-
private static _handler;
|
|
27
|
-
private constructor();
|
|
28
|
-
/** Add a "telemetry tracker" for unexpected errors. Useful for tracking/reporting errors without changing handler.
|
|
29
|
-
* @returns a method to remove the tracker
|
|
30
|
-
*/
|
|
31
|
-
static addTelemetry(tracker: OnUnexpectedError): () => void;
|
|
32
|
-
/** call this method when an unexpected error happens so the global handler can process it.
|
|
33
|
-
* @param error the unexpected error
|
|
34
|
-
* @param notifyTelemetry if false, don't notify telemetry trackers. Use this for exceptions from third-party code, for example.
|
|
35
|
-
*/
|
|
36
|
-
static handle(error: any, notifyTelemetry?: boolean): void;
|
|
37
|
-
/** establish a new global *unexpected error* handler.
|
|
38
|
-
* @param handler the new global handler. You may provide your own function or use one of the static members of this class.
|
|
39
|
-
* The default is [[errorLog]].
|
|
40
|
-
* @returns the previous handler. Useful to temporarily change the handler.
|
|
41
|
-
*/
|
|
42
|
-
static setHandler(handler: OnUnexpectedError): OnUnexpectedError;
|
|
43
|
-
}
|
|
1
|
+
/** @packageDocumentation
|
|
2
|
+
* @module Errors
|
|
3
|
+
*/
|
|
4
|
+
/// <reference types="node" />
|
|
5
|
+
/** A function to be notified when an unexpected error happens
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
export type OnUnexpectedError = (error: any) => void;
|
|
9
|
+
/**
|
|
10
|
+
* Utility for handling/reporting unexpected runtime errors. This class establishes a global handler for
|
|
11
|
+
* unexpected errors, and programmers should use its `handle` method when they occur. Generally, unexpected
|
|
12
|
+
* errors should not cause program termination, and should instead be logged and swallowed. However, for
|
|
13
|
+
* development/debugging, it can be helpful to re-throw exceptions so they are not missed.
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
export declare class UnexpectedErrors {
|
|
17
|
+
/** handler for re-throwing exceptions directly */
|
|
18
|
+
static readonly reThrowImmediate: (e: any) => never;
|
|
19
|
+
/** handler for re-throwing exceptions from an asynchronous interval (so the current call stack is not aborted) */
|
|
20
|
+
static readonly reThrowDeferred: (e: any) => NodeJS.Timeout;
|
|
21
|
+
/** handler for logging exception to console */
|
|
22
|
+
static readonly consoleLog: (e: any) => void;
|
|
23
|
+
/** handler for logging exception with [[Logger]] */
|
|
24
|
+
static readonly errorLog: (e: any) => void;
|
|
25
|
+
private static _telemetry;
|
|
26
|
+
private static _handler;
|
|
27
|
+
private constructor();
|
|
28
|
+
/** Add a "telemetry tracker" for unexpected errors. Useful for tracking/reporting errors without changing handler.
|
|
29
|
+
* @returns a method to remove the tracker
|
|
30
|
+
*/
|
|
31
|
+
static addTelemetry(tracker: OnUnexpectedError): () => void;
|
|
32
|
+
/** call this method when an unexpected error happens so the global handler can process it.
|
|
33
|
+
* @param error the unexpected error
|
|
34
|
+
* @param notifyTelemetry if false, don't notify telemetry trackers. Use this for exceptions from third-party code, for example.
|
|
35
|
+
*/
|
|
36
|
+
static handle(error: any, notifyTelemetry?: boolean): void;
|
|
37
|
+
/** establish a new global *unexpected error* handler.
|
|
38
|
+
* @param handler the new global handler. You may provide your own function or use one of the static members of this class.
|
|
39
|
+
* The default is [[errorLog]].
|
|
40
|
+
* @returns the previous handler. Useful to temporarily change the handler.
|
|
41
|
+
*/
|
|
42
|
+
static setHandler(handler: OnUnexpectedError): OnUnexpectedError;
|
|
43
|
+
}
|
|
44
44
|
//# sourceMappingURL=UnexpectedErrors.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"UnexpectedErrors.d.ts","sourceRoot":"","sources":["../../src/UnexpectedErrors.ts"],"names":[],"mappings":"AAIA;;GAEG;;AAIH;;GAEG;AACH,
|
|
1
|
+
{"version":3,"file":"UnexpectedErrors.d.ts","sourceRoot":"","sources":["../../src/UnexpectedErrors.ts"],"names":[],"mappings":"AAIA;;GAEG;;AAIH;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG,CAAC,KAAK,EAAE,GAAG,KAAK,IAAI,CAAC;AAErD;;;;;;GAMG;AACH,qBAAa,gBAAgB;IAC3B,kDAAkD;IAClD,gBAAuB,gBAAgB,MAAO,GAAG,WAAkB;IACnE,kHAAkH;IAClH,gBAAuB,eAAe,MAAO,GAAG,oBAAuC;IACvF,+CAA+C;IAC/C,gBAAuB,UAAU,MAAO,GAAG,UAAsB;IACjE,oDAAoD;IACpD,gBAAuB,QAAQ,MAAO,GAAG,UAAyC;IAElF,OAAO,CAAC,MAAM,CAAC,UAAU,CAA2B;IACpD,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAiB;IACxC,OAAO;IAEP;;OAEG;WACW,YAAY,CAAC,OAAO,EAAE,iBAAiB,GAAG,MAAM,IAAI;IAKlE;;;OAGG;WACW,MAAM,CAAC,KAAK,EAAE,GAAG,EAAE,eAAe,UAAO,GAAG,IAAI;IAa9D;;;;OAIG;WACW,UAAU,CAAC,OAAO,EAAE,iBAAiB,GAAG,iBAAiB;CAKxE"}
|
|
@@ -1,65 +1,66 @@
|
|
|
1
|
-
/*---------------------------------------------------------------------------------------------
|
|
2
|
-
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
|
|
3
|
-
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
4
|
-
*--------------------------------------------------------------------------------------------*/
|
|
5
|
-
/** @packageDocumentation
|
|
6
|
-
* @module Errors
|
|
7
|
-
*/
|
|
8
|
-
var _a;
|
|
9
|
-
import { Logger } from "./Logger";
|
|
10
|
-
/**
|
|
11
|
-
* Utility for handling/reporting unexpected runtime errors. This class establishes a global handler for
|
|
12
|
-
* unexpected errors, and programmers should use its `handle` method when they occur. Generally, unexpected
|
|
13
|
-
* errors should not cause program termination, and should instead be logged and swallowed. However, for
|
|
14
|
-
* development/debugging, it can be helpful to re-throw exceptions so they are not missed.
|
|
15
|
-
* @public
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
constructor() { } // this is a singleton
|
|
19
|
-
/** Add a "telemetry tracker" for unexpected errors. Useful for tracking/reporting errors without changing handler.
|
|
20
|
-
* @returns a method to remove the tracker
|
|
21
|
-
*/
|
|
22
|
-
static addTelemetry(tracker) {
|
|
23
|
-
this._telemetry.push(tracker);
|
|
24
|
-
return () => this._telemetry.splice(this._telemetry.indexOf(tracker), 1);
|
|
25
|
-
}
|
|
26
|
-
/** call this method when an unexpected error happens so the global handler can process it.
|
|
27
|
-
* @param error the unexpected error
|
|
28
|
-
* @param notifyTelemetry if false, don't notify telemetry trackers. Use this for exceptions from third-party code, for example.
|
|
29
|
-
*/
|
|
30
|
-
static handle(error, notifyTelemetry = true) {
|
|
31
|
-
this._handler(error);
|
|
32
|
-
if (notifyTelemetry) {
|
|
33
|
-
this._telemetry.forEach((telemetry) => {
|
|
34
|
-
try {
|
|
35
|
-
telemetry(error);
|
|
36
|
-
}
|
|
37
|
-
catch (_) {
|
|
38
|
-
// ignore errors from telemetry trackers
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
/** establish a new global *unexpected error* handler.
|
|
44
|
-
* @param handler the new global handler. You may provide your own function or use one of the static members of this class.
|
|
45
|
-
* The default is [[errorLog]].
|
|
46
|
-
* @returns the previous handler. Useful to temporarily change the handler.
|
|
47
|
-
*/
|
|
48
|
-
static setHandler(handler) {
|
|
49
|
-
const oldHandler = this._handler;
|
|
50
|
-
this._handler = handler;
|
|
51
|
-
return oldHandler;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
_a = UnexpectedErrors;
|
|
55
|
-
/** handler for re-throwing exceptions directly */
|
|
56
|
-
UnexpectedErrors.reThrowImmediate = (e) => { throw e; };
|
|
57
|
-
/** handler for re-throwing exceptions from an asynchronous interval (so the current call stack is not aborted) */
|
|
58
|
-
UnexpectedErrors.reThrowDeferred = (e) => setTimeout(() => { throw e; }, 0);
|
|
59
|
-
/** handler for logging exception to console */
|
|
60
|
-
UnexpectedErrors.consoleLog = (e) => console.error(e); // eslint-disable-line no-console
|
|
61
|
-
/** handler for logging exception with [[Logger]] */
|
|
62
|
-
UnexpectedErrors.errorLog = (e) => Logger.logException("unhandled", e);
|
|
63
|
-
UnexpectedErrors._telemetry = [];
|
|
64
|
-
UnexpectedErrors._handler = _a.errorLog; // default to error logging
|
|
1
|
+
/*---------------------------------------------------------------------------------------------
|
|
2
|
+
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
|
|
3
|
+
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
4
|
+
*--------------------------------------------------------------------------------------------*/
|
|
5
|
+
/** @packageDocumentation
|
|
6
|
+
* @module Errors
|
|
7
|
+
*/
|
|
8
|
+
var _a;
|
|
9
|
+
import { Logger } from "./Logger";
|
|
10
|
+
/**
|
|
11
|
+
* Utility for handling/reporting unexpected runtime errors. This class establishes a global handler for
|
|
12
|
+
* unexpected errors, and programmers should use its `handle` method when they occur. Generally, unexpected
|
|
13
|
+
* errors should not cause program termination, and should instead be logged and swallowed. However, for
|
|
14
|
+
* development/debugging, it can be helpful to re-throw exceptions so they are not missed.
|
|
15
|
+
* @public
|
|
16
|
+
*/
|
|
17
|
+
class UnexpectedErrors {
|
|
18
|
+
constructor() { } // this is a singleton
|
|
19
|
+
/** Add a "telemetry tracker" for unexpected errors. Useful for tracking/reporting errors without changing handler.
|
|
20
|
+
* @returns a method to remove the tracker
|
|
21
|
+
*/
|
|
22
|
+
static addTelemetry(tracker) {
|
|
23
|
+
this._telemetry.push(tracker);
|
|
24
|
+
return () => this._telemetry.splice(this._telemetry.indexOf(tracker), 1);
|
|
25
|
+
}
|
|
26
|
+
/** call this method when an unexpected error happens so the global handler can process it.
|
|
27
|
+
* @param error the unexpected error
|
|
28
|
+
* @param notifyTelemetry if false, don't notify telemetry trackers. Use this for exceptions from third-party code, for example.
|
|
29
|
+
*/
|
|
30
|
+
static handle(error, notifyTelemetry = true) {
|
|
31
|
+
this._handler(error);
|
|
32
|
+
if (notifyTelemetry) {
|
|
33
|
+
this._telemetry.forEach((telemetry) => {
|
|
34
|
+
try {
|
|
35
|
+
telemetry(error);
|
|
36
|
+
}
|
|
37
|
+
catch (_) {
|
|
38
|
+
// ignore errors from telemetry trackers
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** establish a new global *unexpected error* handler.
|
|
44
|
+
* @param handler the new global handler. You may provide your own function or use one of the static members of this class.
|
|
45
|
+
* The default is [[errorLog]].
|
|
46
|
+
* @returns the previous handler. Useful to temporarily change the handler.
|
|
47
|
+
*/
|
|
48
|
+
static setHandler(handler) {
|
|
49
|
+
const oldHandler = this._handler;
|
|
50
|
+
this._handler = handler;
|
|
51
|
+
return oldHandler;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
_a = UnexpectedErrors;
|
|
55
|
+
/** handler for re-throwing exceptions directly */
|
|
56
|
+
UnexpectedErrors.reThrowImmediate = (e) => { throw e; };
|
|
57
|
+
/** handler for re-throwing exceptions from an asynchronous interval (so the current call stack is not aborted) */
|
|
58
|
+
UnexpectedErrors.reThrowDeferred = (e) => setTimeout(() => { throw e; }, 0);
|
|
59
|
+
/** handler for logging exception to console */
|
|
60
|
+
UnexpectedErrors.consoleLog = (e) => console.error(e); // eslint-disable-line no-console
|
|
61
|
+
/** handler for logging exception with [[Logger]] */
|
|
62
|
+
UnexpectedErrors.errorLog = (e) => Logger.logException("unhandled", e);
|
|
63
|
+
UnexpectedErrors._telemetry = [];
|
|
64
|
+
UnexpectedErrors._handler = _a.errorLog; // default to error logging
|
|
65
|
+
export { UnexpectedErrors };
|
|
65
66
|
//# sourceMappingURL=UnexpectedErrors.js.map
|