@vertexvis/utils 0.12.0-canary.9 → 0.12.0

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.
@@ -1,6 +1,34 @@
1
1
  import { __awaiter, __generator, __assign, __extends, __read, __spreadArray, __values } from 'tslib';
2
2
  import crypto from 'crypto';
3
3
 
4
+ /**
5
+ * Adds a listener to the given `target`, and returns a promise that
6
+ * resolves with the first event emitted of the given `type`.
7
+ *
8
+ * @param target The target to add an event listener to.
9
+ * @param type The event type to listen for.
10
+ * @param opts Options to pass to `addEventListener`.
11
+ * @returns A promise that resolves with the first event emitted of `type`.
12
+ */
13
+ function once(target, type, opts) {
14
+ return __awaiter(this, void 0, void 0, function () {
15
+ return __generator(this, function (_a) {
16
+ return [2 /*return*/, new Promise(function (resolve) {
17
+ function handler(event) {
18
+ target.removeEventListener(type, handler);
19
+ resolve(event);
20
+ }
21
+ target.addEventListener(type, handler, opts);
22
+ })];
23
+ });
24
+ });
25
+ }
26
+
27
+ var eventTargets = /*#__PURE__*/Object.freeze({
28
+ __proto__: null,
29
+ once: once
30
+ });
31
+
4
32
  function delay() {
5
33
  var args = [];
6
34
  for (var _i = 0; _i < arguments.length; _i++) {
@@ -33,28 +61,132 @@ function timeout() {
33
61
  for (var _i = 0; _i < arguments.length; _i++) {
34
62
  args[_i] = arguments[_i];
35
63
  }
36
- var ms = args[0];
37
- if (typeof ms === 'number') {
38
- var promise = args[1];
39
- var timeout_1 = new Promise(function (_, reject) {
40
- return setTimeout(function () { return reject(new Error("Promise timed out after " + ms + "ms")); }, ms);
64
+ return __awaiter(this, void 0, void 0, function () {
65
+ var ms, promise, timer_1, timeout_1, res;
66
+ return __generator(this, function (_a) {
67
+ switch (_a.label) {
68
+ case 0:
69
+ ms = args[0];
70
+ if (!(typeof ms === 'number')) return [3 /*break*/, 4];
71
+ promise = args[1];
72
+ timeout_1 = new Promise(function (_, reject) {
73
+ timer_1 = setTimeout(function () { return reject(new Error("Promise timed out after ".concat(ms, "ms"))); }, ms);
74
+ });
75
+ if (!(promise != null)) return [3 /*break*/, 2];
76
+ return [4 /*yield*/, Promise.race([promise, timeout_1])];
77
+ case 1:
78
+ res = _a.sent();
79
+ clearTimeout(timer_1);
80
+ return [2 /*return*/, res];
81
+ case 2: return [2 /*return*/, timeout_1];
82
+ case 3: return [3 /*break*/, 5];
83
+ case 4: return [2 /*return*/, Promise.reject('First argument to `timeout` must be a number')];
84
+ case 5: return [2 /*return*/];
85
+ }
41
86
  });
42
- if (promise != null) {
43
- return Promise.race([promise, timeout_1]);
44
- }
45
- else {
46
- return timeout_1;
87
+ });
88
+ }
89
+ /**
90
+ * Executes and reattempts execution of an asynchronous function if it throws an
91
+ * error. By default, this function will only retry once and reexecute
92
+ * immediately after the previous execution throws. You can configure the number
93
+ * of retry attempts and delays with the `maxRetries` and `delaysInMs` options.
94
+ *
95
+ * The `delaysInMs` is an array of delays in milliseconds for each retry
96
+ * attempt. If there are more retry attempts than delays, the last delay will be
97
+ * used.
98
+ *
99
+ * @param process The process to execute.
100
+ * @param opts Options to configure retry behavior.
101
+ * @returns A promise that resolves with a successful value, or the original
102
+ * rejected value if the process fails.
103
+ */
104
+ function retry(process, opts) {
105
+ if (opts === void 0) { opts = {}; }
106
+ return __awaiter(this, void 0, void 0, function () {
107
+ function execute(attempt, process, opts) {
108
+ return __awaiter(this, void 0, void 0, function () {
109
+ var _a, delaysInMs, _b, maxRetries, delayInMs, e_1;
110
+ return __generator(this, function (_c) {
111
+ switch (_c.label) {
112
+ case 0:
113
+ _a = opts.delaysInMs, delaysInMs = _a === void 0 ? [] : _a, _b = opts.maxRetries, maxRetries = _b === void 0 ? 1 : _b;
114
+ _c.label = 1;
115
+ case 1:
116
+ _c.trys.push([1, 4, , 8]);
117
+ delayInMs = attempt === 0 || delaysInMs.length === 0
118
+ ? 0
119
+ : delaysInMs[Math.min(attempt - 1, delaysInMs.length - 1)];
120
+ return [4 /*yield*/, delay(delayInMs)];
121
+ case 2:
122
+ _c.sent();
123
+ return [4 /*yield*/, process()];
124
+ case 3: return [2 /*return*/, _c.sent()];
125
+ case 4:
126
+ e_1 = _c.sent();
127
+ if (!(attempt < maxRetries)) return [3 /*break*/, 6];
128
+ return [4 /*yield*/, execute(attempt + 1, process, opts)];
129
+ case 5: return [2 /*return*/, _c.sent()];
130
+ case 6: throw e_1;
131
+ case 7: return [3 /*break*/, 8];
132
+ case 8: return [2 /*return*/];
133
+ }
134
+ });
135
+ });
47
136
  }
137
+ return __generator(this, function (_a) {
138
+ return [2 /*return*/, execute(0, process, opts)];
139
+ });
140
+ });
141
+ }
142
+ /**
143
+ * Returns a promise that either resolves with the result of `promise`, or a
144
+ * value that indicates the execution was aborted.
145
+ *
146
+ * **Note:** Because Promises in JS cannot be canceled, an abort signal will not
147
+ * cancel the execution of the promise.
148
+ *
149
+ * @param signal A signal that communicates the process should be aborted.
150
+ * @param promise A promise who's value will be returned if not aborted.
151
+ * @returns A value indicating if the process was aborted, or the value of
152
+ * `promise`.
153
+ */
154
+ function abort(signal, promise) {
155
+ return __awaiter(this, void 0, void 0, function () {
156
+ var controller, pendingAbort, result;
157
+ return __generator(this, function (_a) {
158
+ switch (_a.label) {
159
+ case 0:
160
+ controller = new AbortController();
161
+ pendingAbort = once(signal, 'abort', { signal: controller.signal });
162
+ return [4 /*yield*/, Promise.race([promise, pendingAbort])];
163
+ case 1:
164
+ result = _a.sent();
165
+ if (isAbortEvent(result)) {
166
+ return [2 /*return*/, { aborted: true }];
167
+ }
168
+ else {
169
+ controller.abort();
170
+ return [2 /*return*/, { aborted: false, result: result }];
171
+ }
172
+ }
173
+ });
174
+ });
175
+ }
176
+ function isAbortEvent(obj) {
177
+ if (obj instanceof Event) {
178
+ return obj.type === 'abort';
48
179
  }
49
- else {
50
- return Promise.reject('First argument to `timeout` must be a number');
51
- }
180
+ else
181
+ return false;
52
182
  }
53
183
 
54
184
  var async = /*#__PURE__*/Object.freeze({
55
185
  __proto__: null,
56
186
  delay: delay,
57
- timeout: timeout
187
+ timeout: timeout,
188
+ retry: retry,
189
+ abort: abort
58
190
  });
59
191
 
60
192
  /**
@@ -184,7 +316,7 @@ var isOpaque = function (color) {
184
316
  * `#`.
185
317
  */
186
318
  var toHexString = function (color) {
187
- return "#" + componentToHex(color.r) + componentToHex(color.g) + componentToHex(color.b);
319
+ return "#".concat(componentToHex(color.r)).concat(componentToHex(color.g)).concat(componentToHex(color.b));
188
320
  };
189
321
  var componentToHex = function (num) {
190
322
  var hex = num.toString(16);
@@ -279,8 +411,9 @@ var color = /*#__PURE__*/Object.freeze({
279
411
  var MapperValidationError = /** @class */ (function (_super) {
280
412
  __extends(MapperValidationError, _super);
281
413
  function MapperValidationError(errors) {
282
- var _this = _super.call(this, 'Validation error while mapping object.') || this;
414
+ var _this = _super.call(this, 'Validation error mapping object.') || this;
283
415
  _this.errors = errors;
416
+ Object.setPrototypeOf(_this, MapperValidationError.prototype);
284
417
  return _this;
285
418
  }
286
419
  return MapperValidationError;
@@ -296,7 +429,7 @@ function required(name) {
296
429
  return input;
297
430
  }
298
431
  else {
299
- return { errors: [name + " is required."] };
432
+ return { errors: ["".concat(name, " is required.")] };
300
433
  }
301
434
  };
302
435
  }
@@ -314,7 +447,7 @@ function requiredProp(prop) {
314
447
  return value;
315
448
  }
316
449
  else {
317
- return { errors: [prop + " is required"] };
450
+ return { errors: ["".concat(prop, " is required")] };
318
451
  }
319
452
  };
320
453
  }
@@ -2845,7 +2978,7 @@ var isEqual = function (a, b) {
2845
2978
  queryA === queryB);
2846
2979
  };
2847
2980
  var replacePath = function (path, uri) {
2848
- var pathWithForwardSlash = path[0] === '/' ? path : "/" + path;
2981
+ var pathWithForwardSlash = path[0] === '/' ? path : "/".concat(path);
2849
2982
  return __assign(__assign({}, uri), { path: pathWithForwardSlash });
2850
2983
  };
2851
2984
  var pathAsArray = function (uri) {
@@ -2921,17 +3054,17 @@ var queryAsMap = function (uri) {
2921
3054
  var toString = function (uri) {
2922
3055
  var result = '';
2923
3056
  if (uri.scheme != null && uri.scheme.length > 0) {
2924
- result = uri.scheme + ":";
3057
+ result = "".concat(uri.scheme, ":");
2925
3058
  }
2926
3059
  if (uri.authority != null && uri.authority.length > 0) {
2927
- result += "//" + uri.authority;
3060
+ result += "//".concat(uri.authority);
2928
3061
  }
2929
3062
  result += uri.path;
2930
3063
  if (uri.query != null && uri.query.length > 0) {
2931
- result += "?" + uri.query;
3064
+ result += "?".concat(uri.query);
2932
3065
  }
2933
3066
  if (uri.fragment != null && uri.fragment.length > 0) {
2934
- result += "#" + uri.fragment;
3067
+ result += "#".concat(uri.fragment);
2935
3068
  }
2936
3069
  return result;
2937
3070
  };
@@ -3116,10 +3249,50 @@ var EventDispatcher = /** @class */ (function () {
3116
3249
  function EventDispatcher() {
3117
3250
  this.listeners = [];
3118
3251
  }
3119
- EventDispatcher.prototype.on = function (listener) {
3252
+ EventDispatcher.prototype.on = function (listener, opts) {
3120
3253
  var _this = this;
3254
+ var _a;
3255
+ if (opts === void 0) { opts = {}; }
3121
3256
  this.listeners.push(listener);
3122
- return { dispose: function () { return _this.off(listener); } };
3257
+ var controller = new AbortController();
3258
+ controller.signal.addEventListener('abort', function () { return _this.off(listener); });
3259
+ (_a = opts.abort) === null || _a === void 0 ? void 0 : _a.addEventListener('abort', function () { return controller.abort(); });
3260
+ return { dispose: function () { return controller.abort(); } };
3261
+ };
3262
+ EventDispatcher.prototype.once = function (opts) {
3263
+ var _this = this;
3264
+ if (opts === void 0) { opts = {}; }
3265
+ return new Promise(function (resolve) {
3266
+ _this.on(function (event) { return resolve(event); }, opts);
3267
+ });
3268
+ };
3269
+ EventDispatcher.prototype.onceWhen = function (predicate, opts) {
3270
+ var _a;
3271
+ if (opts === void 0) { opts = {}; }
3272
+ return __awaiter(this, void 0, void 0, function () {
3273
+ var controller;
3274
+ var _this = this;
3275
+ return __generator(this, function (_b) {
3276
+ controller = new AbortController();
3277
+ (_a = opts.abort) === null || _a === void 0 ? void 0 : _a.addEventListener('abort', function () { return controller.abort(); });
3278
+ return [2 /*return*/, new Promise(function (resolve) {
3279
+ _this.when(predicate, function (event) {
3280
+ if (predicate(event)) {
3281
+ controller.abort();
3282
+ resolve(event);
3283
+ }
3284
+ }, __assign(__assign({}, opts), { abort: controller.signal }));
3285
+ })];
3286
+ });
3287
+ });
3288
+ };
3289
+ EventDispatcher.prototype.when = function (predicate, listener, opts) {
3290
+ if (opts === void 0) { opts = {}; }
3291
+ return this.on(function (event) {
3292
+ if (predicate(event)) {
3293
+ listener(event);
3294
+ }
3295
+ }, opts);
3123
3296
  };
3124
3297
  EventDispatcher.prototype.off = function (listener) {
3125
3298
  var index = this.listeners.indexOf(listener);
@@ -3133,5 +3306,5 @@ var EventDispatcher = /** @class */ (function () {
3133
3306
  return EventDispatcher;
3134
3307
  }());
3135
3308
 
3136
- export { async as Async, binaryReader as BinaryReader, color as Color, EventDispatcher, mapper as Mapper, objects as Objects, range as Range, sets as Sets, strings as Strings, uuid as UUID, uri as Uri };
3309
+ export { async as Async, binaryReader as BinaryReader, color as Color, EventDispatcher, eventTargets as EventTargets, mapper as Mapper, objects as Objects, range as Range, sets as Sets, strings as Strings, uuid as UUID, uri as Uri };
3137
3310
  //# sourceMappingURL=bundle.esm.js.map